KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > property > Property


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.property;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import com.methodhead.persistable.Persistable;
28 import com.methodhead.persistable.PersistableException;
29
30 import org.apache.commons.beanutils.DynaClass;
31 import org.apache.commons.beanutils.DynaProperty;
32 import org.apache.commons.beanutils.BasicDynaClass;
33
34 import com.methodhead.sitecontext.SiteContextCapable;
35 import com.methodhead.sitecontext.SiteContext;
36
37 /**
38  * A name/value pair with convenient methods to get and set them.
39  * <ul>
40  * <li><tt>int sitecontext_id = 0</tt></li>
41  * <li><tt>String name = ""</tt></li>
42  * <li><tt>String value = ""</tt></li>
43  * <li><tt>String description = ""</tt></li>
44  * <li><tt>boolean system_property = false</tt> A flag indicating the property is a
45  * system property; what that actually means is up to the application</li>
46  * </ul>
47  */

48 public class Property
49 extends
50   Persistable
51 implements
52   SiteContextCapable {
53
54   private static DynaClass dynaClass_ = null;
55
56   static {
57     DynaProperty[] dynaProperties =
58       new DynaProperty[] {
59         new DynaProperty( "sitecontext_id", Integer JavaDoc.class ),
60         new DynaProperty( "name", String JavaDoc.class ),
61         new DynaProperty( "value", String JavaDoc.class ),
62         new DynaProperty( "description", String JavaDoc.class ),
63         new DynaProperty( "system_property", Boolean JavaDoc.class )
64       };
65
66     dynaClass_ =
67       new BasicDynaClass(
68         "mh_property", Property.class, dynaProperties );
69   }
70
71   // constructors /////////////////////////////////////////////////////////////
72

73   public Property() {
74     super( dynaClass_ );
75     setInt( "sitecontext_id", 0 );
76     setString( "name", "" );
77     setString( "value", "" );
78     setString( "description", "" );
79     setBoolean( "system_property", false );
80   }
81
82   public Property(
83     DynaClass dynaClass ) {
84     super( dynaClass );
85     setInt( "sitecontext_id", 0 );
86     setString( "name", "" );
87     setString( "value", "" );
88     setString( "description", "" );
89     setBoolean( "system_property", false );
90   }
91
92   // constants ////////////////////////////////////////////////////////////////
93

94   // classes //////////////////////////////////////////////////////////////////
95

96   // methods //////////////////////////////////////////////////////////////////
97

98   public void setSiteContext(
99     SiteContext siteContext ) {
100
101     siteContext_ = siteContext;
102   }
103
104   public SiteContext getSiteContext() {
105
106     if ( siteContext_ == null )
107       siteContext_ = SiteContext.getDefaultContext();
108
109     return siteContext_;
110   }
111
112   /**
113    * Updates the property <tt>name</tt> with <tt>value</tt>,
114    * <tt>description</tt>, and <tt>system</tt>, overwriting any values there
115    * already. If the property exists, each attribute is only updated if its
116    * corresponding method argument is not <tt>null</tt>. If the property
117    * doesn't exist and a method argument is <tt>null</tt>, its corresponding
118    * attribute is set to an empty string (or <tt>false</tt> in
119    * <tt>system</tt>'s case). already.
120    */

121   public void setProperty(
122     String JavaDoc name,
123     String JavaDoc value,
124     String JavaDoc description,
125     Boolean JavaDoc system ) {
126
127     try {
128       load(
129         "sitecontext_id=" + getSiteContext().getInt( "id" ) +
130         " AND name=" + getSqlLiteral( name ) );
131     }
132     catch ( PersistableException e ) {
133       //
134
// property didn't exist; create it new
135
//
136
setString( "name", name );
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       saveNew();
148
149       return;
150     }
151
152     setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
153
154     if ( value != null )
155       setString( "value", value );
156     if ( description != null )
157       setString( "description", description );
158     if ( system != null )
159       setBoolean( "system_property", system.booleanValue() );
160
161     save(
162       "sitecontext_id=" + getSiteContext().getInt( "id" ) +
163       " AND name=" + getSqlLiteral( name ) );
164   }
165
166   public static void setProperty(
167     SiteContext siteContext,
168     String JavaDoc name,
169     String JavaDoc value,
170     String JavaDoc description,
171     Boolean JavaDoc system ) {
172
173     Property p = new Property();
174     p.setSiteContext( siteContext );
175     p.setProperty( name, value, description, system );
176   }
177
178   /**
179    * Updates the property <tt>name</tt> with <tt>value</tt>
180    */

181   public static void setProperty(
182     SiteContext siteContext,
183     String JavaDoc name,
184     String JavaDoc value ) {
185
186     setProperty( siteContext, name, value, null, null );
187   }
188
189   /**
190    * Returns the value of property named <tt>name</tt> or <tt>null</tt> if the
191    * property is not defined.
192    */

193   public static String JavaDoc getProperty(
194     SiteContext siteContext,
195     String JavaDoc name ) {
196
197     Property p = new Property();
198     p.setSiteContext( siteContext );
199     return p.getProperty( name );
200   }
201
202   /**
203    * Returns the value of property named <tt>name</tt> or <tt>null</tt> if the
204    * property is not defined.
205    */

206   public String JavaDoc getProperty(
207     String JavaDoc name ) {
208
209     try {
210       Property p = new Property();
211       p.load(
212         "sitecontext_id=" + getSiteContext().getInt( "id" ) +
213         " AND name=" + getSqlLiteral( name ) );
214       return p.getString( "value" );
215     }
216     catch ( PersistableException e ) {
217       return null;
218     }
219   }
220
221   /**
222    * Returns the value of property named <tt>name</tt> or <tt>defaultVal</tt> if
223    * the property is not defined.
224    */

225   public static String JavaDoc getProperty(
226     SiteContext siteContext,
227     String JavaDoc name,
228     String JavaDoc defaultVal ) {
229
230     String JavaDoc val = getProperty( siteContext, name );
231
232     if ( val == null )
233       return defaultVal;
234
235     return val;
236   }
237
238   /**
239    * Returns the property named <tt>name</tt> split on commas (e.g.,
240    * <tt>"one,two,three"</tt>) or <tt>null</tt> if the property is not defined.
241    * Values are trimmed before being returned.
242    */

243   public static String JavaDoc[] getPropertyArray(
244     SiteContext siteContext,
245     String JavaDoc name ) {
246
247     String JavaDoc val = getProperty( siteContext, name );
248
249     if ( val == null )
250       return null;
251
252     String JavaDoc[] vals = val.split( "," );
253
254     String JavaDoc[] trimmedVals = new String JavaDoc[ vals.length ];
255     for ( int i = 0; i < vals.length; i++ )
256       trimmedVals[ i ] = vals[ i ].trim();
257
258     return trimmedVals;
259   }
260
261   /**
262    * Loads the property named <tt>name</tt>.
263    */

264   public void loadForName(
265     String JavaDoc name ) {
266
267     load(
268       "sitecontext_id=" + getSiteContext().getInt( "id" ) +
269       " AND name=" + getSqlLiteral( name ) );
270   }
271
272   /**
273    * Returns a list containing all defined properties in alphabetic order.
274    */

275   public static List JavaDoc loadAll(
276     SiteContext siteContext ) {
277
278     Property p = new Property();
279     p.setSiteContext( siteContext );
280     return p.loadAll();
281   }
282
283   /**
284    * Returns a list containing all defined properties in alphabetic order.
285    */

286   public List JavaDoc loadAll() {
287
288     return loadAll(
289       dynaClass_,
290       "sitecontext_id=" + getSiteContext().getInt( "id" ),
291       "name" );
292   }
293
294   // properties ///////////////////////////////////////////////////////////////
295

296   // attributes ///////////////////////////////////////////////////////////////
297

298   private SiteContext siteContext_ = null;
299 }
300
Popular Tags