View Javadoc

1   /*
2    * Copyright 2004-2005 Emmanouil Batsis
3    * 
4    * Licensed under the GNU General Public License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.gnu.org/licenses/gpl.txt
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   */
17  package com.geekologue.md4j.dao;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Set;
23  import org.apache.commons.lang.math.NumberUtils;
24  import org.apache.log4j.Logger;
25  
26  /***
27   * An abstract DAO class to be subclassed by other DAO implementations.
28   * 
29   * @author manos
30   */
31  public abstract class AbstractDAO {
32      /***
33       * The class object that the implementing subclass is responsible for its
34       * database operations. This variable must be set by all subclasses to the
35       * appropriate value.
36       */
37      protected Class  daoClass;
38  
39      protected String identifierName;
40  
41      /***
42       * Private Constructor
43       */
44      private AbstractDAO() {
45      }
46  
47      /***
48       * Subclasses must call this constructor
49       * 
50       * @param clazz
51       */
52      protected AbstractDAO(Class clazz, String identifierName) {
53          this.daoClass = clazz;
54          this.identifierName = identifierName;
55      }
56  
57      /***
58       * Get a new instance of the Class associated with this DAO (e.g. an
59       * instance of User for UserDAO)
60       * 
61       * @return
62       */
63      protected Object getDaoClassInstance() {
64          Object pojo = null;
65          try {
66              pojo = this.daoClass.newInstance();
67          } catch (Exception e) {
68              throw new DataAccessException("Could not create a new "
69                      + this.daoClass.getName() + " instance ", e);
70          }
71          return pojo;
72      }
73  
74      /***
75       * Retreive the object matching the class handled by this DAO and the given
76       * identifier. If the identifier is not match, an exception is thrown
77       * 
78       * @param identifier
79       * @return the maching pojo
80       * @throws DataAccessException
81       */
82      public abstract Object load(Serializable identifier);
83  
84      /***
85       * Retreive the object matching the class handled by this DAO and the given
86       * identifier or null if no match is found.
87       * 
88       * @param identifier
89       * @return the maching pojo if any, <code>null</code> otherwise
90       * @throws DataAccessException
91       */
92      public abstract Object get(Serializable identifier);
93  
94      /***
95       * Retreive the properties of the object matching the given identifier as a
96       * Map of attribute-value pairs.
97       * 
98       * @param identifier
99       * @param projectionProperties
100      *            the names of properties to retrieve
101      * @return the requested properties for the object matching the identifier,
102      *         if any, <code>null</code> otherwise.
103      * 
104      */
105     // public abstract Map get(Serializable identifier, Set
106     // projectionProperties);
107     /***
108      * Update the object matching the given identifier according to the property
109      * value pairs in the given map. If no match for the identifier is found,
110      * return <code>null</code>
111      * 
112      * @param map
113      *            the map with the property value pairs to update
114      * @throws DataAccessException
115      */
116     public abstract void update(Map map);
117 
118     /***
119      * Persist any changes to the given POJO
120      * 
121      * @param pojo
122      *            the POJO to update
123      * @throws DataAccessException
124      */
125     public abstract void update(Object pojo);
126 
127     /***
128      * Get a page of results.
129      * 
130      * @param projectionProps
131      *            the names of the property values to return
132      * @param params
133      *            the attribute-value pairs the results have to match
134      * @param pageNumber
135      *            the number of the page
136      * @param pageSize
137      *            the number of results in the page
138      * @return a result page containing the property values of the matching
139      *         entities
140      */
141     public abstract Page getPage(Set projectionProps, Map params, Order order,
142             int pageNumber, int pageSize);
143 
144     /***
145      * Get a page of results.
146      * 
147      * @param params
148      *            the attribute-value pairs the results have to match
149      * @param pageNumber
150      *            the number of the page
151      * @param pageSize
152      *            the number of results in the page
153      * @return a result page containing the matching entities
154      */
155     public abstract Page getPage(Map params, Order order, int pageNumber,
156             int pageSize);
157 
158     /***
159      * Save the given object
160      * 
161      * @param pojo
162      *            the POJO to save
163      * @return the saved pojo
164      * @throws DataAccessException
165      */
166     public abstract Serializable save(Object pojo);
167 
168     /***
169      * Delete the object coresponding to the given identifier
170      * 
171      * @param pojo
172      *            the object to delete.
173      * @throws DataAccessException
174      */
175     public abstract void delete(Serializable identifier);
176 
177     /***
178      * Get unique constraint violations before persisting new objects
179      * 
180      * @param params
181      *            the property value pairs to check
182      * @return a set of the broken property names
183      */
184     public abstract Set getBrokenUConstraints(Map params, Serializable id);
185 
186     /***
187      * Get data to populate drop downs refering to parent objects by many-to-one
188      * relationships.
189      * 
190      * @return a Map where each key is the string name of the property for that
191      *         parent object. The value for that key is yet another Map where
192      *         the key value pair is used to the value and text (respectively)
193      *         of HTML option elements
194      */
195     public abstract Map getParentOptions();
196 }