View Javadoc

1   package com.atlassian.plugins.rest.common.expand.resolver;
2   
3   import com.atlassian.plugins.rest.common.expand.EntityCrawler;
4   import com.atlassian.plugins.rest.common.expand.EntityExpander;
5   import com.atlassian.plugins.rest.common.expand.ExpandContext;
6   import com.atlassian.plugins.rest.common.expand.entity.ListWrapper;
7   import com.google.common.base.Preconditions;
8   import com.google.common.collect.Sets;
9   
10  import java.lang.reflect.Field;
11  import java.util.Collection;
12  import java.util.Set;
13  
14  import static com.atlassian.plugins.rest.common.util.ReflectionUtils.setFieldValue;
15  
16  /**
17   * An {@link EntityExpanderResolver entity expander resolver} that can create a {@link EntityExpander entity expander} for {@link ListWrapper list wrappers}
18   */
19  public class ListWrapperEntityExpanderResolver implements EntityExpanderResolver
20  {
21      static final ListWrapperEntityExpander EXPANDER = new ListWrapperEntityExpander();
22  
23      public boolean hasExpander(Class<?> type)
24      {
25          return ListWrapper.class.isAssignableFrom(Preconditions.checkNotNull(type));
26      }
27  
28      public <T> EntityExpander<T> getExpander(Class<? extends T> type)
29      {
30          return ListWrapper.class.isAssignableFrom(type) ? (EntityExpander<T>) EXPANDER : null;
31      }
32  
33      static class ListWrapperEntityExpander<T> implements EntityExpander<ListWrapper<T>>
34      {
35          public ListWrapper<T> expand(ExpandContext<ListWrapper<T>> context, EntityExpanderResolver expanderResolver, EntityCrawler entityCrawler)
36          {
37              final ListWrapper<T> entity = context.getEntity();
38  
39              final Set<Field> collectionFields = Sets.newHashSet();
40              for (Class cls = entity.getClass(); cls != null; cls = cls.getSuperclass())
41              {
42                  final Field[] fields = cls.getDeclaredFields();
43                  for (Field field : fields)
44                  {
45                      if (Collection.class.isAssignableFrom(field.getType()))
46                      {
47                          collectionFields.add(field);
48                      }
49                  }
50              }
51              if (collectionFields.isEmpty())
52              {
53                  throw new RuntimeException("Entity " + entity.getClass() + " has no collection field, cannot expand.");
54              }
55              if (collectionFields.size() > 1)
56              {
57                  throw new RuntimeException("Entity " + entity.getClass() + " has more than one collection field, cannot determine which collection to expand.");
58              }
59  
60              setFieldValue(collectionFields.iterator().next(), entity, entity.getCallback().getItems(context.getEntityExpandParameter().getIndexes(context.getExpandable())));
61  
62              entityCrawler.crawl(entity, context.getEntityExpandParameter().getExpandParameter(context.getExpandable()), expanderResolver);
63              return entity;
64          }
65  
66      }
67  }