001: /*
002: * Copyright 2007 Giordano Maestro (giordano.maestro@assetdata.it)
003: *
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License.
007: *
008: * You may obtain a copy of the License at
009: * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
010: * or agreed to in writing, software distributed under the License is
011: * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
012: * KIND, either express or implied. See the License for the specific language
013: * governing permissions and limitations under the License.
014: */
015: package org.romaframework.module.designer.view.domain.radpage.aspect;
016:
017: import java.util.Collection;
018:
019: import org.romaframework.aspect.core.CoreAspect;
020: import org.romaframework.aspect.core.annotation.AnnotationConstants;
021: import org.romaframework.aspect.view.annotation.ViewClass;
022: import org.romaframework.aspect.view.annotation.ViewField;
023: import org.romaframework.aspect.view.page.CallerHandler;
024: import org.romaframework.aspect.view.page.EntityPage;
025: import org.romaframework.core.aspect.Aspect;
026: import org.romaframework.core.aspect.AspectManager;
027: import org.romaframework.core.exception.ConfigurationNotFoundException;
028: import org.romaframework.core.flow.ObjectContext;
029: import org.romaframework.core.schema.SchemaClass;
030: import org.romaframework.core.schema.SchemaFeatures;
031: import org.romaframework.core.schema.SchemaManager;
032: import org.romaframework.module.designer.DesignerException;
033: import org.romaframework.module.designer.view.domain.DesignerContainerPage;
034: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
035:
036: /**
037: * This is the page used for the configuration of an element. It register all
038: * the aspects defined for the element searching for the pages
039: * <aspect-name>AspectPage.
040: *
041: * @author Giordano Maestro(giordano.maestro@assetdata.it) 09/nov/07
042: *
043: */
044: @ViewClass(layout="screen://body")
045: public abstract class AspectsPage extends DesignerContainerPage
046: implements CallerHandler {
047:
048: @ViewField(visible=AnnotationConstants.FALSE)
049: private Object backObject;
050:
051: public AspectsPage() {
052: }
053:
054: public abstract void ok() throws Exception;
055:
056: public abstract void cancel() throws Exception;
057:
058: public Object getBackObject() {
059: return backObject;
060: }
061:
062: public void setBackObject(Object backObject) {
063: this .backObject = backObject;
064: }
065:
066: /**
067: * Check if the aspect name is imported in the application.
068: *
069: * @param iAspectClass
070: * @return
071: * @throws DesignerException
072: */
073: protected boolean isModuleDefined(Class iAspectClass)
074: throws DesignerException {
075: try {
076: ObjectContext.getInstance().getComponent(
077: firstToUppercase(getAspectName(iAspectClass))
078: + "Aspect");
079: } catch (NoSuchBeanDefinitionException ex) {
080: return false;
081: }
082: return true;
083: }
084:
085: /**
086: * Search for the aspects pages with the convetion <aspect-name>AspectPage and
087: * add it to the current element page.
088: *
089: * @param iFeatures
090: */
091: protected void addRegisteredAspects(SchemaFeatures iFeatures) {
092: AspectManager manager = AspectManager.getInstance();
093: Collection<Aspect> aspects = manager.getConfigurationValues();
094: for (Aspect aspect : aspects) {
095: String pageName = null;
096: try {
097: String aspectName = getAspectName(aspect.getClass());
098: pageName = aspectName + "AspectPage";
099: SchemaManager schemaManager = ObjectContext
100: .getInstance()
101: .getComponent(SchemaManager.class);
102: SchemaClass schemaClass;
103: try {
104: schemaClass = schemaManager
105: .getClassInfo(firstToUppercase(pageName));
106: } catch (ConfigurationNotFoundException ex) {
107: schemaClass = null;
108: }
109: if (schemaClass != null) {
110: Class iAspectPageClass = schemaClass.getClazz();
111: addAspectPage(iAspectPageClass, aspect.getClass(),
112: iFeatures);
113: }
114: } catch (DesignerException e) {
115:
116: }
117: }
118: }
119:
120: /**
121: * Add the aspect page to the current page.
122: * @param iAspectPageClass The class of the aspect page
123: * @param iAspectClass The aspect Class
124: * @param iFeatures The features of the element
125: * @throws DesignerException
126: */
127: private void addAspectPage(Class iAspectPageClass,
128: Class iAspectClass, SchemaFeatures iFeatures)
129: throws DesignerException {
130: Object page = null;
131: try {
132: if (isModuleDefined(iAspectClass)
133: && !iAspectClass.equals(CoreAspect.class)
134: && iFeatures
135: .getFeatures(getAspectName(iAspectClass)) != null) {
136: Class[] contructorPageParameters = new Class[1];
137: contructorPageParameters[0] = SchemaFeatures.class;
138: Object[] paramsInstance = new Object[1];
139: paramsInstance[0] = iFeatures;
140: page = iAspectPageClass.getConstructor(
141: contructorPageParameters).newInstance(
142: paramsInstance);
143: addPage(page);
144: }
145: } catch (Exception e) {
146: log.error("Error adding aspect page " + page
147: + e.getMessage(), e);
148: throw new DesignerException("Cannot create aspect page: "
149: + e.getMessage(), e);
150: }
151: }
152:
153: /**
154: * Return the aspect name of the Aspect
155: * @param iAspectClass
156: * @return
157: * @throws DesignerException
158: */
159: private String getAspectName(Class iAspectClass)
160: throws DesignerException {
161: try {
162: return (String) iAspectClass.getField("ASPECT_NAME").get(
163: null);
164: } catch (Exception e) {
165: throw new DesignerException("Cannot get aspect name for "
166: + iAspectClass.getSimpleName() + ": "
167: + e.getMessage(), e.getCause());
168: }
169: }
170:
171: private String firstToUppercase(String s) {
172: return s.substring(0, 1).toUpperCase() + s.substring(1);
173: }
174: }
|