protected void exportAssetCategory(
      PortletDataContext portletDataContext,
      Element assetVocabulariesElement,
      Element assetCategoriesElement,
      AssetCategory assetCategory)
      throws Exception {

    exportAssetVocabulary(
        portletDataContext, assetVocabulariesElement, assetCategory.getVocabularyId());

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

      exportAssetCategory(
          portletDataContext,
          assetVocabulariesElement,
          assetCategoriesElement,
          assetCategory.getParentCategoryId());
    }

    String path = getAssetCategoryPath(portletDataContext, assetCategory.getCategoryId());

    if (!portletDataContext.isPathNotProcessed(path)) {
      return;
    }

    Element assetCategoryElement = assetCategoriesElement.addElement("category");

    assetCategoryElement.addAttribute("path", path);

    assetCategory.setUserUuid(assetCategory.getUserUuid());

    portletDataContext.addZipEntry(path, assetCategory);

    List<AssetCategoryProperty> assetCategoryProperties =
        AssetCategoryPropertyLocalServiceUtil.getCategoryProperties(assetCategory.getCategoryId());

    for (AssetCategoryProperty assetCategoryProperty : assetCategoryProperties) {

      Element propertyElement = assetCategoryElement.addElement("property");

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

    portletDataContext.addPermissions(AssetCategory.class, assetCategory.getCategoryId());
  }
  @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);
  }
  protected JSONArray toJSONArray(List<AssetCategory> categories)
      throws PortalException, SystemException {

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    for (AssetCategory category : categories) {
      String categoryJSON = JSONFactoryUtil.looseSerialize(category);

      JSONObject categoryJSONObject = JSONFactoryUtil.createJSONObject(categoryJSON);

      List<String> names = new ArrayList<String>();

      AssetCategory curCategory = category;

      while (curCategory.getParentCategoryId() > 0) {
        AssetCategory parentCategory = getCategory(curCategory.getParentCategoryId());

        names.add(parentCategory.getName());
        names.add(StringPool.SPACE + StringPool.GREATER_THAN + StringPool.SPACE);

        curCategory = parentCategory;
      }

      Collections.reverse(names);

      AssetVocabulary vocabulary = assetVocabularyService.getVocabulary(category.getVocabularyId());

      StringBundler sb = new StringBundler(1 + names.size());

      sb.append(vocabulary.getName());
      sb.append(names.toArray(new String[names.size()]));

      categoryJSONObject.put("path", sb.toString());

      jsonArray.put(categoryJSONObject);
    }

    return jsonArray;
  }
Пример #4
0
  protected void importAssetCategory(
      PortletDataContext portletDataContext,
      Map<Long, Long> assetVocabularyPKs,
      Map<Long, Long> assetCategoryPKs,
      Map<String, String> assetCategoryUuids,
      Element assetCategoryElement,
      AssetCategory assetCategory)
      throws Exception {

    long userId = portletDataContext.getUserId(assetCategory.getUserUuid());
    long assetVocabularyId =
        MapUtil.getLong(
            assetVocabularyPKs, assetCategory.getVocabularyId(), assetCategory.getVocabularyId());
    long parentAssetCategoryId =
        MapUtil.getLong(
            assetCategoryPKs,
            assetCategory.getParentCategoryId(),
            assetCategory.getParentCategoryId());

    if ((parentAssetCategoryId != AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID)
        && (parentAssetCategoryId == assetCategory.getParentCategoryId())) {

      String path = getAssetCategoryPath(portletDataContext, parentAssetCategoryId);

      AssetCategory parentAssetCategory =
          (AssetCategory) portletDataContext.getZipEntryAsObject(path);

      Node parentCategoryNode =
          assetCategoryElement.getParent().selectSingleNode("./category[@path='" + path + "']");

      if (parentCategoryNode != null) {
        importAssetCategory(
            portletDataContext,
            assetVocabularyPKs,
            assetCategoryPKs,
            assetCategoryUuids,
            (Element) parentCategoryNode,
            parentAssetCategory);

        parentAssetCategoryId =
            MapUtil.getLong(
                assetCategoryPKs,
                assetCategory.getParentCategoryId(),
                assetCategory.getParentCategoryId());
      }
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);
    serviceContext.setCreateDate(assetCategory.getCreateDate());
    serviceContext.setModifiedDate(assetCategory.getModifiedDate());
    serviceContext.setScopeGroupId(portletDataContext.getScopeGroupId());

    AssetCategory importedAssetCategory = null;

    try {
      if (parentAssetCategoryId != AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) {

        AssetCategoryUtil.findByPrimaryKey(parentAssetCategoryId);
      }

      List<Element> propertyElements = assetCategoryElement.elements("property");

      String[] properties = new String[propertyElements.size()];

      for (int i = 0; i < propertyElements.size(); i++) {
        Element propertyElement = propertyElements.get(i);

        String key = propertyElement.attributeValue("key");
        String value = propertyElement.attributeValue("value");

        properties[i] = key.concat(StringPool.COLON).concat(value);
      }

      AssetCategory existingAssetCategory =
          AssetCategoryUtil.fetchByP_N_V(
              parentAssetCategoryId, assetCategory.getName(), assetVocabularyId);

      if (existingAssetCategory == null) {
        serviceContext.setUuid(assetCategory.getUuid());

        importedAssetCategory =
            AssetCategoryLocalServiceUtil.addCategory(
                userId,
                parentAssetCategoryId,
                getAssetCategoryTitleMap(assetCategory),
                assetCategory.getDescriptionMap(),
                assetVocabularyId,
                properties,
                serviceContext);
      } else {
        importedAssetCategory =
            AssetCategoryLocalServiceUtil.updateCategory(
                userId,
                existingAssetCategory.getCategoryId(),
                parentAssetCategoryId,
                getAssetCategoryTitleMap(assetCategory),
                assetCategory.getDescriptionMap(),
                assetVocabularyId,
                properties,
                serviceContext);
      }

      assetCategoryPKs.put(assetCategory.getCategoryId(), importedAssetCategory.getCategoryId());

      assetCategoryUuids.put(assetCategory.getUuid(), importedAssetCategory.getUuid());

      portletDataContext.importPermissions(
          AssetCategory.class,
          assetCategory.getCategoryId(),
          importedAssetCategory.getCategoryId());
    } catch (NoSuchCategoryException nsce) {
      _log.error(
          "Could not find the parent category for category " + assetCategory.getCategoryId());
    }
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, AssetCategory category)
      throws Exception {

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

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

      StagedModelDataHandlerUtil.importReferenceStagedModel(
          portletDataContext, category, AssetCategory.class, category.getParentCategoryId());
    } else {
      StagedModelDataHandlerUtil.importReferenceStagedModel(
          portletDataContext, category, AssetVocabulary.class, category.getVocabularyId());
    }

    Map<Long, Long> vocabularyIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(AssetVocabulary.class);

    long vocabularyId =
        MapUtil.getLong(vocabularyIds, category.getVocabularyId(), category.getVocabularyId());

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

    long parentCategoryId =
        MapUtil.getLong(
            categoryIds, category.getParentCategoryId(), category.getParentCategoryId());

    Element categoryElement = portletDataContext.getImportDataElement(category);

    List<Element> propertyElements = categoryElement.elements("property");

    String[] properties = new String[propertyElements.size()];

    for (int i = 0; i < propertyElements.size(); i++) {
      Element propertyElement = propertyElements.get(i);

      String key = propertyElement.attributeValue("key");
      String value = propertyElement.attributeValue("value");

      properties[i] = key.concat(AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR).concat(value);
    }

    ServiceContext serviceContext = createServiceContext(portletDataContext, category);

    AssetCategory importedCategory = null;

    AssetCategory existingCategory =
        AssetCategoryUtil.fetchByUUID_G(category.getUuid(), portletDataContext.getScopeGroupId());

    if (existingCategory == null) {
      existingCategory =
          AssetCategoryUtil.fetchByUUID_G(
              category.getUuid(), portletDataContext.getCompanyGroupId());
    }

    if (existingCategory == null) {
      String name =
          getCategoryName(
              null,
              portletDataContext.getScopeGroupId(),
              parentCategoryId,
              category.getName(),
              category.getVocabularyId(),
              2);

      serviceContext.setUuid(category.getUuid());

      importedCategory =
          AssetCategoryLocalServiceUtil.addCategory(
              userId,
              parentCategoryId,
              getCategoryTitleMap(portletDataContext.getScopeGroupId(), category, name),
              category.getDescriptionMap(),
              vocabularyId,
              properties,
              serviceContext);
    } else {
      String name =
          getCategoryName(
              category.getUuid(),
              portletDataContext.getScopeGroupId(),
              parentCategoryId,
              category.getName(),
              category.getVocabularyId(),
              2);

      importedCategory =
          AssetCategoryLocalServiceUtil.updateCategory(
              userId,
              existingCategory.getCategoryId(),
              parentCategoryId,
              getCategoryTitleMap(portletDataContext.getScopeGroupId(), category, name),
              category.getDescriptionMap(),
              vocabularyId,
              properties,
              serviceContext);
    }

    categoryIds.put(category.getCategoryId(), importedCategory.getCategoryId());

    Map<String, String> categoryUuids =
        (Map<String, String>)
            portletDataContext.getNewPrimaryKeysMap(AssetCategory.class + ".uuid");

    categoryUuids.put(category.getUuid(), importedCategory.getUuid());

    portletDataContext.importPermissions(
        AssetCategory.class, category.getCategoryId(), importedCategory.getCategoryId());
  }