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.module.crud;
018:
019: import org.romaframework.aspect.core.annotation.AnnotationConstants;
020: import org.romaframework.aspect.core.annotation.CoreClass;
021: import org.romaframework.aspect.persistence.PersistenceAspect;
022: import org.romaframework.aspect.persistence.PersistenceConstants;
023: import org.romaframework.aspect.persistence.annotation.Persistence;
024: import org.romaframework.aspect.reporting.ReportingAspect;
025: import org.romaframework.aspect.view.ViewAspect;
026: import org.romaframework.aspect.view.ViewCallback;
027: import org.romaframework.aspect.view.ViewHelper;
028: import org.romaframework.aspect.view.annotation.ViewAction;
029: import org.romaframework.aspect.view.annotation.ViewClass;
030: import org.romaframework.aspect.view.annotation.ViewField;
031: import org.romaframework.aspect.view.feature.ViewActionFeatures;
032: import org.romaframework.aspect.view.feature.ViewElementFeatures;
033: import org.romaframework.core.config.Refreshable;
034: import org.romaframework.core.domain.reporting.ReportGenerator;
035: import org.romaframework.core.flow.ObjectContext;
036:
037: /**
038: * Page to handle Create, Modify and View sides of CRUD.
039: *
040: * @author Luca Garulli (luca.garulli@assetdata.it)
041: * @param <T>
042: * Domain Object to handle
043: */
044: @CoreClass(orderActions="save back")
045: @ViewClass(label="")
046: public abstract class CRUDInstance<T> extends CRUDEntity<T> implements
047: ViewCallback, CRUDWorkingMode {
048:
049: @ViewField(visible=AnnotationConstants.FALSE)
050: private int mode;
051:
052: public static final int MODE_CREATE = 0;
053: public static final int MODE_READ = 1;
054: public static final int MODE_UPDATE = 2;
055:
056: public CRUDInstance() {
057: super (null);
058: }
059:
060: public CRUDInstance(T iEntity, Object iBackObject) {
061: super (iEntity);
062: setBackObject(iBackObject);
063: }
064:
065: public void onShow() {
066:
067: if (!ObjectContext.getInstance().existComponent(
068: ReportingAspect.class.getSimpleName()))
069: ObjectContext.getInstance().setActionFeature(this ,
070: ViewAspect.ASPECT_NAME, "report",
071: ViewActionFeatures.VISIBLE, Boolean.FALSE);
072:
073: if (mode == MODE_READ) {
074: // DISABLE ALL FIELDS BY DEFAULT
075: ViewHelper.disableFields(this );
076: }
077:
078: // HIDE/SHOW THE SAVE BUTTON
079: ObjectContext.getInstance().setActionFeature(this ,
080: ViewAspect.ASPECT_NAME, "save",
081: ViewElementFeatures.VISIBLE, mode != MODE_READ);
082: }
083:
084: public void onDispose() {
085: }
086:
087: /**
088: * Overwrite this method to catch the event before to display a CRUD instance in CREATE mode.
089: */
090: public void onCreate() {
091: }
092:
093: /**
094: * Overwrite this method to catch the event before to display a CRUD instance in READ mode.
095: */
096: public void onRead() {
097: }
098:
099: /**
100: * Overwrite this method to catch the event before to display a CRUD instance in UPDATE mode.
101: */
102: public void onUpdate() {
103: }
104:
105: @ViewAction(validation=AnnotationConstants.TRUE)
106: @Persistence(mode=PersistenceConstants.MODE_TX)
107: public void save() {
108: switch (mode) {
109: case MODE_CREATE:
110: if (getSourceObject() == null)
111: ObjectContext.getInstance().getContextComponent(
112: PersistenceAspect.class).createObject(
113: getEntity());
114: break;
115:
116: case MODE_UPDATE:
117: if (getSourceObject() == null)
118: ObjectContext.getInstance().getContextComponent(
119: PersistenceAspect.class).updateObject(
120: getEntity());
121: break;
122:
123: default:
124: return;
125: }
126:
127: bindValue();
128:
129: Object backForm = getBackObject();
130: if (backForm != null) {
131: if (backForm instanceof CRUDSelect) {
132: // CALLER FORM IS A CRUD SELECT: SELECT THE CREATED ELEMENT AND
133: // BIND INTO CALLER FORM (INVOKE SELECTION)
134: ((CRUDSelect) backForm)
135: .setSelection(new Object[] { getEntity() });
136: ((CRUDSelect) backForm).selectAndForceClosing(true);
137: return;
138: } else if (backForm instanceof Refreshable) {
139: // RE-EXECUTE THE SEARCH
140: ((Refreshable) backForm).refresh();
141: }
142: }
143: close();
144: }
145:
146: public int getMode() {
147: return mode;
148: }
149:
150: public void setMode(int mode) {
151: this .mode = mode;
152: }
153:
154: /**
155: * Generate a report for the current view using the ReportingAspect.
156: */
157: public void report() {
158: ReportGenerator form = new ReportGenerator(this , this
159: .getClass().getSimpleName());
160: ObjectContext.getInstance().show(form);
161: }
162:
163: protected PersistenceAspect getPersistenceAspect() {
164: return ObjectContext.getInstance().getContextComponent(
165: PersistenceAspect.class);
166: }
167:
168: }
|