fa7e06521cd13a964fc99a157288fef5eb910cef
[mir.git] / source / mir / entity / EntityRelation.java
1 package mir.entity;
2
3 import mir.storage.*;
4 import mir.misc.*;
5 import freemarker.template.*;
6
7 public class EntityRelation {
8
9   public String         fromId;
10   public String         toId;
11   public StorageObject  storage;
12   public int            type;
13
14   public final static int TO_ONE    =  1;
15   public final static int TO_MANY   =  2;
16
17
18   /**
19    *  Kontruktor fuer EntityRelation
20    *  @param fromId ist der Feldname in der ausgehenden Tabelle fuer die Relation
21    *  @param toId ist der Feldname in der Zieltablle
22    *  @param storage ist das StorageObject, ueber das der Zugriff auf die abhaengige
23    *         Tabelle erfolgt.
24    *  @param type ist der Typ der EntityRelation (TO_ONE oder TO_MANY)
25    */
26
27   public EntityRelation(String fromId, String toId, StorageObject storage, int type) {
28       this.fromId = fromId;
29       this.toId = toId;
30       this.storage = storage;
31       this.type = type;
32   }
33
34   /**
35    *   @return Liefert eine abhaengige Entity mit den verknuepften
36    *           Entities, wenn es sich um eine TO_ONE Beziehung handelt, ansonsten
37    *           null.
38    */
39
40   public Entity getOne(Entity entity) throws StorageObjectException {
41     if (type==TO_ONE) {
42       return storage.selectById(entity.getValue(fromId));
43     }
44     else return null;
45   }
46
47   /**
48    *   @return Liefert ein freemarker.template.SimpleHash mit den verknuepften
49    *           Entities, wenn es sich um eine TO_ONE Beziehung handelt, ansonsten
50    *           null.
51    */
52
53   public Entity getOneAsSimpleHash(Entity entity) throws StorageObjectException {
54     if (type==TO_ONE) {
55         Entity returnEntity = (Entity)getOne(entity);
56         if (returnEntity!=null) return returnEntity;
57     }
58     return null;
59   }
60
61   /**
62    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
63    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
64    *           null.
65    */
66
67   public EntityList getMany(Entity entity) throws StorageObjectException{
68     if (type==TO_MANY) {
69       return storage.selectByFieldValue(toId, entity.getValue(fromId));
70     }
71     else return null;
72   }
73
74   /**
75    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
76    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
77    *           null.
78    */
79
80   public EntityList getMany(Entity entity, String order) throws StorageObjectException{
81     if (type==TO_MANY) {
82       return storage.selectByWhereClause(toId+"="+entity.getValue(fromId), order,-1);
83     }
84     else return null;
85   }
86
87   /**
88    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
89    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
90    *           null.
91    */
92
93   public EntityList getMany(Entity entity, String order, String whereClause) throws StorageObjectException{
94     if (type==TO_MANY) {
95       return storage.selectByWhereClause(toId + "=" + entity.getValue(fromId) + " and " + whereClause, order,-1);
96     }
97     else return null;
98   }
99
100   /**
101    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
102    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
103    *           null.
104    */
105
106   public SimpleList getManyAsSimpleList(Entity entity)
107     throws StorageObjectException {
108
109     if (type==TO_MANY) {
110       EntityList returnList = getMany(entity);
111       if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
112     }
113     return null;
114   }
115
116   /**
117    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
118    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
119    *           null.
120    */
121
122   public SimpleList getManyAsSimpleList(Entity entity, String order)
123     throws StorageObjectException {
124
125     if (type==TO_MANY) {
126       EntityList returnList = getMany(entity, order);
127       if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
128     }
129     return null;
130   }
131
132   /**
133    *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
134    *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
135    *           null.
136    */
137
138   public SimpleList getManyAsSimpleList(Entity entity, String order, String whereClause)
139     throws StorageObjectException {
140
141     if (type==TO_MANY) {
142       EntityList returnList = getMany(entity, order, whereClause);
143       if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
144     }
145     return null;
146   }
147   /**
148    *   @return Liefert den Referenznamen der abhaengigen Tabelle
149    */
150
151   public String getName() {
152     return "to" + storage.getTableName();
153   }
154
155
156 }