Exemplo n.º 1
0
  protected void importAssetVocabulary(
      PortletDataContext portletDataContext,
      Map<Long, Long> assetVocabularyPKs,
      Element assetVocabularyElement,
      AssetVocabulary assetVocabulary)
      throws Exception {

    long userId = portletDataContext.getUserId(assetVocabulary.getUserUuid());
    long groupId = portletDataContext.getScopeGroupId();

    ServiceContext serviceContext = new ServiceContext();

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

    AssetVocabulary importedAssetVocabulary = null;

    AssetVocabulary existingAssetVocabulary =
        AssetVocabularyUtil.fetchByG_N(groupId, assetVocabulary.getName());

    if (existingAssetVocabulary == null) {
      Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(portletDataContext.getCompanyId());

      existingAssetVocabulary =
          AssetVocabularyUtil.fetchByG_N(companyGroup.getGroupId(), assetVocabulary.getName());
    }

    if (existingAssetVocabulary == null) {
      serviceContext.setUuid(assetVocabulary.getUuid());

      importedAssetVocabulary =
          AssetVocabularyLocalServiceUtil.addVocabulary(
              userId,
              assetVocabulary.getTitle(),
              getAssetVocabularyTitleMap(assetVocabulary),
              assetVocabulary.getDescriptionMap(),
              assetVocabulary.getSettings(),
              serviceContext);
    } else {
      importedAssetVocabulary =
          AssetVocabularyLocalServiceUtil.updateVocabulary(
              existingAssetVocabulary.getVocabularyId(),
              assetVocabulary.getTitle(),
              getAssetVocabularyTitleMap(assetVocabulary),
              assetVocabulary.getDescriptionMap(),
              assetVocabulary.getSettings(),
              serviceContext);
    }

    assetVocabularyPKs.put(
        assetVocabulary.getVocabularyId(), importedAssetVocabulary.getVocabularyId());

    portletDataContext.importPermissions(
        AssetVocabulary.class,
        assetVocabulary.getVocabularyId(),
        importedAssetVocabulary.getVocabularyId());
  }
  protected AssetCategory getAssetCategory(long userId, long groupId, String name)
      throws PortalException, SystemException {

    AssetVocabulary assetVocabulary =
        assetVocabularyPersistence.fetchByG_N(groupId, _ASSET_VOCABULARY_NAME);

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setScopeGroupId(groupId);
    serviceContext.setUserId(userId);

    if (assetVocabulary == null) {
      assetVocabulary =
          assetVocabularyLocalService.addVocabulary(userId, _ASSET_VOCABULARY_NAME, serviceContext);
    }

    AssetCategory assetCategory =
        assetCategoryPersistence.fetchByP_N_V(
            AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID,
            name,
            assetVocabulary.getVocabularyId());

    if (assetCategory != null) {
      return assetCategory;
    }

    return assetCategoryLocalService.addCategory(
        userId, name, assetVocabulary.getVocabularyId(), serviceContext);
  }
  @Indexable(type = IndexableType.DELETE)
  @Override
  @SystemEvent(action = SystemEventConstants.ACTION_SKIP, type = SystemEventConstants.TYPE_DELETE)
  public void deleteVocabulary(AssetVocabulary vocabulary) throws PortalException {

    // Vocabulary

    assetVocabularyPersistence.remove(vocabulary);

    // Resources

    resourceLocalService.deleteResource(
        vocabulary.getCompanyId(),
        AssetVocabulary.class.getName(),
        ResourceConstants.SCOPE_INDIVIDUAL,
        vocabulary.getVocabularyId());

    // Categories

    assetCategoryLocalService.deleteVocabularyCategories(vocabulary.getVocabularyId());
  }
  protected void exportAssetVocabulary(
      PortletDataContext portletDataContext,
      Element assetVocabulariesElement,
      AssetVocabulary assetVocabulary)
      throws Exception {

    String path = getAssetVocabulariesPath(portletDataContext, assetVocabulary.getVocabularyId());

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

    Element assetVocabularyElement = assetVocabulariesElement.addElement("vocabulary");

    assetVocabularyElement.addAttribute("path", path);

    assetVocabulary.setUserUuid(assetVocabulary.getUserUuid());

    portletDataContext.addZipEntry(path, assetVocabulary);

    portletDataContext.addPermissions(AssetVocabulary.class, assetVocabulary.getVocabularyId());
  }
  @Override
  public void addVocabularyResources(
      AssetVocabulary vocabulary, String[] groupPermissions, String[] guestPermissions)
      throws PortalException {

    resourceLocalService.addModelResources(
        vocabulary.getCompanyId(),
        vocabulary.getGroupId(),
        vocabulary.getUserId(),
        AssetVocabulary.class.getName(),
        vocabulary.getVocabularyId(),
        groupPermissions,
        guestPermissions);
  }
  @Override
  public void addVocabularyResources(
      AssetVocabulary vocabulary, boolean addGroupPermissions, boolean addGuestPermissions)
      throws PortalException {

    resourceLocalService.addResources(
        vocabulary.getCompanyId(),
        vocabulary.getGroupId(),
        vocabulary.getUserId(),
        AssetVocabulary.class.getName(),
        vocabulary.getVocabularyId(),
        false,
        addGroupPermissions,
        addGuestPermissions);
  }
  public long[] getAvailableAssetVocabularyIds() throws PortalException {
    if (_availableAssetVocabularyIds != null) {
      return _availableAssetVocabularyIds;
    }

    List<AssetVocabulary> assetVocabularies = getAssetVocabularies();

    _availableAssetVocabularyIds = new long[assetVocabularies.size()];

    for (int i = 0; i < assetVocabularies.size(); i++) {
      AssetVocabulary assetVocabulary = assetVocabularies.get(i);

      _availableAssetVocabularyIds[i] = assetVocabulary.getVocabularyId();
    }

    return _availableAssetVocabularyIds;
  }
  public JSONArray getJSONSearch(
      long groupId, String name, long[] vocabularyIds, int start, int end)
      throws PortalException, SystemException {

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    for (AssetVocabulary vocabulary : assetVocabularyService.getVocabularies(vocabularyIds)) {

      List<AssetCategory> vocabularyCategory =
          assetCategoryFinder.findByG_N_V(
              groupId, name, vocabulary.getVocabularyId(), start, end, null);

      JSONArray vocabularyCategoryJSONArray = toJSONArray(vocabularyCategory);

      for (int i = 0; i < vocabularyCategoryJSONArray.length(); ++i) {
        JSONObject vocabularyCategoryJSONObject = vocabularyCategoryJSONArray.getJSONObject(i);

        jsonArray.put(vocabularyCategoryJSONObject);
      }
    }

    return jsonArray;
  }
  @Override
  public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
      throws IOException, PortletException {

    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
    Map<AssetVocabulary, List<AssetCategory>> vocabulariesMap =
        new HashMap<AssetVocabulary, List<AssetCategory>>();
    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
    long[] categoryIds = ParamUtil.getLongValues(renderRequest, "categoryIds");
    try {

      Group siteGroup = themeDisplay.getSiteGroup();
      long scopeGroupId = themeDisplay.getScopeGroupId();
      vocabularies.addAll(
          AssetVocabularyServiceUtil.getGroupVocabularies(siteGroup.getGroupId(), false));

      if (scopeGroupId != themeDisplay.getCompanyGroupId()) {
        vocabularies.addAll(
            AssetVocabularyServiceUtil.getGroupVocabularies(
                themeDisplay.getCompanyGroupId(), false));
      }

      long classNameId = PortalUtil.getClassNameId(Announcement.class);

      // Select announcement vocabularies for announcement only.
      for (AssetVocabulary vocabulary : vocabularies) {
        vocabulary = vocabulary.toEscapedModel();

        int vocabularyCategoriesCount =
            AssetCategoryServiceUtil.getVocabularyCategoriesCount(
                vocabulary.getGroupId(), vocabulary.getVocabularyId());

        if (vocabularyCategoriesCount == 0) {
          continue;
        }

        UnicodeProperties settingsProperties = vocabulary.getSettingsProperties();

        long[] selectedClassNameIds =
            StringUtil.split(settingsProperties.getProperty("selectedClassNameIds"), 0L);

        if ((selectedClassNameIds.length > 0)
            && (selectedClassNameIds[0] != AssetCategoryConstants.ALL_CLASS_NAME_IDS)
            && !ArrayUtil.contains(selectedClassNameIds, classNameId)) {
          continue;
        }

        List<AssetCategory> assetCategories =
            AssetCategoryServiceUtil.getVocabularyRootCategories(
                themeDisplay.getScopeGroupId(),
                vocabulary.getVocabularyId(),
                QueryUtil.ALL_POS,
                QueryUtil.ALL_POS,
                null);
        vocabulariesMap.put(vocabulary, assetCategories);
      }
    } catch (SystemException e) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(e);
      }
      LOGGER.error("SystemException: unable to get types or currencies or vocabularies");
    } catch (PortalException e) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(e);
      }
      LOGGER.error("SystemException: unable to get types or currencies or vocabularies");
    }

    renderRequest.setAttribute("vocabulariesMap", vocabulariesMap);
    renderRequest.setAttribute("categoryIds", StringUtil.merge(categoryIds));
    renderRequest.setAttribute("htmlUtil", HtmlUtil.getHtml());

    super.doView(renderRequest, renderResponse);
  }
  public void loadCategoriesFromCsv(ActionRequest actionRequest, ActionResponse actionResponse)
      throws IOException, PortletException, PortalException, SystemException {

    LOGGER.info("Begin  AssetCategoriesImporter");
    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    ThemeDisplay themeDisplay =
        (ThemeDisplay) uploadPortletRequest.getAttribute(WebKeys.THEME_DISPLAY);
    Map<String, FileItem[]> multipartParameterMap = uploadPortletRequest.getMultipartParameterMap();

    File file = null;
    BufferedReader bufferedReader = null;
    AssetVocabulary assetVocabulary = null;
    long userId = themeDisplay.getUserId();
    long groupId = themeDisplay.getScopeGroupId();

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(AssetCategory.class.getName(), uploadPortletRequest);

    // create vocabulary
    try {

      assetVocabulary =
          AssetVocabularyLocalServiceUtil.addVocabulary(
              userId,
              "Announcements",
              ServiceContextFactory.getInstance(
                  AssetVocabulary.class.getName(), uploadPortletRequest));
    } catch (DuplicateVocabularyException dve) {
      try {
        assetVocabulary =
            AssetVocabularyLocalServiceUtil.getGroupVocabulary(groupId, "Announcements");
      } catch (PortalException e) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug(e);
        }
        LOGGER.error(e.getMessage());
      } catch (SystemException e) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug(e);
        }
        LOGGER.error(e.getMessage());
      }
    }

    if (multipartParameterMap.keySet().contains("fileCategory")
        && Validator.isNotNull(assetVocabulary)) {
      try {
        file = uploadPortletRequest.getFile("fileCategory");
        bufferedReader = new BufferedReader(new FileReader(file.getAbsolutePath()));
        String line = StringPool.BLANK;
        long vocabularyId = assetVocabulary.getVocabularyId();

        String parentCategoryName = StringPool.BLANK;
        AssetCategory parentCategory = null;

        while ((line = bufferedReader.readLine()) != null) {

          // use comma as separator
          String[] categories = line.split(StringPool.SEMICOLON);

          if (categories.length == 4) {
            String parentCategoryCode = categories[0];

            if (Validator.isNull(parentCategory) || !parentCategoryName.equals(categories[1])) {
              parentCategoryName = categories[1];
              try {
                parentCategory =
                    AssetCategoryLocalServiceUtil.addCategory(
                        userId, parentCategoryName, vocabularyId, serviceContext);
                AssetCategoryPropertyLocalServiceUtil.addCategoryProperty(
                    userId, parentCategory.getCategoryId(), "Code", parentCategoryCode);
              } catch (Exception e) {
                if (LOGGER.isDebugEnabled()) {
                  LOGGER.debug(e);
                }
                LOGGER.error(e.getMessage());
              }
            }

            Map<Locale, String> titles = new HashMap<Locale, String>();
            Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

            titles.put(LocaleUtil.fromLanguageId("fr_FR"), categories[3]);
            titles.put(LocaleUtil.fromLanguageId("en_US"), categories[3]);

            try {
              AssetCategory child =
                  AssetCategoryLocalServiceUtil.addCategory(
                      userId,
                      parentCategory.getCategoryId(),
                      titles,
                      descriptionMap,
                      vocabularyId,
                      null,
                      serviceContext);
              AssetCategoryPropertyLocalServiceUtil.addCategoryProperty(
                  userId, child.getCategoryId(), "Code", categories[2]);
            } catch (Exception e) {
              if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(e);
              }
              LOGGER.error(e.getMessage());
            }
          }
        }
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (bufferedReader != null) {
          try {
            bufferedReader.close();
          } catch (IOException e) {
            if (LOGGER.isDebugEnabled()) {
              LOGGER.debug(e);
            }
            LOGGER.error(e.getMessage());
          }
        }
      }
    }
    LOGGER.info("End  AssetCategoriesImporter");
  }