001: /*
002: * Copyright 2006 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:
017: package org.romaframework.core.schema;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Map.Entry;
025:
026: import org.romaframework.aspect.authentication.UserObjectPermissionListener;
027: import org.romaframework.core.Utility;
028: import org.romaframework.core.flow.Controller;
029: import org.romaframework.core.flow.UserObjectEventListener;
030: import org.romaframework.core.util.DynaBean;
031:
032: public abstract class SchemaClassDefinition extends SchemaFeatures {
033:
034: protected Map<String, SchemaField> fields;
035: protected Map<String, SchemaAction> actions;
036: protected Map<String, SchemaEvent> events;
037:
038: protected List<SchemaField> orderedFields;
039: protected List<SchemaAction> orderedActions;
040:
041: public SchemaClassDefinition() {
042: fields = new HashMap<String, SchemaField>();
043: actions = new HashMap<String, SchemaAction>();
044: events = new HashMap<String, SchemaEvent>();
045:
046: orderedFields = new ArrayList<SchemaField>();
047: orderedActions = new ArrayList<SchemaAction>();
048: }
049:
050: public abstract SchemaClass getSchemaClass();
051:
052: public SchemaField getField(String iFieldName) {
053: String fieldName;
054: int sepPos = iFieldName.indexOf(Utility.PACKAGE_SEPARATOR);
055: if (sepPos == -1)
056: fieldName = iFieldName;
057: else
058: fieldName = iFieldName.substring(0, sepPos);
059:
060: SchemaField field = fields.get(fieldName);
061:
062: if (sepPos > -1)
063: field = field.getComplexClass().getField(
064: iFieldName.substring(sepPos + 1));
065:
066: return field;
067: }
068:
069: public Iterator<SchemaField> getFieldIterator() {
070: return orderedFields.iterator();
071: }
072:
073: public SchemaAction getAction(String iActionName) {
074: String actionName;
075: Map<String, SchemaAction> localActions = null;
076:
077: int sepPos = iActionName.indexOf(Utility.PACKAGE_SEPARATOR);
078: if (sepPos == -1) {
079: localActions = actions;
080: actionName = iActionName;
081: } else {
082: // FOLLOW THE PATH UNTIL LAST FIELD AND THEN GET THE ACTION
083: SchemaField field = getField(iActionName.substring(0,
084: sepPos));
085: if (field == null)
086: return null;
087:
088: localActions = field.getComplexClass().actions;
089: actionName = iActionName.substring(sepPos + 1);
090: }
091:
092: SchemaAction action = localActions.get(actionName);
093:
094: return action;
095: }
096:
097: public Iterator<SchemaAction> getActionIterator() {
098: return orderedActions.iterator();
099: }
100:
101: public SchemaEvent getEvent(String iEventName) {
102: return events.get(iEventName);
103: }
104:
105: public Iterator<SchemaEvent> getEventIterator() {
106: return events.values().iterator();
107: }
108:
109: /**
110: * Copy the definition from class schema.
111: *
112: * @param iSource
113: */
114: public void copyDefinition(SchemaClassDefinition iSource) {
115: try {
116: // COPY ALL FEATURES FROM PARENT ENTITY
117: DynaBean features;
118: for (Entry<String, DynaBean> entry : iSource
119: .getAllFeatures().entrySet()) {
120: features = (DynaBean) entry.getValue().clone();
121: allFeatures.put(entry.getKey(), features);
122: }
123:
124: boolean allowed;
125:
126: // COPY ALL FIELDS FROM PARENT ENTITY
127: SchemaField field;
128: SchemaField sourceSchemaField;
129: for (Iterator<SchemaField> it = iSource.getFieldIterator(); it
130: .hasNext();) {
131: sourceSchemaField = it.next();
132:
133: // CHECK IF ALLOWED
134: List<UserObjectPermissionListener> listeners = Controller
135: .getInstance().getListeners(
136: UserObjectPermissionListener.class);
137: if (listeners != null) {
138: synchronized (listeners) {
139: allowed = true;
140: for (UserObjectPermissionListener listener : listeners) {
141: if (!listener.allowField(getSchemaClass()
142: .getClazz(), sourceSchemaField)) {
143: // NOT ALLOWED
144: allowed = false;
145: break;
146: }
147: }
148: }
149:
150: if (!allowed)
151: continue;
152: }
153:
154: field = (SchemaField) sourceSchemaField.clone();
155: field.entity = this ;
156: setField(field.getName(), field);
157: }
158:
159: // COPY ALL ACTIONS FROM PARENT ENTITY
160: SchemaAction action;
161: SchemaElement sourceSchemaAction;
162: for (Iterator<SchemaAction> it = iSource
163: .getActionIterator(); it.hasNext();) {
164: sourceSchemaAction = it.next();
165:
166: // CHECK IF ALLOWED
167: List<UserObjectPermissionListener> listeners = Controller
168: .getInstance().getListeners(
169: UserObjectPermissionListener.class);
170: if (listeners != null) {
171: synchronized (listeners) {
172: allowed = true;
173: for (UserObjectPermissionListener listener : listeners) {
174: if (!listener.allowAction(getSchemaClass()
175: .getClazz(), sourceSchemaAction)) {
176: // NOT ALLOWED
177: allowed = false;
178: break;
179: }
180: }
181: }
182:
183: if (!allowed)
184: continue;
185: }
186:
187: action = (SchemaAction) sourceSchemaAction.clone();
188: action.entity = this ;
189: setAction(action.getName(), action);
190: }
191:
192: // COPY ALL EVENTS FROM PARENT ENTITY
193: SchemaEvent event;
194: for (Iterator<SchemaEvent> it = iSource.getEventIterator(); it
195: .hasNext();) {
196: event = (SchemaEvent) it.next().clone();
197: event.entity = this ;
198: setEvent(event.getName(), event);
199: }
200: } catch (CloneNotSupportedException e) {
201: }
202: }
203:
204: public void setField(String iFieldName, SchemaField iField) {
205: if (fields.containsKey(iFieldName)) {
206: // REPLACE IT
207: for (int i = 0; i < orderedFields.size(); ++i) {
208: if (orderedFields.get(i).getName().equals(iFieldName)) {
209: orderedFields.remove(i);
210: break;
211: }
212: }
213: }
214:
215: orderedFields.add(iField);
216: fields.put(iFieldName, iField);
217: }
218:
219: public void setAction(String iActionName, SchemaAction iAction) {
220: if (actions.containsKey(iActionName)) {
221: // REPLACE IT
222: for (int i = 0; i < orderedActions.size(); ++i) {
223: if (orderedActions.get(i).getName().equals(iActionName)) {
224: orderedActions.remove(i);
225: break;
226: }
227: }
228: }
229:
230: orderedActions.add(iAction);
231: actions.put(iActionName, iAction);
232: }
233:
234: public void setEvent(String iEventName, SchemaEvent iEvent) {
235: events.put(iEventName, iEvent);
236: }
237:
238: public void setOrderFields(List<SchemaField> iFields) {
239: orderedFields = iFields;
240: }
241:
242: public void setOrderActions(List<SchemaAction> iActions) {
243: orderedActions = iActions;
244: }
245:
246: public Map<String, SchemaField> getFields() {
247: return fields;
248: }
249:
250: }
|