Search in sources :

Example 46 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters in project alfresco-repository by Alfresco.

the class SolrQueryHTTPClientTest method testBuildStats.

@Test
public void testBuildStats() throws UnsupportedEncodingException {
    SearchParameters params = new SearchParameters();
    params.setSearchTerm("bob");
    params.setStats(Arrays.asList(new StatsRequestParameters("created", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null), new StatsRequestParameters("cm:name", "statLabel", Arrays.asList(2.4f, 99.9f), null, null, false, null, false, null, false, null, true, true, true, 0.5f, Arrays.asList("excludeme"))));
    StringBuilder urlBuilder = new StringBuilder();
    client.buildStatsParameters(params, encoder, urlBuilder);
    String url = urlBuilder.toString();
    assertNotNull(url);
    assertTrue(url.contains("&stats=true"));
    assertTrue(url.contains("stats.field=" + encoder.encode("{! countDistinct=false distinctValues=false min=true max=true sum=true count=true missing=true sumOfSquares=true mean=true stddev=true}created", "UTF-8")));
    assertTrue(url.contains("stats.field=" + encoder.encode("{! ex=excludeme tag=statLabel key=statLabel percentiles='2.4,99.9' cardinality=0.5 countDistinct=true distinctValues=true min=true max=true sum=false count=true missing=false sumOfSquares=true mean=false stddev=true}cm:name", "UTF-8")));
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) StatsRequestParameters(org.alfresco.service.cmr.search.StatsRequestParameters) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 47 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters in project alfresco-repository by Alfresco.

the class SolrQueryHTTPClientTest method testBuildRangeDate.

@Test
public void testBuildRangeDate() throws UnsupportedEncodingException {
    TimeZone defaultTimeZone = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    SearchParameters params = new SearchParameters();
    params.setSearchTerm("A*");
    List<RangeParameters> ranges = new ArrayList<RangeParameters>();
    ranges.add(new RangeParameters("created", "2015", "2016", "+1MONTH", true, Collections.emptyList(), Collections.emptyList(), null, null));
    params.setRanges(ranges);
    StringBuilder urlBuilder = new StringBuilder();
    client.buildRangeParameters(params, encoder, urlBuilder);
    String url = urlBuilder.toString();
    assertNotNull(url);
    assertTrue(url.contains("&facet=true"));
    assertTrue(url.contains("&facet.range=created"));
    assertTrue(url.contains("&f.created.facet.range.start=2015-01-01T00%3A00%3A00.000Z"));
    assertTrue(url.contains("&f.created.facet.range.end=2016-12-31T23%3A59%3A59.999Z"));
    assertTrue(url.contains("&f.created.facet.range.gap=%2B1MONTH"));
    TimeZone.setDefault(defaultTimeZone);
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) TimeZone(java.util.TimeZone) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RangeParameters(org.alfresco.service.cmr.search.RangeParameters) Test(org.junit.Test)

Example 48 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters in project alfresco-repository by Alfresco.

the class SolrQueryHTTPClientTest method testBuildTimezone.

@Test
public void testBuildTimezone() throws UnsupportedEncodingException {
    SearchParameters params = new SearchParameters();
    params.setTimezone("");
    StringBuilder urlBuilder = new StringBuilder();
    client.buildUrlParameters(params, false, encoder, urlBuilder);
    String url = urlBuilder.toString();
    assertFalse(url.contains("&TZ"));
    params.setTimezone("bob");
    urlBuilder = new StringBuilder();
    client.buildUrlParameters(params, false, encoder, urlBuilder);
    url = urlBuilder.toString();
    // Timezone formats are not validated here so its just passing a string.
    assertTrue(url.contains("&TZ=bob"));
    ;
}
Also used : SearchParameters(org.alfresco.service.cmr.search.SearchParameters) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 49 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters in project alfresco-repository by Alfresco.

the class QuickShareServiceImpl method getTenantNodeRefFromSharedId.

@Override
public Pair<String, NodeRef> getTenantNodeRefFromSharedId(final String sharedId) {
    NodeRef nodeRef = TenantUtil.runAsDefaultTenant(new TenantRunAsWork<NodeRef>() {

        public NodeRef doWork() throws Exception {
            return (NodeRef) attributeService.getAttribute(ATTR_KEY_SHAREDIDS_ROOT, sharedId);
        }
    });
    if (nodeRef == null) {
        /* TODO
             * Temporary fix for RA-1093 and MNT-16224. The extra lookup should be
             * removed (the same as before, just throw the 'InvalidSharedIdException' exception) when we
             * have a system wide patch to remove the 'shared' aspect of the nodes that have been archived while shared.
             */
        // TMDQ
        final String query = "+TYPE:\"cm:content\" AND +ASPECT:\"qshare:shared\" AND =qshare:sharedId:\"" + sharedId + "\"";
        SearchParameters sp = new SearchParameters();
        sp.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO);
        sp.setQuery(query);
        sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
        List<NodeRef> nodeRefs = null;
        ResultSet results = null;
        try {
            results = searchService.query(sp);
            nodeRefs = results.getNodeRefs();
        } catch (Exception ex) {
            throw new InvalidSharedIdException(sharedId);
        } finally {
            if (results != null) {
                results.close();
            }
        }
        if (nodeRefs.size() != 1) {
            throw new InvalidSharedIdException(sharedId);
        }
        nodeRef = tenantService.getName(nodeRefs.get(0));
    }
    // note: relies on tenant-specific (ie. mangled) nodeRef
    String tenantDomain = tenantService.getDomain(nodeRef.getStoreRef().getIdentifier());
    return new Pair<>(tenantDomain, tenantService.getBaseName(nodeRef));
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) ResultSet(org.alfresco.service.cmr.search.ResultSet) ClientAppNotFoundException(org.alfresco.repo.client.config.ClientAppNotFoundException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) InvalidSharedIdException(org.alfresco.service.cmr.quickshare.InvalidSharedIdException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) QuickShareDisabledException(org.alfresco.service.cmr.quickshare.QuickShareDisabledException) NoSuchPersonException(org.alfresco.service.cmr.security.NoSuchPersonException) Pair(org.alfresco.util.Pair)

Example 50 with SearchParameters

use of org.alfresco.service.cmr.search.SearchParameters in project alfresco-repository by Alfresco.

the class AuthorityDAOImpl method findAuthorities.

public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, String displayNamePattern, String zoneName) {
    Long start = (logger.isDebugEnabled() ? System.currentTimeMillis() : null);
    Pattern pattern = displayNamePattern == null ? null : Pattern.compile(SearchLanguageConversion.convert(SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, displayNamePattern), Pattern.CASE_INSENSITIVE);
    // Use SQL to determine root authorities
    Set<String> rootAuthorities = null;
    if (parentAuthority == null && immediate) {
        rootAuthorities = getRootAuthorities(type, zoneName);
        if (pattern == null) {
            if (start != null) {
                logger.debug("findAuthorities (rootAuthories): " + rootAuthorities.size() + " items in " + (System.currentTimeMillis() - start) + " msecs [type=" + type + ",zone=" + zoneName + "]");
            }
            return rootAuthorities;
        }
    }
    // Use a Lucene search for other criteria
    Set<String> authorities = new TreeSet<String>();
    SearchParameters sp = new SearchParameters();
    sp.addStore(this.storeRef);
    sp.setLanguage("lucene");
    StringBuilder query = new StringBuilder(500);
    if (type == null || type == AuthorityType.USER) {
        if (type == null) {
            query.append("((");
        }
        query.append("TYPE:\"").append(ContentModel.TYPE_PERSON).append("\"");
        if (displayNamePattern != null) {
            query.append(" AND @").append(SearchLanguageConversion.escapeLuceneQuery("{" + ContentModel.PROP_USERNAME.getNamespaceURI() + "}" + ISO9075.encode(ContentModel.PROP_USERNAME.getLocalName()))).append(":\"").append(SearchLanguageConversion.escapeLuceneQuery(displayNamePattern)).append("\"");
        }
        if (type == null) {
            query.append(") OR (");
        }
    }
    if (type != AuthorityType.USER) {
        query.append("TYPE:\"").append(ContentModel.TYPE_AUTHORITY_CONTAINER).append("\"");
        if (displayNamePattern != null) {
            query.append(" AND (");
            if (!displayNamePattern.startsWith("*")) {
                // Allow for the appropriate type prefix in the authority name
                Collection<AuthorityType> authorityTypes = type == null ? SEARCHABLE_AUTHORITY_TYPES : Collections.singleton(type);
                boolean first = true;
                for (AuthorityType subType : authorityTypes) {
                    if (first) {
                        first = false;
                    } else {
                        query.append(" OR ");
                    }
                    query.append("@").append(SearchLanguageConversion.escapeLuceneQuery("{" + ContentModel.PROP_AUTHORITY_NAME.getNamespaceURI() + "}" + ISO9075.encode(ContentModel.PROP_AUTHORITY_NAME.getLocalName()))).append(":\"");
                    query.append(getName(subType, SearchLanguageConversion.escapeLuceneQuery(displayNamePattern))).append("\"");
                }
            } else {
                query.append("@").append(SearchLanguageConversion.escapeLuceneQuery("{" + ContentModel.PROP_AUTHORITY_NAME.getNamespaceURI() + "}" + ISO9075.encode(ContentModel.PROP_AUTHORITY_NAME.getLocalName()))).append(":\"");
                query.append(getName(type, SearchLanguageConversion.escapeLuceneQuery(displayNamePattern))).append("\"");
            }
            query.append(" OR @").append(SearchLanguageConversion.escapeLuceneQuery("{" + ContentModel.PROP_AUTHORITY_DISPLAY_NAME.getNamespaceURI() + "}" + ISO9075.encode(ContentModel.PROP_AUTHORITY_DISPLAY_NAME.getLocalName()))).append(":\"").append(SearchLanguageConversion.escapeLuceneQuery(displayNamePattern)).append("\")");
        }
        if (type == null) {
            query.append("))");
        }
    }
    if (parentAuthority != null) {
        if (immediate) {
            // use PARENT
            NodeRef parentAuthorityNodeRef = getAuthorityNodeRefOrNull(parentAuthority);
            if (parentAuthorityNodeRef != null) {
                query.append(" AND PARENT:\"").append(SearchLanguageConversion.escapeLuceneQuery(parentAuthorityNodeRef.toString())).append("\"");
            } else {
                throw new UnknownAuthorityException("An authority was not found for " + parentAuthority);
            }
        } else {
            // use PATH
            query.append(" AND PATH:\"/sys:system/sys:authorities/cm:").append(ISO9075.encode(parentAuthority));
            query.append("//*\"");
        }
    }
    if (zoneName != null) {
        // Zones are all direct links to those within so it is safe to use PARENT to look them up
        NodeRef zoneNodeRef = getZone(zoneName);
        if (zoneNodeRef != null) {
            query.append(" AND PARENT:\"").append(SearchLanguageConversion.escapeLuceneQuery(zoneNodeRef.toString())).append("\"");
        } else {
            throw new UnknownAuthorityException("A zone was not found for " + zoneName);
        }
    }
    sp.setQuery(query.toString());
    sp.setMaxItems(findAuthoritiesLimit);
    ResultSet rs = null;
    try {
        rs = searchService.query(sp);
        for (ResultSetRow row : rs) {
            NodeRef nodeRef = row.getNodeRef();
            QName idProp = dictionaryService.isSubClass(nodeService.getType(nodeRef), ContentModel.TYPE_AUTHORITY_CONTAINER) ? ContentModel.PROP_AUTHORITY_NAME : ContentModel.PROP_USERNAME;
            addAuthorityNameIfMatches(authorities, DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(nodeRef, idProp)), type, pattern);
        }
        // If we asked for root authorities, we must do an intersection with the set of root authorities
        if (rootAuthorities != null) {
            authorities.retainAll(rootAuthorities);
        }
        if (start != null) {
            logger.debug("findAuthorities: " + authorities.size() + " items in " + (System.currentTimeMillis() - start) + " msecs [type=" + type + ",zone=" + zoneName + ",parent=" + parentAuthority + ",immediate=" + immediate + ",filter=" + displayNamePattern + "]");
        }
        return authorities;
    } finally {
        if (rs != null) {
            rs.close();
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) RegexQNamePattern(org.alfresco.service.namespace.RegexQNamePattern) QName(org.alfresco.service.namespace.QName) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) NodeRef(org.alfresco.service.cmr.repository.NodeRef) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) TreeSet(java.util.TreeSet) ResultSet(org.alfresco.service.cmr.search.ResultSet)

Aggregations

SearchParameters (org.alfresco.service.cmr.search.SearchParameters)120 ResultSet (org.alfresco.service.cmr.search.ResultSet)51 Test (org.junit.Test)41 NodeRef (org.alfresco.service.cmr.repository.NodeRef)33 ArrayList (java.util.ArrayList)25 StoreRef (org.alfresco.service.cmr.repository.StoreRef)16 IOException (java.io.IOException)15 HashMap (java.util.HashMap)14 QName (org.alfresco.service.namespace.QName)13 List (java.util.List)12 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)12 JSONObject (org.json.JSONObject)12 SearchRequestContext (org.alfresco.rest.api.search.context.SearchRequestContext)11 FieldHighlightParameters (org.alfresco.service.cmr.search.FieldHighlightParameters)11 Set (java.util.Set)10 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)10 RangeParameters (org.alfresco.service.cmr.search.RangeParameters)10 JSONArray (org.json.JSONArray)10 JSONTokener (org.json.JSONTokener)10 Locale (java.util.Locale)9