Initial revision
[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 SimpleHash getOneAsSimpleHash(Entity entity) throws StorageObjectException {
54                 if (type==TO_ONE) {
55                                 Entity returnEntity = getOne(entity);
56                                 if (returnEntity!=null) return HTMLTemplateProcessor.makeSimpleHash(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 SimpleList getManyAsSimpleList(Entity entity)
94                 throws StorageObjectException {
95
96                 if (type==TO_MANY) {
97                         EntityList returnList = getMany(entity);
98                         if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
99                 }
100                 return null;
101         }
102
103         /**
104          *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
105          *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
106          *           null.
107          */
108
109         public SimpleList getManyAsSimpleList(Entity entity, String order)
110                 throws StorageObjectException {
111
112                 if (type==TO_MANY) {
113                         EntityList returnList = getMany(entity, order);
114                         if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
115                 }
116                 return null;
117         }
118
119         /**
120          *   @return Liefert den Referenznamen der abhaengigen Tabelle
121          */
122
123         public String getName() {
124                 return "to" + storage.getTableName();
125         }
126
127
128 }