@Override
  protected PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    if (portletDataContext.getBooleanParameter(NAMESPACE, "categories")) {
      Element categoriesElement = portletDataContext.getImportDataGroupElement(AssetCategory.class);

      List<Element> categoryElements = categoriesElement.elements();

      for (Element categoryElement : categoryElements) {
        StagedModelDataHandlerUtil.importStagedModel(portletDataContext, categoryElement);
      }
    }

    if (portletDataContext.getBooleanParameter(NAMESPACE, "vocabularies")) {
      Element vocabulariesElement =
          portletDataContext.getImportDataGroupElement(AssetVocabulary.class);

      List<Element> vocabularyElements = vocabulariesElement.elements();

      for (Element vocabularyElement : vocabularyElements) {
        StagedModelDataHandlerUtil.importStagedModel(portletDataContext, vocabularyElement);
      }
    }

    return null;
  }
예제 #2
0
  @Override
  protected PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    portletDataContext.importPermissions(
        "com.liferay.portlet.dynamicdatamapping",
        portletDataContext.getSourceGroupId(),
        portletDataContext.getScopeGroupId());

    Element structuresElement = portletDataContext.getImportDataGroupElement(DDMStructure.class);

    List<Element> structureElements = structuresElement.elements();

    for (Element structureElement : structureElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, structureElement);
    }

    if (portletDataContext.getBooleanParameter(NAMESPACE, "templates")) {
      Element templatesElement = portletDataContext.getImportDataGroupElement(DDMTemplate.class);

      List<Element> templateElements = templatesElement.elements();

      for (Element templateElement : templateElements) {
        StagedModelDataHandlerUtil.importStagedModel(portletDataContext, templateElement);
      }
    }

    return portletPreferences;
  }
  @Override
  protected PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    portletDataContext.importPermissions(
        "com.liferay.portlet.blogs",
        portletDataContext.getSourceGroupId(),
        portletDataContext.getScopeGroupId());

    Element entriesElement = portletDataContext.getImportDataGroupElement(BlogsEntry.class);

    JournalPortletDataHandler.importReferenceData(portletDataContext, entriesElement);

    List<Element> entryElements = entriesElement.elements();

    for (Element entryElement : entryElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, entryElement);
    }

    return null;
  }
  protected void importLayouts(
      PortletDataContext portletDataContext, LayoutPrototype layoutPrototype, long importedGroupId)
      throws PortalException {

    long groupId = portletDataContext.getGroupId();
    boolean privateLayout = portletDataContext.isPrivateLayout();
    long scopeGroupId = portletDataContext.getScopeGroupId();

    try {
      portletDataContext.setGroupId(importedGroupId);
      portletDataContext.setPrivateLayout(true);
      portletDataContext.setScopeGroupId(importedGroupId);

      List<Element> layoutElements =
          portletDataContext.getReferenceDataElements(layoutPrototype, Layout.class);

      for (Element layoutElement : layoutElements) {
        StagedModelDataHandlerUtil.importStagedModel(portletDataContext, layoutElement);
      }
    } finally {
      portletDataContext.setGroupId(groupId);
      portletDataContext.setPrivateLayout(privateLayout);
      portletDataContext.setScopeGroupId(scopeGroupId);
    }
  }
  @Test
  public void testTypeArticle() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    JournalArticle journalArticle =
        JournalTestUtil.addArticle(
            stagingGroup.getGroupId(),
            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString());

    addDependentStagedModel(dependentStagedModelsMap, JournalArticle.class, journalArticle);

    Layout layout =
        LayoutTestUtil.addTypeArticleLayout(
            stagingGroup.getGroupId(), journalArticle.getArticleId());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);
  }
  public static void importStagedModel(PortletDataContext portletDataContext, Element element)
      throws PortletDataException {

    String path = element.attributeValue("path");

    StagedModel stagedModel = (StagedModel) portletDataContext.getZipEntryAsObject(element, path);

    importStagedModel(portletDataContext, stagedModel);
  }
  @Test
  public void testTypeLinkToLayout() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout linkedLayout = LayoutTestUtil.addLayout(stagingGroup);

    List<LayoutFriendlyURL> linkedLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(linkedLayout.getPlid());

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, linkedLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, linkedLayout);

    Layout layout =
        LayoutTestUtil.addTypeLinkToLayoutLayout(
            stagingGroup.getGroupId(), linkedLayout.getLayoutId());

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(layout.getPlid());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        linkedLayout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL linkedLayoutFriendlyURL = linkedLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        linkedLayoutFriendlyURL.getUuid(), liveGroup.getGroupId());

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        layout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        layoutFriendlyURL.getUuid(), liveGroup.getGroupId());
  }
  @Override
  protected PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    portletDataContext.importPortalPermissions();

    Element rolesElement = portletDataContext.getImportDataGroupElement(Role.class);

    List<Element> roleElements = rolesElement.elements();

    for (Element roleElement : roleElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, roleElement);
    }

    return null;
  }
  @Test
  public void testCompanyScopeDependencies() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = addCompanyDependencies();

    StagedModel stagedModel = addStagedModel(stagingGroup, dependentStagedModelsMap);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, stagedModel);

    initImport();

    StagedModel exportedStagedModel = readExportedStagedModel(stagedModel);

    Assert.assertNotNull(exportedStagedModel);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedStagedModel);

    validateCompanyDependenciesImport(dependentStagedModelsMap, liveGroup);
  }
  @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);
  }
  @Override
  protected PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    portletDataContext.importPermissions(
        PortletKeys.PORTAL,
        portletDataContext.getSourceCompanyId(),
        portletDataContext.getCompanyId());

    Element layoutPrototypesElement =
        portletDataContext.getImportDataGroupElement(LayoutPrototype.class);

    List<Element> layoutPrototypeElements = layoutPrototypesElement.elements();

    for (Element layoutPrototypeElement : layoutPrototypeElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, layoutPrototypeElement);
    }

    return null;
  }
  @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);
      }
    }
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, Element element, String path, MDRRule rule)
      throws Exception {

    String ruleGroupPath =
        StagedModelPathUtil.getPath(
            portletDataContext, MDRRuleGroup.class.getName(), rule.getRuleGroupId());

    MDRRuleGroup ruleGroup = (MDRRuleGroup) portletDataContext.getZipEntryAsObject(ruleGroupPath);

    StagedModelDataHandlerUtil.importStagedModel(
        portletDataContext, element, ruleGroupPath, ruleGroup);

    Map<Long, Long> ruleGroupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MDRRuleGroup.class);

    long ruleGroupId = MapUtil.getLong(ruleGroupIds, rule.getRuleGroupId(), rule.getRuleGroupId());

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(element, rule, MDRPortletDataHandler.NAMESPACE);

    serviceContext.setUserId(portletDataContext.getUserId(rule.getUserUuid()));

    MDRRule importedRule = null;

    if (portletDataContext.isDataStrategyMirror()) {
      MDRRule existingRule =
          MDRRuleUtil.fetchByUUID_G(rule.getUuid(), portletDataContext.getScopeGroupId());

      if (existingRule == null) {
        serviceContext.setUuid(rule.getUuid());

        importedRule =
            MDRRuleLocalServiceUtil.addRule(
                ruleGroupId,
                rule.getNameMap(),
                rule.getDescriptionMap(),
                rule.getType(),
                rule.getTypeSettingsProperties(),
                serviceContext);
      } else {
        importedRule =
            MDRRuleLocalServiceUtil.updateRule(
                existingRule.getRuleId(), rule.getNameMap(),
                rule.getDescriptionMap(), rule.getType(),
                rule.getTypeSettingsProperties(), serviceContext);
      }
    } else {
      importedRule =
          MDRRuleLocalServiceUtil.addRule(
              ruleGroupId,
              rule.getNameMap(),
              rule.getDescriptionMap(),
              rule.getType(),
              rule.getTypeSettingsProperties(),
              serviceContext);
    }

    portletDataContext.importClassedModel(rule, importedRule, MDRPortletDataHandler.NAMESPACE);
  }
예제 #14
0
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, DDLRecordSet recordSet)
      throws Exception {

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

    Map<Long, Long> ddmStructureIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DDMStructure.class);

    String structurePath =
        ExportImportPathUtil.getModelPath(
            portletDataContext, DDMStructure.class.getName(), recordSet.getDDMStructureId());

    DDMStructure ddmStructure =
        (DDMStructure) portletDataContext.getZipEntryAsObject(structurePath);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, ddmStructure);

    long ddmStructureId =
        MapUtil.getLong(
            ddmStructureIds, recordSet.getDDMStructureId(), recordSet.getDDMStructureId());

    List<Element> ddmTemplateElements =
        portletDataContext.getReferenceDataElements(recordSet, DDMTemplate.class);

    for (Element ddmTemplateElement : ddmTemplateElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, ddmTemplateElement);
    }

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(recordSet, DDLPortletDataHandler.NAMESPACE);

    DDLRecordSet importedRecordSet = null;

    if (portletDataContext.isDataStrategyMirror()) {
      DDLRecordSet existingRecordSet =
          DDLRecordSetUtil.fetchByUUID_G(recordSet.getUuid(), portletDataContext.getScopeGroupId());

      if (existingRecordSet == null) {
        serviceContext.setUuid(recordSet.getUuid());

        importedRecordSet =
            DDLRecordSetLocalServiceUtil.addRecordSet(
                userId,
                portletDataContext.getScopeGroupId(),
                ddmStructureId,
                recordSet.getRecordSetKey(),
                recordSet.getNameMap(),
                recordSet.getDescriptionMap(),
                recordSet.getMinDisplayRows(),
                recordSet.getScope(),
                serviceContext);
      } else {
        importedRecordSet =
            DDLRecordSetLocalServiceUtil.updateRecordSet(
                existingRecordSet.getRecordSetId(), ddmStructureId,
                recordSet.getNameMap(), recordSet.getDescriptionMap(),
                recordSet.getMinDisplayRows(), serviceContext);
      }
    } else {
      importedRecordSet =
          DDLRecordSetLocalServiceUtil.addRecordSet(
              userId,
              portletDataContext.getScopeGroupId(),
              ddmStructureId,
              recordSet.getRecordSetKey(),
              recordSet.getNameMap(),
              recordSet.getDescriptionMap(),
              recordSet.getMinDisplayRows(),
              recordSet.getScope(),
              serviceContext);
    }

    portletDataContext.importClassedModel(
        recordSet, importedRecordSet, DDLPortletDataHandler.NAMESPACE);
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

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

    List<Element> ddmStructureElements =
        portletDataContext.getReferenceDataElements(fileEntryType, DDMStructure.class);

    for (Element ddmStructureElement : ddmStructureElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, ddmStructureElement);
    }

    List<Element> ddmStructureReferenceElements =
        portletDataContext.getReferenceElements(fileEntryType, DDMStructure.class);

    long[] ddmStructureIdsArray = new long[ddmStructureReferenceElements.size()];

    Map<Long, Long> ddmStructureIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DDMStructure.class);

    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
      Element ddmStructureReferenceElement = ddmStructureReferenceElements.get(i);

      long ddmStructureId =
          GetterUtil.getLong(ddmStructureReferenceElement.attributeValue("class-pk"));

      ddmStructureIdsArray[i] = MapUtil.getLong(ddmStructureIds, ddmStructureId);
    }

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(fileEntryType, DLPortletDataHandler.NAMESPACE);

    DLFileEntryType importedDLFileEntryType = null;

    if (portletDataContext.isDataStrategyMirror()) {
      DLFileEntryType existingDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(
              fileEntryType.getUuid(), portletDataContext.getScopeGroupId());

      if (existingDLFileEntryType == null) {
        existingDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(
                fileEntryType.getUuid(), portletDataContext.getCompanyGroupId());
      }

      if (existingDLFileEntryType == null) {
        serviceContext.setUuid(fileEntryType.getUuid());

        importedDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
                userId,
                portletDataContext.getScopeGroupId(),
                fileEntryType.getFileEntryTypeKey(),
                fileEntryType.getNameMap(),
                fileEntryType.getDescriptionMap(),
                ddmStructureIdsArray,
                serviceContext);
      } else if (portletDataContext.isCompanyStagedGroupedModel(existingDLFileEntryType)) {

        return;
      } else {
        DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
            userId,
            existingDLFileEntryType.getFileEntryTypeId(),
            fileEntryType.getNameMap(),
            fileEntryType.getDescriptionMap(),
            ddmStructureIdsArray,
            serviceContext);
      }
    } else {
      importedDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              userId,
              portletDataContext.getScopeGroupId(),
              fileEntryType.getFileEntryTypeKey(),
              fileEntryType.getNameMap(),
              fileEntryType.getDescriptionMap(),
              ddmStructureIdsArray,
              serviceContext);
    }

    portletDataContext.importClassedModel(
        fileEntryType, importedDLFileEntryType, DLPortletDataHandler.NAMESPACE);

    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(importedDLFileEntryType);

    List<DDMStructure> importedDDMStructures = importedDLFileEntryType.getDDMStructures();

    for (DDMStructure importedDDMStructure : importedDDMStructures) {
      String ddmStructureKey = importedDDMStructure.getStructureKey();

      if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(ddmStructureKey)) {

        continue;
      }

      if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
        continue;
      }

      importedDDMStructure.setStructureKey(importedDLFileEntryDDMStructureKey);

      DDMStructureLocalServiceUtil.updateDDMStructure(importedDDMStructure);
    }
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext,
      Element ruleGroupInstanceElement,
      String path,
      MDRRuleGroupInstance ruleGroupInstance)
      throws Exception {

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

    String ruleGroupPath =
        StagedModelPathUtil.getPath(
            portletDataContext, MDRRuleGroup.class.getName(), ruleGroupInstance.getRuleGroupId());

    MDRRuleGroup ruleGroup = (MDRRuleGroup) portletDataContext.getZipEntryAsObject(ruleGroupPath);

    StagedModelDataHandlerUtil.importStagedModel(
        portletDataContext, ruleGroupInstanceElement, ruleGroupPath, ruleGroup);

    Map<Long, Long> ruleGroupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(MDRRuleGroup.class);

    Long ruleGroupId =
        MapUtil.getLong(
            ruleGroupIds, ruleGroupInstance.getRuleGroupId(), ruleGroupInstance.getRuleGroupId());

    long classPK = 0;

    String layoutUuid = ruleGroupInstanceElement.attributeValue("layout-uuid");

    try {
      if (Validator.isNotNull(layoutUuid)) {
        Layout layout =
            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
                layoutUuid,
                portletDataContext.getScopeGroupId(),
                portletDataContext.isPrivateLayout());

        classPK = layout.getPrimaryKey();
      } else {
        LayoutSet layoutSet =
            LayoutSetLocalServiceUtil.getLayoutSet(
                portletDataContext.getScopeGroupId(), portletDataContext.isPrivateLayout());

        classPK = layoutSet.getLayoutSetId();
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(5);

        sb.append("Layout ");
        sb.append(layoutUuid);
        sb.append(" is missing for rule group instance ");
        sb.append(ruleGroupInstance.getRuleGroupInstanceId());
        sb.append(", skipping this rule group instance.");

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

      return;
    }

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(
            ruleGroupInstanceElement, ruleGroupInstance, MDRPortletDataHandler.NAMESPACE);

    serviceContext.setUserId(userId);

    MDRRuleGroupInstance importedRuleGroupInstance = null;

    if (portletDataContext.isDataStrategyMirror()) {
      MDRRuleGroupInstance existingMDRRuleGroupInstance =
          MDRRuleGroupInstanceUtil.fetchByUUID_G(
              ruleGroupInstance.getUuid(), portletDataContext.getScopeGroupId());

      if (existingMDRRuleGroupInstance == null) {
        serviceContext.setUuid(ruleGroupInstance.getUuid());

        importedRuleGroupInstance =
            MDRRuleGroupInstanceLocalServiceUtil.addRuleGroupInstance(
                portletDataContext.getScopeGroupId(),
                ruleGroupInstance.getClassName(),
                classPK,
                ruleGroupId,
                ruleGroupInstance.getPriority(),
                serviceContext);
      } else {
        importedRuleGroupInstance =
            MDRRuleGroupInstanceLocalServiceUtil.updateRuleGroupInstance(
                existingMDRRuleGroupInstance.getRuleGroupInstanceId(),
                ruleGroupInstance.getPriority());
      }
    } else {
      importedRuleGroupInstance =
          MDRRuleGroupInstanceLocalServiceUtil.addRuleGroupInstance(
              portletDataContext.getScopeGroupId(),
              ruleGroupInstance.getClassName(),
              classPK,
              ruleGroupId,
              ruleGroupInstance.getPriority(),
              serviceContext);
    }

    portletDataContext.importClassedModel(
        ruleGroupInstance, importedRuleGroupInstance, MDRPortletDataHandler.NAMESPACE);
  }