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 java.util.List;
020:
021: import org.romaframework.aspect.core.annotation.AnnotationConstants;
022: import org.romaframework.aspect.core.annotation.CoreClass;
023: import org.romaframework.aspect.persistence.PersistenceAspect;
024: import org.romaframework.aspect.persistence.PersistenceConstants;
025: import org.romaframework.aspect.persistence.PersistenceException;
026: import org.romaframework.aspect.persistence.annotation.Persistence;
027: import org.romaframework.aspect.view.annotation.ViewField;
028: import org.romaframework.aspect.view.form.ContentComponent;
029: import org.romaframework.aspect.view.form.ContentForm;
030: import org.romaframework.aspect.view.page.EntityPage;
031: import org.romaframework.core.binding.Bindable;
032: import org.romaframework.core.entity.ComposedEntity;
033: import org.romaframework.core.flow.ObjectContext;
034: import org.romaframework.core.schema.SchemaClass;
035: import org.romaframework.core.schema.SchemaField;
036: import org.romaframework.core.schema.SchemaHelper;
037: import org.romaframework.core.schema.SchemaManager;
038:
039: /**
040: * Base class to handle CRUDInstances.
041: *
042: * @author Luca Garulli (luca.garulli@assetdata.it)
043: * @param <T>
044: * Domain Object to handle
045: */
046: @CoreClass(orderActions="cancel")
047: public class CRUDEntity<T> extends EntityPage<T> implements Bindable {
048:
049: /**
050: * Create a CRUDEntity base object.
051: *
052: * @param iEntity
053: * Inherited entity
054: * @param iBackObject
055: * The object to call on back action
056: */
057: public CRUDEntity(T iEntity) {
058: super (iEntity);
059: }
060:
061: @Persistence(mode=PersistenceConstants.MODE_ATOMIC)
062: public void cancel() {
063: Object backObj = getBackObject();
064:
065: if (backObj != null && backObj instanceof CRUDMain) {
066: // REPLACE OBJECT IN THE SOURCE COLLECTION
067: List<ComposedEntity> result = ((CRUDMain) backObj)
068: .getResult();
069: int pos = -1;
070:
071: // SEARCH THE OBJECT INSIDE THE CONTAINER INSTANCE
072: for (int i = 0; i < result.size(); ++i) {
073: if (result.get(i).getEntity().equals(getEntity())) {
074: // FOUND!
075: pos = i;
076: break;
077: }
078: }
079:
080: if (pos != -1) {
081: try {
082: PersistenceAspect db = ObjectContext.getInstance()
083: .getContextComponent(
084: PersistenceAspect.class);
085: T reloadedObj = db.loadObject(getEntity(), null,
086: PersistenceAspect.STRATEGY_DETACHING);
087:
088: if (reloadedObj != null)
089: result.get(pos).setEntity(reloadedObj);
090: } catch (PersistenceException e) {
091: }
092: }
093: }
094:
095: close();
096: }
097:
098: /**
099: * Close the window or go back.
100: */
101: protected void close() {
102: close(false);
103: }
104:
105: /**
106: * Force window closing.
107: *
108: * @param iForceWindowClose
109: */
110: protected void close(boolean iForceWindowClose) {
111: entity = null;
112:
113: if (iForceWindowClose) {
114: ObjectContext.getInstance().close(this );
115: return;
116: }
117:
118: Object backForm = getBackObject();
119: if (backForm != null) {
120: ContentForm currentComponent = (ContentForm) ObjectContext
121: .getInstance().getFormByObject(this );
122: if (currentComponent != null
123: && currentComponent.isFirstToOpenPopup(this ))
124: ObjectContext.getInstance().close(this );
125: else
126: back();
127: } else
128: ObjectContext.getInstance().close(this );
129: }
130:
131: public void setSource(Object iSourceObject, String iSourceFieldName) {
132: if (iSourceFieldName.contains(".")) {
133: // GET NESTED SOURCE OBJECT
134: sourceObject = SchemaHelper.getFieldObject(iSourceObject,
135: iSourceFieldName);
136: } else
137: sourceObject = iSourceObject;
138:
139: if (iSourceObject != null) {
140: // ASSIGN OBJECT SCHEMA FIELD
141: SchemaClass cls = ObjectContext.getInstance().getComponent(
142: SchemaManager.class).getClassInfo(
143: iSourceObject.getClass().getSimpleName());
144: sourceField = cls.getField(iSourceFieldName);
145: }
146: }
147:
148: @ViewField(visible=AnnotationConstants.FALSE)
149: public Object getSourceObject() {
150: return sourceObject;
151: }
152:
153: @ViewField(visible=AnnotationConstants.FALSE)
154: public SchemaField getSourceField() {
155: return sourceField;
156: }
157:
158: protected void bindValue() {
159: if (getSourceObject() != null) {
160: // GET THE CALLER FORM: TRY TO SEE IF A SPECIFIC FIELD FORM THERE IS,
161: // OTHERWISE GET THE OBJECT FORM
162: Object fieldValue = SchemaHelper.getFieldValue(
163: getSourceField(), getSourceObject());
164:
165: ContentComponent callerForm = ObjectContext.getInstance()
166: .getFormByObject(
167: fieldValue != null ? fieldValue
168: : getSourceObject());
169:
170: // BIND NEW OBJECT CREATED
171: callerForm.bind(getSourceField(), getEntity());
172: }
173: }
174:
175: protected SchemaField sourceField;
176:
177: protected Object sourceObject;
178: }
|