protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration) throws PortalException {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    long sourcePlid = MapUtil.getLong(settingsMap, "sourcePlid");
    long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
    String portletId = MapUtil.getString(settingsMap, "portletId");
    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    DateRange dateRange = ExportImportDateUtil.getDateRange(exportImportConfiguration);

    Layout layout = _layoutLocalService.getLayout(sourcePlid);
    ZipWriter zipWriter = ExportImportHelperUtil.getPortletZipWriter(portletId);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            layout.getCompanyId(),
            sourceGroupId,
            parameterMap,
            dateRange.getStartDate(),
            dateRange.getEndDate(),
            zipWriter);

    portletDataContext.setOldPlid(sourcePlid);
    portletDataContext.setPlid(sourcePlid);
    portletDataContext.setPortletId(portletId);

    return portletDataContext;
  }
  protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration, File file) throws PortalException {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    String portletId = MapUtil.getString(settingsMap, "portletId");
    long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    long userId = MapUtil.getLong(settingsMap, "userId");

    Layout layout = _layoutLocalService.getLayout(targetPlid);

    String userIdStrategyString =
        MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);

    UserIdStrategy userIdStrategy =
        ExportImportHelperUtil.getUserIdStrategy(userId, userIdStrategyString);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            layout.getCompanyId(), targetGroupId, parameterMap, userIdStrategy, zipReader);

    portletDataContext.setOldPlid(targetPlid);
    portletDataContext.setPlid(targetPlid);
    portletDataContext.setPortletId(portletId);
    portletDataContext.setPrivateLayout(layout.isPrivateLayout());

    return portletDataContext;
  }
  protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration) throws Exception {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
    boolean privateLayout = MapUtil.getBoolean(settingsMap, "privateLayout");
    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    DateRange dateRange = ExportImportDateUtil.getDateRange(exportImportConfiguration);

    Group group = _groupLocalService.getGroup(sourceGroupId);
    ZipWriter zipWriter = ExportImportHelperUtil.getLayoutSetZipWriter(sourceGroupId);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            group.getCompanyId(),
            sourceGroupId,
            parameterMap,
            dateRange.getStartDate(),
            dateRange.getEndDate(),
            zipWriter);

    portletDataContext.setPrivateLayout(privateLayout);

    return portletDataContext;
  }
  @Override
  protected void doReceive(Message message, MessageStatus messageStatus) throws PortalException {

    long exportImportConfigurationId = GetterUtil.getLong(message.getPayload());

    ExportImportConfiguration exportImportConfiguration =
        _exportImportConfigurationLocalService.fetchExportImportConfiguration(
            exportImportConfigurationId);

    if (exportImportConfiguration == null) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "Unable to find export import configuration with ID " + exportImportConfigurationId);
      }

      return;
    }

    messageStatus.setPayload(exportImportConfiguration);

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    long userId = MapUtil.getLong(settingsMap, "userId");
    long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    boolean privateLayout = MapUtil.getBoolean(settingsMap, "privateLayout");
    long[] layoutIds = GetterUtil.getLongValues(settingsMap.get("layoutIds"));
    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");

    initThreadLocals(userId, parameterMap);

    try {
      StagingUtil.publishLayouts(
          userId,
          sourceGroupId,
          targetGroupId,
          privateLayout,
          layoutIds,
          exportImportConfiguration.getName(),
          parameterMap);
    } finally {
      resetThreadLocals();
    }
  }
  @Override
  public void importDataDeletions(ExportImportConfiguration exportImportConfiguration, File file)
      throws Exception {

    ZipReader zipReader = null;

    try {

      // LAR validation

      ExportImportThreadLocal.setPortletDataDeletionImportInProcess(true);

      Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

      Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
      String portletId = MapUtil.getString(settingsMap, "portletId");
      long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
      long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");

      Layout layout = _layoutLocalService.getLayout(targetPlid);

      zipReader = ZipReaderFactoryUtil.getZipReader(file);

      validateFile(layout.getCompanyId(), targetGroupId, portletId, zipReader);

      PortletDataContext portletDataContext =
          getPortletDataContext(exportImportConfiguration, file);

      boolean deletePortletData =
          MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.DELETE_PORTLET_DATA);

      // Portlet data deletion

      if (deletePortletData) {
        if (_log.isDebugEnabled()) {
          _log.debug("Deleting portlet data");
        }

        deletePortletData(portletDataContext);
      }

      // Deletion system events

      populateDeletionStagedModelTypes(portletDataContext);

      _deletionSystemEventImporter.importDeletionSystemEvents(portletDataContext);
    } finally {
      ExportImportThreadLocal.setPortletDataDeletionImportInProcess(false);

      if (zipReader != null) {
        zipReader.close();
      }
    }
  }
  protected File exportLayoutsAsFile(
      ExportImportConfiguration exportImportConfiguration,
      Map<Long, Boolean> layoutIdMap,
      long remoteGroupId,
      HttpPrincipal httpPrincipal)
      throws PortalException {

    List<Layout> layouts = new ArrayList<>();

    if (layoutIdMap != null) {
      for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
        long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
        boolean includeChildren = entry.getValue();

        Layout layout = LayoutLocalServiceUtil.getLayout(plid);

        if (!layouts.contains(layout)) {
          layouts.add(layout);
        }

        List<Layout> parentLayouts =
            getMissingRemoteParentLayouts(httpPrincipal, layout, remoteGroupId);

        for (Layout parentLayout : parentLayouts) {
          if (!layouts.contains(parentLayout)) {
            layouts.add(parentLayout);
          }
        }

        if (includeChildren) {
          for (Layout childLayout : layout.getAllChildren()) {
            if (!layouts.contains(childLayout)) {
              layouts.add(childLayout);
            }
          }
        }
      }
    }

    long[] layoutIds = ExportImportHelperUtil.getLayoutIds(layouts);

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    settingsMap.remove("layoutIdMap");

    settingsMap.put("layoutIds", layoutIds);

    return ExportImportLocalServiceUtil.exportLayoutsAsFile(exportImportConfiguration);
  }
Ejemplo n.º 7
0
  public static ExportImportConfiguration cloneExportImportConfiguration(
      ExportImportConfiguration exportImportConfiguration) throws PortalException {

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

    return ExportImportConfigurationLocalServiceUtil.addExportImportConfiguration(
        exportImportConfiguration.getUserId(),
        exportImportConfiguration.getGroupId(),
        exportImportConfiguration.getName(),
        exportImportConfiguration.getDescription(),
        exportImportConfiguration.getType(),
        exportImportConfiguration.getSettingsMap(),
        exportImportConfiguration.getStatus(),
        new ServiceContext());
  }
  @Override
  public MissingReferences validateFile(
      ExportImportConfiguration exportImportConfiguration, File file) throws Exception {

    ZipReader zipReader = null;

    try {
      ExportImportThreadLocal.setPortletValidationInProcess(true);

      Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

      String portletId = MapUtil.getString(settingsMap, "portletId");
      long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
      long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");

      Layout layout = _layoutLocalService.getLayout(targetPlid);

      zipReader = ZipReaderFactoryUtil.getZipReader(file);

      validateFile(layout.getCompanyId(), targetGroupId, portletId, zipReader);

      PortletDataContext portletDataContext =
          getPortletDataContext(exportImportConfiguration, file);

      MissingReferences missingReferences =
          ExportImportHelperUtil.validateMissingReferences(portletDataContext);

      Map<String, MissingReference> dependencyMissingReferences =
          missingReferences.getDependencyMissingReferences();

      if (!dependencyMissingReferences.isEmpty()) {
        throw new MissingReferenceException(missingReferences);
      }

      return missingReferences;
    } finally {
      ExportImportThreadLocal.setPortletValidationInProcess(false);

      if (zipReader != null) {
        zipReader.close();
      }
    }
  }
  @Override
  public void importFile(ExportImportConfiguration exportImportConfiguration, File file)
      throws Exception {

    PortletDataContext portletDataContext = null;

    try {
      ExportImportThreadLocal.setPortletImportInProcess(true);

      portletDataContext = getPortletDataContext(exportImportConfiguration, file);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_PORTLET_IMPORT_STARTED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

      Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

      long userId = MapUtil.getLong(settingsMap, "userId");

      doImportPortletInfo(portletDataContext, userId);

      ExportImportThreadLocal.setPortletImportInProcess(false);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_PORTLET_IMPORT_SUCCEEDED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext),
          userId);
    } catch (Throwable t) {
      ExportImportThreadLocal.setPortletImportInProcess(false);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_PORTLET_IMPORT_FAILED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext),
          t);

      throw t;
    }
  }
  @Override
  public File export(ExportImportConfiguration exportImportConfiguration) throws Exception {

    PortletDataContext portletDataContext = null;

    try {
      ExportImportThreadLocal.setLayoutExportInProcess(true);

      Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

      long[] layoutIds = GetterUtil.getLongValues(settingsMap.get("layoutIds"));

      portletDataContext = getPortletDataContext(exportImportConfiguration);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_LAYOUT_EXPORT_STARTED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

      File file = doExport(portletDataContext, layoutIds);

      ExportImportThreadLocal.setLayoutExportInProcess(false);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_LAYOUT_EXPORT_SUCCEEDED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

      return file;
    } catch (Throwable t) {
      ExportImportThreadLocal.setLayoutExportInProcess(false);

      _exportImportLifecycleManager.fireExportImportLifecycleEvent(
          EVENT_LAYOUT_EXPORT_FAILED,
          getProcessFlag(),
          PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext),
          t);

      throw t;
    }
  }
  @Override
  public BackgroundTaskResult execute(BackgroundTask backgroundTask) {
    ExportImportConfiguration exportImportConfiguration =
        getExportImportConfiguration(backgroundTask);

    clearBackgroundTaskStatus(backgroundTask);

    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    File file = null;
    HttpPrincipal httpPrincipal = null;
    MissingReferences missingReferences = null;
    long stagingRequestId = 0L;

    try {
      currentThread.setContextClassLoader(ClassLoaderUtil.getPortalClassLoader());

      ExportImportThreadLocal.setLayoutStagingInProcess(true);

      ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
          EVENT_PUBLICATION_LAYOUT_REMOTE_STARTED,
          PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
          exportImportConfiguration);

      Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

      long sourceGroupId = MapUtil.getLong(settingsMap, "sourceGroupId");
      boolean privateLayout = MapUtil.getBoolean(settingsMap, "privateLayout");

      initThreadLocals(sourceGroupId, privateLayout);

      Map<Long, Boolean> layoutIdMap = (Map<Long, Boolean>) settingsMap.get("layoutIdMap");
      long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");

      Map<String, Serializable> taskContextMap = backgroundTask.getTaskContextMap();

      httpPrincipal = (HttpPrincipal) taskContextMap.get("httpPrincipal");

      file =
          exportLayoutsAsFile(exportImportConfiguration, layoutIdMap, targetGroupId, httpPrincipal);

      String checksum = FileUtil.getMD5Checksum(file);

      stagingRequestId =
          StagingServiceHttp.createStagingRequest(httpPrincipal, targetGroupId, checksum);

      transferFileToRemoteLive(file, stagingRequestId, httpPrincipal);

      markBackgroundTask(backgroundTask.getBackgroundTaskId(), "exported");

      missingReferences =
          StagingServiceHttp.publishStagingRequest(
              httpPrincipal, stagingRequestId, exportImportConfiguration);

      ExportImportThreadLocal.setLayoutStagingInProcess(false);

      ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
          EVENT_PUBLICATION_LAYOUT_REMOTE_SUCCEEDED,
          PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
          exportImportConfiguration);
    } catch (Throwable t) {
      ExportImportThreadLocal.setLayoutStagingInProcess(false);

      ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
          EVENT_PUBLICATION_LAYOUT_REMOTE_FAILED,
          PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
          exportImportConfiguration);

      if (_log.isDebugEnabled()) {
        _log.debug(t, t);
      } else if (_log.isWarnEnabled()) {
        _log.warn("Unable to publish layout: " + t.getMessage());
      }

      deleteTempLarOnFailure(file);

      throw new SystemException(t);
    } finally {
      currentThread.setContextClassLoader(contextClassLoader);

      if ((stagingRequestId > 0) && (httpPrincipal != null)) {
        try {
          StagingServiceHttp.cleanUpStagingRequest(httpPrincipal, stagingRequestId);
        } catch (PortalException pe) {
          _log.warn("Unable to clean up the remote live site");
        }
      }
    }

    deleteTempLarOnSuccess(file);

    return processMissingReferences(backgroundTask.getBackgroundTaskId(), missingReferences);
  }