ee35074051d2e9928d9bc08a44ce63e75b1ccc0a
[mir.git] / source / mir / entity / AbstractEntity.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package  mir.entity;
31
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
37 import mir.config.MirPropertiesConfiguration;
38 import mir.log.LoggerWrapper;
39 import mir.storage.Database;
40 import mir.storage.StorageObjectExc;
41 import mir.storage.StorageObjectFailure;
42
43 /**
44  * Base class the entities are derived from. Provides base functionality of
45  * an entity.
46  *
47  * @version $Id: AbstractEntity.java,v 1.8.2.6 2005/02/10 16:22:30 rhindes Exp $
48  */
49
50 public class AbstractEntity implements Entity {
51   protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
52
53   protected Map values;
54   protected Database database;
55   protected LoggerWrapper logger;
56
57   public AbstractEntity() {
58     logger = new LoggerWrapper("Entity");
59
60     values = new HashMap();
61   }
62
63   public void setStorage(Database aDatabase) {
64     database = aDatabase;
65   }
66
67   /** {@inheritDoc} */
68   public void setFieldValues(Map aMap) {
69     if (aMap!=null) {
70       Iterator i = aMap.entrySet().iterator();
71       synchronized(this) {
72         while (i.hasNext()) {
73           Map.Entry entry = (Map.Entry) i.next();
74
75           setFieldValue( (String) entry.getKey(), (String) entry.getValue());
76         }
77       }
78     }
79   }
80
81   /** {@inheritDoc} */
82   public String getId() {
83     return getFieldValue(database.getIdFieldName());
84   }
85
86   /** {@inheritDoc} */
87   public void setId(String id) {
88     setFieldValue(database.getIdFieldName(), id);
89   }
90
91   /** {@inheritDoc} */
92   public String insert() throws StorageObjectExc {
93     logger.debug("Entity: trying to insert ...");
94
95     if (database != null) {
96       return database.insert(this);
97     }
98                 throw new StorageObjectExc("database == null!");
99   }
100
101   /** {@inheritDoc} */
102   public void update() throws StorageObjectFailure {
103     database.update(this);
104   }
105
106   /** {@inheritDoc} */
107   public String getFieldValue(String aFieldName) {
108     String returnValue = null;
109
110     if (aFieldName != null) {
111       returnValue = (String) values.get(aFieldName);
112     }
113     return returnValue;
114   }
115
116   /** {@inheritDoc} */
117   public boolean hasFieldValue(String aFieldName) {
118     return values.containsKey(aFieldName);
119   }
120
121   /**
122    * Sets the value for a field. Issues a log message if the field name
123    * supplied was not found in the Entity.
124    * @param theProp The field name whose value has to be set
125    * @param theValue The new value of the field
126    * @exception StorageObjectFailure
127    */
128   public void setFieldValue(String theProp, String theValue) throws StorageObjectFailure {
129     if (hasField(theProp))
130       values.put(theProp, theValue);
131     else {
132       logger.warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
133     }
134   }
135
136   /**
137    * Returns the field names of the Entity
138    */
139   public List getFieldNames() throws StorageObjectFailure {
140     return database.getFieldNames();
141   }
142
143   /** Returns whether fieldName is a valid field name of this Entity.
144    * @param fieldName
145    * @return true in case fieldName is a field name, else false.
146    * @exception StorageObjectFailure
147    */
148   public boolean hasField(String fieldName) throws StorageObjectFailure {
149     return getFieldNames().contains(fieldName);
150   }
151 }
152