@SuppressWarnings("unchecked") public static DescribableList<GhprcExtension, GhprcExtensionDescriptor> getJobExtensions(GhprcTrigger trigger, Class<?> ...types) { // First get all global extensions DescribableList<GhprcExtension, GhprcExtensionDescriptor> copied = copyExtensions(trigger.getDescriptor().getExtensions()); // Remove extensions that are specified by job filterList(copied, PredicateUtils.notPredicate(InstanceofPredicate.getInstance(GhprcProjectExtension.class))); // Then get the rest of the extensions from the job copied = copyExtensions(copied, trigger.getExtensions()); // Filter extensions by desired interface filterList(copied, PredicateUtils.anyPredicate(createPredicate(types))); return copied; }
/** * Checks that historic criteria are added to search */ @Test public void modifySearchBean() { Map<String, Object> params = new HashMap<String, Object>(); params.put(PersistableEntity.APPLICATION_TYPE, Foo.TYPE); SearchBean bean = modifier.doModify(searchbean, params); Collection<SearchCriterion> searchCriteria = bean.getCriteria(); assertEquals(3, searchCriteria.size()); // Expect three criterion added implicitly assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new InstanceofPredicate(InSearchCriterion.class), new AndPredicate(new BeanPropertyValueEqualsPredicate("name", "token.domainObjectType"), new AndPredicate(new BeanPropertyValueEqualsPredicate("value", Collections.singleton(Foo.TYPE)), new BeanPropertyValueEqualsPredicate("rootAlias", SearchCriterionMarshaller.HISTORY_ALIAS)))))); assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new InstanceofPredicate(JoinCriterion.class), new AndPredicate(new BeanPropertyValueEqualsPredicate("lhs", "id"), new BeanPropertyValueEqualsPredicate("rhs", "token.domainObjectId"))))); assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new BeanPropertyValueEqualsPredicate("rootAlias", SearchCriterionMarshaller.HISTORY_ALIAS), new AndPredicate(new BeanPropertyValueEqualsPredicate(NAME_ALIAS, ID_PROPERTY), new BeanPropertyValueEqualsPredicate("subSelect", "select max(hr.id) from HistoryRecord hr where hr.token.domainObjectId = foo.id and hr.token.domainObjectType = 'Foo'"))))); }
/** * Checks that historic criteria are added to search */ @Test public void modifySearchBean() { Map<String, Object> params = new HashMap<String, Object>(); params.put(PersistableEntity.APPLICATION_TYPE, EntityDescriptor.class.getSimpleName()); SearchBean bean = modifier.doModify(searchbean, params); Collection<SearchCriterion> searchCriteria = bean.getCriteria(); assertEquals(3, searchCriteria.size()); // Expect three criterion added implicitly, subjectAlias assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new InstanceofPredicate(JoinCriterion.class), new AndPredicate(new BeanPropertyValueEqualsPredicate("lhs", EntityDescriptor.APPLICATION_TYPE_PROP), new BeanPropertyValueEqualsPredicate("rhs", "token.domainObjectType"))))); assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new InstanceofPredicate(JoinCriterion.class), new AndPredicate(new BeanPropertyValueEqualsPredicate("lhs", "id"), new BeanPropertyValueEqualsPredicate("rhs", "token.domainObjectId"))))); assertNotNull(CollectionUtils.find(searchCriteria, new AndPredicate(new BeanPropertyValueEqualsPredicate("rootAlias", SearchCriterionMarshaller.HISTORY_ALIAS), new AndPredicate(new BeanPropertyValueEqualsPredicate(NAME_ALIAS, ID_PROPERTY), new BeanPropertyValueEqualsPredicate("subSelect", "select max(hr.id) from HistoryRecord hr where hr.token.domainObjectId = foo.id and hr.token.domainObjectType = " + NameUtils.concat(ALIAS, EntityDescriptor.APPLICATION_TYPE_PROP)))))); }
private static void filterExtensions(DescriptorExtensionList<GhprcExtension, GhprcExtensionDescriptor> descriptors, Class<? extends GhprcExtensionType>... types) { List<Predicate> predicates = new ArrayList<Predicate>(types.length); for (Class<? extends GhprcExtensionType> type : types) { predicates.add(InstanceofPredicate.getInstance(type)); } Predicate anyPredicate = PredicateUtils.anyPredicate(predicates); for (GhprcExtensionDescriptor descriptor : descriptors) { if (!anyPredicate.evaluate(descriptor)) { descriptors.remove(descriptor); } } }
private static List<Predicate> createPredicate(Class<?> ...types) { List<Predicate> predicates = new ArrayList<Predicate>(types.length); for (Class<?> type : types) { predicates.add(InstanceofPredicate.getInstance(type)); } return predicates; }
private static boolean addExtension(GhprcExtension extension, List<Predicate> predicates, Set<Class<?>> extSet) { for (Predicate predicate: predicates) { if (predicate.evaluate(extension)) { Class<?> clazz = ((InstanceofPredicate)predicate).getType(); if (extSet.contains(clazz)) { return false; } else { extSet.add(clazz); return true; } } } return true; }
public static void addIfMissing(DescribableList<GhprcExtension, GhprcExtensionDescriptor> extensions, GhprcExtension ext, Class<?> type) { Predicate predicate = InstanceofPredicate.getInstance(type); for (GhprcExtension extension : extensions) { if (predicate.evaluate(extension)){ return; } } extensions.add(ext); }