001: package org.julp;
002:
003: import java.util.*;
004: import java.sql.*;
005: import java.beans.*;
006: import java.lang.reflect.*;
007:
008: public class DBDataWriter implements DataWriter, java.io.Serializable,
009: Cloneable {
010:
011: public DBDataWriter() {
012: if (Boolean.getBoolean("debug-julp")
013: || Boolean.getBoolean("debug-" + getClass().getName())) {
014: setDebug(true);
015: }
016: }
017:
018: protected int removedCount = 0;
019: protected int createdCount = 0;
020: protected int modifiedCount = 0;
021: protected List removedObjects = null;
022: protected List createdObjects = null;
023: protected List modifiedObjects = null;
024: protected List generatedSQL = null;
025: protected DomainObjectFactory requestor = null;
026: protected String fullTableName = null;
027: protected String modifiedCatalog = null;
028: protected String modifiedSchema = null;
029: protected String modifiedTable = null;
030: protected Object[] EMPTY_READ_ARG = new Object[0];
031: protected boolean debug = false;
032: protected Throwable whatIsWrong = null;
033: protected boolean exceptionOnEmptyObjectList = false;
034:
035: protected void init() throws SQLException {
036: removedCount = 0;
037: createdCount = 0;
038: modifiedCount = 0;
039: if (requestor.getMetaData() == null) {
040: requestor.populateMetaData();
041: }
042: modifiedCatalog = this .requestor.getCatalog();
043: modifiedSchema = this .requestor.getSchema();
044: modifiedTable = this .requestor.getTable();
045: whatIsWrong = null;
046: if (removedObjects == null)
047: removedObjects = new ArrayList();
048: if (createdObjects == null)
049: createdObjects = new ArrayList();
050: if (modifiedObjects == null)
051: modifiedObjects = new ArrayList();
052: if (modifiedTable == null) {
053: throw new SQLException("Table name is missing");
054: }
055: fullTableName = modifiedTable;
056: if (modifiedSchema != null) {
057: fullTableName = modifiedSchema + "." + fullTableName;
058: if (modifiedCatalog != null) {
059: fullTableName = modifiedCatalog + "." + fullTableName;
060: }
061: }
062: }
063:
064: public boolean writeData(DomainObjectFactory requestor) {
065: if (requestor.isReadOnly()) {
066: setWhatIsWrong(new SQLException("Read Only"));
067: return false;
068: }
069: this .requestor = requestor;
070:
071: try {
072: this .init();
073: } catch (SQLException sqle) {
074: setWhatIsWrong(sqle);
075: return false;
076: }
077: boolean success = true;
078: boolean empty = false;
079: if (debug)
080: System.out
081: .println("julp ============= "
082: + new java.util.Date()
083: + " "
084: + this .getClass()
085: + "::"
086: + this
087: + "::writeData()::requestor.getObjectList() =================== "
088: + requestor.getObjectList());
089: if (requestor.getObjectList() == null
090: || requestor.getObjectList().isEmpty()) {
091: empty = true;
092: }
093: removedObjects = requestor.getRemovedObjects();
094: if (debug)
095: System.out
096: .println("julp ============= "
097: + new java.util.Date()
098: + " "
099: + this .getClass()
100: + "::"
101: + this
102: + "::writeData()::removedObjects =================== "
103: + removedObjects);
104: if ((removedObjects == null || removedObjects.isEmpty())
105: && empty) {
106: if (exceptionOnEmptyObjectList) {
107: setWhatIsWrong(new SQLException("Nothing to write"));
108: return false;
109: }
110: return true;
111: }
112: if (requestor.getDataModificationSequence() != null) {
113: Iterator it = requestor.getObjectList().iterator();
114: while (it.hasNext()) {
115: DomainObject domainObject = (DomainObject) it.next();
116: if (debug)
117: System.out
118: .println("julp ============= "
119: + new java.util.Date()
120: + " "
121: + this .getClass()
122: + "::"
123: + this
124: + "::writeData()::domainObject =================== "
125: + domainObject);
126: if (domainObject.getPersistentState() == DomainObject.REMOVED
127: && domainObject.getOriginalValues() != null
128: && domainObject.getOriginalValues()
129: .getFieldCount() > 0) {
130: removedObjects.add(domainObject);
131: } else if (domainObject.getPersistentState() == DomainObject.STORED
132: && domainObject.getOriginalValues() != null
133: && domainObject.getOriginalValues()
134: .getFieldCount() > 0) {
135: modifiedObjects.add(domainObject);
136: } else if (domainObject.getPersistentState() == DomainObject.CREATED
137: && domainObject.getOriginalValues() == null
138: || domainObject.getOriginalValues()
139: .getFieldCount() < 1) {
140: createdObjects.add(domainObject);
141: }
142: }
143: for (int i = 0; i < requestor.getDataModificationSequence().length; i++) {
144: if (requestor.getDataModificationSequence(i) == DomainObjectFactory.DATA_MODIFICATION_SEQUENCE_DELETE) {
145: removeObjects();
146: } else if (requestor.getDataModificationSequence(i) == DomainObjectFactory.DATA_MODIFICATION_SEQUENCE_UPDATE) {
147: storeObjects();
148: } else if (requestor.getDataModificationSequence(i) == DomainObjectFactory.DATA_MODIFICATION_SEQUENCE_INSERT) {
149: createObjects();
150: }
151: }
152: } else { // no DML sequence
153: requestor.getObjectList().addAll(removedObjects);
154: Iterator it = requestor.getObjectList().iterator();
155: while (it.hasNext()) {
156: DomainObject domainObject = (DomainObject) it.next();
157: if (debug)
158: System.out
159: .println("julp ============= "
160: + new java.util.Date()
161: + " "
162: + this .getClass()
163: + "::"
164: + this
165: + "::writeData()::domainObject =================== "
166: + domainObject);
167: if (domainObject.getPersistentState() == DomainObject.REMOVED
168: && domainObject.getOriginalValues() != null
169: && domainObject.getOriginalValues()
170: .getFieldCount() > 0) {
171: if (!removeObject(domainObject)) {
172: return false;
173: }
174: } else if (domainObject.getPersistentState() == DomainObject.STORED
175: && domainObject.getOriginalValues() != null
176: && domainObject.getOriginalValues()
177: .getFieldCount() > 0) {
178: if (!storeObject(domainObject)) {
179: return false;
180: }
181: } else if (domainObject.getPersistentState() == DomainObject.CREATED
182: && domainObject.getOriginalValues() == null
183: || domainObject.getOriginalValues()
184: .getFieldCount() < 1) {
185: if (!createObject(domainObject)) {
186: return false;
187: }
188: } else if (domainObject.getPersistentState() == DomainObject.ORIGINAL) {
189: // do nothing
190: } else {
191: setWhatIsWrong(new SQLException(
192: "Invalid PersistentState"));
193: return false;
194: }
195: }
196: }
197: return success;
198: }
199:
200: protected boolean removeObjects() {
201: if (removedObjects.isEmpty()) {
202: return true;
203: }
204: Iterator iter = removedObjects.iterator();
205: while (iter.hasNext()) {
206: DomainObject domainObject = (DomainObject) iter.next();
207: if (!removeObject(domainObject)) {
208: return false;
209: }
210: }
211: return true;
212: }
213:
214: protected boolean storeObjects() {
215: Iterator iter = modifiedObjects.iterator();
216: while (iter.hasNext()) {
217: DomainObject domainObject = (DomainObject) iter.next();
218: if (!storeObject(domainObject)) {
219: return false;
220: }
221: }
222: return true;
223: }
224:
225: protected boolean createObjects() {
226: Iterator iter = createdObjects.iterator();
227: while (iter.hasNext()) {
228: DomainObject domainObject = (DomainObject) iter.next();
229: if (!createObject(domainObject)) {
230: return false;
231: }
232: }
233: return true;
234: }
235:
236: protected boolean removeObject(DomainObject domainObject) {
237: String fieldName = null;
238: Collection params = new ArrayList();
239: StringBuffer sql = new StringBuffer("DELETE FROM ");
240: try {
241: sql.append(fullTableName);
242: sql.append(" WHERE ");
243: /* Always use PrimaryKeys columns in WHERE statement */
244: List pk = requestor.getPrimaryKey(modifiedCatalog,
245: modifiedSchema, modifiedTable);
246: Iterator iter = pk.iterator();
247: while (iter.hasNext()) {
248: String columnName = (String) iter.next(); // this is full column name
249: fieldName = (String) requestor.getMapping().get(
250: columnName);
251: int index = requestor.getMetaData()
252: .getColumnIndexByFieldName(fieldName);
253: Method readMethod = requestor.getMetaData()
254: .getReadMethod(index);
255: Object origValue = domainObject
256: .getOriginalValue(fieldName);
257: if (origValue == null) {
258: sql.append(columnName).append(" IS NULL AND ");
259: } else {
260: sql.append(columnName).append(" = ? AND ");
261: params.add(origValue);
262: }
263: }
264: if (requestor.getOptimisticLock() == DomainObjectFactory.KEY_COLUMNS) { /* built already */
265:
266: /* For DELETE make KEY_AND_MODIFIED_COLUMNS and KEY_AND_UPDATEBLE_COLUMNS the same: include all table columns in WHERE statement */
267: } else if (requestor.getOptimisticLock() == requestor.KEY_AND_MODIFIED_COLUMNS
268: || requestor.getOptimisticLock() == DomainObjectFactory.KEY_AND_UPDATEBLE_COLUMNS) {
269: for (int i = 1; i <= requestor.getMetaData()
270: .getColumnCount(); i++) {
271: String catalog = requestor.getMetaData()
272: .getCatalogName(i);
273: String schema = requestor.getMetaData()
274: .getSchemaName(i);
275: String table = requestor.getMetaData()
276: .getTableName(i);
277: if (modifiedCatalog != null) {
278: if (!modifiedCatalog.equals(catalog)) {
279: continue;
280: }
281: }
282: if (modifiedSchema != null) {
283: if (!modifiedSchema.equals(schema)) {
284: continue;
285: }
286: }
287: if (modifiedTable != null) {
288: if (!modifiedTable.equals(table)) {
289: continue;
290: }
291: }
292: String columnName = requestor.getMetaData()
293: .getFullColumnName(i);
294: if (sql.indexOf(columnName) > -1) {
295: continue;// this is PK column - proccesed already
296: }
297: if (!requestor.getMetaData().isWritable(i)
298: || requestor.getMetaData().isReadOnly(i)) {
299: continue;
300: }
301: fieldName = requestor.getMetaData().getFieldName(i);
302: try {
303: Object value = readValue(domainObject,
304: requestor.getMetaData()
305: .getReadMethod(i));
306: Object origValue = domainObject
307: .getOriginalValue(fieldName);
308: if (origValue == null) {
309: sql.append(columnName).append(
310: " IS NULL AND ");
311: } else {
312: sql.append(columnName).append(" = ? AND ");
313: params.add(origValue);
314: }
315: } catch (Throwable t) {
316: throw new SQLException(t.getMessage());
317: }
318: }
319: } else {
320: throw new SQLException(
321: "Invalid Optimistic Lock settings");
322: }
323: int len = sql.length();
324: sql = sql.delete(len - 5, len - 1);
325: if (requestor.isGenerateSQLOnly()) {
326: if (generatedSQL == null) {
327: generatedSQL = new ArrayList();
328: }
329: Object[] entry = new Object[2];
330: entry[0] = sql.toString();
331: entry[1] = params.toArray();
332: generatedSQL.add(entry);
333: } else {
334: int rowsAffected = requestor.getDBServices().execute(
335: sql.toString(), params);
336: if (requestor.isThrowOptimisticLockDeleteException()
337: && rowsAffected != 1) {
338: return false;
339: }
340: setRemovedCount(removedCount + rowsAffected);
341: }
342: } catch (SQLException e) {
343: if (debug)
344: System.out.println("julp ============= "
345: + new java.util.Date() + " " + this .getClass()
346: + "::" + this
347: + "::removeObject::Exception in removeObject: "
348: + e);
349: setWhatIsWrong(e);
350: e.printStackTrace();
351: return handleException(domainObject, sql.toString(),
352: params, e);
353: }
354: return true;
355: }
356:
357: protected boolean storeObject(DomainObject domainObject) {
358: String fieldName = null;
359: StringBuffer sql = new StringBuffer("UPDATE ");
360: List params = new ArrayList();
361: if (debug)
362: System.out
363: .println("julp ============= "
364: + new java.util.Date() + " "
365: + this .getClass() + "::" + this
366: + "::storeObject ===================");
367: try {
368: sql.append(fullTableName).append(" SET ");
369: for (int i = 1; i <= requestor.getMetaData()
370: .getColumnCount(); i++) {
371: String catalog = requestor.getMetaData()
372: .getCatalogName(i);
373: String schema = requestor.getMetaData()
374: .getSchemaName(i);
375: String table = requestor.getMetaData().getTableName(i);
376: if (modifiedCatalog != null) {
377: if (!modifiedCatalog.equals(catalog)) {
378: continue;
379: }
380: }
381: if (modifiedSchema != null) {
382: if (!modifiedSchema.equals(schema)) {
383: continue;
384: }
385: }
386: if (modifiedTable != null) {
387: if (!modifiedTable.equals(table)) {
388: continue;
389: }
390: }
391: if (!requestor.getMetaData().isWritable(i)
392: || requestor.getMetaData().isReadOnly(i)) {
393: continue;
394: }
395: String columnName = null;
396: if (requestor.isNoFullColumnName()) {
397: columnName = requestor.getMetaData().getColumnName(
398: i);
399: } else {
400: columnName = requestor.getMetaData()
401: .getFullColumnName(i);
402: }
403: fieldName = requestor.getMetaData().getFieldName(i);
404: Object value = readValue(domainObject, requestor
405: .getMetaData().getReadMethod(i));
406: Object origValue = domainObject
407: .getOriginalValue(fieldName);
408: if (debug)
409: System.out.println("julp ============= "
410: + new java.util.Date() + " "
411: + this .getClass() + "::" + this
412: + "::storeObject::fieldName: " + fieldName
413: + " value: " + value + " origValue: "
414: + origValue + " isWritable: "
415: + requestor.getMetaData().isWritable(i)
416: + " isReadOnly: "
417: + requestor.getMetaData().isReadOnly(i)
418: + " ===================");
419: if (origValue == null) {
420: if (value == null) {
421: continue; // the same value - do not include in update
422: } else {
423: sql.append(columnName).append(" = ?, ");
424: params.add(value);
425: }
426: } else {
427: if (value == null) {
428: sql.append(columnName).append(" = NULL, ");
429: } else {
430: if (!origValue.equals(value)) {
431: sql.append(columnName).append(" = ?, ");
432: params.add(value);
433: } else {
434: continue;
435: }
436: }
437: }
438: }
439: int idx = sql.lastIndexOf(", ");
440: if (idx > -1) {
441: sql = sql.delete(idx, idx + 2);
442: } else { // No modified column's data
443: domainObject.setPersistentState(DomainObject.ORIGINAL);
444: return true;
445: }
446: /* Building WHERE */
447: /* Always use PrimaryKeys columns in WHERE statement */
448: StringBuffer where = new StringBuffer(" WHERE ");
449: List pk = requestor.getPrimaryKey(modifiedCatalog,
450: modifiedSchema, modifiedTable);
451: if (debug)
452: System.out.println("julp ============= "
453: + new java.util.Date() + " " + this .getClass()
454: + "::" + this + "::storeObject::pk: " + pk
455: + " modifiedSchema: " + modifiedSchema
456: + " modifiedTable: " + modifiedTable);
457: Iterator iter = pk.iterator();
458: while (iter.hasNext()) {
459: String columnName = (String) iter.next();
460: fieldName = (String) requestor.getMapping().get(
461: columnName);
462: if (debug)
463: System.out.println("julp ============= "
464: + new java.util.Date() + " "
465: + this .getClass() + "::" + this
466: + "::storeObject::columnName: "
467: + columnName + " fieldName: " + fieldName);
468: int index = requestor.getMetaData()
469: .getColumnIndexByFieldName(fieldName);
470: Object origValue = domainObject
471: .getOriginalValue(fieldName);
472: if (origValue == null) {
473: where.append(columnName).append(" IS NULL AND ");
474: } else {
475: where.append(columnName).append(" = ? AND ");
476: params.add(origValue);
477: }
478: }
479: if (requestor.getOptimisticLock() == DomainObjectFactory.KEY_COLUMNS) {
480: idx = where.lastIndexOf(" AND ");
481: where = where.delete(idx + 1, idx + 5);
482: }
483: if (requestor.getOptimisticLock() == DomainObjectFactory.KEY_COLUMNS) {
484: // ... built already
485: } else if (requestor.getOptimisticLock() == DomainObjectFactory.KEY_AND_MODIFIED_COLUMNS) {
486: for (int i = 1; i <= requestor.getMetaData()
487: .getColumnCount(); i++) {
488: String catalog = requestor.getMetaData()
489: .getCatalogName(i);
490: String schema = requestor.getMetaData()
491: .getSchemaName(i);
492: String table = requestor.getMetaData()
493: .getTableName(i);
494: if (modifiedCatalog != null) {
495: if (!modifiedCatalog.equals(catalog)) {
496: continue;
497: }
498: }
499: if (modifiedSchema != null) {
500: if (!modifiedSchema.equals(schema)) {
501: continue;
502: }
503: }
504: if (modifiedTable != null) {
505: if (!modifiedTable.equals(table)) {
506: continue;
507: }
508: }
509: String columnName = requestor.getMetaData()
510: .getFullColumnName(i);
511: if (where.indexOf(columnName) > -1) {
512: continue;// this is PK column - proccesed already
513: }
514: if (!requestor.getMetaData().isWritable(i)
515: || requestor.getMetaData().isReadOnly(i)) {
516: continue;
517: }
518: fieldName = requestor.getMetaData().getFieldName(i);
519: Object value = readValue(domainObject, requestor
520: .getMetaData().getReadMethod(i));
521: Object origValue = domainObject
522: .getOriginalValue(fieldName);
523: if (origValue == null) {
524: if (value == null) {
525: continue;
526: } else {
527: where.append(columnName).append(
528: " IS NULL AND ");
529: }
530: } else {
531: if (value == null) {
532: where.append(columnName)
533: .append(" = ? AND ");
534: params.add(origValue);
535: } else {
536: if (origValue.equals(value)) {
537: continue;
538: } else {
539: where.append(columnName).append(
540: " = ? AND ");
541: params.add(origValue);
542: }
543: }
544: }
545: }
546: } else if (requestor.getOptimisticLock() == DomainObjectFactory.KEY_AND_UPDATEBLE_COLUMNS) {
547: for (int i = 1; i <= requestor.getMetaData()
548: .getColumnCount(); i++) {
549: String catalog = requestor.getMetaData()
550: .getCatalogName(i);
551: String schema = requestor.getMetaData()
552: .getSchemaName(i);
553: String table = requestor.getMetaData()
554: .getTableName(i);
555: if (modifiedCatalog != null) {
556: if (!modifiedCatalog.equals(catalog)) {
557: continue;
558: }
559: }
560: if (modifiedSchema != null) {
561: if (!modifiedSchema.equals(schema)) {
562: continue;
563: }
564: }
565: if (modifiedTable != null) {
566: if (!modifiedTable.equals(table)) {
567: continue;
568: }
569: }
570: String columnName = requestor.getMetaData()
571: .getFullColumnName(i);
572: if (where.indexOf(columnName) > -1) {
573: continue; // this is PK column - proccesed already
574: }
575: if (!requestor.getMetaData().isWritable(i)
576: || requestor.getMetaData().isReadOnly(i)) {
577: continue;
578: }
579: fieldName = requestor.getMetaData().getFieldName(i);
580: Object value = readValue(domainObject, requestor
581: .getMetaData().getReadMethod(i));
582: Object origValue = domainObject
583: .getOriginalValue(fieldName);
584: if (origValue == null) {
585: where.append(columnName)
586: .append(" IS NULL AND ");
587: } else {
588: where.append(columnName).append(" = ? AND ");
589: params.add(origValue);
590: }
591: }
592: } else {
593: throw new SQLException(
594: "Invalid Optimistic Lock settings");
595: }
596: if (debug)
597: System.out.println("julp ============= "
598: + new java.util.Date() + " " + this .getClass()
599: + "::" + this + "::storeObject::where 1 : "
600: + where);
601: if (debug)
602: System.out.println("julp ============= "
603: + new java.util.Date() + " " + this .getClass()
604: + "::" + this
605: + "::storeObject::requestor.KEY_COLUMNS 1 : "
606: + requestor.KEY_COLUMNS);
607: if (requestor.getOptimisticLock() != DomainObjectFactory.KEY_COLUMNS) {
608: idx = where.length();
609: where = where.delete(idx - 5, idx - 1);
610: }
611: if (debug)
612: System.out.println("julp ============= "
613: + new java.util.Date() + " " + this .getClass()
614: + "::" + this + "::storeObject::where 2 : "
615: + where);
616: sql.append(where);
617: if (debug)
618: System.out.println("julp ============= "
619: + new java.util.Date() + " " + this .getClass()
620: + "::" + this + "::storeObject::storeObject: "
621: + sql + "; params: " + params);
622: if (requestor.isGenerateSQLOnly()) {
623: if (generatedSQL == null) {
624: generatedSQL = new ArrayList();
625: }
626: Object[] entry = new Object[2];
627: entry[0] = sql.toString();
628: entry[1] = params.toArray();
629: generatedSQL.add(entry);
630: } else {
631: int rowsAffected = requestor.getDBServices().execute(
632: sql.toString(), params);
633: if (requestor.isThrowOptimisticLockUpdateException()
634: && rowsAffected != 1) {
635: if (debug)
636: System.out.println("julp ============= "
637: + new java.util.Date() + " "
638: + this .getClass() + "::" + this
639: + "::storeObject::rowsAffected: "
640: + rowsAffected);
641: return false;
642: }
643: setModifiedCount(modifiedCount + rowsAffected);
644: }
645: } catch (SQLException e) {
646: if (debug)
647: System.out.println("julp ============= "
648: + new java.util.Date() + " " + this .getClass()
649: + "::" + this
650: + "::storeObject::Exception in storeObject: "
651: + e);
652: setWhatIsWrong(e);
653: e.printStackTrace();
654: return handleException(domainObject, sql.toString(),
655: params, e);
656: }
657: return true;
658: }
659:
660: protected boolean createObject(DomainObject domainObject) {
661: String fieldName = null;
662: StringBuffer sql = new StringBuffer("INSERT INTO ");
663: List params = new ArrayList();
664: try {
665: sql.append(fullTableName);
666: sql.append(" (");
667: for (int i = 1; i <= requestor.getMetaData()
668: .getColumnCount(); i++) {
669: String catalog = requestor.getMetaData()
670: .getCatalogName(i);
671: String schema = requestor.getMetaData()
672: .getSchemaName(i);
673: String table = requestor.getMetaData().getTableName(i);
674: if (modifiedCatalog != null) {
675: if (!modifiedCatalog.equals(catalog)) {
676: continue;
677: }
678: }
679: if (modifiedSchema != null) {
680: if (!modifiedSchema.equals(schema)) {
681: continue;
682: }
683: }
684: if (modifiedTable != null) {
685: if (!modifiedTable.equals(table)) {
686: continue;
687: }
688: }
689: String fieldNameTemp = requestor.getMetaData()
690: .getFieldName(i);
691: if (!requestor.getMetaData().isWritable(i)
692: || requestor.getMetaData().isReadOnly(i)) {
693: continue;
694: }
695: String columnName = null;
696: if (requestor.isNoFullColumnName()) {
697: columnName = requestor.getMetaData().getColumnName(
698: i);
699: } else {
700: columnName = requestor.getMetaData()
701: .getFullColumnName(i);
702: }
703: sql.append(columnName).append(", ");
704: Object value = readValue(domainObject, requestor
705: .getMetaData().getReadMethod(i));
706: params.add(value);
707: }
708: int idx = sql.lastIndexOf(", ");
709: sql = sql.delete(idx, idx + 2);
710: sql.append(") VALUES (");
711: ListIterator li = params.listIterator();
712: while (li.hasNext()) {
713: Object value = li.next();
714: if (value == null) {
715: sql.append("NULL, ");
716: li.remove();
717: } else {
718: sql.append("?, ");
719: }
720: }
721: idx = sql.lastIndexOf(",");
722: sql = sql.delete(idx, idx + 2);
723: sql.append(")");
724: if (requestor.isGenerateSQLOnly()) {
725: if (generatedSQL == null) {
726: generatedSQL = new ArrayList();
727: }
728: Object[] entry = new Object[2];
729: entry[0] = sql.toString();
730: entry[1] = params.toArray();
731: generatedSQL.add(entry);
732: } else {
733: int rowsAffected = requestor.getDBServices().execute(
734: sql.toString(), params);
735: if (requestor.isThrowFailedInsertException()
736: && rowsAffected != 1) {
737: throw new SQLException("INSERT INTO "
738: + fullTableName + " has failed");
739: }
740: setCreatedCount(createdCount + rowsAffected);
741: }
742: } catch (SQLException e) {
743: if (debug)
744: System.out.println("julp ============= "
745: + new java.util.Date() + " " + this .getClass()
746: + "::" + this
747: + "::createObject::Exception in createObject: "
748: + e);
749: setWhatIsWrong(e);
750: e.printStackTrace();
751: return handleException(domainObject, sql.toString(),
752: params, e);
753: }
754: return true;
755: }
756:
757: /** Getter for property removedCount.
758: * @return Value of property removedCount.
759: *
760: */
761: public int getRemovedCount() {
762: return removedCount;
763: }
764:
765: /** Setter for property removedCount.
766: * @param removedCount New value of property removedCount.
767: *
768: */
769: protected void setRemovedCount(int removedCount) {
770: this .removedCount = removedCount;
771: }
772:
773: /** Getter for property createdCount.
774: * @return Value of property createdCount.
775: *
776: */
777: public int getCreatedCount() {
778: return createdCount;
779: }
780:
781: /** Setter for property createdCount.
782: * @param createdCount New value of property createdCount.
783: *
784: */
785: protected void setCreatedCount(int createdCount) {
786: this .createdCount = createdCount;
787: }
788:
789: /** Getter for property modifiedCount.
790: * @return Value of property modifiedCount.
791: *
792: */
793: public int getModifiedCount() {
794: return modifiedCount;
795: }
796:
797: /** Setter for property modifiedCount.
798: * @param modifiedCount New value of property modifiedCount.
799: *
800: */
801: protected void setModifiedCount(int modifiedCount) {
802: this .modifiedCount = modifiedCount;
803: }
804:
805: public void reset() {
806: removedCount = 0;
807: createdCount = 0;
808: modifiedCount = 0;
809: //objectList = null;
810: generatedSQL = null;
811: removedObjects = null;
812: createdObjects = null;
813: modifiedObjects = null;
814: //metaData = null;
815: fullTableName = null;
816: modifiedTable = null;
817: modifiedSchema = null;
818: modifiedCatalog = null;
819: whatIsWrong = null;
820: }
821:
822: /** Getter for property generatedSQL.
823: * @return Value of property generatedSQL.
824: *
825: */
826: public java.util.List getGeneratedSQL() {
827: return generatedSQL;
828: }
829:
830: /** Getter for property whatIsWrong.
831: * @return Value of property whatIsWrong.
832: *
833: */
834: public java.lang.Throwable getWhatIsWrong() {
835: return whatIsWrong;
836: }
837:
838: /** Setter for property whatIsWrong.
839: * @param whatIsWrong New value of property whatIsWrong.
840: *
841: */
842: public void setWhatIsWrong(java.lang.Throwable whatIsWrong) {
843: this .whatIsWrong = whatIsWrong;
844: }
845:
846: /** Getter for property modifiedCatalog.
847: * @return Value of property modifiedCatalog.
848: *
849: */
850: public java.lang.String getModifiedCatalog() {
851: return modifiedCatalog;
852: }
853:
854: /** Setter for property modifiedCatalog.
855: * @param modifiedCatalog New value of property modifiedCatalog.
856: *
857: */
858: public void setModifiedCatalog(java.lang.String modifiedCatalog) {
859: this .modifiedCatalog = modifiedCatalog;
860: }
861:
862: /** Getter for property generatedSQLasXML.
863: * @return Value of property generatedSQLasXML.
864: *
865: */
866: public java.lang.String getGeneratedSQLasXML() {
867: if (this .generatedSQL == null || this .generatedSQL.isEmpty()) {
868: return "";
869: }
870: StringBuffer sb = new StringBuffer(
871: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<generated-sql>\n");
872: Iterator it = this .generatedSQL.iterator();
873: while (it.hasNext()) {
874: sb.append(" <sql>\n");
875: Object[] obj = (Object[]) it.next();
876: sb.append(" <statement><![CDATA[").append(obj[0]).append(
877: "]]></statement>\n");
878: Object[] params = (Object[]) obj[1];
879: if (params != null && params.length > 0) {
880: sb.append(" <params>\n");
881: for (int i = 0; i < params.length; i++) {
882: sb.append(" <param>\n");
883: sb.append(" <value>").append("<![CDATA[")
884: .append(params[i].toString()).append("]]>")
885: .append("</value>\n");
886: sb.append(" <datatype>").append(
887: params[i].getClass().getName()).append(
888: "</datatype>\n");
889: sb.append(" </param>\n");
890: }
891: sb.append(" </params>\n");
892: }
893: sb.append(" </sql>\n");
894: }
895: sb.append("</generated-sql>\n");
896: return sb.toString();
897: }
898:
899: protected boolean handleException(DomainObject domainObject,
900: String sql, Collection params, Throwable t) {
901: boolean ignore = false;
902: if (t instanceof java.lang.StringIndexOutOfBoundsException) {
903: System.out
904: .println("Make sure to use correct schema and/or table");
905: }
906: if (requestor.getExceptionHandler() != null) {
907: ignore = requestor.getExceptionHandler().handleException(
908: domainObject, sql, params, t);
909: }
910: return ignore;
911: }
912:
913: public boolean isDebug() {
914: return debug;
915: }
916:
917: public void setDebug(boolean debug) {
918: this .debug = debug;
919: }
920:
921: public boolean isExceptionOnEmptyObjectList() {
922: return exceptionOnEmptyObjectList;
923: }
924:
925: public void setExceptionOnEmptyObjectList(
926: boolean exceptionOnEmptyObjectList) {
927: this .exceptionOnEmptyObjectList = exceptionOnEmptyObjectList;
928: }
929:
930: protected Object readValue(DomainObject domainObject, Method method)
931: throws SQLException {
932: Object value = null;
933: try {
934: value = method.invoke(domainObject, EMPTY_READ_ARG);
935: } catch (Throwable t) {
936: if (t instanceof InvocationTargetException) {
937: throw new RuntimeException(
938: ((InvocationTargetException) t)
939: .getTargetException());
940: } else {
941: throw new RuntimeException(t);
942: }
943: }
944: return value;
945: }
946:
947: }
|