001: /*
002: *
003: * Copyright 2007 Luca Molino (luca.molino@assetdata.it)
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.romaframework.core.domain.binding;
019:
020: import java.util.Collection;
021:
022: import org.romaframework.aspect.core.annotation.AnnotationConstants;
023: import org.romaframework.aspect.view.ViewConstants;
024: import org.romaframework.aspect.view.annotation.ViewField;
025: import org.romaframework.aspect.view.page.Page;
026: import org.romaframework.core.binding.Bindable;
027: import org.romaframework.core.flow.ObjectContext;
028: import org.romaframework.core.schema.SchemaClass;
029: import org.romaframework.core.schema.SchemaField;
030: import org.romaframework.core.schema.SchemaHelper;
031: import org.romaframework.core.schema.SchemaManager;
032: import org.romaframework.module.crud.CRUDException;
033:
034: /**
035: * @author Luca Molino
036: *
037: */
038: public class CollectionBinder<T> extends Page implements Bindable {
039:
040: private Object sourceObject;
041:
042: private SchemaField sourceField;
043:
044: @ViewField(label="",render=ViewConstants.RENDER_SELECT,selectionField="selection")
045: private Collection collection;
046:
047: @ViewField(visible=AnnotationConstants.FALSE)
048: private T[] selection;
049:
050: public CollectionBinder() {
051: }
052:
053: public CollectionBinder(Object iBackObject) {
054: setBackObject(iBackObject);
055: }
056:
057: public CollectionBinder(Collection<? extends Object> iCollection) {
058: this (null, iCollection);
059: }
060:
061: public CollectionBinder(Object iBackObject,
062: Collection<? extends Object> iCollection) {
063: this (iBackObject);
064: collection = iCollection;
065: }
066:
067: @ViewField(visible=AnnotationConstants.FALSE)
068: public SchemaField getSourceField() {
069: return sourceField;
070: }
071:
072: @ViewField(visible=AnnotationConstants.FALSE)
073: public Object getSourceObject() {
074: return sourceObject;
075: }
076:
077: public void setSource(Object iSourceObject, String iSourceFieldName) {
078: sourceObject = iSourceObject;
079:
080: // ASSIGN OBJECT SCHEMA FIELD
081: SchemaClass cls = ObjectContext.getInstance().getComponent(
082: SchemaManager.class).getClassInfo(
083: iSourceObject.getClass().getSimpleName());
084: sourceField = cls.getField(iSourceFieldName);
085:
086: if (sourceField == null)
087: throw new CRUDException("Cannot find field name "
088: + iSourceObject.getClass().getSimpleName() + "."
089: + iSourceFieldName + ". Check class definition");
090: }
091:
092: public Collection getCollection() {
093: return collection;
094: }
095:
096: public void setCollection(Collection collection) {
097: this .collection = collection;
098: }
099:
100: public T[] getSelection() {
101: return selection;
102: }
103:
104: public void setSelection(T[] selection) {
105: this .selection = selection;
106: }
107:
108: public void add() {
109: addAndForceClosing(false);
110: }
111:
112: public void addAndForceClosing(boolean iForceClosign) {
113: SchemaHelper.insertElements(sourceField, sourceObject,
114: selection);
115: if (!iForceClosign && getBackObject() != null) {
116: ObjectContext.getInstance().show(getBackObject());
117: } else {
118: ObjectContext.getInstance().close(this);
119: }
120: }
121:
122: }
|