protected void doExportStagedModel(
      PortletDataContext portletDataContext, CalendarResource calendarResource) throws Exception {

    Element calendarResourceElement = portletDataContext.getExportDataElement(calendarResource);

    for (Calendar calendar : calendarResource.getCalendars()) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, calendarResource, calendar, PortletDataContext.REFERENCE_TYPE_STRONG);
    }

    if (calendarResource.getClassNameId() == PortalUtil.getClassNameId(User.class)) {

      User user = UserLocalServiceUtil.getUser(calendarResource.getClassPK());

      portletDataContext.addReferenceElement(
          calendarResource,
          calendarResourceElement,
          user,
          User.class,
          PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE,
          true);
    }

    portletDataContext.addClassedModel(
        calendarResourceElement,
        ExportImportPathUtil.getModelPath(calendarResource),
        calendarResource);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, DDLRecord record)
      throws Exception {

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext,
        record,
        record.getRecordSet(),
        PortletDataContext.REFERENCE_TYPE_STRONG);

    DDLRecordVersion recordVersion = record.getRecordVersion();

    Fields fields = StorageEngineUtil.getFields(recordVersion.getDDMStorageId());

    String fieldsPath = ExportImportPathUtil.getModelPath(record, "fields.xml");

    portletDataContext.addZipEntry(fieldsPath, fields);

    Element recordElement = portletDataContext.getExportDataElement(record);

    recordElement.addAttribute("fields-path", fieldsPath);

    portletDataContext.addClassedModel(
        recordElement, ExportImportPathUtil.getModelPath(record), record);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, MBMessage message)
      throws Exception {

    if ((message.getStatus() != WorkflowConstants.STATUS_APPROVED)
        || (message.getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {

      return;
    }

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, message.getCategory());

    Element messageElement = portletDataContext.getExportDataStagedModelElement(message);

    message.setPriority(message.getPriority());

    MBThread thread = message.getThread();

    messageElement.addAttribute("question", String.valueOf(thread.isQuestion()));

    boolean hasAttachmentsFileEntries = message.getAttachmentsFileEntriesCount() > 0;

    messageElement.addAttribute(
        "hasAttachmentsFileEntries", String.valueOf(hasAttachmentsFileEntries));

    if (portletDataContext.getBooleanParameter(MBPortletDataHandler.NAMESPACE, "attachments")
        && hasAttachmentsFileEntries) {

      for (FileEntry fileEntry : message.getAttachmentsFileEntries()) {
        String name = fileEntry.getTitle();
        String binPath = ExportImportPathUtil.getModelPath(message, name);

        Element attachmentElement = messageElement.addElement("attachment");

        attachmentElement.addAttribute("name", name);
        attachmentElement.addAttribute("bin-path", binPath);

        portletDataContext.addZipEntry(binPath, fileEntry.getContentStream());
      }

      long folderId = message.getAttachmentsFolderId();

      if (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
        message.setAttachmentsFolderId(folderId);
      }
    }

    portletDataContext.addClassedModel(
        messageElement,
        ExportImportPathUtil.getModelPath(message),
        message,
        MBPortletDataHandler.NAMESPACE);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

    Element fileEntryTypeElement = portletDataContext.getExportDataElement(fileEntryType);

    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      Element referenceElement =
          StagedModelDataHandlerUtil.exportReferenceStagedModel(
              portletDataContext,
              fileEntryType,
              ddmStructure,
              PortletDataContext.REFERENCE_TYPE_STRONG);

      referenceElement.addAttribute(
          "structure-id", StringUtil.valueOf(ddmStructure.getStructureId()));
    }

    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(fileEntryType.getCompanyId());

    if (defaultUserId == fileEntryType.getUserId()) {
      fileEntryTypeElement.addAttribute("preloaded", "true");
    }

    portletDataContext.addClassedModel(
        fileEntryTypeElement, ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

    Element fileEntryTypeElement = portletDataContext.getExportDataElement(fileEntryType);

    List<DDMStructure> ddmStructures = fileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, ddmStructure);

      Element referenceElement =
          portletDataContext.addReferenceElement(
              fileEntryType,
              fileEntryTypeElement,
              ddmStructure,
              PortletDataContext.REFERENCE_TYPE_STRONG,
              false);

      referenceElement.addAttribute(
          "structure-id", StringUtil.valueOf(ddmStructure.getStructureId()));
    }

    portletDataContext.addClassedModel(
        fileEntryTypeElement,
        ExportImportPathUtil.getModelPath(fileEntryType),
        fileEntryType,
        DLPortletDataHandler.NAMESPACE);
  }
Пример #6
0
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, DDLRecordSet recordSet)
      throws Exception {

    DDMStructure ddmStructure = recordSet.getDDMStructure();

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, ddmStructure);

    List<DDMTemplate> ddmTemplates = ddmStructure.getTemplates();

    Element recordSetElement = portletDataContext.getExportDataElement(recordSet);

    for (DDMTemplate ddmTemplate : ddmTemplates) {
      StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, ddmTemplate);

      portletDataContext.addReferenceElement(
          recordSet,
          recordSetElement,
          ddmTemplate,
          PortletDataContext.REFERENCE_TYPE_STRONG,
          false);
    }

    portletDataContext.addClassedModel(
        recordSetElement,
        ExportImportPathUtil.getModelPath(recordSet),
        recordSet,
        DDLPortletDataHandler.NAMESPACE);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Repository repository)
      throws Exception {

    Element repositoryElement = portletDataContext.getExportDataElement(repository);

    Folder folder = DLAppLocalServiceUtil.getFolder(repository.getDlFolderId());

    if (folder.getModel() instanceof DLFolder) {
      DLFolder dlFolder = (DLFolder) folder.getModel();

      repositoryElement.addAttribute("hidden", String.valueOf(dlFolder.isHidden()));
    }

    portletDataContext.addClassedModel(
        repositoryElement, ExportImportPathUtil.getModelPath(repository), repository);

    List<RepositoryEntry> repositoryEntries =
        RepositoryEntryLocalServiceUtil.getRepositoryEntries(repository.getRepositoryId());

    for (RepositoryEntry repositoryEntry : repositoryEntries) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, repository, repositoryEntry, PortletDataContext.REFERENCE_TYPE_CHILD);
    }
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, MBThreadFlag threadFlag)
      throws Exception {

    MBThread thread = MBThreadLocalServiceUtil.getThread(threadFlag.getThreadId());

    MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());

    if ((rootMessage.getStatus() != WorkflowConstants.STATUS_APPROVED)
        || (rootMessage.getCategoryId() == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {

      return;
    }

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, rootMessage);

    Element threadFlagElement = portletDataContext.getExportDataElement(threadFlag);

    threadFlagElement.addAttribute("root-message-id", String.valueOf(rootMessage.getMessageId()));

    portletDataContext.addClassedModel(
        threadFlagElement,
        ExportImportPathUtil.getModelPath(threadFlag),
        threadFlag,
        MBPortletDataHandler.NAMESPACE);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Role role)
      throws Exception {

    String permissionsPath = ExportImportPathUtil.getModelPath(role, "permissions.xml");

    List<Permission> permissions =
        PermissionConverterUtil.convertPermissions(role, _permissionConversionFilter);

    String xml = portletDataContext.toXML(permissions);

    portletDataContext.addZipEntry(permissionsPath, xml);

    Element roleElement = portletDataContext.getExportDataElement(role);

    portletDataContext.addClassedModel(roleElement, ExportImportPathUtil.getModelPath(role), role);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, WikiNode node)
      throws Exception {

    Element nodeElement = portletDataContext.getExportDataElement(node);

    portletDataContext.addClassedModel(nodeElement, ExportImportPathUtil.getModelPath(node), node);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Address address)
      throws Exception {

    Element addressElement = portletDataContext.getExportDataElement(address);

    portletDataContext.addClassedModel(
        addressElement, ExportImportPathUtil.getModelPath(address), address);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, MDRRuleGroup ruleGroup)
      throws Exception {

    Element ruleGroupElement = portletDataContext.getExportDataElement(ruleGroup);

    portletDataContext.addClassedModel(
        ruleGroupElement, ExportImportPathUtil.getModelPath(ruleGroup), ruleGroup);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, PollsVote vote)
      throws Exception {

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext, vote, vote.getChoice(), PortletDataContext.REFERENCE_TYPE_STRONG);

    Element voteElement = portletDataContext.getExportDataElement(vote);

    portletDataContext.addClassedModel(voteElement, ExportImportPathUtil.getModelPath(vote), vote);
  }
Пример #14
0
  protected String getEventPath(PortletDataContext portletDataContext, CalEvent event) {

    StringBundler sb = new StringBundler(4);

    sb.append(ExportImportPathUtil.getPortletPath(portletDataContext, PortletKeys.CALENDAR));
    sb.append("/events/");
    sb.append(event.getEventId());
    sb.append(".xml");

    return sb.toString();
  }
  protected String getLayoutSetPrototype(
      PortletDataContext portletDataContext, String layoutSetPrototypeUuid) {

    StringBundler sb = new StringBundler(3);

    sb.append(ExportImportPathUtil.getRootPath(portletDataContext));
    sb.append("/layout-set-prototype/");
    sb.append(layoutSetPrototypeUuid);

    return sb.toString();
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, RepositoryEntry repositoryEntry) throws Exception {

    Element repositoryEntryElement = portletDataContext.getExportDataElement(repositoryEntry);

    portletDataContext.addClassedModel(
        repositoryEntryElement,
        ExportImportPathUtil.getModelPath(repositoryEntry),
        repositoryEntry);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, LayoutFriendlyURL layoutFriendlyURL) throws Exception {

    Element layoutFriendlyURLElement = portletDataContext.getExportDataElement(layoutFriendlyURL);

    portletDataContext.addClassedModel(
        layoutFriendlyURLElement,
        ExportImportPathUtil.getModelPath(layoutFriendlyURL),
        layoutFriendlyURL);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, PasswordPolicy passwordPolicy) throws Exception {

    Element passwordPolicyElement =
        portletDataContext.getExportDataStagedModelElement(passwordPolicy);

    portletDataContext.addClassedModel(
        passwordPolicyElement,
        ExportImportPathUtil.getModelPath(passwordPolicy),
        passwordPolicy,
        PasswordPolicyPortletDataHandler.NAMESPACE);
  }
  protected String getLayoutIconPath(
      PortletDataContext portletDataContext, Layout layout, Image image) {

    StringBundler sb = new StringBundler(5);

    sb.append(ExportImportPathUtil.getLayoutPath(portletDataContext, layout.getPlid()));
    sb.append("/icons/");
    sb.append(image.getImageId());
    sb.append(StringPool.PERIOD);
    sb.append(image.getType());

    return sb.toString();
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, LayoutPrototype layoutPrototype) throws Exception {

    Element layoutPrototypeElement = portletDataContext.getExportDataElement(layoutPrototype);

    exportLayouts(portletDataContext, layoutPrototype, layoutPrototypeElement);

    portletDataContext.addClassedModel(
        layoutPrototypeElement,
        ExportImportPathUtil.getModelPath(layoutPrototype),
        layoutPrototype,
        LayoutPrototypePortletDataHandler.NAMESPACE);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, BookmarksFolder folder)
      throws Exception {

    if (folder.getParentFolderId() != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      exportStagedModel(portletDataContext, folder.getParentFolder());
    }

    Element folderElement = portletDataContext.getExportDataElement(folder);

    portletDataContext.addClassedModel(
        folderElement,
        ExportImportPathUtil.getModelPath(folder),
        folder,
        BookmarksPortletDataHandler.NAMESPACE);
  }
Пример #22
0
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, JournalFolder folder)
      throws Exception {

    if (folder.getParentFolderId() != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext,
          folder,
          folder.getParentFolder(),
          PortletDataContext.REFERENCE_TYPE_PARENT);
    }

    Element folderElement = portletDataContext.getExportDataElement(folder);

    portletDataContext.addClassedModel(
        folderElement, ExportImportPathUtil.getModelPath(folder), folder);
  }
  protected void exportAssetCategories(
      PortletDataContext portletDataContext,
      boolean exportPortletDataAll,
      boolean exportCategories,
      boolean companyGroup)
      throws Exception {

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("categories-hierarchy");

    if (exportPortletDataAll || exportCategories || companyGroup) {
      if (_log.isDebugEnabled()) {
        _log.debug("Export categories");
      }

      Element assetVocabulariesElement = rootElement.addElement("vocabularies");

      List<AssetVocabulary> assetVocabularies =
          AssetVocabularyLocalServiceUtil.getGroupVocabularies(portletDataContext.getGroupId());

      for (AssetVocabulary assetVocabulary : assetVocabularies) {
        _portletExporter.exportAssetVocabulary(
            portletDataContext, assetVocabulariesElement, assetVocabulary);
      }

      Element categoriesElement = rootElement.addElement("categories");

      List<AssetCategory> assetCategories =
          AssetCategoryUtil.findByGroupId(portletDataContext.getGroupId());

      for (AssetCategory assetCategory : assetCategories) {
        _portletExporter.exportAssetCategory(
            portletDataContext, assetVocabulariesElement,
            categoriesElement, assetCategory);
      }
    }

    _portletExporter.exportAssetCategories(portletDataContext, rootElement);

    portletDataContext.addZipEntry(
        ExportImportPathUtil.getRootPath(portletDataContext) + "/categories-hierarchy.xml",
        document.formattedString());
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, AssetCategory category)
      throws Exception {

    if (category.getParentCategoryId() != AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

      AssetCategory parentCategory =
          AssetCategoryLocalServiceUtil.fetchAssetCategory(category.getParentCategoryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, category, parentCategory, PortletDataContext.REFERENCE_TYPE_PARENT);
    } else {
      AssetVocabulary vocabulary =
          AssetVocabularyLocalServiceUtil.fetchAssetVocabulary(category.getVocabularyId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, category, vocabulary, PortletDataContext.REFERENCE_TYPE_PARENT);
    }

    Element categoryElement = portletDataContext.getExportDataElement(category);

    category.setUserUuid(category.getUserUuid());

    List<AssetCategoryProperty> categoryProperties =
        AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(category.getCategoryId());

    for (AssetCategoryProperty categoryProperty : categoryProperties) {
      Element propertyElement = categoryElement.addElement("property");

      propertyElement.addAttribute("userUuid", categoryProperty.getUserUuid());
      propertyElement.addAttribute("key", categoryProperty.getKey());
      propertyElement.addAttribute("value", categoryProperty.getValue());
    }

    String categoryPath = ExportImportPathUtil.getModelPath(category);

    categoryElement.addAttribute("path", categoryPath);

    portletDataContext.addPermissions(AssetCategory.class, category.getCategoryId());

    portletDataContext.addZipEntry(categoryPath, category);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, WSRPConsumerPortlet wsrpConsumerPortlet)
      throws Exception {

    WSRPConsumer wsrpConsumer =
        WSRPConsumerLocalServiceUtil.getWSRPConsumer(wsrpConsumerPortlet.getWsrpConsumerId());

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext,
        wsrpConsumerPortlet,
        wsrpConsumer,
        PortletDataContext.REFERENCE_TYPE_STRONG);

    Element wsrpConsumerPortletElement =
        portletDataContext.getExportDataElement(wsrpConsumerPortlet);

    portletDataContext.addClassedModel(
        wsrpConsumerPortletElement,
        ExportImportPathUtil.getModelPath(wsrpConsumerPortlet),
        wsrpConsumerPortlet);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, MBThreadFlag threadFlag)
      throws Exception {

    Map<Long, Long> threadIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBThread.class);

    long threadId = MapUtil.getLong(threadIds, threadFlag.getThreadId(), threadFlag.getThreadId());

    if (threadId == threadFlag.getThreadId()) {
      Element element = portletDataContext.getImportDataStagedModelElement(threadFlag);

      long rootMessageId = GetterUtil.getLong(element.attributeValue("root-message-id"));

      String rootMessagePath =
          ExportImportPathUtil.getModelPath(
              portletDataContext, MBMessage.class.getName(), rootMessageId);

      MBMessage rootMessage = (MBMessage) portletDataContext.getZipEntryAsObject(rootMessagePath);

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, rootMessage);

      threadId = MapUtil.getLong(threadIds, threadFlag.getThreadId(), threadFlag.getThreadId());
    }

    MBThread thread = MBThreadLocalServiceUtil.fetchThread(threadId);

    if (thread == null) {
      return;
    }

    long userId = portletDataContext.getUserId(threadFlag.getUserUuid());

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(threadFlag, MBPortletDataHandler.NAMESPACE);

    MBThreadFlagLocalServiceUtil.addThreadFlag(userId, thread, serviceContext);
  }
Пример #27
0
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, DDMStructure structure)
      throws Exception {

    Element structureElement = portletDataContext.getExportDataElement(structure);

    if (structure.getParentStructureId() != DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID) {

      DDMStructure parentStructure =
          DDMStructureLocalServiceUtil.getStructure(structure.getParentStructureId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, structure, parentStructure, PortletDataContext.REFERENCE_TYPE_PARENT);
    }

    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(structure.getCompanyId());

    if (defaultUserId == structure.getUserId()) {
      structureElement.addAttribute("preloaded", "true");
    }

    portletDataContext.addClassedModel(
        structureElement, ExportImportPathUtil.getModelPath(structure), structure);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, MBMessage message)
      throws Exception {

    long userId = portletDataContext.getUserId(message.getUserUuid());

    String userName = message.getUserName();

    Map<Long, Long> categoryIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBCategory.class);

    long parentCategoryId =
        MapUtil.getLong(categoryIds, message.getCategoryId(), message.getCategoryId());

    Map<Long, Long> threadIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBThread.class);

    long threadId = MapUtil.getLong(threadIds, message.getThreadId(), 0);

    Map<Long, Long> messageIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MBMessage.class);

    long parentMessageId =
        MapUtil.getLong(messageIds, message.getParentMessageId(), message.getParentMessageId());

    Element element = portletDataContext.getImportDataStagedModelElement(message);

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
        getAttachments(portletDataContext, element, message);

    try {
      ServiceContext serviceContext =
          portletDataContext.createServiceContext(message, MBPortletDataHandler.NAMESPACE);

      if (message.getStatus() != WorkflowConstants.STATUS_APPROVED) {
        serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
      }

      if ((parentCategoryId != MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)
          && (parentCategoryId != MBCategoryConstants.DISCUSSION_CATEGORY_ID)
          && (parentCategoryId == message.getCategoryId())) {

        String categoryPath =
            ExportImportPathUtil.getModelPath(
                portletDataContext, MBCategory.class.getName(), parentCategoryId);

        MBCategory category = (MBCategory) portletDataContext.getZipEntryAsObject(categoryPath);

        StagedModelDataHandlerUtil.importStagedModel(portletDataContext, category);

        parentCategoryId =
            MapUtil.getLong(categoryIds, message.getCategoryId(), message.getCategoryId());
      }

      MBMessage importedMessage = null;

      if (portletDataContext.isDataStrategyMirror()) {
        MBMessage existingMessage =
            MBMessageUtil.fetchByUUID_G(message.getUuid(), portletDataContext.getScopeGroupId());

        if (existingMessage == null) {
          serviceContext.setUuid(message.getUuid());

          importedMessage =
              MBMessageLocalServiceUtil.addMessage(
                  userId,
                  userName,
                  portletDataContext.getScopeGroupId(),
                  parentCategoryId,
                  threadId,
                  parentMessageId,
                  message.getSubject(),
                  message.getBody(),
                  message.getFormat(),
                  inputStreamOVPs,
                  message.getAnonymous(),
                  message.getPriority(),
                  message.getAllowPingbacks(),
                  serviceContext);
        } else {
          importedMessage =
              MBMessageLocalServiceUtil.updateMessage(
                  userId,
                  existingMessage.getMessageId(),
                  message.getSubject(),
                  message.getBody(),
                  inputStreamOVPs,
                  new ArrayList<String>(),
                  message.getPriority(),
                  message.getAllowPingbacks(),
                  serviceContext);
        }
      } else {
        importedMessage =
            MBMessageLocalServiceUtil.addMessage(
                userId,
                userName,
                portletDataContext.getScopeGroupId(),
                parentCategoryId,
                threadId,
                parentMessageId,
                message.getSubject(),
                message.getBody(),
                message.getFormat(),
                inputStreamOVPs,
                message.getAnonymous(),
                message.getPriority(),
                message.getAllowPingbacks(),
                serviceContext);
      }

      importedMessage.setAnswer(message.getAnswer());

      if (importedMessage.isRoot()) {
        MBThreadLocalServiceUtil.updateQuestion(
            importedMessage.getThreadId(),
            GetterUtil.getBoolean(element.attributeValue("question")));
      }

      threadIds.put(message.getThreadId(), importedMessage.getThreadId());

      portletDataContext.importClassedModel(
          message, importedMessage, MBPortletDataHandler.NAMESPACE);
    } finally {
      for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

        InputStream inputStream = inputStreamOVP.getValue();

        StreamUtil.cleanUp(inputStream);
      }
    }
  }
  protected File doExportLayoutsAsFile(
      long groupId,
      boolean privateLayout,
      long[] layoutIds,
      Map<String, String[]> parameterMap,
      Date startDate,
      Date endDate)
      throws Exception {

    boolean exportCategories = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.CATEGORIES);
    boolean exportIgnoreLastPublishDate =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.IGNORE_LAST_PUBLISH_DATE);
    boolean exportPermissions =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PERMISSIONS);
    boolean exportPortletDataAll =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_DATA_ALL);
    boolean exportTheme = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME);
    boolean exportThemeSettings =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME_REFERENCE);
    boolean exportLogo = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LOGO);
    boolean exportLayoutSetSettings =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LAYOUT_SET_SETTINGS);
    boolean updateLastPublishDate =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE);

    if (_log.isDebugEnabled()) {
      _log.debug("Export permissions " + exportPermissions);
      _log.debug("Export theme " + exportTheme);
    }

    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout);

    long companyId = layoutSet.getCompanyId();
    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

    if (serviceContext == null) {
      serviceContext = new ServiceContext();

      serviceContext.setCompanyId(companyId);
      serviceContext.setSignedIn(false);
      serviceContext.setUserId(defaultUserId);

      ServiceContextThreadLocal.pushServiceContext(serviceContext);
    }

    serviceContext.setAttribute("exporting", Boolean.TRUE);

    long layoutSetBranchId = MapUtil.getLong(parameterMap, "layoutSetBranchId");

    serviceContext.setAttribute("layoutSetBranchId", layoutSetBranchId);

    long lastPublishDate = System.currentTimeMillis();

    if (endDate != null) {
      lastPublishDate = endDate.getTime();
    }

    if (exportIgnoreLastPublishDate) {
      endDate = null;
      startDate = null;
    }

    StopWatch stopWatch = null;

    if (_log.isInfoEnabled()) {
      stopWatch = new StopWatch();

      stopWatch.start();
    }

    LayoutCache layoutCache = new LayoutCache();

    ZipWriter zipWriter = ZipWriterFactoryUtil.getZipWriter();

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            companyId, groupId, parameterMap, startDate, endDate, zipWriter);

    portletDataContext.setPortetDataContextListener(
        new PortletDataContextListenerImpl(portletDataContext));

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("root");

    portletDataContext.setExportDataRootElement(rootElement);

    Element headerElement = rootElement.addElement("header");

    headerElement.addAttribute(
        "available-locales",
        StringUtil.merge(LanguageUtil.getAvailableLocales(portletDataContext.getScopeGroupId())));
    headerElement.addAttribute("build-number", String.valueOf(ReleaseInfo.getBuildNumber()));
    headerElement.addAttribute("export-date", Time.getRFC822());

    if (portletDataContext.hasDateRange()) {
      headerElement.addAttribute("start-date", String.valueOf(portletDataContext.getStartDate()));
      headerElement.addAttribute("end-date", String.valueOf(portletDataContext.getEndDate()));
    }

    headerElement.addAttribute("company-id", String.valueOf(portletDataContext.getCompanyId()));
    headerElement.addAttribute(
        "company-group-id", String.valueOf(portletDataContext.getCompanyGroupId()));
    headerElement.addAttribute("group-id", String.valueOf(groupId));
    headerElement.addAttribute(
        "user-personal-site-group-id",
        String.valueOf(portletDataContext.getUserPersonalSiteGroupId()));
    headerElement.addAttribute("private-layout", String.valueOf(privateLayout));

    Group group = layoutSet.getGroup();

    String type = "layout-set";

    if (group.isLayoutPrototype()) {
      type = "layout-prototype";

      LayoutPrototype layoutPrototype =
          LayoutPrototypeLocalServiceUtil.getLayoutPrototype(group.getClassPK());

      headerElement.addAttribute("type-uuid", layoutPrototype.getUuid());
    } else if (group.isLayoutSetPrototype()) {
      type = "layout-set-prototype";

      LayoutSetPrototype layoutSetPrototype =
          LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(group.getClassPK());

      headerElement.addAttribute("type-uuid", layoutSetPrototype.getUuid());
    }

    headerElement.addAttribute("type", type);

    if (exportTheme || exportThemeSettings) {
      headerElement.addAttribute("theme-id", layoutSet.getThemeId());
      headerElement.addAttribute("color-scheme-id", layoutSet.getColorSchemeId());
    }

    if (exportLogo) {
      Image image = ImageLocalServiceUtil.getImage(layoutSet.getLogoId());

      if ((image != null) && (image.getTextObj() != null)) {
        String logoPath = ExportImportPathUtil.getRootPath(portletDataContext);

        logoPath += "/logo";

        headerElement.addAttribute("logo-path", logoPath);

        portletDataContext.addZipEntry(logoPath, image.getTextObj());
      }
    }

    if (exportLayoutSetSettings) {
      Element settingsElement = headerElement.addElement("settings");

      settingsElement.addCDATA(layoutSet.getSettings());
    }

    Element cssElement = headerElement.addElement("css");

    cssElement.addCDATA(layoutSet.getCss());

    Map<String, Object[]> portletIds = new LinkedHashMap<String, Object[]>();

    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(groupId, privateLayout);

    List<Portlet> portlets = getDataSiteLevelPortlets(companyId);

    long plid = LayoutConstants.DEFAULT_PLID;

    if (!layouts.isEmpty()) {
      Layout firstLayout = layouts.get(0);

      plid = firstLayout.getPlid();
    }

    if (group.isStagingGroup()) {
      group = group.getLiveGroup();
    }

    for (Portlet portlet : portlets) {
      String portletId = portlet.getRootPortletId();

      if (!group.isStagedPortlet(portletId)) {
        continue;
      }

      String key = PortletPermissionUtil.getPrimaryKey(0, portletId);

      if (portletIds.get(key) == null) {
        portletIds.put(
            key, new Object[] {portletId, plid, groupId, StringPool.BLANK, StringPool.BLANK});
      }
    }

    Element missingReferencesElement = rootElement.addElement("missing-references");

    portletDataContext.setMissingReferencesElement(missingReferencesElement);

    portletDataContext.addDeletionSystemEventStagedModelTypes(new StagedModelType(Layout.class));

    Element layoutsElement = portletDataContext.getExportDataGroupElement(Layout.class);

    String layoutSetPrototypeUuid = layoutSet.getLayoutSetPrototypeUuid();

    if (Validator.isNotNull(layoutSetPrototypeUuid)) {
      LayoutSetPrototype layoutSetPrototype =
          LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuidAndCompanyId(
              layoutSetPrototypeUuid, companyId);

      layoutsElement.addAttribute("layout-set-prototype-uuid", layoutSetPrototypeUuid);

      layoutsElement.addAttribute(
          "layout-set-prototype-name", layoutSetPrototype.getName(LocaleUtil.getDefault()));
    }

    for (Layout layout : layouts) {
      exportLayout(portletDataContext, portlets, layoutIds, portletIds, layout);
    }

    long previousScopeGroupId = portletDataContext.getScopeGroupId();

    Element portletsElement = rootElement.addElement("portlets");

    for (Map.Entry<String, Object[]> portletIdsEntry : portletIds.entrySet()) {

      Object[] portletObjects = portletIdsEntry.getValue();

      String portletId = null;
      plid = LayoutConstants.DEFAULT_PLID;
      long scopeGroupId = 0;
      String scopeType = StringPool.BLANK;
      String scopeLayoutUuid = null;

      if (portletObjects.length == 4) {
        portletId = (String) portletIdsEntry.getValue()[0];
        plid = (Long) portletIdsEntry.getValue()[1];
        scopeGroupId = (Long) portletIdsEntry.getValue()[2];
        scopeLayoutUuid = (String) portletIdsEntry.getValue()[3];
      } else {
        portletId = (String) portletIdsEntry.getValue()[0];
        plid = (Long) portletIdsEntry.getValue()[1];
        scopeGroupId = (Long) portletIdsEntry.getValue()[2];
        scopeType = (String) portletIdsEntry.getValue()[3];
        scopeLayoutUuid = (String) portletIdsEntry.getValue()[4];
      }

      Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);

      if (layout == null) {
        if (!group.isCompany() && (plid <= LayoutConstants.DEFAULT_PLID)) {

          continue;
        }

        if (_log.isWarnEnabled()) {
          _log.warn("Assuming global scope because no layout was found");
        }

        layout = new LayoutImpl();

        layout.setGroupId(groupId);
        layout.setCompanyId(companyId);
      }

      portletDataContext.setPlid(plid);
      portletDataContext.setOldPlid(plid);
      portletDataContext.setScopeGroupId(scopeGroupId);
      portletDataContext.setScopeType(scopeType);
      portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);

      boolean[] exportPortletControls =
          getExportPortletControls(companyId, portletId, parameterMap, type);

      _portletExporter.exportPortlet(
          portletDataContext,
          layoutCache,
          portletId,
          layout,
          portletsElement,
          defaultUserId,
          exportPermissions,
          exportPortletControls[0],
          exportPortletControls[1],
          exportPortletControls[2],
          exportPortletControls[3]);
    }

    portletDataContext.setScopeGroupId(previousScopeGroupId);

    exportAssetCategories(
        portletDataContext, exportPortletDataAll, exportCategories, group.isCompany());

    _portletExporter.exportAssetLinks(portletDataContext);
    _portletExporter.exportAssetTags(portletDataContext);
    _portletExporter.exportComments(portletDataContext);
    _portletExporter.exportExpandoTables(portletDataContext);
    _portletExporter.exportLocks(portletDataContext);

    _deletionSystemEventExporter.exportDeletionSystemEvents(portletDataContext);

    if (exportPermissions) {
      _permissionExporter.exportPortletDataPermissions(portletDataContext);
    }

    _portletExporter.exportRatingsEntries(portletDataContext, rootElement);

    if (exportTheme && !portletDataContext.isPerformDirectBinaryImport()) {
      exportTheme(layoutSet, zipWriter);
    }

    ExportImportHelperUtil.writeManifestSummary(document, portletDataContext.getManifestSummary());

    if (_log.isInfoEnabled()) {
      if (stopWatch != null) {
        _log.info("Exporting layouts takes " + stopWatch.getTime() + " ms");
      } else {
        _log.info("Exporting layouts is finished");
      }
    }

    portletDataContext.addZipEntry("/manifest.xml", document.formattedString());

    try {
      return zipWriter.getFile();
    } finally {
      if (updateLastPublishDate) {
        updateLastPublishDate(layoutSet, lastPublishDate);
      }
    }
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Role role)
      throws Exception {

    long userId = portletDataContext.getUserId(role.getUserUuid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(role);

    Role existingRole =
        RoleLocalServiceUtil.fetchRoleByUuidAndCompanyId(
            role.getUuid(), portletDataContext.getCompanyId());

    if (existingRole == null) {
      existingRole =
          RoleLocalServiceUtil.fetchRole(portletDataContext.getCompanyId(), role.getName());
    }

    Role importedRole = null;

    if (existingRole == null) {
      serviceContext.setUuid(role.getUuid());

      importedRole =
          RoleLocalServiceUtil.addRole(
              userId,
              null,
              0,
              role.getName(),
              role.getTitleMap(),
              role.getDescriptionMap(),
              role.getType(),
              role.getSubtype(),
              serviceContext);
    } else {
      importedRole =
          RoleLocalServiceUtil.updateRole(
              existingRole.getRoleId(),
              role.getName(),
              role.getTitleMap(),
              role.getDescriptionMap(),
              role.getSubtype(),
              serviceContext);

      deleteRolePermissions(portletDataContext, importedRole);
    }

    String permissionsPath = ExportImportPathUtil.getModelPath(role, "permissions.xml");

    List<Permission> permissions =
        (List<Permission>) portletDataContext.getZipEntryAsObject(permissionsPath);

    for (Permission permission : permissions) {
      if (ResourceBlockLocalServiceUtil.isSupported(permission.getName())) {

        importResourceBlock(portletDataContext, importedRole, permission);
      } else {
        importResourcePermissions(portletDataContext, importedRole, permission);
      }
    }

    portletDataContext.importClassedModel(role, importedRole);
  }