protected void copyPreferences(long userId, String sourcePortletId, String targetPortletId) {

    Layout layout = getLayout();

    try {
      PortletPreferencesIds portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), 0, layout, sourcePortletId, false);

      javax.portlet.PortletPreferences sourcePortletPreferences =
          PortletPreferencesLocalServiceUtil.getStrictPreferences(portletPreferencesIds);

      portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), userId, layout, targetPortletId, false);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          portletPreferencesIds.getOwnerId(),
          portletPreferencesIds.getOwnerType(),
          portletPreferencesIds.getPlid(),
          portletPreferencesIds.getPortletId(),
          sourcePortletPreferences);
    } catch (Exception e) {
    }
  }
  @Test
  public void testPreferencesOwnedByGroup() throws Exception {
    PowerMockito.mockStatic(PortletLocalServiceUtil.class);

    Mockito.when(PortletLocalServiceUtil.getPortletById(_layout.getCompanyId(), _PORTLET_ID))
        .thenReturn(getGroupPortlet());

    long siteGroupId = _layout.getGroupId();
    boolean modeEditGuest = false;

    PortletPreferencesIds portletPreferencesIds =
        PortletPreferencesFactoryUtil.getPortletPreferencesIds(
            siteGroupId, _USER_ID, _layout, _PORTLET_ID, modeEditGuest);

    Assert.assertEquals(
        "The owner type should be of type group",
        PortletKeys.PREFS_OWNER_TYPE_GROUP,
        portletPreferencesIds.getOwnerType());
    Assert.assertEquals(
        "The owner ID should be the ID of the group",
        siteGroupId,
        portletPreferencesIds.getOwnerId());
    Assert.assertEquals(
        "The PLID should not be a real value",
        PortletKeys.PREFS_PLID_SHARED,
        portletPreferencesIds.getPlid());
  }
  protected void copyPreferences(String sourcePortletId, String targetPortletId) {

    Layout layout = getLayout();

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    try {
      PortletPreferencesIds portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), permissionChecker.getUserId(), layout, sourcePortletId, false);

      javax.portlet.PortletPreferences sourcePortletPreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds);

      portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), permissionChecker.getUserId(), layout, targetPortletId, false);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          portletPreferencesIds.getOwnerId(),
          portletPreferencesIds.getOwnerType(),
          portletPreferencesIds.getPlid(),
          portletPreferencesIds.getPortletId(),
          sourcePortletPreferences);
    } catch (Exception e) {
    }
  }
  @Test
  public void testPreferencesOwnedByUserLayout() throws Exception {
    PowerMockito.mockStatic(PortletLocalServiceUtil.class);

    Mockito.when(PortletLocalServiceUtil.getPortletById(_layout.getCompanyId(), _PORTLET_ID))
        .thenReturn(getUserLayoutPortlet());

    long siteGroupId = _layout.getGroupId();
    boolean modeEditGuest = false;

    PortletPreferencesIds portletPreferencesIds =
        PortletPreferencesFactoryUtil.getPortletPreferencesIds(
            siteGroupId, _USER_ID, _layout, _PORTLET_ID, modeEditGuest);

    Assert.assertEquals(
        "The owner type should be of type user",
        PortletKeys.PREFS_OWNER_TYPE_USER,
        portletPreferencesIds.getOwnerType());
    Assert.assertEquals(
        "The owner ID should be the ID of the user who added it",
        _USER_ID,
        portletPreferencesIds.getOwnerId());
    Assert.assertEquals(
        "The PLID should be the PLID of the current layout",
        _layout.getPlid(),
        portletPreferencesIds.getPlid());
  }
  public static void copyPreferences(
      HttpServletRequest request, Layout targetLayout, Layout sourceLayout) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    LayoutTypePortlet sourceLayoutTypePortlet = (LayoutTypePortlet) sourceLayout.getLayoutType();

    List<String> sourcePortletIds = sourceLayoutTypePortlet.getPortletIds();

    for (String sourcePortletId : sourcePortletIds) {

      // Copy preference

      PortletPreferencesIds portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              request, targetLayout, sourcePortletId);

      PortletPreferencesLocalServiceUtil.getPreferences(portletPreferencesIds);

      PortletPreferencesIds sourcePortletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              request, sourceLayout, sourcePortletId);

      PortletPreferences sourcePreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(sourcePortletPreferencesIds);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          portletPreferencesIds.getOwnerId(),
          portletPreferencesIds.getOwnerType(),
          portletPreferencesIds.getPlid(),
          portletPreferencesIds.getPortletId(),
          sourcePreferences);

      // Copy portlet setup

      PortletPreferences targetPreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(
              themeDisplay.getCompanyId(),
              PortletKeys.PREFS_OWNER_ID_DEFAULT,
              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
              targetLayout.getPlid(),
              sourcePortletId);

      sourcePreferences =
          PortletPreferencesLocalServiceUtil.getPreferences(
              themeDisplay.getCompanyId(),
              PortletKeys.PREFS_OWNER_ID_DEFAULT,
              PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
              sourceLayout.getPlid(),
              sourcePortletId);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          PortletKeys.PREFS_OWNER_ID_DEFAULT,
          PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
          targetLayout.getPlid(),
          sourcePortletId,
          sourcePreferences);

      SitesUtil.updateLayoutScopes(
          themeDisplay.getUserId(),
          sourceLayout,
          targetLayout,
          sourcePreferences,
          targetPreferences,
          sourcePortletId,
          themeDisplay.getLanguageId());

      Portlet sourcePortlet =
          PortletLocalServiceUtil.getPortletById(sourceLayout.getCompanyId(), sourcePortletId);

      PortletLayoutListener sourcePortletLayoutListener =
          sourcePortlet.getPortletLayoutListenerInstance();

      if (sourcePortletLayoutListener != null) {
        sourcePortletLayoutListener.onAddToLayout(sourcePortletId, targetLayout.getPlid());
      }
    }
  }