001: /*
002: $Header: /cvsroot/xorm/xorm/tools/src/org/xorm/tools/editor/EditAction.java,v 1.8 2002/09/23 01:38:17 wbiggs Exp $
003:
004: This file is part of XORM.
005:
006: XORM is free software; you can redistribute it and/or modify
007: it under the terms of the GNU General Public License as published by
008: the Free Software Foundation; either version 2 of the License, or
009: (at your option) any later version.
010:
011: XORM is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU General Public License for more details.
015:
016: You should have received a copy of the GNU General Public License
017: along with XORM; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package org.xorm.tools.editor;
021:
022: import org.xorm.XORM;
023: import java.io.*;
024: import java.beans.*;
025: import java.lang.reflect.Method;
026: import java.util.Collection;
027: import javax.jdo.PersistenceManager;
028:
029: public class EditAction extends Action {
030: private Object proxy;
031: private PropertyDescriptor[] pds;
032:
033: public EditAction(PersistenceManager mgr, Object proxy) {
034: super (mgr);
035: this .proxy = proxy;
036:
037: BeanInfo info = null;
038: try {
039: info = Introspector.getBeanInfo(proxy.getClass(),
040: Object.class);
041: } catch (IntrospectionException e) {
042: // shouldn't happen on a proxy
043: }
044: pds = info.getPropertyDescriptors();
045: }
046:
047: private static Object getProperty(Object bean, PropertyDescriptor pd) {
048: Method m = pd.getReadMethod();
049: try {
050: return m.invoke(bean, null);
051: } catch (Exception e) {
052: e.printStackTrace();
053: }
054: return null;
055: }
056:
057: private static void setProperty(Object bean, PropertyDescriptor pd,
058: Object value) {
059: Method m = pd.getWriteMethod();
060: try {
061: m.invoke(bean, new Object[] { value });
062: } catch (Exception e) {
063: e.printStackTrace();
064: }
065: }
066:
067: private static Object convert(String str, Class type) {
068: if (type.equals(String.class))
069: return str;
070: if (type.equals(Integer.class))
071: return Integer.valueOf(str);
072: if (type.equals(Long.class))
073: return Long.valueOf(str);
074: // TODO need more
075: System.err.println("Type not supported for set in this tool.");
076: return null;
077: }
078:
079: public Object go() {
080: try {
081: System.out.println("EDIT OBJECT");
082:
083: while (true) {
084: System.out.println("Object ID: "
085: + mgr.getObjectId(proxy));
086: for (int i = 0; i < pds.length; i++) {
087: Class pdType = pds[i].getPropertyType();
088: if (Collection.class.isAssignableFrom(pdType)
089: || (pds[i].getWriteMethod() != null)) {
090: System.out.print("[" + i + "] ");
091: } else {
092: System.out.print(" ");
093: }
094: System.out.print(pdType.getName());
095: System.out.print(" ");
096: System.out.print(pds[i].getName());
097: System.out.print(": ");
098: System.out.println(EditAction.getProperty(proxy,
099: pds[i]));
100: }
101: System.out.println("[A] Abort, rollback");
102: System.out.println("[S] Save to DB");
103: System.out.println("[X] Exit");
104:
105: String optLine = readLine();
106: int propIndex = -1;
107: try {
108: propIndex = Integer.parseInt(optLine);
109: } catch (NumberFormatException e) {
110: }
111: if (propIndex == -1) {
112: switch (optLine.charAt(0)) {
113: case 'A':
114: mgr.currentTransaction().rollback();
115: mgr.currentTransaction().begin();
116: break;
117: case 'S':
118: mgr.currentTransaction().commit();
119: mgr.currentTransaction().begin();
120: break;
121: case 'X':
122: return proxy;
123: }
124: } else {
125: // Edit the numbered property
126: Class type = pds[propIndex].getPropertyType();
127: if (Collection.class.isAssignableFrom(type)) {
128: Action action = new EditCollectionAction(mgr,
129: (Collection) EditAction.getProperty(
130: proxy, pds[propIndex]));
131: action.go();
132: } else if (XORM.getModelMapping(mgr).isManagedType(
133: type)) {
134: // Reference type
135: System.out
136: .print("[D] Delete, [E] Edit or [R] Replace: ");
137: String value = readLine();
138: char ch = value.charAt(0);
139: if (ch == 'D') {
140: EditAction.setProperty(proxy,
141: pds[propIndex], null);
142: } else if (ch == 'E') {
143: Action edit = new EditAction(mgr,
144: EditAction.getProperty(proxy,
145: pds[propIndex]));
146: edit.go();
147: } else if (ch == 'R') {
148: EditAction.setProperty(proxy,
149: pds[propIndex], null);
150: Action replace = new MainMenu(mgr);
151: Object out = replace.go();
152: EditAction.setProperty(proxy,
153: pds[propIndex], out);
154: }
155: } else {
156: System.out.print("Enter new value for "
157: + pds[propIndex].getName() + ": ");
158: String value = readLine();
159: EditAction.setProperty(proxy, pds[propIndex],
160: EditAction.convert(value, type));
161: }
162: }
163: } // while true
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: return proxy;
168: }
169: }
|