dcd493bd8af58bde5b6fe93b7596e6b934cf6562
[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.config.MirPropertiesConfiguration.PropertiesConfigExc;
39 import mir.log.LoggerWrapper;
40 import mir.storage.StorageObject;
41 import mir.storage.StorageObjectExc;
42 import mir.storage.StorageObjectFailure;
43
44 /**
45  * Base class the entities are derived from. Provides base functionality of
46  * an entity.
47  *
48  * @version $Id: AbstractEntity.java,v 1.8.2.2 2004/02/08 21:05:00 zapata Exp $
49  */
50
51 public class AbstractEntity implements Entity {
52   protected static MirPropertiesConfiguration configuration;
53
54   protected Map values;
55   protected StorageObject storageObject;
56   protected LoggerWrapper logger;
57
58   static {
59     try {
60       configuration = MirPropertiesConfiguration.instance();
61     }
62     catch (PropertiesConfigExc e) {
63       throw new RuntimeException(e.getMessage());
64     }
65   }
66
67   public AbstractEntity() {
68     logger = new LoggerWrapper("Entity");
69
70     values = new HashMap();
71   }
72
73   /**
74    * Constructor
75    * @param StorageObject The StorageObject of the Entity.
76    */
77
78   public AbstractEntity(StorageObject StorageObject) {
79     this();
80
81     setStorage(StorageObject);
82   }
83
84   public void setStorage(StorageObject storage) {
85     this.storageObject = storage;
86   }
87
88   /** {@inheritDoc} */
89   public void setFieldValues(Map aMap) {
90     if (aMap!=null) {
91       Iterator i = aMap.entrySet().iterator();
92       synchronized(this) {
93         while (i.hasNext()) {
94           Map.Entry entry = (Map.Entry) i.next();
95
96           setFieldValue( (String) entry.getKey(), (String) entry.getValue());
97         }
98       }
99     }
100   }
101
102   /** {@inheritDoc} */
103   public String getId() {
104     return getFieldValue(storageObject.getIdName());
105   }
106
107   /** {@inheritDoc} */
108   public void setId(String id) {
109     setFieldValue(storageObject.getIdName(), id);
110   }
111
112   /** {@inheritDoc} */
113   public String insert() throws StorageObjectExc {
114     logger.debug("Entity: trying to insert ...");
115
116     if (storageObject != null) {
117       return storageObject.insert(this);
118     }
119     else
120       throw new StorageObjectExc("storageObject == null!");
121   }
122
123   /** {@inheritDoc} */
124   public void update() throws StorageObjectFailure {
125     storageObject.update(this);
126   }
127
128   /** {@inheritDoc} */
129   public String getFieldValue(String aFieldName) {
130     String returnValue = null;
131
132     if (aFieldName != null) {
133       returnValue = (String) values.get(aFieldName);
134     }
135     return returnValue;
136   }
137
138   /** {@inheritDoc} */
139   public boolean hasFieldValue(String aFieldName) {
140     return values.containsKey(aFieldName);
141   }
142
143   /**
144    * Sets the value for a field. Issues a log message if the field name
145    * supplied was not found in the Entity.
146    * @param theProp The field name whose value has to be set
147    * @param theValue The new value of the field
148    * @exception StorageObjectFailure
149    */
150   public void setFieldValue(String theProp, String theValue) throws StorageObjectFailure {
151     if (hasField(theProp))
152       values.put(theProp, theValue);
153     else {
154       logger.warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
155     }
156   }
157
158   /**
159    * Returns the field names of the Entity
160    */
161   public List getFieldNames() throws StorageObjectFailure {
162     return storageObject.getFields();
163   }
164
165   /** Returns whether fieldName is a valid field name of this Entity.
166    * @param fieldName
167    * @return true in case fieldName is a field name, else false.
168    * @exception StorageObjectFailure
169    */
170   public boolean hasField(String fieldName) throws StorageObjectFailure {
171     return getFieldNames().contains(fieldName);
172   }
173 }
174