001package de.deepamehta.core.model; 002 003import org.codehaus.jettison.json.JSONException; 004import org.codehaus.jettison.json.JSONObject; 005 006 007 008public class RelatedTopicModel extends TopicModel { 009 010 // ---------------------------------------------------------------------------------------------- Instance Variables 011 012 private AssociationModel relatingAssoc; 013 014 // ---------------------------------------------------------------------------------------------------- Constructors 015 016 public RelatedTopicModel(long topicId) { 017 super(topicId); 018 this.relatingAssoc = new AssociationModel(); 019 } 020 021 public RelatedTopicModel(long topicId, AssociationModel relatingAssoc) { 022 super(topicId); 023 this.relatingAssoc = relatingAssoc; 024 } 025 026 public RelatedTopicModel(String topicUri) { 027 super(topicUri, (String) null); // topicTypeUri=null 028 this.relatingAssoc = new AssociationModel(); 029 } 030 031 public RelatedTopicModel(String topicUri, AssociationModel relatingAssoc) { 032 super(topicUri, (String) null); // topicTypeUri=null 033 this.relatingAssoc = relatingAssoc; 034 } 035 036 public RelatedTopicModel(String topicTypeUri, SimpleValue value) { 037 super(topicTypeUri, value); 038 this.relatingAssoc = new AssociationModel(); 039 } 040 041 public RelatedTopicModel(String topicTypeUri, ChildTopicsModel childTopics) { 042 super(topicTypeUri, childTopics); 043 this.relatingAssoc = new AssociationModel(); 044 } 045 046 public RelatedTopicModel(TopicModel topic) { 047 super(topic); 048 this.relatingAssoc = new AssociationModel(); 049 } 050 051 public RelatedTopicModel(TopicModel topic, AssociationModel relatingAssoc) { 052 super(topic); 053 this.relatingAssoc = relatingAssoc; 054 } 055 056 // -------------------------------------------------------------------------------------------------- Public Methods 057 058 public AssociationModel getRelatingAssociation() { 059 return relatingAssoc; 060 } 061 062 // === Serialization === 063 064 @Override 065 public JSONObject toJSON() { 066 try { 067 JSONObject o = super.toJSON(); 068 // Note: the relating association might be uninitialized and thus not serializable. 069 // This is the case at least for enrichments which have no underlying topics (e.g. timestamps). 070 // ### TODO: remodel enrichments? Don't put them in a child topics model but in a proprietary field? 071 if (relatingAssoc.getRoleModel1() != null) { 072 o.put("assoc", relatingAssoc.toJSON()); 073 } 074 // 075 return o; 076 } catch (Exception e) { 077 throw new RuntimeException("Serialization failed (" + this + ")", e); 078 } 079 } 080 081 // === Java API === 082 083 @Override 084 public RelatedTopicModel clone() { 085 try { 086 return (RelatedTopicModel) super.clone(); 087 } catch (Exception e) { 088 throw new RuntimeException("Cloning a RelatedTopicModel failed", e); 089 } 090 } 091 092 @Override 093 public String toString() { 094 return super.toString() + ", relating " + relatingAssoc; 095 } 096}