@Override
  public String toXML(PortalPreferences portalPreferences) {
    PortalPreferencesImpl portalPreferencesImpl = null;

    if (portalPreferences instanceof PortalPreferencesWrapper) {
      PortalPreferencesWrapper portalPreferencesWrapper =
          (PortalPreferencesWrapper) portalPreferences;

      portalPreferencesImpl = portalPreferencesWrapper.getPortalPreferencesImpl();
    } else {
      portalPreferencesImpl = (PortalPreferencesImpl) portalPreferences;
    }

    return portalPreferencesImpl.toXML();
  }
  @Override
  public PortalPreferences getPortalPreferences(
      HttpSession session, long userId, boolean signedIn) {

    long ownerId = userId;
    int ownerType = PortletKeys.PREFS_OWNER_TYPE_USER;

    PortalPreferences portalPreferences = null;

    if (signedIn) {
      PortalPreferencesWrapper portalPreferencesWrapper =
          PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);

      if (portalPreferencesWrapper == null) {
        portalPreferencesWrapper =
            (PortalPreferencesWrapper)
                PortalPreferencesLocalServiceUtil.getPreferences(ownerId, ownerType);

        portalPreferences = portalPreferencesWrapper.getPortalPreferencesImpl();
      } else {
        PortalPreferencesImpl portalPreferencesImpl =
            portalPreferencesWrapper.getPortalPreferencesImpl();

        portalPreferences = portalPreferencesImpl.clone();
      }
    } else {
      if (session != null) {
        portalPreferences = (PortalPreferences) session.getAttribute(WebKeys.PORTAL_PREFERENCES);
      }

      if (portalPreferences == null) {
        PortalPreferencesWrapper portalPreferencesWrapper =
            PortalPreferencesWrapperCacheUtil.get(ownerId, ownerType);

        if (portalPreferencesWrapper == null) {
          portalPreferencesWrapper =
              (PortalPreferencesWrapper)
                  PortalPreferencesLocalServiceUtil.getPreferences(ownerId, ownerType);

          portalPreferences = portalPreferencesWrapper.getPortalPreferencesImpl();
        } else {
          PortalPreferencesImpl portalPreferencesImpl =
              portalPreferencesWrapper.getPortalPreferencesImpl();

          portalPreferences = portalPreferencesImpl.clone();
        }

        if (session != null) {
          session.setAttribute(WebKeys.PORTAL_PREFERENCES, portalPreferences);
        }
      }
    }

    portalPreferences.setSignedIn(signedIn);
    portalPreferences.setUserId(userId);

    return portalPreferences;
  }