added:
[mir.git] / source / mir / module / AbstractModule.java
index 5f16a22..d65dd2b 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package  mir.module;\r
-\r
-import java.sql.SQLException;\r
-import java.util.Map;\r
-\r
-import mir.entity.Entity;\r
-import mir.entity.EntityList;\r
-import mir.storage.StorageObject;\r
-import mir.storage.StorageObjectExc;\r
-import mir.storage.StorageObjectFailure;\r
-import freemarker.template.SimpleHash;\r
-\r
-\r
-/**\r
- * This class provides the base functionality for the derived Module-Classes.\r
- * These classes should provide methods to make more or less complex actions\r
- * on Database and Entity classes. The modules are used by ServletModules.\r
- * Future possibility could be access via Applications.\r
- *\r
- * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.\r
- * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,\r
- * die von mehreren ServletModulen verwendet werden.\r
- *\r
- */\r
-\r
-public class AbstractModule {\r
-  protected StorageObject theStorage;\r
-\r
-  public void setStorage(StorageObject storage) {\r
-    this.theStorage = storage;\r
-  }\r
-\r
-  /**\r
-   * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.\r
-   * @return Standard-StorageObject\r
-   */\r
-  public StorageObject getStorageObject () {\r
-    return theStorage;\r
-  }\r
-\r
-  /**\r
-   *   Holt eine Entity anhand der Id via StorageObject\r
-   *   @param String der Entity\r
-   *   @return Entity\r
-   */\r
-  public Entity getById (String id) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      if (theStorage == null)\r
-        throw  new ModuleExc("AbstractModule.getById: No StorageObject set!");\r
-      Entity entity = (Entity)theStorage.selectById(id);\r
-\r
-      if (entity == null)\r
-        throw new ModuleExc("AbstractModule.getById: No object for id = " + id);\r
-      else\r
-        return entity;\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *   Holt eine EntityListe anhand des WhereClause via StorageObject\r
-   *   @param String whereclause\r
-   *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
-   *   @return EntityList Liste der gematchten Datens?tze\r
-   */\r
-  public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      if (theStorage == null)\r
-        throw  new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");\r
-\r
-      return theStorage.selectByWhereClause(whereClause, offset);\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject\r
-   *   @param String where WhereClause\r
-   *   @param String order Sortierreihenfolge\r
-   *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
-   *   @return EntityList Liste der gematchten Datens?tze\r
-   */\r
-  public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      if (theStorage==null)\r
-        throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");\r
-\r
-      return theStorage.selectByWhereClause(where, order, offset);\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-  /**\r
-   *   Executes a where clause on the StorageObject with order criteria\r
-   *   fetching from offset the number of limit objects\r
-   *\r
-   *   @param String where\r
-   *   @param String order\r
-   *   @param int offset\r
-   *   @param int limit\r
-   *   @return EntityList\r
-   */\r
-\r
-  public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure   {\r
-    try {\r
-      if (theStorage==null)\r
-        throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");\r
-\r
-      return theStorage.selectByWhereClause(where, order, offset, limit);\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject\r
-   *   @param String aField - Feldname im StorageObject\r
-   *   @param String aValue - Wert in Feld im StorageObject\r
-   *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
-   *   @return EntityList Liste der gematchten Datens?tze\r
-   */\r
-  public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {\r
-    String whereClause;\r
-    whereClause = aField + " like '%" + aValue + "%'";\r
-    return getByWhereClause(whereClause, offset);\r
-  }\r
-\r
-  /**\r
-   *    * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen\r
-   * @param theValues Hash mit Spalte/Wert-Paaren\r
-   * @return Id des eingef?gten Objekts\r
-   * @exception ModuleExc\r
-   * @exception ModuleFailure\r
-   */\r
-  public String add (Map theValues) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();\r
-      theEntity.setStorage(theStorage);\r
-      theEntity.setValues(theValues);\r
-      return theEntity.insert();\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren\r
-   * @param theValues Hash mit Spalte/Wert-Paaren\r
-   * @return Id des eingef?gten Objekts\r
-   * @exception ModuleExc\r
-   * @exception ModuleFailure\r
-   */\r
-  public String set (Map theValues) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      Entity theEntity = theStorage.selectById((String)theValues.get("id"));\r
-      if (theEntity == null)\r
-        throw new ModuleExc("No object found with id " + theValues.get("id"));\r
-      theEntity.setValues(theValues);\r
-      theEntity.update();\r
-      return theEntity.getId();\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   * Deletes a record using an id\r
-   * @param idParam\r
-   * @exception ModuleExc\r
-   * @exception ModuleFailure\r
-   */\r
-  public void deleteById (String idParam) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      theStorage.delete(idParam);\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-  /**\r
-   * Liefert den Lookuptable aller Objekte des StorageObjects\r
-   * @return freemarker.template.SimpleHash\r
-   */\r
-  public SimpleHash getHashData() {\r
-    return theStorage.getHashData();\r
-  }\r
-\r
-  /**\r
-   * returns the number of rows\r
-   */\r
-  public int getSize(String where) throws ModuleExc, ModuleFailure {\r
-    try {\r
-      return theStorage.getSize(where);\r
-    }\r
-    catch (Throwable e) {\r
-      throw new ModuleFailure(e);\r
-    }\r
-  }\r
-\r
-}\r
+/*
+ * Copyright (C) 2001-2006 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+
+package  mir.module;
+
+import mir.entity.Entity;
+import mir.entity.EntityList;
+import mir.storage.Database;
+import mir.storage.DatabaseExc;
+
+import java.util.Map;
+
+/**
+ * This class provides the base functionality for the derived Module-Classes.
+ * These classes should provide methods to make more or less complex actions
+ * on Database and Entity classes. The modules are used by ServletModules.
+ * Future possibility could be access via Applications.
+ *
+ * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
+ * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
+ * die von mehreren ServletModulen verwendet werden.
+ *
+ */
+
+public class AbstractModule {
+  protected Database database;
+
+  public AbstractModule(Database aDatabase) {
+    database = aDatabase;
+  }
+
+  /**
+   * Returns the database object associated with this module
+   */
+  public Database getStorageObject () {
+    return database;
+  }
+
+  public Entity getById (String anId) throws ModuleFailure, EntityNotFoundExc {
+      if (database == null) {
+        throw  new ModuleFailure("AbstractModule.getById: No Database set!");
+      }
+
+      try {
+        Entity result = database.selectById(anId);
+
+        if (result == null) {
+          throw new EntityNotFoundExc("AbstractModule.getById: No object for id = " + anId);
+        }
+
+        return result;
+      }
+      catch (DatabaseExc e) {
+        throw new ModuleFailure("Database exception while retrieving entity with id " + anId);
+      }
+  }
+
+  public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
+    try {
+      if (database == null)
+        throw  new ModuleExc("AbstractModule.getByWhereClause: No Database set!");
+
+      return database.selectByWhereClause(whereClause, offset);
+    }
+    catch (Throwable e) {
+      throw new ModuleFailure(e);
+    }
+  }
+
+  public String add(Map theValues) throws ModuleExc, ModuleFailure {
+    try {
+      Entity entity = database.createNewEntity();
+      entity.setFieldValues(theValues);
+
+      return entity.insert();
+    }
+    catch (Throwable e) {
+      throw new ModuleFailure(e);
+    }
+  }
+
+  public String set (Map theValues) throws ModuleExc, ModuleFailure {
+    try {
+      Entity theEntity = database.selectById((String) theValues.get("id"));
+      if (theEntity == null)
+        throw new ModuleExc("No object found with id " + theValues.get("id"));
+      theEntity.setFieldValues(theValues);
+      theEntity.update();
+      return theEntity.getId();
+    }
+    catch (Throwable e) {
+      throw new ModuleFailure(e);
+    }
+  }
+
+  /**
+   * Deletes a record using an id
+   * @param idParam
+   * @exception ModuleExc
+   * @exception ModuleFailure
+   */
+  public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
+    try {
+      database.delete(idParam);
+    }
+    catch (Throwable e) {
+      throw new ModuleFailure(e);
+    }
+  }
+}