public Indexes getIndexes(Expandable expandable) { // we do not merge indexes, // so if we find an IndexParser.ALL that's what we return // if we find only one non-empty, that's what we return // else we throw an exception Indexes indexes = null; for (ExpandParameter expandParameter : expandParameters) { final Indexes i = expandParameter.getIndexes(expandable); if (i.equals(IndexParser.ALL)) { return IndexParser.ALL; } if (!i.equals(IndexParser.EMPTY)) { if (indexes == null) { indexes = i; } else { throw new IndexException("Cannot merge multiple indexed expand parameters."); } } } return indexes != null ? indexes : IndexParser.EMPTY; }
public ListWrapper<T> expand(ExpandContext<ListWrapper<T>> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final ListWrapper<T> entity = context.getEntity(); final Set<Field> collectionFields = Sets.newHashSet(); for (Class cls = entity.getClass(); cls != null; cls = cls.getSuperclass()) { final Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { if (Collection.class.isAssignableFrom(field.getType())) { collectionFields.add(field); } } } if (collectionFields.isEmpty()) { throw new RuntimeException("Entity " + entity.getClass() + " has no collection field, cannot expand."); } if (collectionFields.size() > 1) { throw new RuntimeException("Entity " + entity.getClass() + " has more than one collection field, cannot determine which collection to expand."); } setFieldValue(collectionFields.iterator().next(), entity, entity.getCallback().getItems(context.getEntityExpandParameter().getIndexes(context.getExpandable()))); entityCrawler.crawl(entity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver); return entity; }
public Object expand(ExpandContext<Object> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler) { final Object entity = context.getEntity(); try { method.invoke(entity, context.getEntityExpandParameter().getIndexes(context.getExpandable())); } catch (IllegalAccessException e) { throw new ExpandException(e); } catch (InvocationTargetException e) { throw new ExpandException(e); } entityCrawler.crawl(entity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver); return entity; } }