@Indexable(type = IndexableType.REINDEX)
  @Override
  public AssetCategory moveCategory(
      long categoryId, long parentCategoryId, long vocabularyId, ServiceContext serviceContext)
      throws PortalException {

    AssetCategory category = assetCategoryPersistence.findByPrimaryKey(categoryId);

    validate(categoryId, parentCategoryId, category.getName(), vocabularyId);

    if (parentCategoryId > 0) {
      assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
    }

    if (vocabularyId != category.getVocabularyId()) {
      assetVocabularyPersistence.findByPrimaryKey(vocabularyId);

      category.setVocabularyId(vocabularyId);

      updateChildrenVocabularyId(category, vocabularyId);
    }

    category.setParentCategoryId(parentCategoryId);

    assetCategoryPersistence.update(category);

    return category;
  }
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public AssetCategory addCategory(
      long userId,
      long groupId,
      long parentCategoryId,
      Map<Locale, String> titleMap,
      Map<Locale, String> descriptionMap,
      long vocabularyId,
      String[] categoryProperties,
      ServiceContext serviceContext)
      throws PortalException {

    // Category

    User user = userPersistence.findByPrimaryKey(userId);

    String name = titleMap.get(LocaleUtil.getSiteDefault());

    name = ModelHintsUtil.trimString(AssetCategory.class.getName(), "name", name);

    if (categoryProperties == null) {
      categoryProperties = new String[0];
    }

    validate(0, parentCategoryId, name, vocabularyId);

    if (parentCategoryId > 0) {
      assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
    }

    assetVocabularyPersistence.findByPrimaryKey(vocabularyId);

    long categoryId = counterLocalService.increment();

    AssetCategory category = assetCategoryPersistence.create(categoryId);

    category.setUuid(serviceContext.getUuid());
    category.setGroupId(groupId);
    category.setCompanyId(user.getCompanyId());
    category.setUserId(user.getUserId());
    category.setUserName(user.getFullName());
    category.setParentCategoryId(parentCategoryId);
    category.setName(name);
    category.setTitleMap(titleMap);
    category.setDescriptionMap(descriptionMap);
    category.setVocabularyId(vocabularyId);

    assetCategoryPersistence.update(category);

    // Resources

    if (serviceContext.isAddGroupPermissions() || serviceContext.isAddGuestPermissions()) {

      addCategoryResources(
          category, serviceContext.isAddGroupPermissions(), serviceContext.isAddGuestPermissions());
    } else {
      addCategoryResources(category, serviceContext.getModelPermissions());
    }

    // Properties

    for (int i = 0; i < categoryProperties.length; i++) {
      String[] categoryProperty =
          StringUtil.split(
              categoryProperties[i], AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);

      if (categoryProperty.length <= 1) {
        categoryProperty = StringUtil.split(categoryProperties[i], CharPool.COLON);
      }

      String key = StringPool.BLANK;
      String value = StringPool.BLANK;

      if (categoryProperty.length > 1) {
        key = GetterUtil.getString(categoryProperty[0]);
        value = GetterUtil.getString(categoryProperty[1]);
      }

      if (Validator.isNotNull(key)) {
        assetCategoryPropertyLocalService.addCategoryProperty(userId, categoryId, key, value);
      }
    }

    return category;
  }
  @Indexable(type = IndexableType.REINDEX)
  @Override
  public AssetCategory updateCategory(
      long userId,
      long categoryId,
      long parentCategoryId,
      Map<Locale, String> titleMap,
      Map<Locale, String> descriptionMap,
      long vocabularyId,
      String[] categoryProperties,
      ServiceContext serviceContext)
      throws PortalException {

    // Category

    String name = titleMap.get(LocaleUtil.getSiteDefault());

    name = ModelHintsUtil.trimString(AssetCategory.class.getName(), "name", name);

    if (categoryProperties == null) {
      categoryProperties = new String[0];
    }

    validate(categoryId, parentCategoryId, name, vocabularyId);

    if (parentCategoryId > 0) {
      assetCategoryPersistence.findByPrimaryKey(parentCategoryId);
    }

    AssetCategory category = assetCategoryPersistence.findByPrimaryKey(categoryId);

    String oldName = category.getName();

    if (vocabularyId != category.getVocabularyId()) {
      assetVocabularyPersistence.findByPrimaryKey(vocabularyId);

      parentCategoryId = AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;

      category.setVocabularyId(vocabularyId);

      updateChildrenVocabularyId(category, vocabularyId);
    }

    category.setParentCategoryId(parentCategoryId);
    category.setName(name);
    category.setTitleMap(titleMap);
    category.setDescriptionMap(descriptionMap);

    assetCategoryPersistence.update(category);

    // Properties

    List<AssetCategoryProperty> oldCategoryProperties =
        assetCategoryPropertyPersistence.findByCategoryId(categoryId);

    oldCategoryProperties = ListUtil.copy(oldCategoryProperties);

    for (int i = 0; i < categoryProperties.length; i++) {
      String[] categoryProperty =
          StringUtil.split(
              categoryProperties[i], AssetCategoryConstants.PROPERTY_KEY_VALUE_SEPARATOR);

      if (categoryProperty.length <= 1) {
        categoryProperty = StringUtil.split(categoryProperties[i], CharPool.COLON);
      }

      String key = StringPool.BLANK;

      if (categoryProperty.length > 0) {
        key = GetterUtil.getString(categoryProperty[0]);
      }

      String value = StringPool.BLANK;

      if (categoryProperty.length > 1) {
        value = GetterUtil.getString(categoryProperty[1]);
      }

      if (Validator.isNotNull(key)) {
        boolean addCategoryProperty = true;

        AssetCategoryProperty oldCategoryProperty = null;

        Iterator<AssetCategoryProperty> iterator = oldCategoryProperties.iterator();

        while (iterator.hasNext()) {
          oldCategoryProperty = iterator.next();

          if ((categoryId == oldCategoryProperty.getCategoryId())
              && key.equals(oldCategoryProperty.getKey())) {

            addCategoryProperty = false;

            if (!value.equals(oldCategoryProperty.getValue())) {
              assetCategoryPropertyLocalService.updateCategoryProperty(
                  userId, oldCategoryProperty.getCategoryPropertyId(), key, value);
            }

            iterator.remove();

            break;
          }
        }

        if (addCategoryProperty) {
          assetCategoryPropertyLocalService.addCategoryProperty(userId, categoryId, key, value);
        }
      }
    }

    for (AssetCategoryProperty categoryProperty : oldCategoryProperties) {
      assetCategoryPropertyLocalService.deleteAssetCategoryProperty(categoryProperty);
    }

    // Indexer

    if (!oldName.equals(name)) {
      List<AssetEntry> entries = assetCategoryPersistence.getAssetEntries(category.getCategoryId());

      assetEntryLocalService.reindex(entries);
    }

    return category;
  }