001: package org.julp.util.common;
002:
003: import java.beans.PropertyDescriptor;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.io.IOException;
007: import java.lang.reflect.Field;
008: import java.lang.reflect.Method;
009: import java.util.Enumeration;
010: import java.util.Properties;
011: import org.julp.DataWriter;
012: import org.julp.DomainObjectFactory;
013: import org.julp.ExceptionHandler;
014:
015: /**
016: * Populate settings for DomainObjectFactory
017: */
018: public class DomainObjectFactoryConfig {
019:
020: private boolean debug = false;
021: /* Useful to send only modified objects to another tier */
022: private boolean discardUnmodifiedObjects;
023: /* Set sequence or disable update/insert/delete */
024: private char[] dataModificationSequence;
025: /* Disable DB modifications*/
026: private boolean readOnly = false;
027:
028: /* Optimistic lock settings ****************************************/
029:
030: private char optimisticLock = DomainObjectFactory.KEY_COLUMNS;
031:
032: /*
033: Throw exception if PreparedStatement.executeUpdate() does not return 1
034: for each DomainObject.
035: It means that the row in DB Table most likely was modified or deleted
036: by another user/process.
037: */
038: private boolean throwOptimisticLockDeleteException = true;
039: private boolean throwOptimisticLockUpdateException = true;
040: private boolean throwFailedInsertException = true;
041:
042: /* END of Optimistic lock settings *********************************/
043: /* Target DB catalog */
044: private boolean overrideCatalogName = false;
045:
046: /* This object generates SQL for data modification */
047: private String dataWriter = "org.julp.DBDataWriter";
048: /* use synchronized Collections */
049: private boolean synchronizedAccess = false;
050: /* If lazyLoding == true than set only original values vs. original and current */
051: private boolean lazyLoading = false;
052: /* Records per page */
053: private int pageSize = 0;
054: /* Do not execute INSERTS/UPDATES/DELETES - just generate SQL and parameters */
055: private boolean generateSQLOnly = false;
056: /* control if load() append objects to objectList )*/
057: private boolean append = false;
058: /* Some databases (like hsqdb 1.7.1) failed when UPDATE statement is
059: using TABLE_NAME.COLUMN_NAME in SET clause. INSERT also failes */
060: private boolean noFullColumnName = false;
061: /* Throw Exception or ignore if DomainObject has less fields than mapping */
062: private boolean throwMissingFieldException = false;
063: /* if not null object should handle exceptions */
064: private String exceptionHandler;
065: /* If true suppress exception when call writeData on empty objectList */
066: private boolean exceptionOnEmptyObjectList = true;
067: /* Create and populate MetaData automatically */
068: private boolean createDefaultMetaData = true;
069:
070: //
071: private Properties settings;
072: private String path;
073:
074: public DomainObjectFactoryConfig() {
075: }
076:
077: public void configure(DomainObjectFactory factory) {
078: factory.setDebug(debug);
079: factory.setDiscardUnmodifiedObjects(discardUnmodifiedObjects);
080: factory.setReadOnly(readOnly);
081: factory.setOptimisticLock(optimisticLock);
082: factory
083: .setThrowFailedInsertException(throwFailedInsertException);
084: factory
085: .setThrowOptimisticLockDeleteException(throwOptimisticLockDeleteException);
086: factory
087: .setThrowOptimisticLockUpdateException(throwOptimisticLockUpdateException);
088: factory.setOverrideCatalogName(overrideCatalogName);
089: factory.setSynchronizedAccess(synchronizedAccess);
090: factory.setPageSize(pageSize);
091: factory.setGenerateSQLOnly(generateSQLOnly);
092: factory.setAppend(append);
093: factory.setNoFullColumnName(noFullColumnName);
094: factory
095: .setThrowMissingFieldException(throwMissingFieldException);
096: factory
097: .setExceptionOnEmptyObjectList(exceptionOnEmptyObjectList);
098: factory.setCreateDefaultMetaData(createDefaultMetaData);
099: if (dataModificationSequence != null
100: && dataModificationSequence.length > 0) {
101: factory
102: .setDataModificationSequence(dataModificationSequence);
103: }
104: try {
105: factory.setExceptionHandler((ExceptionHandler) Class
106: .forName(exceptionHandler).newInstance());
107: } catch (Exception e) {
108: e.printStackTrace();
109: // ignore
110: }
111: try {
112: factory.setDataWriter((DataWriter) Class
113: .forName(dataWriter).newInstance());
114: } catch (Exception e) {
115: e.printStackTrace();
116: // ignore
117: }
118: }
119:
120: public void populate(Properties settings) {
121: this .settings = settings;
122: populate();
123: }
124:
125: public void populate() {
126: Enumeration enumeration = settings.keys();
127: while (enumeration.hasMoreElements()) {
128: String property = (String) enumeration.nextElement();
129: String value = settings.getProperty(property);
130: if (property.equals("dataWriter")) {
131: try {
132: //dataWriter = (DataWriter) Class.forName(value).newInstance();
133: dataWriter = value;
134: } catch (Exception e) {
135: e.printStackTrace();
136: e.printStackTrace();
137: //ignore
138: }
139: } else if (property.equals("exceptionHandler")) {
140: try {
141: //exceptionHandler = (ExceptionHandler) Class.forName(value).newInstance();
142: exceptionHandler = value;
143: } catch (Exception e) {
144: e.printStackTrace();
145: //ignore
146: }
147: //like DIU, UD, UI etc.
148: } else if (property.equals("dataModificationSequence")) {
149: dataModificationSequence = value.toCharArray();
150: } else {
151: try {
152: Method mWrite = (new PropertyDescriptor(property,
153: this .getClass())).getWriteMethod();
154: Object[] param = new Boolean[1];
155: param[0] = new Boolean(value);
156: mWrite.invoke(this , param);
157: } catch (Exception e) {
158: e.printStackTrace();
159: //ignore
160: }
161: }
162: }
163: }
164:
165: public void store() {
166: if (settings == null) {
167: settings = new Properties();
168: }
169: Field[] fields = getClass().getDeclaredFields();
170: for (int i = 0; i < fields.length; i++) {
171: try {
172: String name = fields[i].getName();
173: if (name.equals("exceptionHandler")) {
174: //settings.setProperty(name, fields[i].getType().getName());
175: settings.setProperty(name, fields[i].get(this )
176: .toString());
177: } else if (name.equals("dataWriter")) {
178: //settings.setProperty(name, fields[i].getType().getName());
179: settings.setProperty(name, fields[i].get(this )
180: .toString());
181: } else if (name.equals("dataModificationSequence")) {
182: settings.setProperty(name, new String(
183: dataModificationSequence));
184: }
185: if (fields[i].getType().equals(boolean.class)) {
186: settings.setProperty(name, String.valueOf(fields[i]
187: .getBoolean(this )));
188: }
189: } catch (Exception e) {
190: e.printStackTrace();
191: // ignore
192: }
193: }
194: try {
195: settings
196: .store(new FileOutputStream(path),
197: "DomainObjectFactoryConfig: DomainObjectFactory settings");
198: } catch (IOException e) {
199: e.printStackTrace();
200: }
201: }
202:
203: public void load(String path) {
204: this .path = path;
205: load();
206: }
207:
208: public void load() {
209: java.io.InputStream inStream = null;
210: settings = new Properties();
211: try {
212: inStream = new File(path).toURL().openStream();
213: settings.load(inStream);
214: } catch (java.io.IOException ioe) {
215: throw new RuntimeException(ioe);
216: } finally {
217: try {
218: inStream.close();
219: } catch (java.io.IOException ioe) {
220: throw new RuntimeException(ioe);
221: }
222: }
223: }
224:
225: public boolean isDebug() {
226: return debug;
227: }
228:
229: public void setDebug(boolean debug) {
230: this .debug = debug;
231: }
232:
233: public boolean isDiscardUnmodifiedObjects() {
234: return discardUnmodifiedObjects;
235: }
236:
237: public void setDiscardUnmodifiedObjects(
238: boolean discardUnmodifiedObjects) {
239: this .discardUnmodifiedObjects = discardUnmodifiedObjects;
240: }
241:
242: public char[] getDataModificationSequence() {
243: return dataModificationSequence;
244: }
245:
246: public void setDataModificationSequence(
247: char[] dataModificationSequence) {
248: this .dataModificationSequence = dataModificationSequence;
249: }
250:
251: public boolean isReadOnly() {
252: return readOnly;
253: }
254:
255: public void setReadOnly(boolean readOnly) {
256: this .readOnly = readOnly;
257: }
258:
259: public char getOptimisticLock() {
260: return optimisticLock;
261: }
262:
263: public void setOptimisticLock(char optimisticLock) {
264: this .optimisticLock = optimisticLock;
265: }
266:
267: public boolean isThrowOptimisticLockDeleteException() {
268: return throwOptimisticLockDeleteException;
269: }
270:
271: public void setThrowOptimisticLockDeleteException(
272: boolean throwOptimisticLockDeleteException) {
273: this .throwOptimisticLockDeleteException = throwOptimisticLockDeleteException;
274: }
275:
276: public boolean isThrowOptimisticLockUpdateException() {
277: return throwOptimisticLockUpdateException;
278: }
279:
280: public void setThrowOptimisticLockUpdateException(
281: boolean throwOptimisticLockUpdateException) {
282: this .throwOptimisticLockUpdateException = throwOptimisticLockUpdateException;
283: }
284:
285: public boolean isThrowFailedInsertException() {
286: return throwFailedInsertException;
287: }
288:
289: public void setThrowFailedInsertException(
290: boolean throwFailedInsertException) {
291: this .throwFailedInsertException = throwFailedInsertException;
292: }
293:
294: public boolean isOverrideCatalogName() {
295: return overrideCatalogName;
296: }
297:
298: public void setOverrideCatalogName(boolean overrideCatalogName) {
299: this .overrideCatalogName = overrideCatalogName;
300: }
301:
302: public String getDataWriter() {
303: return dataWriter;
304: }
305:
306: public void setDataWriter(String dataWriter) {
307: this .dataWriter = dataWriter;
308: }
309:
310: public boolean isSynchronizedAccess() {
311: return synchronizedAccess;
312: }
313:
314: public void setSynchronizedAccess(boolean synchronizedAccess) {
315: this .synchronizedAccess = synchronizedAccess;
316: }
317:
318: public boolean isLazyLoading() {
319: return lazyLoading;
320: }
321:
322: public void setLazyLoading(boolean lazyLoading) {
323: this .lazyLoading = lazyLoading;
324: }
325:
326: public int getPageSize() {
327: return pageSize;
328: }
329:
330: public void setPageSize(int pageSize) {
331: this .pageSize = pageSize;
332: }
333:
334: public boolean isGenerateSQLOnly() {
335: return generateSQLOnly;
336: }
337:
338: public void setGenerateSQLOnly(boolean generateSQLOnly) {
339: this .generateSQLOnly = generateSQLOnly;
340: }
341:
342: public boolean isAppend() {
343: return append;
344: }
345:
346: public void setAppend(boolean append) {
347: this .append = append;
348: }
349:
350: public boolean isNoFullColumnName() {
351: return noFullColumnName;
352: }
353:
354: public void setNoFullColumnName(boolean noFullColumnName) {
355: this .noFullColumnName = noFullColumnName;
356: }
357:
358: public boolean isThrowMissingFieldException() {
359: return throwMissingFieldException;
360: }
361:
362: public void setThrowMissingFieldException(
363: boolean throwMissingFieldException) {
364: this .throwMissingFieldException = throwMissingFieldException;
365: }
366:
367: public String getExceptionHandler() {
368: return exceptionHandler;
369: }
370:
371: public void setExceptionHandler(String exceptionHandler) {
372: this .exceptionHandler = exceptionHandler;
373: }
374:
375: public boolean isExceptionOnEmptyObjectList() {
376: return exceptionOnEmptyObjectList;
377: }
378:
379: public void setExceptionOnEmptyObjectList(
380: boolean exceptionOnEmptyObjectList) {
381: this .exceptionOnEmptyObjectList = exceptionOnEmptyObjectList;
382: }
383:
384: public boolean isCreateDefaultMetaData() {
385: return createDefaultMetaData;
386: }
387:
388: public void setCreateDefaultMetaData(boolean createDefaultMetaData) {
389: this .createDefaultMetaData = createDefaultMetaData;
390: }
391:
392: public String getPath() {
393: return path;
394: }
395:
396: public void setPath(String path) {
397: this .path = path;
398: }
399:
400: //test
401: public static void main(String[] args) {
402: DomainObjectFactoryConfig dfc = new DomainObjectFactoryConfig();
403: dfc
404: .setPath("/home/leonard/projects/ProjectJulp/julp-util/src/org/julp/util/common/settings.properties");
405: dfc.load();
406: dfc.populate();
407: dfc.getSettings().list(System.out);
408: TestFactory tf = new TestFactory();
409: System.out.println("before configure: " + tf);
410: dfc.configure(tf);
411: System.out.println("after configure: " + tf);
412: dfc.store();
413: }
414:
415: public Properties getSettings() {
416: return settings;
417: }
418:
419: public void setSettings(Properties settings) {
420: this .settings = settings;
421: }
422:
423: }
424:
425: class TestFactory extends DomainObjectFactory {
426:
427: /** Creates a new instance of TestFactory */
428: public TestFactory() {
429: }
430:
431: }
|