/** * Return unnamed criteria. * * @return new criteria */ public Criteria criteria() { Criteria criteria = criteria(null); return criteria; }
@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; }
/** * get the id of a user given his login. * * @param login the login of the user to search for * @return a wikitty id * @deprecated since 3.4 {@link #getUserWikittyId(org.nuiton.wikitty.WikittyClient, java.lang.String) } */ @Deprecated static public String getUserWikittyId(WikittyProxy proxy, String login) { String userWikittyId = null; Wikitty user = proxy.findByCriteria(Search.query().eq( WikittyUser.FQ_FIELD_WIKITTYUSER_LOGIN, login).criteria()); if (user != null) { userWikittyId = user.getWikittyId(); } return userWikittyId; }
/** * Recherche le premier objet qui a ce label * @param proxy * @param label * @return * @deprecated since 3.4 use {@link #findByLabel(org.nuiton.wikitty.WikittyClient, java.lang.String) } */ @Deprecated static public Wikitty findByLabel(WikittyProxy proxy, String label) { WikittyLabelImpl l = new WikittyLabelImpl(); l.addLabels(label); Criteria criteria = Search.query(l.getWikitty()).criteria(); Wikitty result = proxy.findByCriteria(criteria); return result; }
protected void checkSearch( Search search, RestrictionName name ) throws Exception { Restriction dto = search.criteria().getRestriction(); checkRestriction( dto, name, elementName, value ); }
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);
public <E extends BusinessEntity> List<E> findByCriteria( Class<E> clazz, List<Criteria> criterias) { long start = TimeLog.getTime(); List<E> result = null; if (criterias != null) { BusinessEntityImpl sample = (BusinessEntityImpl) WikittyUtil.newInstance(clazz); Wikitty wikitty = sample.getWikitty(); Collection<String> extensions = wikitty.getExtensionNames(); List<Criteria> serviceCriterias = new ArrayList<Criteria>(criterias.size()); for (Criteria criteria : criterias) { Search search = Search.query(criteria); search = search.exteq(extensions); criteria = search.criteria(); serviceCriterias.add(criteria); } List<String> id = findIdByCriteria(serviceCriterias); result = restore(clazz, id); } timeLog.log(start, "multiFindByCriteria<Business>(List>"); return result; }
/** * 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; }
/** * Null field are not used in search request. * * @param e sample wikitty * @return */ public <E extends BusinessEntityImpl> E findByExample(E e) { long start = TimeLog.getTime(); Criteria criteria = Search.query(e.getWikitty()).criteria(); String id = findIdByCriteria(criteria); E result = null; if (id != null) { Wikitty w = wikittyService.restore(securityToken, id); result = (E) WikittyUtil.newInstance( securityToken, wikittyService, e.getClass(), w); } timeLog.log(start, "findByExample"); 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; }
@Test public void testXML() throws Exception { Criteria criteria = q().bw(elementName, "from", "to").criteria(); // Serialize String xml = Criteria.toXML(criteria); log.debug("[XML] " + xml); Assert.assertNotNull(xml); // Deserialize criteria = Criteria.fromXML(xml); Assert.assertNotNull(criteria); }
@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"); }
/** * Check that the logged in user is in a group. A #SecurityException might * be thrown at runtime if the #WikittyUser session timed out. * @param groupName the name of the group to check * @return true is the logged in user is in the group */ public boolean isMember(String groupName) { long start = TimeLog.getTime(); boolean result = false; WikittyUser user = getLoggedInUser(); //Find the group from its name Search search = Search.query() .eq(Element.ELT_EXTENSION, WikittyGroup.EXT_WIKITTYGROUP) .eq(WikittyGroup.FQ_FIELD_WIKITTYGROUP_NAME, groupName); Criteria criteria = search.criteria(); Wikitty group = findByCriteria(criteria); if (group != null && user != null) { result = WikittySecurityUtil.isMember(wikittyService, securityToken, user.getWikittyId(), group.getWikittyId()); } timeLog.log(start, "isMember"); return result; }
@Test public void testAssociated() throws Exception { /* Following request mean is : * Looking for any product which price is between 15 and 25, * and name starts with 'Paint', * and with an associated category which name is 'Hardware' */ Restriction dto = q() .bw( "Product.price", "15", "25") .sw( "Product.name", "Paint") .associated( "Product.category" ) .eq( "Category.name", "Hardware" ) .criteria().getRestriction(); Assert.assertTrue( "Restriction is " + dto.getClass().getName(), dto instanceof AssociatedRestriction ); AssociatedRestriction ass = (AssociatedRestriction) dto; Restriction embedded = ass.getRestriction(); Assert.assertTrue( "Restriction is " + dto.getClass().getName(), embedded instanceof Equals ); checkRestriction(embedded, RestrictionName.EQUALS, "Category.name", "Hardware"); Assert.assertEquals( "Product.category", ass.getElement().getName() ); }
@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() ); }