001: package org.enhydra.shark.toolagent;
002:
003: import java.lang.reflect.Method;
004: import java.sql.Date;
005: import java.util.Calendar;
006: import java.util.HashMap;
007: import java.util.StringTokenizer;
008:
009: import org.enhydra.shark.api.client.wfmc.wapi.WMSessionHandle;
010: import org.enhydra.shark.api.client.wfservice.WMEntity;
011: import org.enhydra.shark.api.internal.toolagent.AppParameter;
012: import org.enhydra.shark.api.internal.toolagent.ApplicationBusy;
013: import org.enhydra.shark.api.internal.toolagent.ApplicationNotDefined;
014: import org.enhydra.shark.api.internal.toolagent.ApplicationNotStarted;
015: import org.enhydra.shark.api.internal.toolagent.ToolAgentGeneralException;
016: import org.enhydra.shark.api.internal.working.CallbackUtilities;
017: import org.enhydra.shark.xpdl.elements.ExtendedAttribute;
018: import org.enhydra.shark.xpdl.elements.ExtendedAttributes;
019:
020: /**
021: * Tool agent for storing data into database. This tool uses default parameters from
022: * config file:
023: *
024: * packageDO - full name og package where is stored DO object
025: * table- name of the table
026: * checkUnique - check unique indexes for database table First two parameters will be
027: * overwriten with value of extended attribute from xpdl (DataTableClass) if
028: * they exist. This value is full package name for DO class + DO class name.
029: * This extended attribute must be called "DataTableClass" and its value must
030: * be full name of DO Class
031: * @author Svjetlana Milidrag
032: */
033: public class StorageToolAgent extends AbstractToolAgent {
034:
035: public static final String DATA_TABLE_CLASS_EXT_ATTR_NAME = "DataTableClass";
036:
037: private static final String KEY_TABLE = "DatabaseResource.storage.database.table";
038:
039: private static final String PACKAGE_KEY = "DatabaseResource.storage.database.packageDO";
040:
041: private static final String CHECK_UNIQUE_KEY = "DatabaseResource.storage.database.table.checkunique";
042:
043: private static String packageDO = "";
044:
045: private static String checkUnique = "";
046:
047: private static String table = "";
048:
049: private static String fullpackageName = "";
050:
051: private Method mth = null;
052:
053: private HashMap parameterMap = new HashMap();
054:
055: /**
056: * Read parameters from config file
057: *
058: * @throws Exception
059: */
060:
061: public void configure(CallbackUtilities c) throws Exception {
062: super .configure(c);
063:
064: table = c.getProperty(KEY_TABLE);
065: packageDO = c.getProperty(PACKAGE_KEY);
066: checkUnique = c.getProperty(CHECK_UNIQUE_KEY);
067: fullpackageName = packageDO + "." + table + "DO";
068:
069: }
070:
071: public void invokeApplication(WMSessionHandle _shandle,
072: long _handle, WMEntity _appInfo, WMEntity _toolInfo,
073: String _applicationName, String _procInstId, String _assId,
074: AppParameter[] _parameters, Integer _appMode)
075: throws ApplicationNotStarted, ApplicationNotDefined,
076: ApplicationBusy, ToolAgentGeneralException {
077: super .invokeApplication(_shandle, _handle, _appInfo, _toolInfo,
078: _applicationName, _procInstId, _assId, _parameters,
079: _appMode);
080:
081: try {
082:
083: status = APP_STATUS_RUNNING;
084:
085: readParamsFromExtAttributes((String) _parameters[0].the_value);
086:
087: store2db();
088:
089: status = APP_STATUS_FINISHED;
090:
091: } catch (Exception e) {
092: if (e instanceof ApplicationNotStarted) {
093: throw (ApplicationNotStarted) e;
094: } else if (e instanceof ApplicationNotDefined) {
095: throw (ApplicationNotDefined) e;
096: } else if (e instanceof ApplicationBusy) {
097: throw (ApplicationBusy) e;
098: } else if (e instanceof ToolAgentGeneralException) {
099: throw (ToolAgentGeneralException) e;
100: } else {
101: throw new ToolAgentGeneralException(e);
102: }
103: }
104: }
105:
106: /**
107: * Function for storing data into database. Check for unique key (objectExist(String
108: * name) )if in property file parameter which sets
109: *
110: * @checkUnique has value yes. All foreign keys should have name with suffix FK in
111: * xpdl.
112: * @throws Exception
113: */
114: private void store2db() throws Exception {
115:
116: String name = fullpackageName;
117: String className = "";
118: String paramName = "";
119: Object paramValue = null;
120: String paramMode = "";
121: boolean objExist = false;
122: Object newDO = null;
123:
124: if (checkUnique.equalsIgnoreCase("Yes")) {
125:
126: fillTheMap();
127:
128: newDO = objectExist(name);
129:
130: if (newDO != null)
131: objExist = true;
132: }
133:
134: if (!objExist) {
135: Object co = Class.forName(name);
136: mth = findMethod(name, "createVirgin");
137: newDO = mth.invoke(co, new Object[] {});
138: }
139:
140: for (int i = 1; i < parameters.length; i++) {
141:
142: paramName = parameters[i].the_formal_name;
143: paramValue = parameters[i].the_value;
144: className = parameters[i].the_class.getName();
145: paramMode = parameters[i].the_mode;
146:
147: if (paramMode.equalsIgnoreCase("IN")
148: && !paramName.endsWith("FK")
149: && !paramName.equalsIgnoreCase("Unique")
150: && !objExist) {
151:
152: if (className.equalsIgnoreCase("java.util.Date")) {
153: paramValue = getDate(paramValue);
154: className = "java.sql.Date";
155: }
156:
157: mth = Class.forName(name).getMethod("set" + paramName,
158: new Class[] { Class.forName(className) });
159: mth.invoke(newDO, new Object[] { (Object) paramValue });
160:
161: } else if (paramMode.equalsIgnoreCase("OUT")) {
162: mth = findMethod(name, "get_OId");
163: parameters[i].the_value = mth.invoke(newDO,
164: new Object[] {}).toString();
165:
166: }
167:
168: if (paramName.endsWith("FK") && !objExist) {
169: mth = Class.forName(name).getMethod(
170: "oid_set"
171: + paramName.substring(0, paramName
172: .length() - 2),
173: new Class[] { Class.forName(className) });
174: mth.invoke(newDO, new Object[] { (Object) paramValue });
175:
176: }
177:
178: }
179:
180: if (!objExist) {
181: mth = findMethod(name, "save");
182: mth.invoke(newDO, new Object[] {});
183: }
184:
185: }
186:
187: /**
188: * Function that check existing of unique index in database table
189: *
190: * @param name-DO class full name
191: * @return- DO object if is found or null value
192: * @throws Exception
193: * @throws IllegalAccessException
194: * @throws ClassNotFoundException
195: */
196: private Object objectExist(String name) throws Exception,
197: IllegalAccessException, ClassNotFoundException {
198:
199: String uniqueParamValue = "";
200: String uniqueName = "";
201: String uniqueValue = "";
202: String className = name.substring(0, name.length() - 2)
203: + "Query";
204:
205: if (parameterMap.containsKey("Unique")) {
206: uniqueParamValue = (String) parameterMap.get("Unique");
207: }
208:
209: StringTokenizer st = new StringTokenizer(uniqueParamValue, ",");
210: if (st.countTokens() > 0) {
211:
212: while (st.hasMoreTokens()) {
213:
214: uniqueName = st.nextToken();
215:
216: if (parameterMap.containsKey(uniqueName)) {
217:
218: uniqueValue = (String) parameterMap.get(uniqueName);
219: Object cl = Class.forName(className).newInstance();
220:
221: mth = Class.forName(className).getMethod(
222: "setQuery" + uniqueName,
223: new Class[] { Class
224: .forName("java.lang.String") });
225: mth.invoke(cl,
226: new Object[] { (Object) uniqueValue });
227: mth = findMethod(className, "requireUniqueInstance");
228: mth.invoke(cl, new Object[] {});
229: mth = findMethod(className, "getNextDO");
230: Object t = mth.invoke(cl, new Object[] {});
231: if (t != null) {
232: return t;
233: }
234: } else {
235: throw new Exception(
236: "Unique parameter doesn't match the other parameter from array! ");
237: }
238: }
239: }
240:
241: return null;
242:
243: }
244:
245: /**
246: * Converts Object into java.util.Date
247: *
248: * @param obj
249: * @return
250: */
251: private Date getDate(Object obj) {
252: Calendar aa = Calendar.getInstance();
253: aa.setTime((java.util.Date) obj);
254: Date ret = new Date(aa.getTimeInMillis());
255: return ret;
256: }
257:
258: /**
259: * Fill the map with parameters values
260: */
261: private void fillTheMap() {
262: for (int i = 0; i < parameters.length; i++) {
263: parameterMap.put(parameters[i].the_formal_name,
264: parameters[i].the_value);
265: }
266: }
267:
268: private Method findMethod(String className, String methodName)
269: throws SecurityException, NoSuchMethodException,
270: ClassNotFoundException {
271: return mth = Class.forName(className).getMethod(methodName,
272: null);
273: }
274:
275: /**
276: * Read extended attributes
277: */
278: protected ExtendedAttributes readParamsFromExtAttributes(
279: String extAttribs) throws Exception {
280: ExtendedAttributes eas = super
281: .readParamsFromExtAttributes(extAttribs);
282: ExtendedAttribute ea = eas
283: .getFirstExtendedAttributeForName(StorageToolAgent.DATA_TABLE_CLASS_EXT_ATTR_NAME);
284: if (ea != null) {
285: fullpackageName = ea.getVValue();
286: }
287:
288: return eas;
289: }
290:
291: }
|