Initial revision
[mir.git] / source / mir / entity / EntityRelation.java
diff --git a/source/mir/entity/EntityRelation.java b/source/mir/entity/EntityRelation.java
new file mode 100755 (executable)
index 0000000..8f44ac3
--- /dev/null
@@ -0,0 +1,128 @@
+package mir.entity;
+
+import mir.storage.*;
+import mir.misc.*;
+import freemarker.template.*;
+
+public class EntityRelation {
+
+       public String         fromId;
+       public String         toId;
+       public StorageObject  storage;
+       public int            type;
+
+       public final static int TO_ONE          =  1;
+       public final static int TO_MANY         =  2;
+
+
+       /**
+        *  Kontruktor fuer EntityRelation
+        *  @param fromId ist der Feldname in der ausgehenden Tabelle fuer die Relation
+        *  @param toId ist der Feldname in der Zieltablle
+        *  @param storage ist das StorageObject, ueber das der Zugriff auf die abhaengige
+        *         Tabelle erfolgt.
+        *  @param type ist der Typ der EntityRelation (TO_ONE oder TO_MANY)
+        */
+
+       public EntityRelation(String fromId, String toId, StorageObject storage, int type) {
+                       this.fromId = fromId;
+                       this.toId = toId;
+                       this.storage = storage;
+                       this.type = type;
+       }
+
+       /**
+        *   @return Liefert eine abhaengige Entity mit den verknuepften
+        *           Entities, wenn es sich um eine TO_ONE Beziehung handelt, ansonsten
+        *           null.
+        */
+
+       public Entity getOne(Entity entity) throws StorageObjectException {
+               if (type==TO_ONE) {
+                       return storage.selectById(entity.getValue(fromId));
+               }
+               else return null;
+       }
+
+       /**
+        *   @return Liefert ein freemarker.template.SimpleHash mit den verknuepften
+        *           Entities, wenn es sich um eine TO_ONE Beziehung handelt, ansonsten
+        *           null.
+        */
+
+       public SimpleHash getOneAsSimpleHash(Entity entity) throws StorageObjectException {
+               if (type==TO_ONE) {
+                               Entity returnEntity = getOne(entity);
+                               if (returnEntity!=null) return HTMLTemplateProcessor.makeSimpleHash(returnEntity);
+               }
+               return null;
+       }
+
+       /**
+        *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
+        *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
+        *           null.
+        */
+
+       public EntityList getMany(Entity entity) throws StorageObjectException{
+               if (type==TO_MANY) {
+                       return storage.selectByFieldValue(toId, entity.getValue(fromId));
+               }
+               else return null;
+       }
+
+       /**
+        *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
+        *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
+        *           null.
+        */
+
+       public EntityList getMany(Entity entity, String order) throws StorageObjectException{
+               if (type==TO_MANY) {
+                       return storage.selectByWhereClause(toId+"="+entity.getValue(fromId), order,-1);
+               }
+               else return null;
+       }
+
+       /**
+        *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
+        *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
+        *           null.
+        */
+
+       public SimpleList getManyAsSimpleList(Entity entity)
+               throws StorageObjectException {
+
+               if (type==TO_MANY) {
+                       EntityList returnList = getMany(entity);
+                       if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
+               }
+               return null;
+       }
+
+       /**
+        *   @return Liefert eine freemarker.template.SimpleList mit den verknuepften
+        *           Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten
+        *           null.
+        */
+
+       public SimpleList getManyAsSimpleList(Entity entity, String order)
+               throws StorageObjectException {
+
+               if (type==TO_MANY) {
+                       EntityList returnList = getMany(entity, order);
+                       if (returnList!=null) return HTMLTemplateProcessor.makeSimpleList(returnList);
+               }
+               return null;
+       }
+
+       /**
+        *   @return Liefert den Referenznamen der abhaengigen Tabelle
+        */
+
+       public String getName() {
+               return "to" + storage.getTableName();
+       }
+
+
+}
\ No newline at end of file