@Override public PagedResult<String> findAllByCriteria(WikittyTransaction transaction, Criteria criteria) { // throw new UnsupportedOperationException("Not supported yet."); int firstIndex = criteria.getFirstIndex(); int endIndex = criteria.getEndIndex(); List<String> ids = new LinkedList<String>(); int currentIndex = 0; for (Entry<String, Wikitty> entry : wikittyStorage.getWikitties().entrySet()) { Wikitty w = entry.getValue(); String id = entry.getKey(); Restriction dto = criteria.getRestriction(); if (!w.isDeleted() && checkRestriction(transaction, dto, w)) { currentIndex++; if (currentIndex > firstIndex) { ids.add(id); } if (endIndex >= 0 && currentIndex >= endIndex) { break; } } } return new PagedResult<String>(criteria.getName(), firstIndex, ids.size(), criteria.getRestriction().toString(), null, ids); }
@Override public void run() { try { // use a facet to get only extension used in export // used for CSV export criteria.addFacetField(Element.ELT_EXTENSION); PagedResult<String> pageResultId = ws.findAllByCriteria( securityToken, Collections.singletonList(criteria)).get(0); long time = 0; if (log.isInfoEnabled()) { time = System.currentTimeMillis(); log.info("Export started"); } // get Wikitty from Id PagedResult<Wikitty> pageResult = pageResultId.cast(securityToken, ws); ImportExportMethod exporter = format.ieporter(); exporter.exportWriter(securityToken, writer, ws, pageResult); if (log.isInfoEnabled()) { time = System.currentTimeMillis() - time; log.info("Export in (ms)" + time); } } catch (Exception eee) { throw new WikittyException("Error during export task", eee); } } }
@Override public List<String> findByCriteria(String securityToken, List<Criteria> criteria) { List<String> result = new ArrayList<String>(criteria.size()); List<Criteria> criteriaLimit = new ArrayList<Criteria>(criteria.size()); for (Criteria c : criteria) { Criteria climit = Search.query(c).criteria().setFirstIndex(0).setEndIndex(1); criteriaLimit.add(climit); } List<PagedResult<String>> idsList = findAllByCriteria( securityToken, criteriaLimit); for (PagedResult<String> ids : idsList) { if (ids.size() > 0) { result.add(ids.getFirst()); } else { result.add(null); } } if (criteria.size() != result.size()) { log.error(String.format("Criteria input list (%s) has not same size that result list (%s)", criteria.size(), result.size())); } return result; }
/** * Null field are not used in search request. * * @param e sample wikitty * @param firstIndex * @param endIndex * @param fieldFacet * @return */ public <E extends BusinessEntityImpl> PagedResult<E> findAllByExample(E e, int firstIndex, int endIndex, String ... fieldFacet ) { long start = TimeLog.getTime(); Criteria criteria = Search.query(e.getWikitty()).criteria() .setFirstIndex(firstIndex).setEndIndex(endIndex) .setFacetField(fieldFacet); PagedResult<String> pagedResult = findAllIdByCriteria(criteria); // we can use autoconvert = true because search by example add automaticaly // restriction on extension PagedResult<E> result = (PagedResult<E>)pagedResult.cast( this, e.getClass(), true); timeLog.log(start, "findAllByExample<limit>"); return result; }
String queryString = restriction2Solr.toSolr(criteria.getRestriction(), getSolrServer()); SolrQuery query = new SolrQuery(SOLR_QUERY_PARSER + queryString); int firstIndex = criteria.getFirstIndex(); int endIndex = criteria.getEndIndex(); List<String> sortAscending = criteria.getSortAscending(); if(sortAscending != null) { for (String sort : sortAscending) { List<String> sortDescending = criteria.getSortDescending(); if(sortDescending != null) { for (String sort : sortDescending) { String select = criteria.getSelect(); boolean hasSelect = StringUtils.isNotEmpty(select); if (hasSelect) { criteria.addFacetField(selectWikitty); criteria.setEndIndex(firstIndex); List<String> facetField = criteria.getFacetField(); List<Criteria> facetCriteria = criteria.getFacetCriteria(); || (facetCriteria != null && !facetCriteria.isEmpty())) { query.setFacet(true); query.setFacetMinCount(criteria.getFacetMinCount()); query.setFacetLimit(criteria.getFacetLimit());
String queryString = restriction2Solr.toSolr(criteria.getRestriction(), solrServer); SolrQuery query = new SolrQuery(SOLR_QUERY_PARSER + queryString); int firstIndex = criteria.getFirstIndex(); int endIndex = criteria.getEndIndex(); List<String> sortAscending = criteria.getSortAscending(); if(sortAscending != null) { for (String sort : sortAscending) { List<String> sortDescending = criteria.getSortDescending(); if(sortDescending != null) { for (String sort : sortDescending) { List<String> facetField = criteria.getFacetField(); List<Criteria> facetCriteria = criteria.getFacetCriteria(); for (Criteria facet : facetCriteria) { String queryFacet = restriction2Solr.toSolr(facet.getRestriction(), solrServer); facetQueryToName.put(queryFacet, facet.getName()); query.addFacetQuery(queryFacet);
new Restriction2Solr(transaction, fieldModifier); String queryString = restriction2Solr.toSolr( treeCriteria.getRestriction(), solrServer); SolrQuery query = new SolrQuery(SOLR_QUERY_PARSER + queryString); QueryResponse resp = SolrUtil.executeQuery(solrServer, query); .setFirstIndex(0).setEndIndex(0) .addFacetField(TREENODE_ATTACHED_ALL); PagedResult<String> attSearch = findAllByCriteria(transaction, attCriteria);
/** * Create a new query on an existing criteria to add new constraints to * the existing criteria. * * @param criteria the existing criteria * @return a new query to add constraints to the criteria. */ public static Search query(Criteria criteria) { Search search = query(); if (criteria != null) { search.restrictions.add(criteria.getRestriction()); } return search; }
serviceCriteria = criteria.clone(); if (StringUtils.isEmpty(criteria.getSelect())) { search = search.exteq(extensions); serviceCriteria.setRestriction(search);
new Restriction2Solr(transaction, fieldModifier); String queryString = restriction2Solr.toSolr( treeCriteria.getRestriction(), getSolrServer()); SolrQuery query = new SolrQuery(SOLR_QUERY_PARSER + queryString); QueryResponse resp = SolrUtil.executeQuery(getSolrServer(), query); .setFirstIndex(0).setEndIndex(0) .addFacetField(TREENODE_ATTACHED_ALL); PagedResult<String> attSearch = findAllByCriteria(transaction, attCriteria);
protected void checkSearch( Search search, RestrictionName name ) throws Exception { Restriction dto = search.criteria().getRestriction(); checkRestriction( dto, name, elementName, value ); }
/** * Recherche tous les objets qui ont ce label * * @param proxy * @param label * @param firstIndex * @param endIndex * @return * @deprecated since 3.4 use {@link #findAllByLabel(org.nuiton.wikitty.WikittyClient, java.lang.String, int, int) } */ @Deprecated static public PagedResult<Wikitty> findAllByLabel(WikittyProxy proxy, String label, int firstIndex, int endIndex) { WikittyLabelImpl l = new WikittyLabelImpl(); l.addLabels(label); Criteria criteria = Search.query(l.getWikitty()).criteria() .setFirstIndex(firstIndex).setEndIndex(endIndex); PagedResult<Wikitty> result = proxy.findAllByCriteria(criteria); return result; }
@Test public void testOr() throws Exception { String[] elementNames = new String[4]; String[] values = new String[4]; for (int i = 0; i < elementNames.length; i++) { elementNames[i] = elementName + i; values[i] = value + i; } Search q = q().eq(elementNames[0], values[0]); Search qor = q.or() .gt(elementNames[1], values[1]) .ew(elementNames[2], values[2]) .lt(elementNames[3], values[3]); Restriction dto = q.criteria().getRestriction(); Assert.assertTrue( "Restriction is " + dto.getClass().getName(), dto instanceof And ); And and = (And) dto; Restriction[] dtos = and.getRestrictions().toArray(new Restriction[]{}); checkRestriction(dtos[0], RestrictionName.EQUALS, elementName + "0", value + "0"); Assert.assertTrue( "Restriction is " + dtos[1].getClass().getName(), dtos[1] instanceof Or ); Or or = (Or) dtos[1]; dtos = or.getRestrictions().toArray(new Restriction[]{}); checkRestriction(dtos[0], RestrictionName.GREATER, elementName + "1", value + "1"); checkRestriction(dtos[1], RestrictionName.ENDS_WITH, elementName + "2", value + "2"); checkRestriction(dtos[2], RestrictionName.LESS, elementName + "3", value + "3"); }
for (Criteria c : criteria) { Criteria cLimit = Search.query(c).criteria() .setFirstIndex(0).setEndIndex(1); criteriaLimit.add(cLimit);
@Test public void testAnd() throws Exception { Restriction dto = q() .eq(elementName + "0", value + "0") .gt(elementName + "1", value + "1") .ew(elementName + "2", value + "2") .criteria().getRestriction(); Assert.assertTrue( dto instanceof And ); And and = (And) dto; Restriction[] dtos = and.getRestrictions().toArray(new Restriction[]{}); checkRestriction(dtos[0], RestrictionName.EQUALS, elementName + "0", value + "0"); checkRestriction(dtos[1], RestrictionName.GREATER, elementName + "1", value + "1"); checkRestriction(dtos[2], RestrictionName.ENDS_WITH, elementName + "2", value + "2"); }
@Test public void testBetween() throws Exception { Criteria criteria = q().bw(elementName, "from", "to").criteria(); Restriction restriction = criteria.getRestriction(); Assert.assertTrue( restriction instanceof Between ); Between between = (Between) restriction; Assert.assertEquals( "from", between.getMin() ); Assert.assertEquals( "to", between.getMax() ); }