Beispiel #1
0
  public static String getUserPreference(
      long userId, long plid, String portletId, String preferenceName) {
    String _preference = StringPool.BLANK;

    PortletPreferences preferences = null;
    try {
      preferences =
          PortletPreferencesLocalServiceUtil.getPortletPreferences(
              userId, PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);
    } catch (PortalException e) {
      // ignore
    } catch (SystemException e) {
      // ignore
    }

    if (Validator.isNull(preferences)) return _preference;

    Document document = null;
    try {
      document = SAXReaderUtil.read(preferences.getPreferences());
    } catch (DocumentException e) {
      e.printStackTrace();
    }

    for (Iterator<Element> itr = document.getRootElement().elementIterator("preference");
        itr.hasNext(); ) {
      Element preference = (Element) itr.next();

      if (preference.element("name").getText().equalsIgnoreCase(preferenceName)) {
        _preference = preference.element("value").getText();
      }
    }

    return _preference;
  }
  @Override
  public Map<Long, PortletPreferences> getPortletSetupMap(
      long companyId,
      long groupId,
      long ownerId,
      int ownerType,
      String portletId,
      boolean privateLayout) {

    Map<Long, PortletPreferences> portletSetupMap = new HashMap<Long, PortletPreferences>();

    List<com.liferay.portal.model.PortletPreferences> portletPreferencesList =
        PortletPreferencesLocalServiceUtil.getPortletPreferences(
            companyId, groupId, ownerId, ownerType, portletId, privateLayout);

    for (com.liferay.portal.model.PortletPreferences portletPreferences : portletPreferencesList) {

      PortletPreferences portletSetup =
          PortletPreferencesLocalServiceUtil.getPreferences(
              companyId, ownerId, ownerType, portletPreferences.getPlid(), portletId);

      portletSetupMap.put(portletPreferences.getPlid(), portletSetup);
    }

    return portletSetupMap;
  }
  protected void importServicePortletPreferences(
      PortletDataContext portletDataContext, Element serviceElement) throws PortalException {

    long ownerId = GetterUtil.getLong(serviceElement.attributeValue("owner-id"));
    int ownerType = GetterUtil.getInteger(serviceElement.attributeValue("owner-type"));
    String serviceName = serviceElement.attributeValue("service-name");

    PortletPreferences portletPreferences =
        getPortletPreferences(
            portletDataContext.getCompanyId(),
            ownerId,
            ownerType,
            LayoutConstants.DEFAULT_PLID,
            serviceName);

    for (Attribute attribute : serviceElement.attributes()) {
      serviceElement.remove(attribute);
    }

    String xml = serviceElement.asXML();

    portletPreferences.setPreferences(xml);

    _portletPreferencesLocalService.updatePortletPreferences(portletPreferences);
  }
  public static PortletPreferencesImpl toPortletPreferencesImpl(
      PortletPreferences portletPreferences) throws Exception {

    return (PortletPreferencesImpl)
        PortletPreferencesFactoryUtil.fromXML(
            TestPropsValues.getCompanyId(),
            portletPreferences.getOwnerId(),
            portletPreferences.getOwnerType(),
            portletPreferences.getPlid(),
            portletPreferences.getPortletId(),
            portletPreferences.getPreferences());
  }
  private long _getPortletPreferencesId(long plid, String portletId)
      throws PortalException, SystemException {

    com.liferay.portal.model.PortletPreferences portletPreferences =
        PortletPreferencesLocalServiceUtil.getPortletPreferences(
            PortletKeys.PREFS_OWNER_ID_DEFAULT,
            PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
            plid,
            portletId);

    return portletPreferences.getPortletPreferencesId();
  }
  protected void deletePortletSetup(String portletId) {
    try {
      List<PortletPreferences> portletPreferencesList =
          PortletPreferencesLocalServiceUtil.getPortletPreferences(getPlid(), portletId);

      for (PortletPreferences portletPreferences : portletPreferencesList) {

        PortletPreferencesLocalServiceUtil.deletePortletPreferences(
            portletPreferences.getPortletPreferencesId());
      }
    } catch (Exception e) {
      _log.error(e, e);
    }
  }
  public PortletPreferences updatePreferences(PortletPreferencesPK pk, PortletPreferencesImpl prefs)
      throws PortalException, SystemException {

    PortletPreferences portletPrefences = PortletPreferencesUtil.findByPrimaryKey(pk);

    String xml = PortletPreferencesSerializer.toXML(prefs);

    PortletPreferencesLocalUtil.getPreferencesPool(pk.userId).put(pk, prefs);

    portletPrefences.setPreferences(xml);

    PortletPreferencesUtil.update(portletPrefences);

    return portletPrefences;
  }
  public com.dotcms.repackage.portlet.javax.portlet.PortletPreferences getPreferences(
      String companyId, PortletPreferencesPK pk) throws PortalException, SystemException {

    //		String groupId = Layout.getGroupId(pk.layoutId);

    //		if (groupId != null) {
    //			if (!GetterUtil.getBoolean(
    //					PropsUtil.get(PropsUtil.GROUP_PAGES_PERSONALIZATION))) {
    //
    //				pk.userId = Layout.GROUP + groupId;
    //			}
    //		}

    Map prefsPool = PortletPreferencesLocalUtil.getPreferencesPool(pk.userId);

    PortletPreferencesImpl prefs = (PortletPreferencesImpl) prefsPool.get(pk);

    if (prefs == null) {
      PortletPreferences portletPreferences = null;

      Portlet portlet = null;
      //			if (groupId != null) {
      //				portlet = PortletManagerUtil.getPortletById(
      //					companyId, groupId, pk.portletId);
      //			}
      //			else {
      portlet = PortletManagerUtil.getPortletById(companyId, pk.portletId);
      //			}

      try {
        portletPreferences = PortletPreferencesUtil.findByPrimaryKey(pk);
      } catch (NoSuchPortletPreferencesException nsppe) {
        portletPreferences = PortletPreferencesUtil.create(pk);

        portletPreferences.setPreferences(portlet.getDefaultPreferences());

        PortletPreferencesUtil.update(portletPreferences);
      }

      prefs =
          PortletPreferencesSerializer.fromXML(companyId, pk, portletPreferences.getPreferences());

      prefsPool.put(pk, prefs);
    }

    return (PortletPreferencesImpl) prefs.clone();
  }
  protected String deletePortletData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences)
      throws Exception {

    Portlet portlet =
        PortletLocalServiceUtil.getPortletById(portletDataContext.getCompanyId(), portletId);

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not delete portlet data for " + portletId + " because the portlet does not exist");
      }

      return null;
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if (portletDataHandler == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not delete portlet data for "
                + portletId
                + " because the portlet does not have a "
                + "PortletDataHandler");
      }

      return null;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Deleting data for " + portletId);
    }

    PortletPreferencesImpl portletPreferencesImpl =
        (PortletPreferencesImpl)
            PortletPreferencesFactoryUtil.fromDefaultXML(portletPreferences.getPreferences());

    try {
      portletPreferencesImpl =
          (PortletPreferencesImpl)
              portletDataHandler.deleteData(portletDataContext, portletId, portletPreferencesImpl);
    } finally {
      portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
    }

    if (portletPreferencesImpl == null) {
      return null;
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
  }
  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }

    if (!(obj instanceof PortletPreferences)) {
      return false;
    }

    PortletPreferences portletPreferences = (PortletPreferences) obj;

    long primaryKey = portletPreferences.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  @Override
  public int compareTo(PortletPreferences portletPreferences) {
    long primaryKey = portletPreferences.getPrimaryKey();

    if (getPrimaryKey() < primaryKey) {
      return -1;
    } else if (getPrimaryKey() > primaryKey) {
      return 1;
    } else {
      return 0;
    }
  }
  @Test
  public void testCleanUpLayoutRevisionPortletPreferences() throws Exception {
    LayoutRevision layoutRevision = getLayoutRevision();

    PortletPreferences portletPreferences =
        PortletPreferencesLocalServiceUtil.addPortletPreferences(
            TestPropsValues.getCompanyId(),
            TestPropsValues.getUserId(),
            0,
            layoutRevision.getLayoutRevisionId(),
            RandomTestUtil.randomString(),
            new PortletImpl(),
            StringPool.BLANK);

    Assert.assertNotNull(portletPreferences);

    doVerify();

    portletPreferences =
        PortletPreferencesLocalServiceUtil.fetchPortletPreferences(
            portletPreferences.getPortletPreferencesId());

    Assert.assertNull(portletPreferences);
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static PortletPreferences toModel(PortletPreferencesSoap soapModel) {
    if (soapModel == null) {
      return null;
    }

    PortletPreferences model = new PortletPreferencesImpl();

    model.setMvccVersion(soapModel.getMvccVersion());
    model.setPortletPreferencesId(soapModel.getPortletPreferencesId());
    model.setOwnerId(soapModel.getOwnerId());
    model.setOwnerType(soapModel.getOwnerType());
    model.setPlid(soapModel.getPlid());
    model.setPortletId(soapModel.getPortletId());
    model.setPreferences(soapModel.getPreferences());

    return model;
  }
  protected String importPortletData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      Element portletDataElement)
      throws Exception {

    Portlet portlet =
        PortletLocalServiceUtil.getPortletById(portletDataContext.getCompanyId(), portletId);

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not import portlet data for " + portletId + " because the portlet does not exist");
      }

      return null;
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if (portletDataHandler == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not import portlet data for "
                + portletId
                + " because the portlet does not have a "
                + "PortletDataHandler");
      }

      return null;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Importing data for " + portletId);
    }

    PortletPreferencesImpl portletPreferencesImpl = null;

    if (portletPreferences != null) {
      portletPreferencesImpl =
          (PortletPreferencesImpl)
              PortletPreferencesFactoryUtil.fromDefaultXML(portletPreferences.getPreferences());
    }

    String portletData =
        portletDataContext.getZipEntryAsString(portletDataElement.attributeValue("path"));

    try {
      AssetValidatorThreadLocal.setEnabled(false);
      SocialActivityThreadLocal.setEnabled(false);
      WorkflowThreadLocal.setEnabled(false);

      portletPreferencesImpl =
          (PortletPreferencesImpl)
              portletDataHandler.importData(
                  portletDataContext, portletId, portletPreferencesImpl, portletData);
    } catch (Exception e) {
      throw e;
    } finally {
      AssetValidatorThreadLocal.setEnabled(true);
      SocialActivityThreadLocal.setEnabled(true);
      WorkflowThreadLocal.setEnabled(true);
    }

    if (portletPreferencesImpl == null) {
      return null;
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
  }
  public String importPortletData(
      PortletDataContext portletDataContext,
      PortletPreferences portletPreferences,
      Element portletDataElement)
      throws Exception {

    Portlet portlet =
        _portletLocalService.getPortletById(
            portletDataContext.getCompanyId(), portletDataContext.getPortletId());

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not import portlet data for "
                + portletDataContext.getPortletId()
                + " because the portlet does not exist");
      }

      return null;
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if ((portletDataHandler == null) || portletDataHandler.isDataPortletInstanceLevel()) {

      if (_log.isDebugEnabled()) {
        StringBundler sb = new StringBundler(4);

        sb.append("Do not import portlet data for ");
        sb.append(portletDataContext.getPortletId());
        sb.append(" because the portlet does not have a portlet data ");
        sb.append("handler");

        _log.debug(sb.toString());
      }

      return null;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Importing data for " + portletDataContext.getPortletId());
    }

    PortletPreferencesImpl portletPreferencesImpl = null;

    if (portletPreferences != null) {
      portletPreferencesImpl =
          (PortletPreferencesImpl)
              PortletPreferencesFactoryUtil.fromDefaultXML(portletPreferences.getPreferences());
    }

    String portletData =
        portletDataContext.getZipEntryAsString(portletDataElement.attributeValue("path"));

    if (Validator.isNull(portletData)) {
      return null;
    }

    portletPreferencesImpl =
        (PortletPreferencesImpl)
            portletDataHandler.importData(
                portletDataContext,
                portletDataContext.getPortletId(),
                portletPreferencesImpl,
                portletData);

    if (portletPreferencesImpl == null) {
      return null;
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
  }
  protected String deletePortletData(
      PortletDataContext portletDataContext, PortletPreferences portletPreferences)
      throws Exception {

    Group group = _groupLocalService.getGroup(portletDataContext.getGroupId());

    if (!group.isStagedPortlet(portletDataContext.getPortletId())) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not delete portlet data for "
                + portletDataContext.getPortletId()
                + " because the portlet is not staged");
      }

      return null;
    }

    Portlet portlet =
        _portletLocalService.getPortletById(
            portletDataContext.getCompanyId(), portletDataContext.getPortletId());

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not delete portlet data for "
                + portletDataContext.getPortletId()
                + " because the portlet does not exist");
      }

      return null;
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if (portletDataHandler == null) {
      if (_log.isDebugEnabled()) {
        StringBundler sb = new StringBundler(4);

        sb.append("Do not delete portlet data for ");
        sb.append(portletDataContext.getPortletId());
        sb.append(" because the portlet does not have a ");
        sb.append("PortletDataHandler");

        _log.debug(sb.toString());
      }

      return null;
    }

    if (_log.isDebugEnabled()) {
      _log.debug("Deleting data for " + portletDataContext.getPortletId());
    }

    PortletPreferencesImpl portletPreferencesImpl =
        (PortletPreferencesImpl)
            PortletPreferencesFactoryUtil.fromDefaultXML(portletPreferences.getPreferences());

    try {
      portletPreferencesImpl =
          (PortletPreferencesImpl)
              portletDataHandler.deleteData(
                  portletDataContext, portletDataContext.getPortletId(), portletPreferencesImpl);
    } finally {
      portletDataContext.setGroupId(portletDataContext.getScopeGroupId());
    }

    if (portletPreferencesImpl == null) {
      return null;
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferencesImpl);
  }
  private void _checkAssetEntries(com.liferay.portal.model.PortletPreferences portletPreferences)
      throws PortalException, SystemException {

    Layout layout = LayoutLocalServiceUtil.getLayout(portletPreferences.getPlid());

    PortletPreferences preferences =
        PortletPreferencesFactoryUtil.fromXML(
            layout.getCompanyId(),
            portletPreferences.getOwnerId(),
            portletPreferences.getOwnerType(),
            portletPreferences.getPlid(),
            portletPreferences.getPortletId(),
            portletPreferences.getPreferences());

    if (!getEmailAssetEntryAddedEnabled(preferences)) {
      return;
    }

    List<AssetEntry> assetEntries =
        getAssetEntries(
            preferences,
            layout,
            layout.getGroupId(),
            PropsValues.ASSET_PUBLISHER_DYNAMIC_SUBSCRIPTION_LIMIT,
            false);

    if (assetEntries.isEmpty()) {
      return;
    }

    long[] notifiedAssetEntryIds =
        GetterUtil.getLongValues(preferences.getValues("notifiedAssetEntryIds", null));

    List<AssetEntry> newAssetEntries = new ArrayList<AssetEntry>();

    for (int i = 0; i < assetEntries.size(); i++) {
      AssetEntry assetEntry = assetEntries.get(i);

      if (!ArrayUtil.contains(notifiedAssetEntryIds, assetEntry.getEntryId())) {

        newAssetEntries.add(assetEntry);
      }
    }

    notifySubscribers(
        preferences,
        portletPreferences.getPlid(),
        portletPreferences.getPortletId(),
        newAssetEntries);

    try {
      preferences.setValues(
          "notifiedAssetEntryIds",
          StringUtil.split(ListUtil.toString(assetEntries, AssetEntry.ENTRY_ID_ACCESSOR)));

      preferences.store();
    } catch (IOException ioe) {
      throw new SystemException(ioe);
    } catch (PortletException pe) {
      throw new SystemException(pe);
    }
  }
  protected List<Portlet> getEmbeddedPortlets(
      List<Portlet> columnPortlets, List<Portlet> staticPortlets) {

    if (_embeddedPortlets != null) {
      return _embeddedPortlets;
    }

    List<Portlet> portlets = new ArrayList<>();

    Layout layout = getLayout();

    List<PortletPreferences> portletPreferences =
        PortletPreferencesLocalServiceUtil.getPortletPreferences(
            PortletKeys.PREFS_OWNER_ID_DEFAULT,
            PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
            layout.getPlid());

    if (isCustomizable() && hasUserPreferences()) {
      portletPreferences = ListUtil.copy(portletPreferences);

      portletPreferences.addAll(
          PortletPreferencesLocalServiceUtil.getPortletPreferences(
              _portalPreferences.getUserId(), PortletKeys.PREFS_OWNER_TYPE_USER, layout.getPlid()));
    }

    for (PortletPreferences portletPreference : portletPreferences) {
      String portletId = portletPreference.getPortletId();

      Portlet portlet = PortletLocalServiceUtil.getPortletById(getCompanyId(), portletId);

      if (Validator.isNull(portletId)
          || columnPortlets.contains(portlet)
          || staticPortlets.contains(portlet)
          || !portlet.isReady()
          || portlet.isUndeployedPortlet()
          || !portlet.isActive()) {

        continue;
      }

      if (portlet != null) {
        Portlet embeddedPortlet = portlet;

        if (portlet.isInstanceable()) {

          // Instanceable portlets do not need to be cloned because
          // they are already cloned. See the method getPortletById in
          // the class PortletLocalServiceImpl and how it references
          // the method getClonedInstance in the class PortletImpl.

        } else {
          embeddedPortlet = (Portlet) embeddedPortlet.clone();
        }

        // We set embedded portlets as static on order to avoid adding
        // the close and/or move icons.

        embeddedPortlet.setStatic(true);

        portlets.add(embeddedPortlet);
      }
    }

    _embeddedPortlets = portlets;

    return _embeddedPortlets;
  }
  protected void exportPortlet(
      PortletDataContext portletDataContext,
      LayoutCache layoutCache,
      String portletId,
      Layout layout,
      Element parentElement,
      long defaultUserId,
      boolean exportPermissions,
      boolean exportPortletArchivedSetups,
      boolean exportPortletData,
      boolean exportPortletSetup,
      boolean exportPortletUserPreferences,
      boolean exportUserPermissions)
      throws Exception {

    Portlet portlet =
        PortletLocalServiceUtil.getPortletById(portletDataContext.getCompanyId(), portletId);

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug("Do not export portlet " + portletId + " because the portlet does not exist");
      }

      return;
    }

    if ((!portlet.isInstanceable())
        && (!portlet.isPreferencesUniquePerLayout())
        && (portletDataContext.hasNotUniquePerLayout(portletId))) {

      return;
    }

    Document document = SAXReaderUtil.createDocument();

    Element portletElement = document.addElement("portlet");

    portletElement.addAttribute("portlet-id", portletId);
    portletElement.addAttribute("root-portlet-id", PortletConstants.getRootPortletId(portletId));
    portletElement.addAttribute("old-plid", String.valueOf(layout.getPlid()));
    portletElement.addAttribute("scope-layout-type", portletDataContext.getScopeType());
    portletElement.addAttribute("scope-layout-uuid", portletDataContext.getScopeLayoutUuid());

    // Data

    if (exportPortletData) {
      javax.portlet.PortletPreferences jxPreferences =
          PortletPreferencesFactoryUtil.getPortletSetup(layout, portletId, StringPool.BLANK);

      if (!portlet.isPreferencesUniquePerLayout()) {
        StringBundler sb = new StringBundler(5);

        sb.append(portletId);
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeType());
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeLayoutUuid());

        String dataKey = sb.toString();

        if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
          portletDataContext.putNotUniquePerLayout(dataKey);

          exportPortletData(portletDataContext, portlet, layout, jxPreferences, portletElement);
        }
      } else {
        exportPortletData(portletDataContext, portlet, layout, jxPreferences, portletElement);
      }
    }

    // Portlet preferences

    long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;

    if (layout != null) {
      plid = layout.getPlid();
    }

    if (exportPortletSetup) {
      exportPortletPreferences(
          portletDataContext,
          PortletKeys.PREFS_OWNER_ID_DEFAULT,
          PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
          false,
          layout,
          plid,
          portletId,
          portletElement);

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getScopeGroupId(),
          PortletKeys.PREFS_OWNER_TYPE_GROUP,
          false,
          layout,
          plid,
          portletId,
          portletElement);

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getCompanyId(),
          PortletKeys.PREFS_OWNER_TYPE_COMPANY,
          false,
          layout,
          plid,
          portletId,
          portletElement);
    }

    // Portlet preferences

    if (exportPortletUserPreferences) {
      List<PortletPreferences> portletPreferencesList =
          PortletPreferencesLocalServiceUtil.getPortletPreferences(
              PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId);

      for (PortletPreferences portletPreferences : portletPreferencesList) {

        boolean defaultUser = false;

        if (portletPreferences.getOwnerId() == PortletKeys.PREFS_OWNER_ID_DEFAULT) {

          defaultUser = true;
        }

        exportPortletPreferences(
            portletDataContext,
            portletPreferences.getOwnerId(),
            PortletKeys.PREFS_OWNER_TYPE_USER,
            defaultUser,
            layout,
            plid,
            portletId,
            portletElement);
      }

      try {
        PortletPreferences groupPortletPreferences =
            PortletPreferencesLocalServiceUtil.getPortletPreferences(
                portletDataContext.getScopeGroupId(),
                PortletKeys.PREFS_OWNER_TYPE_GROUP,
                PortletKeys.PREFS_PLID_SHARED,
                portletId);

        exportPortletPreference(
            portletDataContext,
            portletDataContext.getScopeGroupId(),
            PortletKeys.PREFS_OWNER_TYPE_GROUP,
            false,
            groupPortletPreferences,
            portletId,
            PortletKeys.PREFS_PLID_SHARED,
            portletElement);
      } catch (NoSuchPortletPreferencesException nsppe) {
      }
    }

    // Archived setups

    if (exportPortletArchivedSetups) {
      String rootPortletId = PortletConstants.getRootPortletId(portletId);

      List<PortletItem> portletItems =
          PortletItemLocalServiceUtil.getPortletItems(
              portletDataContext.getGroupId(), rootPortletId, PortletPreferences.class.getName());

      for (PortletItem portletItem : portletItems) {
        exportPortletPreferences(
            portletDataContext,
            portletItem.getPortletItemId(),
            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED,
            false,
            null,
            plid,
            portletItem.getPortletId(),
            portletElement);
      }
    }

    // Permissions

    if (exportPermissions) {
      _permissionExporter.exportPortletPermissions(
          portletDataContext, layoutCache, portletId, layout, portletElement);
    }

    // Zip

    StringBundler sb = new StringBundler(4);

    sb.append(portletDataContext.getPortletPath(portletId));
    sb.append(StringPool.SLASH);
    sb.append(layout.getPlid());
    sb.append("/portlet.xml");

    String path = sb.toString();

    Element element = parentElement.addElement("portlet");

    element.addAttribute("portlet-id", portletId);
    element.addAttribute("layout-id", String.valueOf(layout.getLayoutId()));
    element.addAttribute("path", path);

    if (portletDataContext.isPathNotProcessed(path)) {
      try {
        portletDataContext.addZipEntry(path, document.formattedString());
      } catch (IOException ioe) {
        if (_log.isWarnEnabled()) {
          _log.warn(ioe.getMessage());
        }
      }

      portletDataContext.addPrimaryKey(String.class, path);
    }
  }
  protected void exportPortletPreference(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      boolean defaultUser,
      PortletPreferences portletPreferences,
      String portletId,
      long plid,
      Element parentElement)
      throws Exception {

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
      preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }

    Document document = SAXReaderUtil.read(preferencesXML);

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
    rootElement.addAttribute("plid", String.valueOf(plid));
    rootElement.addAttribute("portlet-id", portletId);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
      PortletItem portletItem = PortletItemLocalServiceUtil.getPortletItem(ownerId);

      rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
      rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
      User user = UserLocalServiceUtil.fetchUserById(ownerId);

      if (user == null) {
        return;
      }

      rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes =
        document.selectNodes(
            "/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
      document.remove(node);
    }

    String path =
        getPortletPreferencesPath(portletDataContext, portletId, ownerId, ownerType, plid);

    Element portletPreferencesElement = parentElement.addElement("portlet-preferences");

    portletPreferencesElement.addAttribute("path", path);

    if (portletDataContext.isPathNotProcessed(path)) {
      portletDataContext.addZipEntry(path, document.formattedString(StringPool.TAB, false, false));
    }
  }
  protected void exportServicePortletPreference(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      PortletPreferences portletPreferences,
      String serviceName,
      Element parentElement)
      throws Exception {

    String path =
        ExportImportPathUtil.getServicePortletPreferencesPath(
            portletDataContext, serviceName, ownerId, ownerType);

    if (portletDataContext.isPathProcessed(path)) {
      return;
    }

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
      preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }

    javax.portlet.PortletPreferences jxPortletPreferences =
        PortletPreferencesFactoryUtil.fromDefaultXML(preferencesXML);

    Element serviceElement = parentElement.addElement("service");

    serviceElement.addAttribute("service-name", serviceName);

    Document document =
        SAXReaderUtil.read(PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(false));
    rootElement.addAttribute("service-name", serviceName);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
      PortletItem portletItem = _portletItemLocalService.getPortletItem(ownerId);

      rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
      rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
      User user = _userLocalService.fetchUserById(ownerId);

      if (user == null) {
        return;
      }

      rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes =
        document.selectNodes(
            "/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
      document.remove(node);
    }

    serviceElement.addAttribute("path", path);

    portletDataContext.addZipEntry(path, document.formattedString());
  }
  protected void exportPortletPreference(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      boolean defaultUser,
      PortletPreferences portletPreferences,
      String portletId,
      long plid,
      Element parentElement)
      throws Exception {

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
      preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }

    javax.portlet.PortletPreferences jxPortletPreferences =
        PortletPreferencesFactoryUtil.fromDefaultXML(preferencesXML);

    Portlet portlet =
        _portletLocalService.getPortletById(portletDataContext.getCompanyId(), portletId);

    Element portletPreferencesElement = parentElement.addElement("portlet-preferences");

    if ((portlet != null) && (portlet.getPortletDataHandlerInstance() != null)) {

      Element exportDataRootElement = portletDataContext.getExportDataRootElement();

      try {
        portletDataContext.clearScopedPrimaryKeys();

        Element preferenceDataElement = portletPreferencesElement.addElement("preference-data");

        portletDataContext.setExportDataRootElement(preferenceDataElement);

        ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor =
            ExportImportPortletPreferencesProcessorRegistryUtil
                .getExportImportPortletPreferencesProcessor(portlet.getRootPortletId());

        if (exportImportPortletPreferencesProcessor != null) {
          List<Capability> exportCapabilities =
              exportImportPortletPreferencesProcessor.getExportCapabilities();

          if (ListUtil.isNotEmpty(exportCapabilities)) {
            for (Capability exportCapability : exportCapabilities) {
              exportCapability.process(portletDataContext, jxPortletPreferences);
            }
          }

          exportImportPortletPreferencesProcessor.processExportPortletPreferences(
              portletDataContext, jxPortletPreferences);
        } else {
          PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

          jxPortletPreferences =
              portletDataHandler.processExportPortletPreferences(
                  portletDataContext, portletId, jxPortletPreferences);
        }
      } finally {
        portletDataContext.setExportDataRootElement(exportDataRootElement);
      }
    }

    Document document =
        SAXReaderUtil.read(PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
    rootElement.addAttribute("plid", String.valueOf(plid));
    rootElement.addAttribute("portlet-id", portletId);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
      PortletItem portletItem = _portletItemLocalService.getPortletItem(ownerId);

      rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
      rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
      User user = _userLocalService.fetchUserById(ownerId);

      if (user == null) {
        return;
      }

      rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes =
        document.selectNodes(
            "/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
      document.remove(node);
    }

    String path =
        ExportImportPathUtil.getPortletPreferencesPath(
            portletDataContext, portletId, ownerId, ownerType, plid);

    portletPreferencesElement.addAttribute("path", path);

    portletDataContext.addZipEntry(path, document.formattedString(StringPool.TAB, false, false));
  }
  protected void exportPortlet(
      PortletDataContext portletDataContext,
      Layout layout,
      Element parentElement,
      boolean exportPermissions,
      boolean exportPortletArchivedSetups,
      boolean exportPortletData,
      boolean exportPortletSetup,
      boolean exportPortletUserPreferences)
      throws Exception {

    long plid = PortletKeys.PREFS_OWNER_ID_DEFAULT;
    long layoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;

    if (layout != null) {
      plid = layout.getPlid();
      layoutId = layout.getLayoutId();
    }

    Portlet portlet =
        _portletLocalService.getPortletById(
            portletDataContext.getCompanyId(), portletDataContext.getPortletId());

    if ((portlet == null) || portlet.isUndeployedPortlet()) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not export portlet "
                + portletDataContext.getPortletId()
                + " because the portlet is not deployed");
      }

      return;
    }

    if (!portlet.isInstanceable()
        && !portlet.isPreferencesUniquePerLayout()
        && portletDataContext.hasNotUniquePerLayout(portletDataContext.getPortletId())) {

      return;
    }

    if (BackgroundTaskThreadLocal.hasBackgroundTask()) {
      PortletDataContext clonedPortletDataContext =
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext);

      ManifestSummary manifestSummary = clonedPortletDataContext.getManifestSummary();

      manifestSummary.resetCounters();

      PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

      portletDataHandler.prepareManifestSummary(clonedPortletDataContext);

      PortletDataHandlerStatusMessageSenderUtil.sendStatusMessage(
          "portlet", portletDataContext.getPortletId(), manifestSummary);
    }

    Document document = SAXReaderUtil.createDocument();

    Element portletElement = document.addElement("portlet");

    portletElement.addAttribute("portlet-id", portletDataContext.getPortletId());
    portletElement.addAttribute("root-portlet-id", portletDataContext.getRootPortletId());
    portletElement.addAttribute("old-plid", String.valueOf(plid));
    portletElement.addAttribute(
        "scope-group-id", String.valueOf(portletDataContext.getScopeGroupId()));
    portletElement.addAttribute("scope-layout-type", portletDataContext.getScopeType());
    portletElement.addAttribute("scope-layout-uuid", portletDataContext.getScopeLayoutUuid());
    portletElement.addAttribute("private-layout", String.valueOf(layout.isPrivateLayout()));

    // Data

    if (exportPortletData) {
      javax.portlet.PortletPreferences jxPortletPreferences = null;

      if (ExportImportThreadLocal.isInitialLayoutStagingInProcess()) {
        Group liveGroup = layout.getGroup();

        Group stagingGroup = liveGroup.getStagingGroup();

        layout.setGroupId(stagingGroup.getGroupId());

        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(
                layout, portletDataContext.getPortletId());

        layout.setGroupId(liveGroup.getGroupId());
      } else {
        jxPortletPreferences =
            PortletPreferencesFactoryUtil.getStrictPortletSetup(
                layout, portletDataContext.getPortletId());
      }

      if (!portlet.isPreferencesUniquePerLayout()) {
        StringBundler sb = new StringBundler(5);

        sb.append(portletDataContext.getPortletId());
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeType());
        sb.append(StringPool.AT);
        sb.append(portletDataContext.getScopeLayoutUuid());

        String dataKey = sb.toString();

        if (!portletDataContext.hasNotUniquePerLayout(dataKey)) {
          portletDataContext.putNotUniquePerLayout(dataKey);

          exportPortletData(
              portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
        }
      } else {
        exportPortletData(
            portletDataContext, portlet, layout, jxPortletPreferences, portletElement);
      }
    }

    // Portlet preferences

    if (exportPortletSetup) {

      // Company

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getCompanyId(),
          PortletKeys.PREFS_OWNER_TYPE_COMPANY,
          false,
          layout,
          plid,
          portlet.getRootPortletId(),
          portletElement);

      // Group

      exportPortletPreferences(
          portletDataContext,
          portletDataContext.getScopeGroupId(),
          PortletKeys.PREFS_OWNER_TYPE_GROUP,
          false,
          layout,
          PortletKeys.PREFS_PLID_SHARED,
          portlet.getRootPortletId(),
          portletElement);

      // Layout

      exportPortletPreferences(
          portletDataContext,
          PortletKeys.PREFS_OWNER_ID_DEFAULT,
          PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
          false,
          layout,
          plid,
          portletDataContext.getPortletId(),
          portletElement);
    }

    // Portlet user preferences

    if (exportPortletUserPreferences) {
      List<PortletPreferences> portletPreferencesList =
          _portletPreferencesLocalService.getPortletPreferences(
              PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletDataContext.getPortletId());

      for (PortletPreferences portletPreferences : portletPreferencesList) {

        boolean defaultUser = false;

        if (portletPreferences.getOwnerId() == PortletKeys.PREFS_OWNER_ID_DEFAULT) {

          defaultUser = true;
        }

        exportPortletPreferences(
            portletDataContext,
            portletPreferences.getOwnerId(),
            PortletKeys.PREFS_OWNER_TYPE_USER,
            defaultUser,
            layout,
            plid,
            portletDataContext.getPortletId(),
            portletElement);
      }

      try {
        PortletPreferences groupPortletPreferences =
            _portletPreferencesLocalService.getPortletPreferences(
                portletDataContext.getScopeGroupId(),
                PortletKeys.PREFS_OWNER_TYPE_GROUP,
                PortletKeys.PREFS_PLID_SHARED,
                portlet.getRootPortletId());

        exportPortletPreference(
            portletDataContext,
            portletDataContext.getScopeGroupId(),
            PortletKeys.PREFS_OWNER_TYPE_GROUP,
            false,
            groupPortletPreferences,
            portlet.getRootPortletId(),
            PortletKeys.PREFS_PLID_SHARED,
            portletElement);
      } catch (NoSuchPortletPreferencesException nsppe) {
      }
    }

    // Archived setups

    if (exportPortletArchivedSetups) {
      List<PortletItem> portletItems =
          _portletItemLocalService.getPortletItems(
              portletDataContext.getGroupId(),
              portletDataContext.getRootPortletId(),
              PortletPreferences.class.getName());

      for (PortletItem portletItem : portletItems) {
        exportPortletPreferences(
            portletDataContext,
            portletItem.getPortletItemId(),
            PortletKeys.PREFS_OWNER_TYPE_ARCHIVED,
            false,
            null,
            plid,
            portletItem.getPortletId(),
            portletElement);
      }
    }

    // Permissions

    if (exportPermissions) {
      _permissionExporter.exportPortletPermissions(
          portletDataContext, portletDataContext.getPortletId(), layout, portletElement);
    }

    // Zip

    StringBundler pathSB = new StringBundler(4);

    pathSB.append(ExportImportPathUtil.getPortletPath(portletDataContext));
    pathSB.append(StringPool.SLASH);
    pathSB.append(plid);
    pathSB.append("/portlet.xml");

    String path = pathSB.toString();

    Element element = parentElement.addElement("portlet");

    element.addAttribute("portlet-id", portletDataContext.getPortletId());
    element.addAttribute("layout-id", String.valueOf(layoutId));
    element.addAttribute("path", path);
    element.addAttribute("portlet-data", String.valueOf(exportPortletData));

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    element.addAttribute("schema-version", portletDataHandler.getSchemaVersion());

    StringBundler configurationOptionsSB = new StringBundler(6);

    if (exportPortletSetup) {
      configurationOptionsSB.append("setup");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletArchivedSetups) {
      configurationOptionsSB.append("archived-setups");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (exportPortletUserPreferences) {
      configurationOptionsSB.append("user-preferences");
      configurationOptionsSB.append(StringPool.COMMA);
    }

    if (configurationOptionsSB.index() > 0) {
      configurationOptionsSB.setIndex(configurationOptionsSB.index() - 1);
    }

    element.addAttribute("portlet-configuration", configurationOptionsSB.toString());

    try {
      portletDataContext.addZipEntry(path, document.formattedString());
    } catch (IOException ioe) {
      if (_log.isWarnEnabled()) {
        _log.warn(ioe.getMessage());
      }
    }
  }