001: /*
002: * Copyright 2007 Luigi Dell'Aquila (luigi.dellaquila@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: package org.romaframework.core.domain.query;
017:
018: import java.util.ArrayList;
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.QueryByFilter;
024: import org.romaframework.aspect.view.ViewConstants;
025: import org.romaframework.aspect.view.annotation.ViewAction;
026: import org.romaframework.aspect.view.annotation.ViewField;
027: import org.romaframework.core.command.CommandContext;
028: import org.romaframework.core.config.Refreshable;
029: import org.romaframework.core.config.RomaApplicationContext;
030: import org.romaframework.core.flow.ObjectContext;
031: import org.romaframework.core.schema.SchemaClassResolver;
032:
033: @CoreClass(orderFields="classesList filters",orderActions="ok cancel")
034: public class VisualQueryComposer implements Refreshable {
035:
036: /////////////CONSTANTS/////////////////////
037:
038: public final static String CONTEXT_MARKER = "#";
039:
040: /////////////FIELDS/////////////////////
041:
042: private List<ElementEventListener> listeners = new ArrayList<ElementEventListener>();
043:
044: @ViewField(visible=AnnotationConstants.FALSE)
045: private Class<?> targetClass;
046:
047: @ViewField(render=ViewConstants.RENDER_SELECT,selectionField="targetClass",label="",layout="form:classCombo")
048: private List<Class<?>> classesList = ObjectContext.getInstance()
049: .getComponent(SchemaClassResolver.class)
050: .getEntityClassesByPackage(
051: RomaApplicationContext.getInstance()
052: .getApplicationPackage()
053: + ".domain");
054:
055: @ViewField(visible=AnnotationConstants.FALSE)
056: private VisualQueryFilter[] filterSelection;
057: @ViewField(render=ViewConstants.RENDER_TABLEEDIT,label="",selectionField="filterSelection",layout="form:filters")
058: private List<VisualQueryFilter> filters = new ArrayList<VisualQueryFilter>();
059:
060: private CommandContext context;
061:
062: /////////////CONSTRUCTORS/////////////////////
063:
064: public VisualQueryComposer() {
065: this (null);
066: }
067:
068: public VisualQueryComposer(Class<?> targetClass) {
069: this .targetClass = targetClass;
070: }
071:
072: /////////////METHODS/////////////////////
073:
074: public List<VisualQueryFilter> getFilters() {
075: return filters;
076: }
077:
078: @ViewAction(visible=AnnotationConstants.FALSE)
079: public QueryByFilter query() {
080: if (this .targetClass == null)
081: return null;
082: QueryByFilter query = new QueryByFilter(targetClass);
083: for (VisualQueryFilter filter : filters) {
084: if (filter.getFieldName() == null)
085: continue;
086: if (filter.getOperator() == null)
087: continue;
088:
089: Object fieldValue = filter.getFieldValue();
090: if ((fieldValue instanceof String)
091: && fieldValue.toString().startsWith(CONTEXT_MARKER))
092: fieldValue = context.getParameter(fieldValue.toString()
093: .substring(CONTEXT_MARKER.length()));
094: query.addItem(filter.getFieldName(), filter.getOperator(),
095: fieldValue);
096: }
097: return query;
098: }
099:
100: public void onFiltersAdd() {
101: filters.add(new VisualQueryFilter(context, this ));
102: ObjectContext.getInstance().refresh(this , "filters");
103: }
104:
105: public Class<?> getTargetClass() {
106: return targetClass;
107: }
108:
109: public void setTargetClass(Class<?> targetClass) {
110: if (this .targetClass == null
111: || !this .targetClass.equals(targetClass)) {
112: filters.clear();
113: refresh();
114: }
115: this .targetClass = targetClass;
116: }
117:
118: public List<Class<?>> getClassesList() {
119: return classesList;
120: }
121:
122: public void setClassesList(List<Class<?>> classesList) {
123: this .classesList = classesList;
124: }
125:
126: @ViewAction(visible=AnnotationConstants.FALSE)
127: public void refresh() {
128: ObjectContext.getInstance().refresh(this , "filters");
129: }
130:
131: @ViewAction(layout="form:buttonOk")
132: public void ok() {
133: notifyOk();
134: ObjectContext.getInstance().close(this );
135: }
136:
137: private void notifyOk() {
138: for (ElementEventListener listener : listeners)
139: listener.eventOk(this );
140: }
141:
142: @ViewAction(layout="form:buttonCancel")
143: public void cancel() {
144: notifyCancel();
145: ObjectContext.getInstance().close(this );
146: }
147:
148: private void notifyCancel() {
149: for (ElementEventListener listener : listeners)
150: listener.eventCancel(this );
151: }
152:
153: @ViewAction(visible=AnnotationConstants.FALSE)
154: public void addEventListener(ElementEventListener listener) {
155: if (listener != null)
156: listeners.add(listener);
157: }
158:
159: @ViewAction(visible=AnnotationConstants.FALSE)
160: public void removeEventListener(ElementEventListener listener) {
161: if (listener != null)
162: listeners.remove(listener);
163: }
164:
165: public VisualQueryFilter[] getFilterSelection() {
166: return filterSelection;
167: }
168:
169: public void setFilterSelection(VisualQueryFilter[] filterSelection) {
170: this .filterSelection = filterSelection;
171: }
172:
173: @ViewField(visible=AnnotationConstants.FALSE)
174: public CommandContext getContext() {
175: return context;
176: }
177:
178: public void setContext(CommandContext context) {
179: this.context = context;
180: }
181: }
|