View Javadoc

1   /***
2    * Copyright (C) 2006 Philipp Mpalampanis
3    *
4    * License: MPL 1.1/GPL 2.0/LGPL 2.1
5    *
6    * The contents of this file are subject to the Mozilla Public License Version
7    * 1.1 (the "License"); you may not use this file except in compliance with
8    * the License. You may obtain a copy of the License at
9    * http://www.mozilla.org/MPL/
10   *
11   * Software distributed under the License is distributed on an "AS IS" basis,
12   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13   * for the specific language governing rights and limitations under the
14   * License.
15   *
16   * Alternatively, the contents of this file may be used under the terms of
17   * either the GNU General Public License Version 2 or later (the "GPL"), or
18   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19   * in which case the provisions of the GPL or the LGPL are applicable instead
20   * of those above. If you wish to allow use of your version of this file only
21   * under the terms of either the GPL or the LGPL, and not to allow others to
22   * use your version of this file under the terms of the MPL, indicate your
23   * decision by deleting the provisions above and replace them with the notice
24   * and other provisions required by the GPL or the LGPL. If you do not delete
25   * the provisions above, a recipient may use your version of this file under
26   * the terms of any one of the MPL, the GPL or the LGPL.
27   */
28  package net.sf.echobinding.persistence;
29  
30  import java.lang.reflect.Constructor;
31  import java.lang.reflect.InvocationTargetException;
32  
33  /***
34   *
35   */
36  public class DefaultPersistenceManager<T> implements PersistenceManager<T> {
37  
38  	private T _object;
39  
40  	
41  	/***
42  	 * Creates a default persistence manager for objects of type T.
43  	 * 
44  	 * @param object
45  	 */
46  	public DefaultPersistenceManager(T object) {
47  		super();
48  		
49  		_object = object;
50  	}
51  
52  	/***
53  	 * Tries to dynamically create a new row object by using the empty
54  	 * constructor.
55  	 * 
56  	 * @see net.sf.echobinding.persistence.PersistenceManager#createNewObject()
57  	 */
58  	@SuppressWarnings("unchecked")
59  	public T createNewObject() {
60  		T newObj = null;
61  		try {
62  			Constructor<T> constructor = (Constructor<T>) _object.getClass().getConstructor(new Class[0]);
63  			newObj = constructor.newInstance(new Object[0]);
64  		} catch (SecurityException e) {
65  			e.printStackTrace();
66  		} catch (NoSuchMethodException e) {
67  			e.printStackTrace();
68  		} catch (IllegalArgumentException e) {
69  			e.printStackTrace();
70  		} catch (InstantiationException e) {
71  			e.printStackTrace();
72  		} catch (IllegalAccessException e) {
73  			e.printStackTrace();
74  		} catch (InvocationTargetException e) {
75  			e.printStackTrace();
76  		}
77  		return newObj;
78  	}
79  
80  	/* (non-Javadoc)
81  	 * @see echobinding.persistence.PersistenceManager#deleteObject(T)
82  	 */
83  	public void deleteObject(T object) {
84  	}
85  
86  	/* (non-Javadoc)
87  	 * @see echobinding.persistence.PersistenceManager#copyObject(T)
88  	 */
89  	public T copyObject(T object) {
90  		T copy = createNewObject();
91  		return copy;
92  	}
93  
94  	/* (non-Javadoc)
95  	 * @see echobinding.persistence.PersistenceManager#saveObject(T)
96  	 */
97  	public void saveObject(T object) {
98  	}
99  
100 	/* (non-Javadoc)
101 	 * @see echobinding.persistence.PersistenceManager#isPersistent(T)
102 	 */
103 	public boolean isPersistent(T object) {
104 		return true;
105 	}
106 	
107 	
108 	
109 }