001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.backend.list.helper.fields;
016:
017: import java.sql.ResultSet;
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.araneaframework.backend.list.helper.ListSqlHelper;
025: import org.araneaframework.backend.util.BeanUtil;
026: import org.araneaframework.core.Assert;
027: import org.araneaframework.uilib.list.structure.ListStructure;
028: import org.araneaframework.uilib.util.NameUtil;
029:
030: /**
031: * List of fields standard implementation.
032: * <p>
033: * All the fields are presented both in the <code>SELECT</code> and in the {@link ResultSet}.
034: * </p>
035: * <p>
036: * Fields can be added/removed either:
037: * <ul>
038: * <li>one by one</li>
039: * <li>as a list</li>
040: * <li>using a {@link ListStructure} (all fields defined in the list)</li>
041: * <li>using a Bean Class (all fields are listed using reflection)</li>
042: * </ul>
043: * <p>
044: * For each add/remove call a <b>prefix</b >can be set.
045: * Prefix should refer to a composite field in a Bean (containing another Bean).
046: * </p>
047: * <p>
048: * Example use:
049: * <pre>
050: * ListSqlHelper helper = ...
051: * StandardFields sf = helper.getStandardFields();
052: * sf.addField("name"); // add "name"
053: * sf.addField("birthdate"); // add "birthdate"
054: * sf.addField("address", "town"); // add "address.town"
055: * sf.addField("address", "zip"); // add "address.zip"
056: * sf.addField("org", Organization.class); // add all fields of Organization.class as sub-fields
057: * Modifier.addListFields(sf.subFields("modifier")); // let Mofifier.class add it's common fields
058: * </pre>
059: *
060: * @see Fields
061: * @see ListSqlHelper#getStandardFields()
062: *
063: * @author Rein Raudjärv
064: *
065: * @since 1.1
066: */
067: public class StandardFields implements Fields {
068:
069: private Collection fields;
070:
071: private String globalPrefix;
072:
073: /**
074: * Create an instance of {@link StandardFields} using an empty set of fields.
075: */
076: public StandardFields() {
077: fields = new ArrayList();
078: }
079:
080: /**
081: * Create an instance of {@link StandardFields} extending the set of fields
082: * of another {@link StandardFields} instance and
083: * using the <code>globalPrefix</code> to all the fields added.
084: */
085: public StandardFields(StandardFields parent, String globalPrefix) {
086: this .fields = parent.fields;
087: this .globalPrefix = globalPrefix;
088: }
089:
090: /**
091: * Create an instance of {@link StandardFields} using the specified set of fields..
092: */
093: public StandardFields(Collection fields) {
094: this .fields = fields;
095: }
096:
097: /**
098: * Create an instance of {@link StandardFields} using the set of fields
099: * defined by the {@link ListStructure}.
100: */
101: public StandardFields(ListStructure structure) {
102: this ();
103: addFields(structure);
104: }
105:
106: /**
107: * Create an instance of {@link StandardFields} extending the set of fields
108: * of another {@link StandardFields} instance and
109: * using the <code>globalPrefix</code> to all the fields added.
110: */
111: public StandardFields subFields(String globalPrefix) {
112: Assert.notNullParam(globalPrefix, "globalPrefix");
113: return new StandardFields(this , NameUtil.getFullName(
114: this .globalPrefix, globalPrefix));
115: }
116:
117: public StandardFields addField(String prefix, String field) {
118: fields.add(addPrefix(prefix, field));
119: return this ;
120: }
121:
122: public StandardFields removeField(String prefix, String field) {
123: fields.remove(addPrefix(prefix, field));
124: return this ;
125: }
126:
127: public StandardFields addFields(String prefix, Collection fields) {
128: this .fields.addAll(addPrefix(prefix, fields));
129: return this ;
130: }
131:
132: public StandardFields removeFields(String prefix, Collection fields) {
133: this .fields.removeAll(addPrefix(prefix, fields));
134: return this ;
135: }
136:
137: public StandardFields addFields(String prefix, String[] fields) {
138: this .fields.addAll(addPrefix(prefix, Arrays.asList(fields)));
139: return this ;
140: }
141:
142: public StandardFields removeFields(String prefix, String[] fields) {
143: this .fields.removeAll(addPrefix(prefix, Arrays.asList(fields)));
144: return this ;
145: }
146:
147: public StandardFields addField(String field) {
148: return addField(null, field);
149: }
150:
151: public StandardFields removeField(String field) {
152: return removeField(null, field);
153: }
154:
155: public StandardFields addFields(ListStructure structure) {
156: return addFields(structure.getFields().keySet());
157: }
158:
159: public StandardFields addFields(Collection fields) {
160: return addFields(null, fields);
161: }
162:
163: public StandardFields removeFields(Collection fields) {
164: return removeFields(null, fields);
165: }
166:
167: public StandardFields addFields(String[] fields) {
168: return addFields(null, fields);
169: }
170:
171: public StandardFields removeFields(String[] fields) {
172: return removeFields(null, fields);
173: }
174:
175: public StandardFields addFields(Class beanClass) {
176: return addFields(null, beanClass);
177: }
178:
179: public StandardFields removeFields(Class beanClass) {
180: return removeFields(null, beanClass);
181: }
182:
183: public StandardFields addFields(String prefix, Class beanClass) {
184: return addFields(prefix, BeanUtil.getFields(beanClass));
185: }
186:
187: public StandardFields removeFields(String prefix, Class beanClass) {
188: return removeFields(prefix, BeanUtil.getFields(beanClass));
189: }
190:
191: public Collection getNames() {
192: return fields;
193: }
194:
195: public Collection getResultSetNames() {
196: return fields;
197: }
198:
199: // ---
200:
201: private String addPrefix(String prefix, String field) {
202: String result = NameUtil.getFullName(prefix, field);
203: return globalPrefix == null ? result : NameUtil.getFullName(
204: globalPrefix, result);
205: }
206:
207: private Collection addPrefix(String prefix, Collection fields) {
208: if (prefix == null || prefix.length() == 0) {
209: return fields;
210: }
211:
212: List result = new ArrayList(fields.size());
213: for (Iterator it = fields.iterator(); it.hasNext();) {
214: String field = (String) it.next();
215: result.add(addPrefix(prefix, field));
216: }
217: return result;
218: }
219:
220: }
|