/*
- * put your module comment here
+ * 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 java.util.*;
-import java.sql.*;
-import freemarker.template.*;
-import mir.storage.*;
-import mir.misc.*;
-import mir.entity.*;
+import mir.entity.Entity;
+import mir.entity.EntityList;
+import mir.storage.Database;
+import mir.storage.DatabaseExc;
+import java.util.Map;
/**
- * Abstrakte Klasse, von denen die Modules die Basisfunktionalität erben.
- * Die Moduleschicht dient dazu, Funktionalitaeten zur Verfügung zu stellen,
+ * 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 StorageObject theStorage;
-
- public void setStorage(StorageObject storage) {
- this.theStorage = storage;
- }
-
- /**
- * Liefert das Standard-StorageObject zurück, mit dem das Module assoziiert ist.
- * @return Standard-StorageObject
- */
- public StorageObject getStorageObject () {
- return theStorage;
- }
-
- /**
- * Holt eine Entity anhand der Id via StorageObject
- * @param String der Entity
- * @return Entity
- */
- public Entity getById (String id) throws ModuleException {
- try {
- if (theStorage == null)
- throw new ModuleException("Kein StorageObject gesetzt");
- Entity entity = (Entity)theStorage.selectById(id);
- if (entity == null)
- throw new ModuleException("Objekt nicht vorhanden: ID=" + id);
- else return entity;
- }
- catch (StorageObjectException e){
- throw new ModuleException(e.toString());
- }
- }
+ protected Database database;
+
+ public AbstractModule(Database aDatabase) {
+ database = aDatabase;
+ }
/**
- * Holt eine EntityListe anhand des WhereClause via StorageObject
- * @param String whereclause
- * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
- * @return EntityList Liste der gematchten Datensätze
- */
- public EntityList getByWhereClause (String whereClause, int offset) throws ModuleException {
- try {
- if (theStorage == null)
- throw new ModuleException("Kein StorageObject gesetzt");
- return theStorage.selectByWhereClause(whereClause, offset);
- }
- catch (StorageObjectException e){
- throw new ModuleException(e.toString());
- }
- }
-
- /**
- * Holt eine EntityListe anhand des WhereClause aus dem StorageObject
- * @param String where WhereClause
- * @param String order Sortierreihenfolge
- * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
- * @return EntityList Liste der gematchten Datensätze
- */
- public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
- try {
- if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
- return theStorage.selectByWhereClause(where, order, offset);
- }
- catch (StorageObjectException e){
- throw new ModuleException(e.toString());
- }
- }
- /**
- * Executes a where clause on the StorageObject with order criteria
- * fetching from offset the number of limit objects
- *
- * @param String where
- * @param String order
- * @param int offset
- * @param int limit
- * @return EntityList
- */
-
- public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
- {
- try {
- if (theStorage==null) throw new ModuleException("StorageObject not set!");
- return theStorage.selectByWhereClause(where, order, offset, limit);
- }
- catch (StorageObjectException e){
- throw new ModuleException(e.toString());
- }
- }
-
- /**
- * Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
- * @param String aField - Feldname im StorageObject
- * @param String aValue - Wert in Feld im StorageObject
- * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
- * @return EntityList Liste der gematchten Datensätze
- */
- public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
- String whereClause;
- whereClause = aField + " like '%" + aValue + "%'";
- return getByWhereClause(whereClause, offset);
- }
-
- /**
- * Standardfunktion, um einen Datensatz via StorageObject einzufügen
- * @param theValues Hash mit Spalte/Wert-Paaren
- * @return Id des eingefügten Objekts
- * @exception ModuleException
- */
- public String add (HashMap theValues) throws ModuleException {
- try {
- Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
- theEntity.setStorage(theStorage);
- theEntity.setValues(theValues);
- return theEntity.insert();
- } catch (Exception e) {
- throw new ModuleException(e.toString());
- }
- }
-
- /**
- * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
- * @param theValues Hash mit Spalte/Wert-Paaren
- * @return Id des eingefügten Objekts
- * @exception ModuleException
- */
- public String set (HashMap theValues) throws ModuleException {
- try {
- Entity theEntity = theStorage.selectById((String)theValues.get("id"));
- if (theEntity == null)
- throw new ModuleException("Kein Objekt mit id in Datenbank id: " + theValues.get("id"));
- theEntity.setValues(theValues);
- theEntity.update();
- return theEntity.getId();
- }
- catch (StorageObjectException e){
- e.printStackTrace(System.err);
- throw new ModuleException(e.toString());
- }
- }
-
- /**
- * Löscht einen Datensatz anhand seiner Id
- * @param idParam
- * @exception ModuleException
- */
- public void deleteById (String idParam) throws ModuleException {
+ * 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 {
- theStorage.delete(idParam);
- } catch (StorageObjectException e){
- throw new ModuleException(e.toString());
+ Entity entity = database.createNewEntity();
+ entity.setFieldValues(theValues);
+
+ return entity.insert();
+ }
+ catch (Throwable e) {
+ throw new ModuleFailure(e);
}
}
- /**
- * Liefert den Lookuptable aller Objekte des StorageObjects
- * @return freemarker.template.SimpleHash
- */
- public SimpleHash getHashData() {
- return theStorage.getHashData();
- }
+ 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);
+ }
+ }
/**
- * returns the number of rows
+ * Deletes a record using an id
+ * @param idParam
+ * @exception ModuleExc
+ * @exception ModuleFailure
*/
- public int getSize(String where)
- throws SQLException,StorageObjectException {
- return theStorage.getSize(where);
+ public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
+ try {
+ database.delete(idParam);
+ }
+ catch (Throwable e) {
+ throw new ModuleFailure(e);
+ }
}
-
}