001: /*
002: * argun 1.0
003: * Web 2.0 delivery framework
004: * Copyright (C) 2007 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.web.properties;
024:
025: import java.lang.reflect.InvocationTargetException;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.util.HashMap;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Set;
033:
034: import org.w3c.dom.Element;
035:
036: import biz.hammurapi.config.RuntimeConfigurationException;
037: import biz.hammurapi.sql.DataAccessObject;
038: import biz.hammurapi.sql.RowProcessor;
039: import biz.hammurapi.sql.SQLProcessor;
040: import biz.hammurapi.sql.SQLRuntimeException;
041: import biz.hammurapi.web.property.sql.PropertyEngine;
042: import biz.hammurapi.web.property.sql.PropertySetImpl;
043: import biz.hammurapi.web.property.sql.PsProperty;
044: import biz.hammurapi.web.property.sql.PsPropertyImpl;
045: import biz.hammurapi.xml.dom.AbstractDomObject;
046:
047: public class DbPropertySet extends PropertySetImpl implements
048: PersistentPropertySet, DataAccessObject {
049:
050: private PropertyEngine engine;
051:
052: public DbPropertySet() {
053: super ();
054: }
055:
056: public DbPropertySet(boolean force) {
057: super (force);
058: }
059:
060: public DbPropertySet(ResultSet rs) throws SQLException {
061: super (rs);
062: }
063:
064: public void delete() throws SQLException {
065: engine.deletePropertySet(getId());
066: setId(-1);
067: }
068:
069: public Set getPropertyNames() {
070: final Set ret = new HashSet();
071: try {
072: engine.processPsPropertyByPropertySet(getId(),
073: new RowProcessor() {
074:
075: public boolean process(ResultSet rs)
076: throws SQLException {
077: ret.add(rs.getString("NAME"));
078: return true;
079: }
080:
081: });
082: } catch (SQLException e) {
083: throw new SQLRuntimeException(e);
084: }
085: return ret;
086: }
087:
088: public PropertySet getSubset(String prefix) {
089: return new PropertySubSet(this , prefix);
090: }
091:
092: public void remove(String name) {
093: try {
094: engine.deletePsProperty(getId(), name);
095: } catch (SQLException e) {
096: throw new SQLRuntimeException(e);
097: }
098: }
099:
100: public void set(String name, Object value) {
101: remove(name);
102: PsProperty psProperty = new PsPropertyImpl(true);
103: psProperty.setName(name);
104: psProperty.setPropertyType(value.getClass().getName());
105: psProperty.setPropertyValue(value.toString());
106: psProperty.setSetId(getId());
107: try {
108: engine.insertPsProperty(psProperty);
109: } catch (SQLException e) {
110: throw new SQLRuntimeException(e);
111: }
112: }
113:
114: public void setSQLProcessor(SQLProcessor processor)
115: throws SQLException {
116: this .engine = new PropertyEngine(processor);
117: }
118:
119: public Object get(String name) {
120: try {
121: PsProperty property = engine.getPsProperty(getId(), name);
122: if (property != null) {
123: try {
124: // Default class is String
125: if (property.getPropertyType() == null
126: || property.getPropertyType().trim()
127: .length() == 0) {
128: return property.getPropertyValue();
129: }
130:
131: Class clazz = Class.forName(property
132: .getPropertyType());
133: if (property.getPropertyValue() == null
134: || property.getPropertyValue().trim()
135: .length() == 0) {
136: return clazz.newInstance();
137: } else {
138: return clazz.getConstructor(
139: new Class[] { String.class })
140: .newInstance(
141: new Object[] { property
142: .getPropertyValue() });
143: }
144: } catch (InstantiationException e) {
145: throw new RuntimeConfigurationException(
146: "Cannot instantiate property '"
147: + property.getName() + "' -> "
148: + property.getPropertyType(), e);
149: } catch (IllegalAccessException e) {
150: throw new RuntimeConfigurationException(
151: "Cannot instantiate property '"
152: + property.getName() + "' -> "
153: + property.getPropertyType(), e);
154: } catch (ClassNotFoundException e) {
155: throw new RuntimeConfigurationException(
156: "Cannot instantiate property '"
157: + property.getName() + "' -> "
158: + property.getPropertyType(), e);
159: } catch (SecurityException e) {
160: throw new RuntimeConfigurationException(
161: "Cannot instantiate property '"
162: + property.getName() + "' -> "
163: + property.getPropertyType(), e);
164: } catch (InvocationTargetException e) {
165: throw new RuntimeConfigurationException(
166: "Cannot instantiate property '"
167: + property.getName() + "' -> "
168: + property.getPropertyType(), e);
169: } catch (IllegalArgumentException e) {
170: throw new RuntimeConfigurationException(
171: "Cannot instantiate property '"
172: + property.getName() + "' -> "
173: + property.getPropertyType(), e);
174: } catch (NoSuchMethodException e) {
175: e.printStackTrace();
176: }
177: }
178: } catch (SQLException ex) {
179: throw new SQLRuntimeException(ex);
180: }
181:
182: Iterator mit = mounts.entrySet().iterator();
183: while (mit.hasNext()) {
184: Map.Entry entry = (Map.Entry) mit.next();
185: String prefix = (String) entry.getKey();
186: if (name.startsWith(prefix)) {
187: Object ret = ((PropertySet) entry.getValue()).get(name
188: .substring(prefix.length()));
189: if (ret != null) {
190: return ret;
191: }
192: }
193: }
194:
195: return null;
196: }
197:
198: /**
199: * Updates set information (name, description, owner, ...) in the databse
200: * @throws SQLException
201: */
202: public void update() throws SQLException {
203: engine.updatePropertySet(this );
204: }
205:
206: public void load(PropertySet source) {
207: Iterator it = source.getPropertyNames().iterator();
208: while (it.hasNext()) {
209: String key = (String) it.next();
210: set(key, source.get(key));
211: }
212: }
213:
214: private Map mounts = new HashMap();
215:
216: public void mount(String prefix, PropertySet source) {
217: mounts.put(prefix, source);
218: }
219:
220: public void clear() {
221: try {
222: engine.deletePsPropertyByPropertySet(getId());
223: } catch (SQLException e) {
224: throw new SQLRuntimeException(e);
225: }
226: }
227:
228: public void toDom(Element holder) {
229: super .toDom(holder);
230: Iterator pit = engine.getPsPropertyByPropertySet(getId())
231: .iterator();
232: while (pit.hasNext()) {
233: PsProperty property = (PsProperty) pit.next();
234: Element pe = AbstractDomObject.addTextElement(holder,
235: "property", property.getPropertyValue());
236: pe.setAttribute("name", property.getName());
237: pe.setAttribute("type", property.getPropertyType());
238: }
239: }
240:
241: public void setAll(PropertySet source) {
242: Iterator nit = source.getPropertyNames().iterator();
243: while (nit.hasNext()) {
244: String pName = (String) nit.next();
245: set(pName, source.get(pName));
246: }
247: }
248:
249: /**
250: * @return true if other property set entries are equal to this property set entries.
251: * Other Property set attribute
252: */
253: public boolean compareProperties(PropertySet otherSet) {
254: Set myNames = new HashSet(getPropertyNames());
255: Set otherNames = new HashSet(otherSet.getPropertyNames());
256: if (!myNames.equals(otherNames)) {
257: return false;
258: }
259:
260: Iterator nit = myNames.iterator();
261: while (nit.hasNext()) {
262: String pName = (String) nit.next();
263: Object myValue = get(pName);
264: Object otherValue = otherSet.get(pName);
265: if (myValue == null) {
266: if (otherValue != null) {
267: return false;
268: }
269: } else if (!myValue.equals(otherValue)) {
270: return false;
271: }
272: }
273:
274: return true;
275: }
276:
277: public boolean containsAll(PropertySet subSet) {
278: Set myNames = new HashSet(getPropertyNames());
279: Set subsetNames = new HashSet(subSet.getPropertyNames());
280: if (!myNames.containsAll(subsetNames)) {
281: return false;
282: }
283:
284: Iterator nit = subsetNames.iterator();
285: while (nit.hasNext()) {
286: String pName = (String) nit.next();
287: Object myValue = get(pName);
288: Object subsetValue = subSet.get(pName);
289: if (myValue == null) {
290: if (subsetValue != null) {
291: return false;
292: }
293: } else if (!myValue.equals(subsetValue)) {
294: return false;
295: }
296: }
297:
298: return true;
299: }
300: }
|