001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.property;
022:
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import com.methodhead.persistable.Persistable;
028: import com.methodhead.persistable.PersistableException;
029:
030: import org.apache.commons.beanutils.DynaClass;
031: import org.apache.commons.beanutils.DynaProperty;
032: import org.apache.commons.beanutils.BasicDynaClass;
033:
034: import com.methodhead.sitecontext.SiteContextCapable;
035: import com.methodhead.sitecontext.SiteContext;
036:
037: /**
038: * A name/value pair with convenient methods to get and set them.
039: * <ul>
040: * <li><tt>int sitecontext_id = 0</tt></li>
041: * <li><tt>String name = ""</tt></li>
042: * <li><tt>String value = ""</tt></li>
043: * <li><tt>String description = ""</tt></li>
044: * <li><tt>boolean system_property = false</tt> A flag indicating the property is a
045: * system property; what that actually means is up to the application</li>
046: * </ul>
047: */
048: public class Property extends Persistable implements SiteContextCapable {
049:
050: private static DynaClass dynaClass_ = null;
051:
052: static {
053: DynaProperty[] dynaProperties = new DynaProperty[] {
054: new DynaProperty("sitecontext_id", Integer.class),
055: new DynaProperty("name", String.class),
056: new DynaProperty("value", String.class),
057: new DynaProperty("description", String.class),
058: new DynaProperty("system_property", Boolean.class) };
059:
060: dynaClass_ = new BasicDynaClass("mh_property", Property.class,
061: dynaProperties);
062: }
063:
064: // constructors /////////////////////////////////////////////////////////////
065:
066: public Property() {
067: super (dynaClass_);
068: setInt("sitecontext_id", 0);
069: setString("name", "");
070: setString("value", "");
071: setString("description", "");
072: setBoolean("system_property", false);
073: }
074:
075: public Property(DynaClass dynaClass) {
076: super (dynaClass);
077: setInt("sitecontext_id", 0);
078: setString("name", "");
079: setString("value", "");
080: setString("description", "");
081: setBoolean("system_property", false);
082: }
083:
084: // constants ////////////////////////////////////////////////////////////////
085:
086: // classes //////////////////////////////////////////////////////////////////
087:
088: // methods //////////////////////////////////////////////////////////////////
089:
090: public void setSiteContext(SiteContext siteContext) {
091:
092: siteContext_ = siteContext;
093: }
094:
095: public SiteContext getSiteContext() {
096:
097: if (siteContext_ == null)
098: siteContext_ = SiteContext.getDefaultContext();
099:
100: return siteContext_;
101: }
102:
103: /**
104: * Updates the property <tt>name</tt> with <tt>value</tt>,
105: * <tt>description</tt>, and <tt>system</tt>, overwriting any values there
106: * already. If the property exists, each attribute is only updated if its
107: * corresponding method argument is not <tt>null</tt>. If the property
108: * doesn't exist and a method argument is <tt>null</tt>, its corresponding
109: * attribute is set to an empty string (or <tt>false</tt> in
110: * <tt>system</tt>'s case). already.
111: */
112: public void setProperty(String name, String value,
113: String description, Boolean system) {
114:
115: try {
116: load("sitecontext_id=" + getSiteContext().getInt("id")
117: + " AND name=" + getSqlLiteral(name));
118: } catch (PersistableException e) {
119: //
120: // property didn't exist; create it new
121: //
122: setString("name", name);
123:
124: setInt("sitecontext_id", getSiteContext().getInt("id"));
125:
126: if (value != null)
127: setString("value", value);
128: if (description != null)
129: setString("description", description);
130: if (system != null)
131: setBoolean("system_property", system.booleanValue());
132:
133: saveNew();
134:
135: return;
136: }
137:
138: setInt("sitecontext_id", getSiteContext().getInt("id"));
139:
140: if (value != null)
141: setString("value", value);
142: if (description != null)
143: setString("description", description);
144: if (system != null)
145: setBoolean("system_property", system.booleanValue());
146:
147: save("sitecontext_id=" + getSiteContext().getInt("id")
148: + " AND name=" + getSqlLiteral(name));
149: }
150:
151: public static void setProperty(SiteContext siteContext,
152: String name, String value, String description,
153: Boolean system) {
154:
155: Property p = new Property();
156: p.setSiteContext(siteContext);
157: p.setProperty(name, value, description, system);
158: }
159:
160: /**
161: * Updates the property <tt>name</tt> with <tt>value</tt>
162: */
163: public static void setProperty(SiteContext siteContext,
164: String name, String value) {
165:
166: setProperty(siteContext, name, value, null, null);
167: }
168:
169: /**
170: * Returns the value of property named <tt>name</tt> or <tt>null</tt> if the
171: * property is not defined.
172: */
173: public static String getProperty(SiteContext siteContext,
174: String name) {
175:
176: Property p = new Property();
177: p.setSiteContext(siteContext);
178: return p.getProperty(name);
179: }
180:
181: /**
182: * Returns the value of property named <tt>name</tt> or <tt>null</tt> if the
183: * property is not defined.
184: */
185: public String getProperty(String name) {
186:
187: try {
188: Property p = new Property();
189: p.load("sitecontext_id=" + getSiteContext().getInt("id")
190: + " AND name=" + getSqlLiteral(name));
191: return p.getString("value");
192: } catch (PersistableException e) {
193: return null;
194: }
195: }
196:
197: /**
198: * Returns the value of property named <tt>name</tt> or <tt>defaultVal</tt> if
199: * the property is not defined.
200: */
201: public static String getProperty(SiteContext siteContext,
202: String name, String defaultVal) {
203:
204: String val = getProperty(siteContext, name);
205:
206: if (val == null)
207: return defaultVal;
208:
209: return val;
210: }
211:
212: /**
213: * Returns the property named <tt>name</tt> split on commas (e.g.,
214: * <tt>"one,two,three"</tt>) or <tt>null</tt> if the property is not defined.
215: * Values are trimmed before being returned.
216: */
217: public static String[] getPropertyArray(SiteContext siteContext,
218: String name) {
219:
220: String val = getProperty(siteContext, name);
221:
222: if (val == null)
223: return null;
224:
225: String[] vals = val.split(",");
226:
227: String[] trimmedVals = new String[vals.length];
228: for (int i = 0; i < vals.length; i++)
229: trimmedVals[i] = vals[i].trim();
230:
231: return trimmedVals;
232: }
233:
234: /**
235: * Loads the property named <tt>name</tt>.
236: */
237: public void loadForName(String name) {
238:
239: load("sitecontext_id=" + getSiteContext().getInt("id")
240: + " AND name=" + getSqlLiteral(name));
241: }
242:
243: /**
244: * Returns a list containing all defined properties in alphabetic order.
245: */
246: public static List loadAll(SiteContext siteContext) {
247:
248: Property p = new Property();
249: p.setSiteContext(siteContext);
250: return p.loadAll();
251: }
252:
253: /**
254: * Returns a list containing all defined properties in alphabetic order.
255: */
256: public List loadAll() {
257:
258: return loadAll(dynaClass_, "sitecontext_id="
259: + getSiteContext().getInt("id"), "name");
260: }
261:
262: // properties ///////////////////////////////////////////////////////////////
263:
264: // attributes ///////////////////////////////////////////////////////////////
265:
266: private SiteContext siteContext_ = null;
267: }
|