@Override
 public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
   try {
     Object entity = response.getEntity();
     if (entity != null) {
       if (entity
           instanceof TopicType) { // Note: type matches both, must take precedence over topic
         firePreSend((TopicType) entity);
       } else if (entity instanceof Topic) {
         firePreSend((Topic) entity);
       } else if (entity instanceof Association) {
         firePreSend((Association) entity);
       } else if (entity instanceof Directives) {
         firePreSend((Directives) entity);
       } else if (isIterable(response, TopicType.class)) {
         firePreSendTopicTypes(DeepaMehtaUtils.<Iterable<TopicType>>cast(entity));
       } else if (isIterable(response, Topic.class)) {
         firePreSendTopics(DeepaMehtaUtils.<Iterable<Topic>>cast(entity));
       }
     }
     return response;
   } catch (Exception e) {
     throw new WebApplicationException(
         new RuntimeException("Jersey response filtering failed", e));
   }
 }
 private List<AssociationDefinitionModel> fetchAssociationDefinitions(Topic typeTopic) {
   Map<Long, AssociationDefinitionModel> assocDefs =
       fetchAssociationDefinitionsUnsorted(typeTopic);
   List<RelatedAssociationModel> sequence = fetchSequence(typeTopic);
   // error check
   if (assocDefs.size() != sequence.size()) {
     throw new RuntimeException(
         "DB inconsistency: type \""
             + typeTopic.getUri()
             + "\" has "
             + assocDefs.size()
             + " association definitions but in sequence are "
             + sequence.size());
   }
   //
   return sortAssocDefs(assocDefs, DeepaMehtaUtils.idList(sequence));
 }
 public JSONObject toJSON() {
   try {
     JSONObject json = new JSONObject();
     for (String childTypeUri : this) {
       Object value = get(childTypeUri);
       if (value instanceof TopicModel) {
         json.put(childTypeUri, ((TopicModel) value).toJSON());
       } else if (value instanceof List) {
         json.put(childTypeUri, DeepaMehtaUtils.objectsToJSON((List<TopicModel>) value));
       } else {
         throw new RuntimeException("Unexpected value in a CompositeValueModel: " + value);
       }
     }
     return json;
   } catch (Exception e) {
     throw new RuntimeException("Serialization of a CompositeValueModel failed (" + this + ")", e);
   }
 }
 private ViewConfigurationModel fetchTypeViewConfig(Topic typeTopic) {
   try {
     // Note: othersTopicTypeUri=null, the view config's topic type is unknown (it is
     // client-specific)
     ResultSet<RelatedTopic> configTopics =
         typeTopic.getRelatedTopics(
             "dm4.core.aggregation",
             "dm4.core.type",
             "dm4.core.view_config",
             null,
             true,
             false,
             0,
             null);
     return new ViewConfigurationModel(DeepaMehtaUtils.toTopicModels(configTopics.getItems()));
   } catch (Exception e) {
     throw new RuntimeException(
         "Fetching view configuration for type \"" + typeTopic.getUri() + "\" failed", e);
   }
 }
 private ViewConfigurationModel fetchAssocDefViewConfig(Association assocDef) {
   try {
     // Note: othersTopicTypeUri=null, the view config's topic type is unknown (it is
     // client-specific)
     ResultSet<RelatedTopic> configTopics =
         assocDef.getRelatedTopics(
             "dm4.core.aggregation",
             "dm4.core.assoc_def",
             "dm4.core.view_config",
             null,
             true,
             false,
             0,
             null);
     return new ViewConfigurationModel(DeepaMehtaUtils.toTopicModels(configTopics.getItems()));
   } catch (Exception e) {
     throw new RuntimeException(
         "Fetching view configuration for association definition " + assocDef.getId() + " failed",
         e);
   }
 }