001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Admin/Sources/es/udc/mypersonalizer/admin/http/view/actionforms/tasks/ExecuteTaskForm.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coru�a
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coru�a nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040: package es.udc.mypersonalizer.admin.http.view.actionforms.tasks;
041:
042: import javax.servlet.http.HttpServletRequest;
043:
044: import org.apache.struts.action.ActionError;
045: import org.apache.struts.action.ActionErrors;
046: import org.apache.struts.action.ActionForm;
047: import org.apache.struts.action.ActionMapping;
048:
049: import es.udc.mypersonalizer.kernel.model.query.ast.Query;
050: import es.udc.mypersonalizer.kernel.model.query.ast.Step;
051: import es.udc.mypersonalizer.kernel.model.query.parser.ParseException;
052: import es.udc.mypersonalizer.kernel.model.query.parser.QueryParser;
053: import es.udc.mypersonalizer.kernel.model.tasks.Task;
054:
055: /**
056: * Form used to execute an action.
057: *
058: * @author Abel Muinho
059: * @author David barral
060: * @since 1.0
061: */
062: public class ExecuteTaskForm extends ActionForm {
063:
064: private String query;
065:
066: private String taskName;
067:
068: private boolean doPreview;
069:
070: private boolean fromForm;
071:
072: private Query compiledQuery;
073:
074: private Task taskInstance;
075:
076: private int startIndex = 0;
077:
078: private int count = 10;
079:
080: /**
081: * Obtains the index of first property to be shown.
082: * @return the first property index.
083: */
084: public int getStartIndex() {
085: return startIndex;
086: }
087:
088: /**
089: * Sets the index of first property to be shown.
090: * @param value the first property index.
091: */
092: public void setStartIndex(int value) {
093: startIndex = value;
094: }
095:
096: /**
097: * Obtains the number of properties to show at once
098: * @return the number of properties.
099: */
100: public int getCount() {
101: return count;
102: }
103:
104: /**
105: * Sets the number of properties to show at once.
106: * @param value number of properties.
107: */
108: public void setCount(int value) {
109: count = value;
110: }
111:
112: /**
113: * Obtains the query, as a <code>String</code>, to be executed.
114: * @return the query string.
115: */
116: public String getQuery() {
117: return query;
118: }
119:
120: /**
121: * Sets the query, as a <code>String</code>, to be executed.
122: * @param string the query string.
123: */
124: public void setQuery(String string) {
125: query = string;
126: }
127:
128: /**
129: * Obtains the name of the task to perform.
130: * @return the name of the task.
131: */
132: public String getTaskName() {
133: return taskName;
134: }
135:
136: /**
137: * Sets the name of the task to perform.
138: * @param string the name of the task.
139: */
140: public void setTaskName(String string) {
141: taskName = string;
142: }
143:
144: /**
145: * Obtains the flag that marks if the query results must be previewed
146: * @return true if preview is selected
147: */
148: public boolean getDoPreview() {
149: return doPreview;
150: }
151:
152: /**
153: * Sets the flag that marks if the query results must be previewed
154: * @param doPreview true if preview is desired
155: */
156: public void setDoPreview(boolean doPreview) {
157: this .doPreview = doPreview;
158: }
159:
160: /**
161: * Obtains the flag that marks if the associated <code>Action</code> was
162: * called from a form or from the menu
163: * @return true if called from a form.
164: */
165: public boolean getFromForm() {
166: return this .fromForm;
167: }
168:
169: /**
170: * Sets the flag that marks if the associated <code>Action</code> was
171: * called from a form or from the menu
172: * @param fromForm true if called from a form
173: */
174: public void setFromForm(boolean fromForm) {
175: this .fromForm = fromForm;
176: }
177:
178: /**
179: * Returns the <code>Query</code> object representing the query string.
180: * <p>
181: * The query will be generated during validation.
182: * @return the query compiled during validation.
183: */
184: public Query getCompiledQuery() {
185: return compiledQuery;
186: }
187:
188: /**
189: * Returns the <code>Task</code> instance represented by the
190: * <code>taskName</code> parameter.
191: * <p>
192: * The object will be generated during validation.
193: * @return the <code>Task</code> to be executed.
194: */
195: public Task getTaskInstance() {
196: return taskInstance;
197: }
198:
199: public ActionErrors validate(ActionMapping mapping,
200: HttpServletRequest request) {
201:
202: compiledQuery = null;
203: taskInstance = null;
204:
205: ActionErrors errors = new ActionErrors();
206:
207: if (query == null || query.trim().length() == 0) {
208: /* The query must be filled. */
209: errors.add("query", new ActionError(
210: "Query.Executor.emptyQueryError"));
211: } else {
212: /* The query must be well formed. */
213: try {
214: QueryParser parser = new QueryParser();
215: compiledQuery = parser.parse(query);
216: } catch (ParseException e) {
217: errors.add("query", new ActionError(
218: "Query.Executor.parseError", e.getMessage()));
219: }
220: }
221:
222: try {
223: /* The task must be instantiable. */
224: taskInstance = (Task) Class.forName(taskName).newInstance();
225: } catch (Exception e) {
226: errors.add("taskName", new ActionError(
227: "Query.Executor.missingImplementationError"));
228: }
229:
230: /* If the query was well formed, check if it is compatible with the
231: * selected task.
232: */
233: if (compiledQuery != null && taskInstance != null) {
234: /* Find target property */
235: Step lastStep = (Step) compiledQuery.getReturnClause().get(
236: compiledQuery.getReturnClause().size() - 1);
237:
238: if (!taskInstance.isCompatibleWith(lastStep
239: .getMetaProperty())) {
240: errors.add("taskName", new ActionError(
241: "Query.Executor.incompatibleTarget"));
242: }
243: }
244: return errors;
245: }
246:
247: public void reset(ActionMapping mapping, HttpServletRequest request) {
248: reset();
249: }
250:
251: private void reset() {
252: query = "";
253: taskName = "";
254: compiledQuery = null;
255: taskInstance = null;
256: startIndex = 0;
257: count = 10;
258: doPreview = false;
259: fromForm = false;
260: }
261:
262: }
|