001: /*
002: * Copyright 2006-2007 Luca Garulli (luca.garulli@assetdata.it)
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: */
016: package org.romaframework.aspect.view;
017:
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Map;
021:
022: import org.romaframework.aspect.view.feature.ViewElementFeatures;
023: import org.romaframework.aspect.view.form.ContentComponent;
024: import org.romaframework.core.flow.ObjectContext;
025: import org.romaframework.core.schema.SchemaClassDefinition;
026: import org.romaframework.core.schema.SchemaField;
027: import org.romaframework.core.schema.SchemaHelper;
028: import org.romaframework.core.util.DynaBean;
029:
030: /**
031: * Helper class to resolve common tasks about View Aspect.
032: *
033: * @author Luca Garulli (luca.garulli@assetdata.it)
034: *
035: */
036: public class ViewHelper {
037: /**
038: * Disable all fields of the user object required. It acts recursively browsing all own fields.
039: *
040: * @param iUserObject
041: * User object to introspect
042: */
043: public static void disableFields(Object iUserObject) {
044: if (iUserObject == null)
045: return;
046:
047: ContentComponent form = ObjectContext.getInstance()
048: .getFormByObject(iUserObject);
049: if (form != null)
050: disableFields(iUserObject, form.getSchemaInstance());
051: }
052:
053: /**
054: * Disable all fields of the user object required. It acts recursively browsing all own fields.
055: *
056: * @param iUserObject
057: * User object to introspect
058: * @param iSchema
059: * Schema definition of object
060: */
061: public static void disableFields(Object iUserObject,
062: SchemaClassDefinition iSchema) {
063: SchemaField field;
064: DynaBean property;
065: for (Iterator<SchemaField> itField = iSchema.getFieldIterator(); itField
066: .hasNext();) {
067: field = itField.next();
068: if (field.getName().equalsIgnoreCase("sector"))
069: field.getName();
070: property = ObjectContext.getInstance().getFieldFeatures(
071: iUserObject, ViewAspect.ASPECT_NAME,
072: field.getName());
073: if (property == null
074: || !(Boolean) property
075: .getAttribute(ViewElementFeatures.VISIBLE))
076: continue;
077:
078: ObjectContext.getInstance().setFieldFeature(iUserObject,
079: ViewAspect.ASPECT_NAME, field.getName(),
080: ViewElementFeatures.ENABLED, Boolean.FALSE);
081:
082: if (java.util.Collection.class.isAssignableFrom(field
083: .getTypeClass())) {
084: // DISABLE ALL SUB-OBJECT IF THEY ARE FORMS
085: Object subObj = SchemaHelper.getFieldValue(field,
086: iUserObject);
087: if (subObj != null) {
088: Collection coll = (Collection) subObj;
089: for (Object o : coll) {
090: disableFields(o);
091: }
092: }
093: } else if (java.util.Map.class.isAssignableFrom(field
094: .getTypeClass())) {
095: // DISABLE ALL SUB-OBJECT IF THEY ARE FORMS
096: Object subObj = SchemaHelper.getFieldValue(field,
097: iUserObject);
098: if (subObj != null) {
099: Map map = (Map) subObj;
100: for (Object o : map.values()) {
101: disableFields(o);
102: }
103: }
104: } else if (field.getComplexClass() != null) {
105: Object subObj = SchemaHelper.getFieldValue(field,
106: iUserObject);
107: if (subObj != null)
108: disableFields(subObj, field.getComplexClass());
109: }
110: }
111: }
112: }
|