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:
017: package org.romaframework.aspect.view;
018:
019: import java.lang.annotation.Annotation;
020: import java.util.Iterator;
021:
022: import org.romaframework.aspect.core.CoreAspect;
023: import org.romaframework.aspect.core.annotation.AnnotationConstants;
024: import org.romaframework.aspect.core.feature.CoreFieldFeatures;
025: import org.romaframework.aspect.view.annotation.ViewAction;
026: import org.romaframework.aspect.view.annotation.ViewClass;
027: import org.romaframework.aspect.view.annotation.ViewField;
028: import org.romaframework.aspect.view.feature.ViewActionFeatures;
029: import org.romaframework.aspect.view.feature.ViewBaseFeatures;
030: import org.romaframework.aspect.view.feature.ViewClassFeatures;
031: import org.romaframework.aspect.view.feature.ViewElementFeatures;
032: import org.romaframework.aspect.view.feature.ViewFieldFeatures;
033: import org.romaframework.core.Utility;
034: import org.romaframework.core.aspect.SelfRegistrantConfigurableAspect;
035: import org.romaframework.core.config.RomaApplicationContext;
036: import org.romaframework.core.schema.SchemaAction;
037: import org.romaframework.core.schema.SchemaClassDefinition;
038: import org.romaframework.core.schema.SchemaClassResolver;
039: import org.romaframework.core.schema.SchemaElement;
040: import org.romaframework.core.schema.SchemaField;
041: import org.romaframework.core.schema.SchemaManager;
042: import org.romaframework.core.util.DynaBean;
043: import org.romaframework.xml.config.XmlConfigActionType;
044: import org.romaframework.xml.config.XmlConfigAreaLayoutType;
045: import org.romaframework.xml.config.XmlConfigAspectActionTypeView;
046: import org.romaframework.xml.config.XmlConfigAspectClassTypeView;
047: import org.romaframework.xml.config.XmlConfigAspectFieldTypeView;
048: import org.romaframework.xml.config.XmlConfigClassType;
049: import org.romaframework.xml.config.XmlConfigFieldType;
050:
051: /**
052: * View Aspect abstract implementation. It configures the ViewAspect from Java5 and XML annotations. This is a good starting point
053: * for all View aspect implementations by extending this.
054: *
055: * @author Luca Garulli (luca.garulli@assetdata.it)
056: *
057: */
058: public abstract class ViewAspectAbstract extends
059: SelfRegistrantConfigurableAspect<String> implements ViewAspect {
060:
061: @Override
062: public void startup() {
063: // REGISTER THE VIEW DOMAIN TO SCHEMA CLASS RESOLVER
064: RomaApplicationContext.getInstance().getBean(
065: SchemaClassResolver.class).addDomainPackage(
066: Utility.getApplicationAspectPackage(aspectName()));
067: }
068:
069: @Override
070: public void configClass(SchemaClassDefinition iClass,
071: Annotation iAnnotation, XmlConfigClassType iXmlNode) {
072: DynaBean features = iClass.getFeatures(ASPECT_NAME);
073: if (features == null) {
074: // CREATE EMPTY FEATURES
075: features = new ViewClassFeatures();
076: iClass.setFeatures(ASPECT_NAME, features);
077: }
078:
079: readClassAnnotation(iAnnotation, features);
080: readClassXml(iClass, iXmlNode);
081: setClassDefaults(iClass);
082: }
083:
084: @Override
085: public void configField(SchemaField iField,
086: Annotation iFieldAnnotation, Annotation iGenericAnnotation,
087: Annotation iGetterAnnotation, XmlConfigFieldType iXmlNode) {
088: DynaBean features = iField.getFeatures(ASPECT_NAME);
089: if (features == null) {
090: // CREATE EMPTY FEATURES
091: features = new ViewFieldFeatures();
092: iField.setFeatures(ASPECT_NAME, features);
093: }
094:
095: readFieldAnnotation(iGenericAnnotation, features);
096: readFieldAnnotation(iFieldAnnotation, features);
097: readFieldAnnotation(iGetterAnnotation, features);
098: readFieldXml(iField, iXmlNode);
099: setFieldDefaults(iField);
100: }
101:
102: @Override
103: public void configAction(SchemaElement iAction,
104: Annotation iActionAnnotation,
105: Annotation iGenericAnnotation, XmlConfigActionType iXmlNode) {
106: DynaBean features = iAction.getFeatures(ASPECT_NAME);
107: if (features == null) {
108: // CREATE EMPTY FEATURES
109: features = new ViewActionFeatures();
110: iAction.setFeatures(ASPECT_NAME, features);
111: }
112:
113: readActionAnnotation(iAction, iActionAnnotation, features);
114: readActionXml(iAction, iXmlNode);
115: setActionDefaults(iAction);
116: }
117:
118: private void readClassAnnotation(Annotation iAnnotation,
119: DynaBean features) {
120: ViewClass annotation = (ViewClass) iAnnotation;
121:
122: if (annotation != null) {
123: // PROCESS ANNOTATIONS
124: if (!annotation.description().equals(
125: AnnotationConstants.DEF_VALUE))
126: features.setAttribute(ViewBaseFeatures.DESCRIPTION,
127: annotation.description());
128: if (!annotation.label().equals(
129: AnnotationConstants.DEF_VALUE))
130: features.setAttribute(ViewBaseFeatures.LABEL,
131: annotation.label());
132: if (!annotation.entity().equals(Object.class))
133: features.setAttribute(ViewClassFeatures.ENTITY,
134: annotation.entity());
135: if (!annotation.style().equals(
136: AnnotationConstants.DEF_VALUE))
137: features.setAttribute(ViewBaseFeatures.STYLE,
138: annotation.style());
139: if (!annotation.render().equals(
140: AnnotationConstants.DEF_VALUE))
141: features.setAttribute(ViewBaseFeatures.RENDER,
142: annotation.render());
143: if (!annotation.layout().equals(
144: AnnotationConstants.DEF_VALUE))
145: features.setAttribute(ViewBaseFeatures.LAYOUT,
146: annotation.layout());
147: if (annotation.explicitElements() != AnnotationConstants.UNSETTED)
148: features.setAttribute(
149: ViewClassFeatures.EXPLICIT_ELEMENTS, annotation
150: .explicitElements());
151: if (annotation.columns() != AnnotationConstants.UNSETTED)
152: features.setAttribute(ViewClassFeatures.COLUMNS,
153: annotation.columns());
154: if (!annotation.orderAreas().equals(
155: AnnotationConstants.DEF_VALUE))
156: features.setAttribute(ViewClassFeatures.ORDER_AREAS,
157: annotation.orderAreas());
158: }
159: }
160:
161: private void readClassXml(SchemaClassDefinition iClass,
162: XmlConfigClassType iXmlNode) {
163: if (iXmlNode == null || iXmlNode.getAspects() == null)
164: return;
165:
166: DynaBean features = iClass.getFeatures(ASPECT_NAME);
167:
168: XmlConfigAspectClassTypeView featureDescriptor = iXmlNode
169: .getAspects().getView();
170:
171: if (featureDescriptor != null) {
172: XmlConfigAreaLayoutType layout = featureDescriptor
173: .getForm();
174: if (layout != null && layout.getArea() != null)
175: features.setAttribute(ViewClassFeatures.FORM, layout
176: .getArea());
177:
178: // PROCESS DESCRIPTOR CFG
179: if (featureDescriptor != null) {
180: if (featureDescriptor.isSetDescription())
181: features.setAttribute(ViewBaseFeatures.DESCRIPTION,
182: featureDescriptor.getDescription());
183: if (featureDescriptor.isSetLabel())
184: features.setAttribute(ViewBaseFeatures.LABEL,
185: featureDescriptor.getLabel());
186: if (featureDescriptor.isSetEntity())
187: features.setAttribute(ViewClassFeatures.ENTITY,
188: RomaApplicationContext.getInstance()
189: .getBean(SchemaManager.class)
190: .getClassInfo(
191: featureDescriptor
192: .getEntity())
193: .getClazz());
194: if (featureDescriptor.isSetStyle())
195: features.setAttribute(ViewBaseFeatures.STYLE,
196: featureDescriptor.getStyle());
197: if (featureDescriptor.isSetRender())
198: features.setAttribute(ViewBaseFeatures.RENDER,
199: featureDescriptor.getRender());
200: if (featureDescriptor.isSetLayout())
201: features.setAttribute(ViewBaseFeatures.LAYOUT,
202: featureDescriptor.getLayout());
203: if (featureDescriptor.isSetExplicitElements())
204: features.setAttribute(
205: ViewClassFeatures.EXPLICIT_ELEMENTS,
206: featureDescriptor.getExplicitElements());
207: if (featureDescriptor.isSetColumns())
208: features.setAttribute(ViewClassFeatures.COLUMNS,
209: (int) featureDescriptor.getColumns());
210: }
211: }
212: }
213:
214: public void setClassDefaults(SchemaClassDefinition iClass) {
215: DynaBean features = iClass.getFeatures(ASPECT_NAME);
216:
217: if ((Boolean) features
218: .getAttribute(ViewClassFeatures.EXPLICIT_ELEMENTS)) {
219: // HIDE ALL INHERITED ELEMENTS
220: for (Iterator<SchemaField> itField = iClass
221: .getFieldIterator(); itField.hasNext();) {
222: itField.next().setFeature(ASPECT_NAME,
223: ViewElementFeatures.VISIBLE, false);
224: }
225: for (Iterator<SchemaAction> itAction = iClass
226: .getActionIterator(); itAction.hasNext();) {
227: itAction.next().setFeature(ASPECT_NAME,
228: ViewElementFeatures.VISIBLE, false);
229: }
230: }
231: }
232:
233: private void readFieldAnnotation(Annotation iAnnotation,
234: DynaBean iFeatures) {
235: ViewField annotation = (ViewField) iAnnotation;
236:
237: if (annotation != null) {
238: if (!annotation.label().equals(
239: AnnotationConstants.DEF_VALUE))
240: iFeatures.setAttribute(ViewBaseFeatures.LABEL,
241: annotation.label());
242: if (!annotation.description().equals(
243: AnnotationConstants.DEF_VALUE))
244: iFeatures.setAttribute(ViewBaseFeatures.DESCRIPTION,
245: annotation.description());
246: if (annotation.visible() != AnnotationConstants.UNSETTED)
247: iFeatures
248: .setAttribute(
249: ViewElementFeatures.VISIBLE,
250: annotation.visible() == AnnotationConstants.TRUE);
251: if (annotation.required() != AnnotationConstants.UNSETTED)
252: iFeatures
253: .setAttribute(
254: ViewFieldFeatures.REQUIRED,
255: annotation.required() == AnnotationConstants.TRUE);
256: if (annotation.enabled() != AnnotationConstants.UNSETTED)
257: iFeatures
258: .setAttribute(
259: ViewElementFeatures.ENABLED,
260: annotation.enabled() == AnnotationConstants.TRUE);
261: if (!annotation.style().equals(
262: AnnotationConstants.DEF_VALUE))
263: iFeatures.setAttribute(ViewBaseFeatures.STYLE,
264: annotation.style());
265: if (!annotation.render().equals(
266: AnnotationConstants.DEF_VALUE))
267: iFeatures.setAttribute(ViewBaseFeatures.RENDER,
268: annotation.render());
269: if (!annotation.layout().equals(
270: AnnotationConstants.DEF_VALUE))
271: iFeatures.setAttribute(ViewBaseFeatures.LAYOUT,
272: annotation.layout());
273: if (!annotation.match().equals(
274: AnnotationConstants.DEF_VALUE))
275: iFeatures.setAttribute(ViewFieldFeatures.MATCH,
276: annotation.match());
277: if (annotation.min() != ViewField.DEF_MIN)
278: iFeatures.setAttribute(ViewFieldFeatures.MIN,
279: annotation.min());
280: if (annotation.max() != ViewField.DEF_MAX)
281: iFeatures.setAttribute(ViewFieldFeatures.MAX,
282: annotation.max());
283: if (!annotation.selectionField().equals(
284: AnnotationConstants.DEF_VALUE))
285: iFeatures.setAttribute(
286: ViewFieldFeatures.SELECTION_FIELD, annotation
287: .selectionField());
288: if (annotation.selectionMode() != AnnotationConstants.UNSETTED)
289: iFeatures.setAttribute(
290: ViewFieldFeatures.SELECTION_MODE, annotation
291: .selectionMode());
292: }
293: }
294:
295: private void readFieldXml(SchemaField iField,
296: XmlConfigFieldType iXmlNode) {
297: // PROCESS DESCRIPTOR CFG
298: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
299: // VALUES
300: if (iXmlNode == null)
301: return;
302:
303: DynaBean features = iField.getFeatures(ASPECT_NAME);
304:
305: // FIELD FOUND IN DESCRIPTOR: ASSUME ITS VISIBILITY
306: features.setAttribute(ViewElementFeatures.VISIBLE, true);
307:
308: if (iXmlNode.getAspects() == null)
309: return;
310:
311: XmlConfigAspectFieldTypeView descriptor = iXmlNode.getAspects()
312: .getView();
313:
314: if (descriptor != null) {
315: if (descriptor.getEmbeddedType() != null) {
316: iField.setEmbeddedType((RomaApplicationContext
317: .getInstance().getBean(
318: SchemaClassResolver.class)
319: .getEntityClass(descriptor.getEmbeddedType())));
320: }
321:
322: XmlConfigAspectFieldTypeView featureDescriptor = descriptor;
323:
324: if (featureDescriptor.isSetLabel())
325: features.setAttribute(ViewBaseFeatures.LABEL,
326: featureDescriptor.getLabel());
327: if (featureDescriptor.isSetDescription())
328: features.setAttribute(ViewBaseFeatures.DESCRIPTION,
329: featureDescriptor.getDescription());
330: if (featureDescriptor.isSetVisible())
331: features.setAttribute(ViewElementFeatures.VISIBLE,
332: featureDescriptor.getVisible());
333: if (featureDescriptor.isSetRequired())
334: features.setAttribute(ViewFieldFeatures.REQUIRED,
335: featureDescriptor.getRequired());
336: if (featureDescriptor.isSetEnabled())
337: features.setAttribute(ViewElementFeatures.ENABLED,
338: featureDescriptor.getEnabled());
339: if (featureDescriptor.isSetStyle())
340: features.setAttribute(ViewBaseFeatures.STYLE,
341: featureDescriptor.getStyle());
342: if (featureDescriptor.isSetRender())
343: features.setAttribute(ViewBaseFeatures.RENDER,
344: featureDescriptor.getRender());
345: if (featureDescriptor.isSetLayout())
346: features.setAttribute(ViewBaseFeatures.LAYOUT,
347: featureDescriptor.getLayout());
348: if (featureDescriptor.isSetMatch())
349: features.setAttribute(ViewFieldFeatures.MATCH,
350: featureDescriptor.getMatch());
351: if (featureDescriptor.isSetMin())
352: features.setAttribute(ViewFieldFeatures.MIN,
353: featureDescriptor.getMin());
354: if (featureDescriptor.isSetMax())
355: features.setAttribute(ViewFieldFeatures.MAX,
356: featureDescriptor.getMax());
357: if (featureDescriptor.isSetSelectionField())
358: features.setAttribute(
359: ViewFieldFeatures.SELECTION_FIELD,
360: featureDescriptor.getSelectionField());
361: if (featureDescriptor.isSetSelectionMode()) {
362: String mode = featureDescriptor.getSelectionMode()
363: .toString();
364: features
365: .setAttribute(
366: ViewFieldFeatures.SELECTION_MODE,
367: mode.equals("index") ? ViewFieldFeatures.SELECTION_MODE_INDEX
368: : ViewFieldFeatures.SELECTION_MODE_VALUE);
369: }
370: }
371: }
372:
373: public void setFieldDefaults(SchemaField iField) {
374: DynaBean features = iField.getFeatures(ASPECT_NAME);
375:
376: // CHECK RENDER AND LAYOUT MODES
377: String classRender = (String) iField.getEntity().getFeature(
378: ASPECT_NAME, ViewBaseFeatures.RENDER);
379:
380: if ((Boolean) iField.getFeature(CoreAspect.ASPECT_NAME,
381: CoreFieldFeatures.EMBEDDED)) {
382: if (iField.getFeature(ViewAspectAbstract.ASPECT_NAME,
383: ViewBaseFeatures.RENDER) == null)
384: // IF THE FIELD IS EMBEDDED, THEN THE DEFAULT RENDER IS OBJECTEMBEDDED
385: iField.setFeature(ViewAspectAbstract.ASPECT_NAME,
386: ViewBaseFeatures.RENDER,
387: ViewConstants.RENDER_OBJECTEMBEDDED);
388: }
389:
390: // if (iField.getFeature(ASPECT_NAME, ViewClassFeatures.LAYOUT) != null) {
391: // iField.setFeature(CoreAspect.ASPECT_NAME, CoreFieldFeatures.EMBEDDED, Boolean.TRUE);
392: // }
393:
394: Boolean enabled = (Boolean) features
395: .getAttribute(ViewElementFeatures.ENABLED);
396: if (enabled == null)
397: features.setAttribute(ViewElementFeatures.ENABLED,
398: Boolean.TRUE);
399:
400: if (classRender != null)
401: if (classRender.equals(ViewConstants.RENDER_MENU)) {
402: // INSIDE A MENU: FORCE MENU RENDERING AND LAYOUT
403: features.setAttribute(ViewBaseFeatures.RENDER,
404: ViewConstants.RENDER_MENU);
405: features.setAttribute(ViewBaseFeatures.LAYOUT,
406: ViewConstants.LAYOUT_MENU);
407: } else if (classRender
408: .equals(ViewConstants.RENDER_ACCORDION)) {
409: // INSIDE AN ACCORDITION: FORCE ACCORDITION LAYOUT
410: features.setAttribute(ViewBaseFeatures.RENDER,
411: ViewConstants.RENDER_ACCORDION);
412: features.setAttribute(ViewBaseFeatures.LAYOUT,
413: ViewConstants.LAYOUT_ACCORDION);
414: }
415: }
416:
417: private void readActionAnnotation(SchemaElement iAction,
418: Annotation iAnnotation, DynaBean features) {
419: ViewAction annotation = (ViewAction) iAnnotation;
420:
421: features.setAttribute(ViewActionFeatures.ACTION_NAME, iAction
422: .getName());
423:
424: if (annotation != null) {
425: // PROCESS ANNOTATIONS
426: // ANNOTATION ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT VALUES
427: if (annotation != null) {
428: if (!annotation.label().equals(
429: AnnotationConstants.DEF_VALUE))
430: features.setAttribute(ViewBaseFeatures.LABEL,
431: annotation.label());
432: if (!annotation.description().equals(
433: AnnotationConstants.DEF_VALUE))
434: features.setAttribute(ViewBaseFeatures.DESCRIPTION,
435: annotation.description());
436: if (!annotation.actionName().equals(
437: AnnotationConstants.DEF_VALUE))
438: features.setAttribute(
439: ViewActionFeatures.ACTION_NAME, annotation
440: .actionName());
441: if (annotation.visible() != AnnotationConstants.UNSETTED)
442: features
443: .setAttribute(
444: ViewElementFeatures.VISIBLE,
445: annotation.visible() == AnnotationConstants.TRUE);
446: if (!annotation.style().equals(
447: AnnotationConstants.DEF_VALUE))
448: features.setAttribute(ViewBaseFeatures.STYLE,
449: annotation.style());
450: if (!annotation.render().equals(
451: AnnotationConstants.DEF_VALUE))
452: features.setAttribute(ViewBaseFeatures.RENDER,
453: annotation.render());
454: if (!annotation.layout().equals(
455: AnnotationConstants.DEF_VALUE))
456: features.setAttribute(ViewBaseFeatures.LAYOUT,
457: annotation.layout());
458: if (annotation.bind() != AnnotationConstants.UNSETTED)
459: features
460: .setAttribute(
461: ViewActionFeatures.BIND,
462: annotation.bind() == AnnotationConstants.TRUE);
463: if (annotation.enabled() != AnnotationConstants.UNSETTED)
464: features
465: .setAttribute(
466: ViewElementFeatures.ENABLED,
467: annotation.enabled() == AnnotationConstants.TRUE);
468: if (annotation.validation() != AnnotationConstants.UNSETTED)
469: features
470: .setAttribute(
471: ViewActionFeatures.VALIDATION,
472: annotation.validation() == AnnotationConstants.TRUE);
473: }
474: }
475: }
476:
477: private void readActionXml(SchemaElement iAction,
478: XmlConfigActionType iXmlNode) {
479: // PROCESS DESCRIPTOR CFG
480: // DESCRIPTOR ATTRIBUTES (IF DEFINED) OVERWRITE DEFAULT AND ANNOTATION
481: // VALUES
482: if (iXmlNode == null)
483: return;
484:
485: DynaBean features = iAction.getFeatures(ASPECT_NAME);
486:
487: // ACTION FOUND IN DESCRIPTOR: ASSUME ITS VISIBILITY
488: features.setAttribute(ViewElementFeatures.VISIBLE, true);
489:
490: if (iXmlNode.getAspects() == null)
491: return;
492:
493: XmlConfigAspectActionTypeView descriptor = iXmlNode
494: .getAspects().getView();
495:
496: if (descriptor != null) {
497: if (descriptor.isSetLabel())
498: features.setAttribute(ViewBaseFeatures.LABEL,
499: descriptor.getLabel());
500: if (descriptor.isSetDescription())
501: features.setAttribute(ViewBaseFeatures.DESCRIPTION,
502: descriptor.getDescription());
503: if (descriptor.isSetVisible())
504: features.setAttribute(ViewElementFeatures.VISIBLE,
505: descriptor.getVisible());
506: if (descriptor.isSetStyle())
507: features.setAttribute(ViewBaseFeatures.STYLE,
508: descriptor.getStyle());
509: if (descriptor.isSetRender())
510: features.setAttribute(ViewBaseFeatures.RENDER,
511: descriptor.getRender());
512: if (descriptor.isSetLayout())
513: features.setAttribute(ViewBaseFeatures.LAYOUT,
514: descriptor.getLayout());
515: if (descriptor.isSetBind())
516: features.setAttribute(ViewActionFeatures.BIND,
517: descriptor.getBind());
518: if (descriptor.isSetEnabled())
519: features.setAttribute(ViewElementFeatures.ENABLED,
520: descriptor.getEnabled());
521: if (descriptor.isSetValidation())
522: features.setAttribute(ViewActionFeatures.VALIDATION,
523: descriptor.getValidation());
524: }
525: }
526:
527: public void setActionDefaults(SchemaElement iAction) {
528: DynaBean features = iAction.getFeatures(ASPECT_NAME);
529:
530: // CHECK RENDER AND LAYOUT MODES
531: String classRender = (String) iAction.getEntity().getFeature(
532: ASPECT_NAME, ViewBaseFeatures.RENDER);
533:
534: if (classRender != null)
535: if (classRender.equals(ViewConstants.RENDER_MENU)) {
536: // INSIDE A MENU: FORCE MENU RENDERING AND LAYOUT
537: features.setAttribute(ViewBaseFeatures.RENDER,
538: ViewConstants.RENDER_MENU);
539: features.setAttribute(ViewBaseFeatures.LAYOUT,
540: ViewConstants.LAYOUT_MENU);
541: } else if (classRender
542: .equals(ViewConstants.RENDER_ACCORDION))
543: features.setAttribute(ViewBaseFeatures.LAYOUT,
544: ViewConstants.LAYOUT_ACCORDION);
545: }
546:
547: public String aspectName() {
548: return ASPECT_NAME;
549: }
550: }
|