示例#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;
  }
  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);
  }
  public static PortletPreferencesImpl toPortletPreferencesImpl(
      PortletPreferences portletPreferences) throws Exception {

    return (PortletPreferencesImpl)
        PortletPreferencesFactoryUtil.fromXML(
            TestPropsValues.getCompanyId(),
            portletPreferences.getOwnerId(),
            portletPreferences.getOwnerType(),
            portletPreferences.getPlid(),
            portletPreferences.getPortletId(),
            portletPreferences.getPreferences());
  }
  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, 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);
  }
  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 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 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 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);
  }
  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));
    }
  }
  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);
    }
  }