public static void addSort(IMutableSearch search, Sort sort) { if (sort == null) return; List<Sort> sorts = search.getSorts(); if (sorts == null) { sorts = new ArrayList<Sort>(); search.setSorts(sorts); } sorts.add(sort); }
/** * Modify the search by adding the given sorts before the current sorts in * the search. */ public static void mergeSortsBefore(IMutableSearch search, List<Sort> sorts) { List<Sort> list = search.getSorts(); if (list == null) { list = new ArrayList<Sort>(); search.setSorts(list); } if (list.size() > 0) { // remove any sorts from the search that already sort on the same // property as one of the new sorts Iterator<Sort> itr = list.iterator(); while (itr.hasNext()) { String property = itr.next().getProperty(); if (property == null) { itr.remove(); } else { for (Sort sort : sorts) { if (property.equals(sort.getProperty())) { itr.remove(); break; } } } } } list.addAll(0, sorts); }
/** * Modify the search by adding the given sorts after the current sorts in * the search. */ public static void mergeSortsAfter(IMutableSearch search, List<Sort> sorts) { List<Sort> list = search.getSorts(); if (list == null) { list = new ArrayList<Sort>(); search.setSorts(list); } int origLen = list.size(); if (origLen > 0) { // don't add sorts that are already in the list for (Sort sort : sorts) { if (sort.getProperty() != null) { boolean found = false; for (int i = 0; i < origLen; i++) { if (sort.getProperty().equals(list.get(i).getProperty())) { found = true; break; } } if (!found) list.add(sort); } } } else { list.addAll(sorts); } }
/** * Copy the contents of the source search object to the destination search * object, overriding any contents previously found in the destination. All * collections are copied into new collections, but the items in those * collections are not duplicated; they still point to the same objects. */ public static <T extends IMutableSearch> T copy(ISearch source, T destination) { shallowCopy(source, destination); ArrayList<String> fetches = new ArrayList<String>(); fetches.addAll(source.getFetches()); destination.setFetches(fetches); ArrayList<Field> fields = new ArrayList<Field>(); fields.addAll(source.getFields()); destination.setFields(fields); ArrayList<Filter> filters = new ArrayList<Filter>(); filters.addAll(source.getFilters()); destination.setFilters(filters); ArrayList<Sort> sorts = new ArrayList<Sort>(); sorts.addAll(source.getSorts()); destination.setSorts(sorts); return destination; }
/** * Copy the contents of the source search object to the destination search * object, overriding any contents previously found in the destination. All * destination properties reference the same objects from the source * properties. */ public static IMutableSearch shallowCopy(ISearch source, IMutableSearch destination) { destination.setSearchClass(source.getSearchClass()); destination.setDistinct(source.isDistinct()); destination.setDisjunction(source.isDisjunction()); destination.setResultMode(source.getResultMode()); destination.setFirstResult(source.getFirstResult()); destination.setPage(source.getPage()); destination.setMaxResults(source.getMaxResults()); destination.setFetches(source.getFetches()); destination.setFields(source.getFields()); destination.setFilters(source.getFilters()); destination.setSorts(source.getSorts()); return destination; }