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.module.crud;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
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.core.binding.Bindable;
029: import org.romaframework.core.entity.ComposedEntity;
030: import org.romaframework.core.flow.ObjectContext;
031: import org.romaframework.core.schema.SchemaClass;
032: import org.romaframework.core.schema.SchemaField;
033: import org.romaframework.core.schema.SchemaHelper;
034: import org.romaframework.core.schema.SchemaManager;
035:
036: /**
037: * Like CRUDMain is an entrypoint for the CRUD but to select instances to be bound on calling page.
038: *
039: * @author Luca Garulli (luca.garulli@assetdata.it)
040: * @param <T>
041: * ComposedEntity class used to display the result in the table.
042: */
043: @CoreClass(orderActions="select cancel search create read update delete selectAll deselectAll")
044: public abstract class CRUDSelect<T extends ComposedEntity<?>> extends
045: CRUDMain<T> implements Bindable {
046:
047: private SchemaField sourceField;
048:
049: private Object sourceObject;
050:
051: protected static Log log = LogFactory.getLog(CRUDSelect.class);
052:
053: public static final int DEF_PAGE_ELEMENTS = 5;
054:
055: public CRUDSelect(Class<? extends ComposedEntity<?>> iListClass,
056: Class<?> iCreateClass, Class<?> iReadClass,
057: Class<?> iEditClass) {
058: super (iListClass, iCreateClass, iReadClass, iEditClass);
059: setPageElements(DEF_PAGE_ELEMENTS);
060: }
061:
062: public void setSource(Object iSourceObject, String iSourceFieldName) {
063: sourceObject = iSourceObject;
064:
065: // ASSIGN OBJECT SCHEMA FIELD
066: SchemaClass cls = ObjectContext.getInstance().getComponent(
067: SchemaManager.class).getClassInfo(
068: iSourceObject.getClass().getSimpleName());
069: sourceField = cls.getField(iSourceFieldName);
070:
071: if (sourceField == null)
072: throw new CRUDException("Cannot find field name "
073: + iSourceObject.getClass().getSimpleName() + "."
074: + iSourceFieldName + ". Check class definition");
075: }
076:
077: @ViewField(visible=AnnotationConstants.FALSE)
078: public Object getSourceObject() {
079: return sourceObject;
080: }
081:
082: @ViewField(visible=AnnotationConstants.FALSE)
083: public SchemaField getSourceField() {
084: return sourceField;
085: }
086:
087: @Persistence(mode=PersistenceConstants.MODE_ATOMIC)
088: public void select() {
089: selectAndForceClosing(false);
090: }
091:
092: public void selectAndForceClosing(boolean iForceWindowClosing) {
093: Object[] fullSelection = getSelection();
094:
095: if (fullSelection != null)
096: for (int i = 0; i < fullSelection.length; i++) {
097: Object o;
098: if (fullSelection[i] instanceof ComposedEntity)
099: o = ((ComposedEntity<?>) fullSelection[i])
100: .getEntity();
101: else
102: o = fullSelection[i];
103: fullSelection[i] = ObjectContext.getInstance()
104: .getContextComponent(PersistenceAspect.class)
105: .loadObject(o,
106: PersistenceAspect.FULL_MODE_LOADING,
107: PersistenceAspect.STRATEGY_DETACHING);
108: if (fullSelection[i] == null) {
109: if (o != null)
110: throw new PersistenceException(
111: "Error on detaching object " + o
112: + ". Check if Class '"
113: + o.getClass()
114: + "' is detachable");
115: else
116: throw new PersistenceException(
117: "Error on detaching selected object.");
118: }
119: }
120:
121: SchemaHelper.insertElements(sourceField, sourceObject,
122: fullSelection);
123:
124: if (!iForceWindowClosing && getBackObject() != null)
125: ObjectContext.getInstance().show(getBackObject());
126: else
127: ObjectContext.getInstance().close(this );
128: }
129:
130: public void cancel() {
131: if (getBackObject() != null)
132: ObjectContext.getInstance().show(getBackObject());
133: else
134: ObjectContext.getInstance().close(this);
135: }
136: }
|