protected void addSearchNotAllTags(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[][] notAllTagIdsArray = _assetEntryQuery.getNotAllTagIdsArray();

    if (notAllTagIdsArray.length == 0) {
      return;
    }

    BooleanQuery tagIdsArrayQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (long[] notAllTagIds : notAllTagIdsArray) {
      if (notAllTagIds.length == 0) {
        continue;
      }

      BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long tagId : notAllTagIds) {
        tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
      }

      tagIdsArrayQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
    }

    contextQuery.add(tagIdsArrayQuery, BooleanClauseOccur.MUST_NOT);
  }
  protected void addSearchKeywords(BooleanQuery searchQuery, SearchContext searchContext)
      throws Exception {

    String keywords = searchContext.getKeywords();

    if (Validator.isNull(keywords)) {
      return;
    }

    BooleanQuery titleQuery = new BooleanQueryImpl();

    _repositorySearchQueryTermBuilder.addTerm(titleQuery, searchContext, Field.TITLE, keywords);

    if (titleQuery.hasClauses() && !contains(searchQuery, titleQuery)) {
      searchQuery.add(titleQuery, BooleanClauseOccur.SHOULD);
    }

    BooleanQuery userNameQuery = new BooleanQueryImpl();

    _repositorySearchQueryTermBuilder.addTerm(
        userNameQuery, searchContext, Field.USER_NAME, keywords);

    if (userNameQuery.hasClauses() && !contains(searchQuery, userNameQuery)) {

      searchQuery.add(userNameQuery, BooleanClauseOccur.SHOULD);
    }

    BooleanQuery contentQuery = new BooleanQueryImpl();

    _repositorySearchQueryTermBuilder.addTerm(contentQuery, searchContext, Field.CONTENT, keywords);

    if (contentQuery.hasClauses() && !contains(searchQuery, contentQuery)) {
      searchQuery.add(contentQuery, BooleanClauseOccur.SHOULD);
    }
  }
  public Hits search(
      long companyId,
      String contactBookCode,
      String contactBookName,
      String contactDescription,
      String sortField,
      int sortType,
      boolean reverse,
      int start,
      int end)
      throws SystemException {
    try {
      BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
      contextQuery.addRequiredTerm(Field.PORTLET_ID, ContactBookIndexer.PORTLET_ID);

      BooleanQuery contactBookCodeQuery = BooleanQueryFactoryUtil.create();
      BooleanQuery contactBookNameQuery = BooleanQueryFactoryUtil.create();
      BooleanQuery contactDescriptionQuery = BooleanQueryFactoryUtil.create();

      List<BooleanQuery> booleanQueries = new ArrayList<BooleanQuery>();

      if (Validator.isNotNull(contactBookCode)) {
        contactBookCodeQuery.addTerm(ContactBookDisplayTerms.CODE, contactBookCode);
        booleanQueries.add(contactBookCodeQuery);
      }

      if (Validator.isNotNull(contactBookName)) {
        contactBookNameQuery.addTerm(ContactBookDisplayTerms.NAME, contactBookName);
        booleanQueries.add(contactBookNameQuery);
      }

      if (Validator.isNotNull(contactDescription)) {
        contactDescriptionQuery.addTerm(ContactBookDisplayTerms.DESCRIPTION, contactDescription);
        booleanQueries.add(contactDescriptionQuery);
      }

      BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
      fullQuery.add(contextQuery, BooleanClauseOccur.MUST);

      for (int i = 0; i < booleanQueries.size(); i++) {
        fullQuery.add(booleanQueries.get(i), BooleanClauseOccur.MUST);
      }

      Sort sort;
      sort = new Sort(sortField, sortType, reverse);
      Hits hits = SearchEngineUtil.search(companyId, fullQuery, sort, start, end);
      return hits;
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  protected void addSearchAllCategories(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    long[] allCategoryIds = _assetEntryQuery.getAllCategoryIds();

    if (allCategoryIds.length == 0) {
      return;
    }

    long[] filteredAllCategoryIds = AssetUtil.filterCategoryIds(permissionChecker, allCategoryIds);

    if (allCategoryIds.length != filteredAllCategoryIds.length) {
      addImpossibleTerm(contextQuery, Field.ASSET_CATEGORY_IDS);

      return;
    }

    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (long allCategoryId : filteredAllCategoryIds) {
      AssetCategory assetCategory = AssetCategoryLocalServiceUtil.fetchAssetCategory(allCategoryId);

      if (assetCategory == null) {
        continue;
      }

      List<Long> categoryIds = new ArrayList<>();

      if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
        categoryIds.addAll(AssetCategoryLocalServiceUtil.getSubcategoryIds(allCategoryId));
      }

      if (categoryIds.isEmpty()) {
        categoryIds.add(allCategoryId);
      }

      BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long categoryId : categoryIds) {
        categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
      }

      categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
    }

    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST);
  }
  protected void addSearchAnyTags(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    long[] anyTagIds = _assetEntryQuery.getAnyTagIds();

    if (anyTagIds.length == 0) {
      return;
    }

    long[] filteredAnyTagIds = AssetUtil.filterTagIds(permissionChecker, anyTagIds);

    if (filteredAnyTagIds.length == 0) {
      addImpossibleTerm(contextQuery, Field.ASSET_TAG_IDS);

      return;
    }

    BooleanQuery tagIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (long tagId : anyTagIds) {
      tagIdsQuery.addTerm(Field.ASSET_TAG_IDS, tagId);
    }

    contextQuery.add(tagIdsQuery, BooleanClauseOccur.MUST);
  }
  protected void addContext(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[] folderIds = searchContext.getFolderIds();

    if (ArrayUtil.isEmpty(folderIds)) {
      return;
    }

    if (folderIds[0] == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
      return;
    }

    BooleanQuery folderIdsQuery = new BooleanQueryImpl();

    for (long folderId : folderIds) {
      try {
        _dlAppService.getFolder(folderId);
      } catch (Exception e) {
        continue;
      }

      folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
    }

    contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
  }
  @Override
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[] folderIds = searchContext.getFolderIds();

    if ((folderIds != null) && (folderIds.length > 0)) {
      if (folderIds[0] == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

        return;
      }

      BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long folderId : folderIds) {
        try {
          BookmarksFolderServiceUtil.getFolder(folderId);
        } catch (Exception e) {
          continue;
        }

        folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
      }

      contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
    }

    int status =
        GetterUtil.getInteger(
            searchContext.getAttribute(Field.STATUS), WorkflowConstants.STATUS_APPROVED);

    if (status != WorkflowConstants.STATUS_ANY) {
      contextQuery.addRequiredTerm(Field.STATUS, status);
    }
  }
  @Override
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[] folderIds = searchContext.getFolderIds();

    if ((folderIds != null) && (folderIds.length > 0)) {
      if (folderIds[0] == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

        return;
      }

      BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long folderId : folderIds) {
        try {
          BookmarksFolderServiceUtil.getFolder(folderId);
        } catch (Exception e) {
          continue;
        }

        folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
      }

      contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
    }
  }
  @Override
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    Long classNameId = (Long) searchContext.getAttribute(Field.CLASS_NAME_ID);

    if (classNameId != null) {
      contextQuery.addRequiredTerm("classNameId", classNameId.toString());
    }

    int status =
        GetterUtil.getInteger(
            searchContext.getAttribute(Field.STATUS), WorkflowConstants.STATUS_APPROVED);

    if (status != WorkflowConstants.STATUS_ANY) {
      contextQuery.addRequiredTerm(Field.STATUS, status);
    }

    long[] folderIds = searchContext.getFolderIds();

    if ((folderIds != null) && (folderIds.length > 0)) {
      if (folderIds[0] == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

        return;
      }

      BooleanQuery folderIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long folderId : folderIds) {
        try {
          JournalFolderServiceUtil.getFolder(folderId);
        } catch (Exception e) {
          continue;
        }

        folderIdsQuery.addTerm(Field.FOLDER_ID, folderId);
      }

      contextQuery.add(folderIdsQuery, BooleanClauseOccur.MUST);
    }

    String articleType = (String) searchContext.getAttribute("articleType");

    if (Validator.isNotNull(articleType)) {
      contextQuery.addRequiredTerm(Field.TYPE, articleType);
    }

    String structureId = (String) searchContext.getAttribute("structureId");

    if (Validator.isNotNull(structureId)) {
      contextQuery.addRequiredTerm("structureId", structureId);
    }

    String templateId = (String) searchContext.getAttribute("templateId");

    if (Validator.isNotNull(templateId)) {
      contextQuery.addRequiredTerm("templateId", templateId);
    }
  }
  @Override
  protected BooleanClause doGetFacetClause() {
    SearchContext searchContext = getSearchContext();

    FacetConfiguration facetConfiguration = getFacetConfiguration();

    JSONObject dataJSONObject = facetConfiguration.getData();

    String[] values = null;

    if (isStatic() && dataJSONObject.has("values")) {
      JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");

      values = new String[valuesJSONArray.length()];

      for (int i = 0; i < valuesJSONArray.length(); i++) {
        values[i] = valuesJSONArray.getString(i);
      }
    }

    String[] valuesParam =
        StringUtil.split(GetterUtil.getString(searchContext.getAttribute(getFieldName())));

    if (!isStatic() && (valuesParam != null) && (valuesParam.length > 0)) {
      values = valuesParam;
    }

    if ((values == null) || (values.length == 0)) {
      return null;
    }

    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (String value : values) {
      FacetValueValidator facetValueValidator = getFacetValueValidator();

      if ((searchContext.getUserId() > 0) && !facetValueValidator.check(searchContext, value)) {

        continue;
      }

      TermQuery termQuery = TermQueryFactoryUtil.create(searchContext, getFieldName(), value);

      try {
        facetQuery.add(termQuery, BooleanClauseOccur.SHOULD);
      } catch (ParseException pe) {
        _log.error(pe, pe);
      }
    }

    if (!facetQuery.hasClauses()) {
      return null;
    }

    return BooleanClauseFactoryUtil.create(
        searchContext, facetQuery, BooleanClauseOccur.MUST.getName());
  }
Example #11
0
  protected void addContextQueryParams(
      BooleanQuery contextQuery, SearchContext searchContext, String key, Object value)
      throws Exception {

    if (key.equals("usersGroups")) {
      if (value instanceof Long[]) {
        Long[] values = (Long[]) value;

        BooleanQuery usersGroupsQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (long groupId : values) {
          usersGroupsQuery.addTerm("groupIds", groupId);
        }

        contextQuery.add(usersGroupsQuery, BooleanClauseOccur.MUST);
      } else {
        contextQuery.addRequiredTerm("groupIds", String.valueOf(value));
      }
    } else if (key.equals("usersOrgs")) {
      if (value instanceof Long[]) {
        Long[] values = (Long[]) value;

        BooleanQuery usersOrgsQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (long organizationId : values) {
          usersOrgsQuery.addTerm("organizationIds", organizationId);
          usersOrgsQuery.addTerm("ancestorOrganizationIds", organizationId);
        }

        contextQuery.add(usersOrgsQuery, BooleanClauseOccur.MUST);
      } else {
        contextQuery.addRequiredTerm("organizationIds", String.valueOf(value));
      }
    } else if (key.equals("usersOrgsCount")) {
      contextQuery.addRequiredTerm("organizationCount", String.valueOf(value));
    } else if (key.equals("usersRoles")) {
      contextQuery.addRequiredTerm("roleIds", String.valueOf(value));
    } else if (key.equals("usersTeams")) {
      contextQuery.addRequiredTerm("teamIds", String.valueOf(value));
    } else if (key.equals("usersUserGroups")) {
      contextQuery.addRequiredTerm("userGroupIds", String.valueOf(value));
    }
  }
  public List<Document> searchAllDocuments(
      SearchContext searchContext,
      List<Query> filterQueries,
      long companyId,
      Sort sort,
      int start,
      int end) {

    LOGGER.info("FilterQueries size: " + filterQueries.size());
    final BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
    final BooleanQuery allEntriesBooleanQuery = BooleanQueryFactoryUtil.create(searchContext);

    allEntriesBooleanQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, EmpDiscipline.class.getName());

    try {
      // add filter queries
      fullQuery.add(allEntriesBooleanQuery, BooleanClauseOccur.MUST);
      if (filterQueries != null && filterQueries.size() > 0) {
        for (Query query : filterQueries) {
          fullQuery.add(query, BooleanClauseOccur.MUST);
        }
      }

      final List<Document> documents =
          SearchEngineUtil.search(
                  SearchEngineUtil.getDefaultSearchEngineId(),
                  companyId,
                  fullQuery,
                  sort,
                  start,
                  end)
              .toList();
      LOGGER.info("RESULT SIZE: " + documents.size());

      return documents;

    } catch (SearchException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    } catch (ParseException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    }
    return new ArrayList<>();
  }
  @Override
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    addStatus(contextQuery, searchContext);

    if (searchContext.isIncludeAttachments()) {
      addRelatedClassNames(contextQuery, searchContext);
    }

    contextQuery.addRequiredTerm(Field.HIDDEN, searchContext.isIncludeAttachments());

    addSearchClassTypeIds(contextQuery, searchContext);

    String ddmStructureFieldName = (String) searchContext.getAttribute("ddmStructureFieldName");
    Serializable ddmStructureFieldValue = searchContext.getAttribute("ddmStructureFieldValue");

    if (Validator.isNotNull(ddmStructureFieldName) && Validator.isNotNull(ddmStructureFieldValue)) {

      String[] ddmStructureFieldNameParts =
          StringUtil.split(ddmStructureFieldName, StringPool.SLASH);

      DDMStructure structure =
          DDMStructureLocalServiceUtil.getStructure(
              GetterUtil.getLong(ddmStructureFieldNameParts[1]));

      String fieldName =
          StringUtil.replaceLast(
              ddmStructureFieldNameParts[2],
              StringPool.UNDERLINE.concat(LocaleUtil.toLanguageId(searchContext.getLocale())),
              StringPool.BLANK);

      try {
        ddmStructureFieldValue =
            DDMUtil.getIndexedFieldValue(ddmStructureFieldValue, structure.getFieldType(fieldName));
      } catch (StructureFieldException sfe) {
      }

      contextQuery.addRequiredTerm(
          ddmStructureFieldName, StringPool.QUOTE + ddmStructureFieldValue + StringPool.QUOTE);
    }

    String[] mimeTypes = (String[]) searchContext.getAttribute("mimeTypes");

    if (ArrayUtil.isNotEmpty(mimeTypes)) {
      BooleanQuery mimeTypesQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (String mimeType : mimeTypes) {
        mimeTypesQuery.addTerm(
            "mimeType", StringUtil.replace(mimeType, CharPool.FORWARD_SLASH, CharPool.UNDERLINE));
      }

      contextQuery.add(mimeTypesQuery, BooleanClauseOccur.MUST);
    }
  }
  public Hits search(
      long companyId,
      String keywords,
      String sortField,
      int sortType,
      boolean reverse,
      int start,
      int end)
      throws SystemException {
    try {
      BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
      contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);

      if (Validator.isNotNull(ContactBookIndexer.PORTLET_ID)) {
        contextQuery.addRequiredTerm(Field.PORTLET_ID, ContactBookIndexer.PORTLET_ID);
      }

      BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();

      if (Validator.isNotNull(keywords)) {
        searchQuery.addTerm(Field.TITLE, keywords);
        searchQuery.addTerm("contactBookCode", keywords + "*");
        searchQuery.addTerm("contactBookName", keywords + "*");
        searchQuery.addTerm("contactDescription", keywords + "*");
      }

      BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
      fullQuery.add(contextQuery, BooleanClauseOccur.MUST);

      if (searchQuery.clauses().size() > 0) {
        fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
      }

      Sort sort;
      sort = new Sort(sortField, sortType, reverse);
      Hits hits = SearchEngineUtil.search(companyId, fullQuery, sort, start, end);
      return hits;
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  protected void addSearchNotAllCategories(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[] notAllCategoryIds = _assetEntryQuery.getNotAllCategoryIds();

    if (notAllCategoryIds.length == 0) {
      return;
    }

    BooleanQuery categoryIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (long notAllCategoryId : notAllCategoryIds) {
      AssetCategory assetCategory =
          AssetCategoryLocalServiceUtil.fetchAssetCategory(notAllCategoryId);

      if (assetCategory == null) {
        continue;
      }

      List<Long> categoryIds = new ArrayList<>();

      if (PropsValues.ASSET_CATEGORIES_SEARCH_HIERARCHICAL) {
        categoryIds.addAll(AssetCategoryLocalServiceUtil.getSubcategoryIds(notAllCategoryId));
      }

      if (categoryIds.isEmpty()) {
        categoryIds.add(notAllCategoryId);
      }

      BooleanQuery categoryIdQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long categoryId : categoryIds) {
        categoryIdQuery.addTerm(Field.ASSET_CATEGORY_IDS, categoryId);
      }

      categoryIdsQuery.add(categoryIdQuery, BooleanClauseOccur.MUST);
    }

    contextQuery.add(categoryIdsQuery, BooleanClauseOccur.MUST_NOT);
  }
  @Override
  public BooleanQuery getFullQuery(SearchContext searchContext) throws SearchException {

    try {
      BooleanQuery contextQuery = new BooleanQueryImpl();

      addContext(contextQuery, searchContext);

      BooleanQuery searchQuery = new BooleanQueryImpl();

      addSearchKeywords(searchQuery, searchContext);

      BooleanQuery fullQuery = new BooleanQueryImpl();

      if (contextQuery.hasClauses()) {
        fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
      }

      if (searchQuery.hasClauses()) {
        fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
      }

      BooleanClause<Query>[] booleanClauses = searchContext.getBooleanClauses();

      if (booleanClauses != null) {
        for (BooleanClause<Query> booleanClause : booleanClauses) {
          fullQuery.add(booleanClause.getClause(), booleanClause.getBooleanClauseOccur());
        }
      }

      fullQuery.setQueryConfig(searchContext.getQueryConfig());

      return fullQuery;
    } catch (Exception e) {
      throw new SearchException(e);
    }
  }
  @Override
  protected Map<String, Query> addSearchLocalizedTerm(
      BooleanQuery searchQuery, SearchContext searchContext, String field, boolean like)
      throws Exception {

    if (Validator.isNull(field)) {
      return Collections.emptyMap();
    }

    String value = String.valueOf(searchContext.getAttribute(field));

    if (Validator.isNull(value)) {
      value = searchContext.getKeywords();
    }

    if (Validator.isNull(value)) {
      return Collections.emptyMap();
    }

    String localizedField = DocumentImpl.getLocalizedName(searchContext.getLocale(), field);

    Map<String, Query> queries = new HashMap<>();

    if (Validator.isNull(searchContext.getKeywords())) {
      BooleanQuery localizedQuery = new BooleanQueryImpl();

      Query query = localizedQuery.addTerm(field, value, like);

      queries.put(field, query);

      Query localizedFieldQuery = localizedQuery.addTerm(localizedField, value, like);

      queries.put(field, localizedFieldQuery);

      BooleanClauseOccur booleanClauseOccur = BooleanClauseOccur.SHOULD;

      if (searchContext.isAndSearch()) {
        booleanClauseOccur = BooleanClauseOccur.MUST;
      }

      searchQuery.add(localizedQuery, booleanClauseOccur);
    } else {
      Query query = searchQuery.addTerm(localizedField, value, like);

      queries.put(field, query);
    }

    return queries;
  }
  @Override
  public void postProcessContextQuery(BooleanQuery contextQuery, SearchContext searchContext)
      throws Exception {

    long[] vocabularyIds = (long[]) searchContext.getAttribute(Field.ASSET_VOCABULARY_IDS);

    if (!ArrayUtil.isEmpty(vocabularyIds)) {
      BooleanQuery vocabularyQuery = BooleanQueryFactoryUtil.create(searchContext);

      for (long vocabularyId : vocabularyIds) {
        vocabularyQuery.addTerm(Field.ASSET_VOCABULARY_ID, String.valueOf(vocabularyId));
      }

      contextQuery.add(vocabularyQuery, BooleanClauseOccur.MUST);
    }
  }
  public Document getIndexedDocument(long id, SearchContext searchContext) {
    searchContext.setPortletIds(new String[] {EMInfo.PORTLET_ID});
    BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);
    BooleanQuery booleanQuery = BooleanQueryFactoryUtil.create(searchContext);
    booleanQuery.addRequiredTerm(Field.ENTRY_CLASS_NAME, EmpDiscipline.class.getName());
    booleanQuery.addExactTerm(EmpDisciplineField.ID, id);

    try {
      fullQuery.add(booleanQuery, BooleanClauseOccur.MUST);
      Hits hits = SearchEngineUtil.search(searchContext, fullQuery);
      return !hits.toList().isEmpty() ? hits.toList().get(0) : null;
    } catch (ParseException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    } catch (SearchException e) {
      LogFactoryUtil.getLog(EmpDisciplineLocalServiceImpl.class).info(e);
    }

    return null;
  }
Example #20
0
  public Hits search(
      long companyId,
      long userId,
      String portletId,
      long groupId,
      long[] repositoryIds,
      String keywords,
      int start,
      int end) {

    try {
      SearchContext searchContext = new SearchContext();

      searchContext.setCompanyId(companyId);
      searchContext.setEnd(end);
      searchContext.setEntryClassNames(new String[] {DLFileEntryConstants.getClassName()});
      searchContext.setGroupIds(new long[] {groupId});

      Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntryConstants.getClassName());

      searchContext.setSearchEngineId(indexer.getSearchEngineId());

      searchContext.setStart(start);
      searchContext.setUserId(userId);

      BooleanQuery contextQuery = BooleanQueryFactoryUtil.create(searchContext);

      contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);

      if (groupId > 0) {
        Group group = groupLocalService.getGroup(groupId);

        if (group.isLayout()) {
          contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);

          groupId = group.getParentGroupId();
        }

        contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
      }

      if (ArrayUtil.isNotEmpty(repositoryIds)) {
        BooleanQuery repositoryIdsQuery = BooleanQueryFactoryUtil.create(searchContext);

        for (long repositoryId : repositoryIds) {
          try {
            if (userId > 0) {
              PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

              DLFolderPermission.check(permissionChecker, groupId, repositoryId, ActionKeys.VIEW);
            }

            if (repositoryId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

              repositoryId = groupId;
            }

            TermQuery termQuery =
                TermQueryFactoryUtil.create(searchContext, "repositoryId", repositoryId);

            repositoryIdsQuery.add(termQuery, BooleanClauseOccur.SHOULD);
          } catch (Exception e) {
          }
        }

        contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
      }

      BooleanQuery searchQuery = BooleanQueryFactoryUtil.create(searchContext);

      searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);

      BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

      fullQuery.add(contextQuery, BooleanClauseOccur.MUST);

      List<BooleanClause> clauses = searchQuery.clauses();

      if (!clauses.isEmpty()) {
        fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
      }

      return SearchEngineUtil.search(searchContext, fullQuery);
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
  protected Query doGetPermissionQuery_6(
      long companyId,
      long[] groupIds,
      long userId,
      String className,
      Query query,
      SearchContext searchContext,
      AdvancedPermissionChecker advancedPermissionChecker,
      List<Group> groups,
      List<Role> roles,
      List<UserGroupRole> userGroupRoles,
      Map<Long, List<Role>> groupIdsToRoles)
      throws Exception {

    BooleanQuery permissionQuery = BooleanQueryFactoryUtil.create(searchContext);

    if (userId > 0) {
      permissionQuery.addTerm(Field.USER_ID, userId);
    }

    BooleanQuery groupsQuery = BooleanQueryFactoryUtil.create(searchContext);
    BooleanQuery rolesQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (Role role : roles) {
      String roleName = role.getName();

      if (roleName.equals(RoleConstants.ADMINISTRATOR)) {
        return query;
      }

      if (ResourcePermissionLocalServiceUtil.hasResourcePermission(
          companyId,
          className,
          ResourceConstants.SCOPE_COMPANY,
          String.valueOf(companyId),
          role.getRoleId(),
          ActionKeys.VIEW)) {

        return query;
      }

      if ((role.getType() == RoleConstants.TYPE_REGULAR)
          && ResourcePermissionLocalServiceUtil.hasResourcePermission(
              companyId,
              className,
              ResourceConstants.SCOPE_GROUP_TEMPLATE,
              String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID),
              role.getRoleId(),
              ActionKeys.VIEW)) {

        return query;
      }

      for (Group group : groups) {
        if (ResourcePermissionLocalServiceUtil.hasResourcePermission(
            companyId,
            className,
            ResourceConstants.SCOPE_GROUP,
            String.valueOf(group.getGroupId()),
            role.getRoleId(),
            ActionKeys.VIEW)) {

          groupsQuery.addTerm(Field.GROUP_ID, group.getGroupId());
        }

        if ((role.getType() != RoleConstants.TYPE_REGULAR)
            && ResourcePermissionLocalServiceUtil.hasResourcePermission(
                companyId,
                className,
                ResourceConstants.SCOPE_GROUP_TEMPLATE,
                String.valueOf(GroupConstants.DEFAULT_PARENT_GROUP_ID),
                role.getRoleId(),
                ActionKeys.VIEW)) {

          List<Role> groupRoles = groupIdsToRoles.get(group.getGroupId());

          if (groupRoles.contains(role)) {
            groupsQuery.addTerm(Field.GROUP_ID, group.getGroupId());
          }
        }

        if (group.isSite()
            && !roleName.equals(RoleConstants.SITE_MEMBER)
            && (role.getType() == RoleConstants.TYPE_SITE)) {

          rolesQuery.addTerm(
              Field.GROUP_ROLE_ID, group.getGroupId() + StringPool.DASH + role.getRoleId());
        }
      }

      rolesQuery.addTerm(Field.ROLE_ID, role.getRoleId());
    }

    for (Group group : groups) {
      addRequiredMemberRole(group, rolesQuery);
    }

    for (UserGroupRole userGroupRole : userGroupRoles) {
      rolesQuery.addTerm(
          Field.GROUP_ROLE_ID,
          userGroupRole.getGroupId() + StringPool.DASH + userGroupRole.getRoleId());
    }

    if (groupsQuery.hasClauses()) {
      permissionQuery.add(groupsQuery, BooleanClauseOccur.SHOULD);
    }

    if (rolesQuery.hasClauses()) {
      permissionQuery.add(rolesQuery, BooleanClauseOccur.SHOULD);
    }

    BooleanQuery fullQuery = BooleanQueryFactoryUtil.create(searchContext);

    fullQuery.add(query, BooleanClauseOccur.MUST);
    fullQuery.add(permissionQuery, BooleanClauseOccur.MUST);

    return fullQuery;
  }