@Override public Iterator<Item> iterator() { PathQuery pq = new PathQuery(services.getModel()); pq.addViews(PQUtil.getStar(services.getModel(), getType())); pq.addConstraint(Constraints.in(type, name)); Iterator<Map<String, Object>> it = services.getQueryService().getRowMapIterator(pq); return new ItemIterator(it); }
@SuppressWarnings("unchecked") @Override public boolean removeAll(@SuppressWarnings("rawtypes") Collection c) { int priorSize = getSize(); if (c instanceof ItemList) { ItemList res = subtract(((ItemList) c)); update(res); delete(); res.rename(getName()); } else if (!c.isEmpty()) { try { Item[] is = (Item[]) c.toArray(new Item[0]); String path = is[0].getType() + ".id"; PathQuery pq = new PathQuery(services.getModel()); pq.addView(path); pq.addConstraint( Constraints.oneOfValues( path, collect( collect(Arrays.asList(is), invokerTransformer("getId")), stringValueTransformer()))); createListAndSubtract(pq); } catch (ArrayStoreException e) { // Do nothing - we didn't get a collection of items. } } return priorSize != getSize(); }
/** * Convenience method for finding members of a collection matching certain properties. * * <p>This search is implemented with a query that performs case-insensitive matching on values. * Null constraints will be honoured, but other lookups will be converted to strings. Wild-cards * are permitted wherever they are permitted in path-queries. <br> * Using a query to back the search means that finding matching members is efficient even over * large lists - you will not need to pull in and iterate over thousands of rows of data to find * items if you have specific conditions. <br> * These conditions must all match for the result to be included. For more specific and flexible * searching strategies, please see the {@link PathQuery} API. * * @param conditions The properties these elements should have. * @return A list of matching items. */ public List<Item> find(Map<String, Object> conditions) { List<Item> ret = new ArrayList<Item>(); PathQuery q = new PathQuery(services.getModel()); q.addViews(PQUtil.getStar(services.getModel(), type)); q.addConstraint(Constraints.in(type, name)); for (Entry<String, Object> condition : conditions.entrySet()) { String path = type + "." + condition.getKey(); if (condition.getValue() == null) { q.addConstraint(Constraints.isNull(path)); } else { q.addConstraint(Constraints.eq(path, condition.getValue().toString())); } } List<Map<String, Object>> results = services.getQueryService().getRowsAsMaps(q); for (Map<String, Object> result : results) { ret.add(new Item(services, type, result)); } return ret; }
/** * Add items to this list by using a query. * * @param items The items to add to the list. */ private void appendItems(Iterable<Item> items) { PathQuery pq = new PathQuery(services.getModel()); pq.addViews(getType() + ".id"); Set<String> values = new HashSet<String>(); for (Item i : items) { values.add(Integer.toString(i.getId())); } pq.addConstraint(Constraints.oneOfValues(getType() + ".id", values)); update(services.getListService().append(this, pq)); }
@Override public boolean remove(Object i) { int priorSize = getSize(); if (i instanceof Item) { Item item = (Item) i; String path = item.getType() + ".id"; PathQuery pq = new PathQuery(services.getModel()); pq.addView(path); pq.addConstraint(Constraints.eq(path, Integer.toString(item.getId()))); createListAndSubtract(pq); } return priorSize != getSize(); }