001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: MaskImpl.java,v $
031: * Revision 1.8 2005/10/12 18:20:36 colinmacleod
032: * Added logging.
033: *
034: * Revision 1.7 2005/10/03 10:17:24 colinmacleod
035: * Fixed some style and javadoc issues.
036: *
037: * Revision 1.6 2005/09/14 12:51:53 colinmacleod
038: * Added serialVersionUID.
039: *
040: * Revision 1.5 2005/04/09 18:04:14 colinmacleod
041: * Changed copyright text to GPL v2 explicitly.
042: *
043: * Revision 1.4 2005/01/19 12:39:39 colinmacleod
044: * Changed Id --> Name.
045: *
046: * Revision 1.3 2005/01/06 22:13:21 colinmacleod
047: * Moved up a version number.
048: * Changed copyright notices to 2005.
049: * Updated the documentation:
050: * - started working on multiproject:site docu.
051: * - changed the logo.
052: * Added checkstyle and fixed LOADS of style issues.
053: * Added separate thirdparty subproject.
054: * Added struts (in web), util and webgui (in webtheme) from ivata op.
055: *
056: * Revision 1.2 2004/12/30 20:15:14 colinmacleod
057: * Moved first and last fields up to Group from Mask.
058: *
059: * Revision 1.1 2004/12/29 20:07:06 colinmacleod
060: * Renamed subproject masks to mask.
061: *
062: * Revision 1.1.1.1 2004/05/16 20:40:31 colinmacleod
063: * Ready for 0.1 release
064: * -----------------------------------------------------------------------------
065: */
066: package com.ivata.mask;
067:
068: import java.beans.PropertyDescriptor;
069: import java.util.Collections;
070: import java.util.List;
071: import java.util.Set;
072: import java.util.Vector;
073:
074: import org.apache.commons.beanutils.PropertyUtils;
075: import org.apache.log4j.Logger;
076:
077: import com.ivata.mask.field.Field;
078: import com.ivata.mask.group.Group;
079: import com.ivata.mask.group.GroupImpl;
080:
081: /**
082: * <p>
083: * In the Masks display configuration, a mask extends a group to define which
084: * fields come first and last, or the fields which should appear explicitly.
085: * </p>
086: *
087: * <p>
088: * Don't try to instantiate this class - use the mask factory instead.
089: * </p>
090: *
091: * @author Colin MacLeod
092: * @since ivata masks 0.1 (2004-05-15) <a
093: * href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com </a>
094: */
095: public class MaskImpl extends GroupImpl implements Mask {
096: /**
097: * Logger for this class.
098: */
099: private static final Logger logger = Logger
100: .getLogger(MaskImpl.class);
101:
102: /**
103: * Serialization version (for <code>Serializable</code> interface).
104: */
105: private static final long serialVersionUID = 1L;
106: /**
107: * The class of the dependent value objects which will be associated with
108: * this mask.
109: */
110: private Class dOClass;
111: /**
112: * <copyDoc>Refer to {@link Mask#getIncludePaths}.</copyDoc>
113: */
114: private List includePaths = new Vector();
115:
116: /**
117: * Create a mask with the specified parent.
118: *
119: * @param dOClassParam
120: * The class of the data object displayed in this mask.
121: * @param parent
122: * Group which contains this mask, or parent mask this mask
123: * extends.
124: * @param nameParam
125: * Name of the mask. This is used in combination with the class
126: * name to build the identifier for each mask uniquely.
127: */
128: public MaskImpl(final Class dOClassParam, final Group parent,
129: final String nameParam) {
130: super (nameParam, parent);
131: this .dOClass = dOClassParam;
132: }
133:
134: /**
135: * <copyDoc>Refer to {@link Mask#getIncludePaths}.</copyDoc>
136: * @param includePathParam The include path to add to the list.
137: */
138: public final void addIncludePath(final String includePathParam) {
139: if (logger.isDebugEnabled()) {
140: logger.debug("addIncludePath(String includePathParam = "
141: + includePathParam + ") - start");
142: }
143:
144: this .includePaths.add(includePathParam);
145:
146: if (logger.isDebugEnabled()) {
147: logger.debug("addIncludePath() - end");
148: }
149: }
150:
151: /**
152: * Get the dependent object class associated with this mask.
153: *
154: * @return The class of the data object displayed in this mask.
155: */
156: public final Class getDOClass() {
157: if (logger.isDebugEnabled()) {
158: logger.debug("getDOClass() - start");
159: }
160:
161: if (logger.isDebugEnabled()) {
162: logger.debug("getDOClass() - end - return value = "
163: + dOClass);
164: }
165: return dOClass;
166: }
167:
168: /**
169: * Get all the fields which should be displayed/hidden (not excluded) in
170: * this mask, in order.
171: *
172: * @return All field names apart from those explicitly excluded.
173: */
174: public final List getFields() {
175: if (logger.isDebugEnabled()) {
176: logger.debug("getFields() - start");
177: }
178:
179: Set allExcludedFieldNames = getAllExcludedFieldNames();
180: List firstFieldNames = getAllFirstFieldNames();
181: List lastFieldNames = getAllLastFieldNames();
182: PropertyDescriptor[] propertyDescriptors = PropertyUtils
183: .getPropertyDescriptors(dOClass);
184: List maskFields = new java.util.ArrayList();
185: // go thro' all the first field names and see if any match this class
186: for (int i = 0; i < firstFieldNames.size(); ++i) {
187: String firstFieldId = (String) firstFieldNames.get(i);
188: for (int j = 0; j < propertyDescriptors.length; j++) {
189: String name = propertyDescriptors[j].getName();
190: if (name.equals(firstFieldId)) {
191: // only add field names which are _not_ on the exclude list
192: // explicitly
193: if (!allExcludedFieldNames.contains(name)) {
194: Field field = getField(name);
195: if (field != null) {
196: maskFields.add(field);
197: }
198: }
199: break;
200: }
201: }
202: }
203: // get all the fields which are in this class, and not specified as
204: // either first or last.
205: for (int i = 0; i < propertyDescriptors.length; i++) {
206: String name = propertyDescriptors[i].getName();
207: // make sure this field is not excluded
208: if (!firstFieldNames.contains(name)
209: && !allExcludedFieldNames.contains(name)
210: && !lastFieldNames.contains(name)) {
211: Field field = getField(name);
212: if (field != null) {
213: maskFields.add(field);
214: }
215: }
216: }
217: // these fields should come at the end, in the order specified
218: for (int i = 0; i < lastFieldNames.size(); ++i) {
219: String lastFieldId = (String) lastFieldNames.get(i);
220: for (int j = 0; j < propertyDescriptors.length; j++) {
221: String name = propertyDescriptors[j].getName();
222: if (name.equals(lastFieldId)) {
223: // only add field names which are _not_ on the exclude list
224: if (!firstFieldNames.contains(name)
225: && !allExcludedFieldNames.contains(name)) {
226: Field field = getField(name);
227: if (field != null) {
228: maskFields.add(field);
229: }
230: }
231: break;
232: }
233: }
234: }
235:
236: if (logger.isDebugEnabled()) {
237: logger.debug("getFields() - end - return value = "
238: + maskFields);
239: }
240: return maskFields;
241: }
242:
243: /**
244: * <copyDoc>Refer to {@link Mask#getIncludePaths}.</copyDoc>
245: * @return Returns all include paths.
246: */
247: public List getIncludePaths() {
248: if (logger.isDebugEnabled()) {
249: logger.debug("getIncludePaths() - start");
250: }
251:
252: List returnList = Collections.unmodifiableList(includePaths);
253: if (logger.isDebugEnabled()) {
254: logger.debug("getIncludePaths() - end - return value = "
255: + returnList);
256: }
257: return returnList;
258: }
259: }
|