01: /*
02: $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/editor/QueryAction.java,v 1.6 2002/04/28 00:34:31 wbiggs Exp $
03:
04: This file is part of XORM.
05:
06: XORM is free software; you can redistribute it and/or modify
07: it under the terms of the GNU General Public License as published by
08: the Free Software Foundation; either version 2 of the License, or
09: (at your option) any later version.
10:
11: XORM is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU General Public License for more details.
15:
16: You should have received a copy of the GNU General Public License
17: along with Foobar; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package org.xorm.tools.editor;
21:
22: import org.xorm.XORM;
23: import java.io.*;
24: import java.util.*;
25: import javax.jdo.PersistenceManager;
26: import javax.jdo.Query;
27: import javax.jdo.spi.PersistenceCapable;
28:
29: public class QueryAction extends Action {
30: public QueryAction(PersistenceManager mgr) {
31: super (mgr);
32: }
33:
34: public Object go() {
35: System.out.println("QUERY FOR OBJECTS");
36: PersistenceCapable pc = null;
37: try {
38: Class clazz = null;
39: while (clazz == null) {
40: System.out.print("Enter result class: ");
41: clazz = readClass();
42: }
43:
44: System.out.print("Enter query: ");
45: String queryText = readLine();
46:
47: if ("".equals(queryText))
48: queryText = null;
49:
50: Query query = mgr.newQuery(clazz, queryText);
51: List args = new ArrayList();
52:
53: if (queryText != null) {
54: System.out.print("Declare parameters: ");
55: String params = readLine();
56: query.declareParameters(params);
57:
58: // TODO take different arg types
59: String arg = null;
60: do {
61: System.out
62: .print("Parameter value or ENTER when done: ");
63: arg = readLine();
64: if (!"".equals(arg))
65: args.add(arg);
66: } while (!"".equals(arg));
67:
68: }
69: Collection results = (Collection) query
70: .executeWithArray(args.toArray());
71:
72: System.out.println("Query returned " + results.size()
73: + " results");
74:
75: Action action = new EditCollectionAction(mgr, results);
76: return action.go();
77: } catch (Exception e) {
78: e.printStackTrace();
79: }
80: return pc;
81: }
82: }
|