Beispiel #1
0
  public String getBaseType() {
    Class<?> model = getModel();

    String type = getType();

    String baseType = null;

    if ((model != null) && Validator.isNull(type)) {
      baseType = ModelHintsUtil.getType(model.getName(), getField());
    } else if (Validator.isNotNull(type)) {
      if (Objects.equals(type, "checkbox")
          || Objects.equals(type, "radio")
          || Objects.equals(type, "resource")) {

        baseType = type;
      } else if (Objects.equals(type, "toggle-card") || Objects.equals(type, "toggle-switch")) {

        baseType = "checkbox";
      }
    }

    if (Validator.isNull(baseType)) {
      baseType = "text";
    }

    return baseType;
  }
Beispiel #2
0
  protected void addModelValidatorTags() {
    Class<?> model = getModel();

    if ((model == null) || Validator.isNotNull(getType())) {
      return;
    }

    List<Tuple> modelValidators = ModelHintsUtil.getValidators(model.getName(), getField());

    if (modelValidators == null) {
      return;
    }

    for (Tuple modelValidator : modelValidators) {
      String validatorName = (String) modelValidator.getObject(1);
      String validatorErrorMessage = (String) modelValidator.getObject(2);
      String validatorValue = (String) modelValidator.getObject(3);
      boolean customValidator = (Boolean) modelValidator.getObject(4);

      ValidatorTag validatorTag =
          new ValidatorTagImpl(
              validatorName, validatorErrorMessage, validatorValue, customValidator);

      addValidatorTag(validatorName, validatorTag);
    }
  }
Beispiel #3
0
  @Override
  protected void setAttributes(HttpServletRequest request) {
    super.setAttributes(request);

    Object bean = getBean();

    if (bean == null) {
      bean = pageContext.getAttribute("aui:model-context:bean");
    }

    Class<?> model = getModel();

    String defaultLanguageId = getDefaultLanguageId();

    if (Validator.isNull(defaultLanguageId)) {
      defaultLanguageId = (String) pageContext.getAttribute("aui:model-context:defaultLanguageId");
    }

    if (Validator.isNull(defaultLanguageId)) {
      if ((model != null) && ModelHintsUtil.hasField(model.getName(), "groupId")) {

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        defaultLanguageId = LocaleUtil.toLanguageId(themeDisplay.getSiteDefaultLocale());
      }
    }

    if (Validator.isNull(defaultLanguageId)) {
      Locale defaultLocale = LocaleUtil.getDefault();

      defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
    }

    String name = getName();

    int pos = name.indexOf(StringPool.DOUBLE_DASH);

    if (pos != -1) {
      name = name.substring(pos + 2, name.length() - 2);
    }

    String field = getField();

    if (Validator.isNull(field)) {
      field = getName();
    }

    String formName = getFormName();

    if (formName == null) {
      FormTag formTag = (FormTag) findAncestorWithClass(this, FormTag.class);

      if (formTag != null) {
        formName = formTag.getName();
      }
    }

    String id = getId();
    String type = getType();

    if (Validator.isNull(id)) {
      String fieldParam = getFieldParam();

      if ((model != null) && Validator.isNull(type) && Validator.isNotNull(fieldParam)) {

        id = AUIUtil.normalizeId(fieldParam);
      } else if (!Objects.equals(type, "assetTags") && !Objects.equals(type, "radio")) {

        id = AUIUtil.normalizeId(name);
      } else {
        id = PortalUtil.getUniqueElementId(request, StringPool.BLANK, AUIUtil.normalizeId(name));
      }
    }

    String forLabel = id;

    if (Objects.equals(type, "assetTags")) {
      forLabel = forLabel.concat("assetTagNames");
    }

    String languageId = getLanguageId();

    if (Validator.isNotNull(languageId)) {
      forLabel = LocalizationUtil.getLocalizedName(forLabel, languageId);
    }

    String label = getLabel();

    if (label == null) {
      label = TextFormatter.format(name, TextFormatter.P);
    }

    String title = getTitle();

    if ((title == null) && (Validator.isNull(label) || Objects.equals(type, "image"))) {

      title = TextFormatter.format(name, TextFormatter.P);
    }

    boolean wrappedField = getWrappedField();

    FieldWrapperTag fieldWrapper =
        (FieldWrapperTag) findAncestorWithClass(this, FieldWrapperTag.class);

    if (fieldWrapper != null) {
      wrappedField = true;
    }

    setNamespacedAttribute(request, "baseType", getBaseType());
    setNamespacedAttribute(request, "bean", bean);
    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
    setNamespacedAttribute(request, "field", field);
    setNamespacedAttribute(request, "forLabel", forLabel);
    setNamespacedAttribute(request, "formName", formName);
    setNamespacedAttribute(request, "id", id);
    setNamespacedAttribute(request, "label", label);
    setNamespacedAttribute(request, "model", model);
    setNamespacedAttribute(request, "title", String.valueOf(title));
    setNamespacedAttribute(request, "wrappedField", wrappedField);

    request.setAttribute(getAttributeNamespace() + "value", getValue());

    Map<String, ValidatorTag> validatorTags = getValidatorTags();

    if ((validatorTags != null) && (validatorTags.get("required") != null)) {

      setNamespacedAttribute(request, "required", Boolean.TRUE.toString());
    }
  }
  @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;
  }