Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PersistableBusinessObjectBase |
|
| 1.3333333333333333;1.333 |
1 | /* | |
2 | * Copyright 2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php | |
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 | package org.kuali.rice.krad.bo; | |
17 | ||
18 | import java.util.ArrayList; | |
19 | import java.util.Collection; | |
20 | import java.util.List; | |
21 | import java.util.UUID; | |
22 | ||
23 | import javax.persistence.Column; | |
24 | import javax.persistence.MappedSuperclass; | |
25 | import javax.persistence.PostLoad; | |
26 | import javax.persistence.PostPersist; | |
27 | import javax.persistence.PostRemove; | |
28 | import javax.persistence.PostUpdate; | |
29 | import javax.persistence.PrePersist; | |
30 | import javax.persistence.PreRemove; | |
31 | import javax.persistence.PreUpdate; | |
32 | import javax.persistence.Transient; | |
33 | import javax.persistence.Version; | |
34 | ||
35 | import org.apache.commons.lang.StringUtils; | |
36 | import org.apache.ojb.broker.PersistenceBroker; | |
37 | import org.apache.ojb.broker.PersistenceBrokerAware; | |
38 | import org.apache.ojb.broker.PersistenceBrokerException; | |
39 | import org.kuali.rice.core.api.mo.common.GloballyUnique; | |
40 | import org.kuali.rice.core.api.mo.common.Versioned; | |
41 | import org.kuali.rice.krad.service.KRADServiceLocator; | |
42 | import org.kuali.rice.krad.service.PersistenceService; | |
43 | import org.kuali.rice.krad.service.PersistenceStructureService; | |
44 | ||
45 | /** | |
46 | * Business Object Base Business Object | |
47 | */ | |
48 | @MappedSuperclass | |
49 | 0 | public abstract class PersistableBusinessObjectBase extends BusinessObjectBase implements PersistableBusinessObject, PersistenceBrokerAware, Versioned, GloballyUnique { |
50 | ||
51 | private static final long serialVersionUID = 1451642350593233282L; | |
52 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PersistableBusinessObjectBase.class); |
53 | ||
54 | @Version | |
55 | @Column(name="VER_NBR") | |
56 | protected Long versionNumber; | |
57 | @Column(name="OBJ_ID") | |
58 | private String objectId; | |
59 | @Transient | |
60 | private boolean newCollectionRecord; | |
61 | @Transient | |
62 | protected PersistableBusinessObjectExtension extension; | |
63 | ||
64 | private static transient PersistenceService persistenceService; | |
65 | private static transient PersistenceStructureService persistenceStructureService; | |
66 | ||
67 | /** | |
68 | * @see PersistableBusinessObject#getVersionNumber() | |
69 | */ | |
70 | public Long getVersionNumber() { | |
71 | 0 | return versionNumber; |
72 | } | |
73 | ||
74 | /** | |
75 | * @see PersistableBusinessObject#getVersionNumber() | |
76 | */ | |
77 | public void setVersionNumber(Long versionNumber) { | |
78 | 0 | this.versionNumber = versionNumber; |
79 | 0 | } |
80 | ||
81 | ||
82 | /** | |
83 | * getter for the guid based object id that is assignable to all objects, in order to support custom attributes a mapping must | |
84 | * also be added to the OJB file and a column must be added to the database for each business object that extension attributes | |
85 | * are supposed to work on. | |
86 | * | |
87 | * @return | |
88 | */ | |
89 | public String getObjectId() { | |
90 | 0 | return objectId; |
91 | } | |
92 | ||
93 | /** | |
94 | * setter for the guid based object id that is assignable to all objects, in order to support custom attributes a mapping must | |
95 | * also be added to the OJB file and column must be added to the database for each business object that extension attributes are | |
96 | * supposed to work on. | |
97 | * | |
98 | * @param objectId | |
99 | */ | |
100 | public void setObjectId(String objectId) { | |
101 | 0 | this.objectId = objectId; |
102 | 0 | } |
103 | ||
104 | ||
105 | /** | |
106 | * Gets the newCollectionRecord attribute. | |
107 | * | |
108 | * @return Returns the newCollectionRecord. | |
109 | */ | |
110 | public boolean isNewCollectionRecord() { | |
111 | 0 | return newCollectionRecord; |
112 | } | |
113 | ||
114 | /** | |
115 | * Sets the newCollectionRecord attribute value. | |
116 | * | |
117 | * @param isNewCollectionRecord The newCollectionRecord to set. | |
118 | */ | |
119 | public void setNewCollectionRecord(boolean isNewCollectionRecord) { | |
120 | 0 | this.newCollectionRecord = isNewCollectionRecord; |
121 | 0 | } |
122 | ||
123 | /** | |
124 | * Implementation of the OJB afterDelete hook which delegates to {@link #postRemove()}. This method is final | |
125 | * because it is recommended that sub-classes override and implement postRemove if they need to take | |
126 | * advantage of this persistence hook. | |
127 | * | |
128 | * @see org.apache.ojb.broker.PersistenceBrokerAware#afterDelete(org.apache.ojb.broker.PersistenceBroker) | |
129 | */ | |
130 | public final void afterDelete(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
131 | 0 | postRemove(); |
132 | 0 | } |
133 | ||
134 | /** | |
135 | * Default implementation of the JPA {@link PostRemove} hook. This implementation currently does nothing, | |
136 | * however sub-classes can override and implement this method if needed. | |
137 | * | |
138 | * <p>This method is currently invoked by the corresponding OJB {@link #afterDelete(PersistenceBroker)} hook. | |
139 | */ | |
140 | @PostRemove | |
141 | protected void postRemove() { | |
142 | // do nothing | |
143 | 0 | } |
144 | ||
145 | /** | |
146 | * Implementation of the OJB afterInsert hook which delegates to {@link #postPersist()}. This method is final | |
147 | * because it is recommended that sub-classes override and implement postPersist if they need to take | |
148 | * advantage of this persistence hook. | |
149 | * | |
150 | * @see org.apache.ojb.broker.PersistenceBrokerAware#afterInsert(org.apache.ojb.broker.PersistenceBroker) | |
151 | */ | |
152 | public final void afterInsert(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
153 | 0 | postPersist(); |
154 | 0 | } |
155 | ||
156 | /** | |
157 | * Default implementation of the JPA {@link PostPersist} hook. This implementation currently does nothing, | |
158 | * however sub-classes can override and implement this method if needed. | |
159 | * | |
160 | * <p>This method is currently invoked by the corresponding OJB {@link #afterInsert(PersistenceBroker)} hook. | |
161 | */ | |
162 | @PostPersist | |
163 | protected void postPersist() { | |
164 | // do nothing | |
165 | 0 | } |
166 | ||
167 | /** | |
168 | * Implementation of the OJB afterLookup hook which delegates to {@link #postLoad()}. This method is final | |
169 | * because it is recommended that sub-classes override and implement postLoad if they need to take | |
170 | * advantage of this persistence hook. | |
171 | * | |
172 | * @see org.apache.ojb.broker.PersistenceBrokerAware#afterLookup(org.apache.ojb.broker.PersistenceBroker) | |
173 | */ | |
174 | public final void afterLookup(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
175 | 0 | postLoad(); |
176 | 0 | } |
177 | ||
178 | /** | |
179 | * Default implementation of the JPA {@link PostLoad} hook. This implementation currently does nothing, | |
180 | * however sub-classes can override and implement this method if needed. | |
181 | * | |
182 | * <p>This method is currently invoked by the corresponding OJB {@link #afterLookup(PersistenceBroker)} hook. | |
183 | */ | |
184 | @PostLoad | |
185 | protected void postLoad() { | |
186 | // do nothing | |
187 | 0 | } |
188 | ||
189 | /** | |
190 | * Implementation of the OJB afterUpdate hook which delegates to {@link #postUpdate()}. This method is final | |
191 | * because it is recommended that sub-classes override and implement postUpdate if they need to take | |
192 | * advantage of this persistence hook. | |
193 | * | |
194 | * @see org.apache.ojb.broker.PersistenceBrokerAware#afterUpdate(org.apache.ojb.broker.PersistenceBroker) | |
195 | */ | |
196 | public final void afterUpdate(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
197 | 0 | postUpdate(); |
198 | 0 | } |
199 | ||
200 | /** | |
201 | * Default implementation of the JPA {@link PostUpdate} hook. This implementation currently does nothing, | |
202 | * however sub-classes can override and implement this method if needed. | |
203 | * | |
204 | * <p>This method is currently invoked by the corresponding OJB {@link #afterUpdate(PersistenceBroker)} hook. | |
205 | */ | |
206 | @PostUpdate | |
207 | protected void postUpdate() { | |
208 | // do nothing | |
209 | 0 | } |
210 | ||
211 | /** | |
212 | * Implementation of the OJB beforeDelete hook which delegates to {@link #preRemove()}. This method is final | |
213 | * because it is recommended that sub-classes override and implement preRemove if they need to take | |
214 | * advantage of this persistence hook. | |
215 | * | |
216 | * @see org.apache.ojb.broker.PersistenceBrokerAware#beforeDelete(org.apache.ojb.broker.PersistenceBroker) | |
217 | */ | |
218 | public final void beforeDelete(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
219 | 0 | preRemove(); |
220 | 0 | } |
221 | ||
222 | /** | |
223 | * Default implementation of the JPA {@link PreRemove} hook. This implementation currently does nothing, | |
224 | * however sub-classes can implement this method if needed. | |
225 | * | |
226 | * <p>This method is currently invoked by the corresponding OJB {@link #beforeDelete(PersistenceBroker)} hook. | |
227 | */ | |
228 | @PreRemove | |
229 | protected void preRemove() { | |
230 | // do nothing | |
231 | 0 | } |
232 | ||
233 | /** | |
234 | * Implementation of the OJB beforeInsert hook which delegates to {@link #prePersist()}. This method is final | |
235 | * because it is recommended that sub-classes override and implement prePersist if they need to take | |
236 | * advantage of this persistence hook. | |
237 | * | |
238 | * @see org.apache.ojb.broker.PersistenceBrokerAware#beforeInsert(org.apache.ojb.broker.PersistenceBroker) | |
239 | */ | |
240 | public final void beforeInsert(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
241 | 0 | prePersist(); |
242 | 0 | } |
243 | ||
244 | /** | |
245 | * Default implementation of the JPA {@link PrePersist} hook which generates the unique objectId for this | |
246 | * persistable business object if it does not already have one. Any sub-class which overrides this method | |
247 | * should take care to invoke super.prePersist to ensure that the objectId for this persistable | |
248 | * business object is generated properly. | |
249 | * | |
250 | * <p>This method is currently invoked by the corresponding OJB {@link #beforeInsert(PersistenceBroker)} hook. | |
251 | */ | |
252 | @PrePersist | |
253 | protected void prePersist() { | |
254 | 0 | generateAndSetObjectIdIfNeeded(); |
255 | 0 | } |
256 | ||
257 | /** | |
258 | * Implementation of the OJB beforeUpdate hook which delegates to {@link #preUpdate()}. This method is final | |
259 | * because it is recommended that sub-classes override and implement preUpdate if they need to take | |
260 | * advantage of this persistence hook. | |
261 | * | |
262 | * @see org.apache.ojb.broker.PersistenceBrokerAware#beforeUpdate(org.apache.ojb.broker.PersistenceBroker) | |
263 | */ | |
264 | public final void beforeUpdate(PersistenceBroker persistenceBroker) throws PersistenceBrokerException { | |
265 | 0 | preUpdate(); |
266 | 0 | } |
267 | ||
268 | /** | |
269 | * Default implementation of the JPA {@link PreUpdate} hook which generates the unique objectId for this | |
270 | * persistable business object if it does not already have one. Any sub-class which overrides this method | |
271 | * should take care to invoke super.preUpdate to ensure that the objectId for this persistable | |
272 | * business object is generated properly. | |
273 | * | |
274 | * <p>This method is currently invoked by the corresponding OJB {@link #beforeUpdate(PersistenceBroker)} hook. | |
275 | */ | |
276 | @PreUpdate | |
277 | protected void preUpdate() { | |
278 | 0 | generateAndSetObjectIdIfNeeded(); |
279 | 0 | } |
280 | ||
281 | /** | |
282 | * If this PersistableBusinessObject does not already have a unique objectId, this method will generate | |
283 | * one and set it's value on this object. | |
284 | */ | |
285 | private void generateAndSetObjectIdIfNeeded() { | |
286 | 0 | if (StringUtils.isEmpty(getObjectId())) { |
287 | 0 | setObjectId(UUID.randomUUID().toString()); |
288 | } | |
289 | 0 | } |
290 | ||
291 | /** | |
292 | * getService Refreshes the reference objects from the primitive values. | |
293 | * | |
294 | * @see org.kuali.rice.kns.bo.BusinessObject#refresh() | |
295 | */ | |
296 | public void refresh() { | |
297 | 0 | getPersistenceService().retrieveNonKeyFields(this); |
298 | 0 | } |
299 | ||
300 | /** | |
301 | * @see BusinessObject#refresh() | |
302 | */ | |
303 | public void refreshNonUpdateableReferences() { | |
304 | 0 | getPersistenceService().refreshAllNonUpdatingReferences(this); |
305 | 0 | } |
306 | ||
307 | public void refreshReferenceObject(String referenceObjectName) { | |
308 | 0 | if ( StringUtils.isNotBlank(referenceObjectName) && !StringUtils.equals(referenceObjectName, "extension")) { |
309 | 0 | final PersistenceStructureService pss = getPersistenceStructureService(); |
310 | 0 | if ( pss.hasReference(this.getClass(), referenceObjectName) || pss.hasCollection(this.getClass(), referenceObjectName)) { |
311 | 0 | getPersistenceService().retrieveReferenceObject( this, referenceObjectName); |
312 | } else { | |
313 | 0 | LOG.warn( "refreshReferenceObject() called with non-reference property: " + referenceObjectName ); |
314 | } | |
315 | } | |
316 | 0 | } |
317 | ||
318 | /** | |
319 | * @see PersistableBusinessObject#buildListOfDeletionAwareLists() | |
320 | */ | |
321 | public List<Collection<PersistableBusinessObject>> buildListOfDeletionAwareLists() { | |
322 | 0 | return new ArrayList<Collection<PersistableBusinessObject>>(); |
323 | } | |
324 | ||
325 | public void linkEditableUserFields() { | |
326 | // do nothing | |
327 | 0 | } |
328 | ||
329 | public PersistableBusinessObjectExtension getExtension() { | |
330 | 0 | if ( extension == null ) { |
331 | try { | |
332 | 0 | Class<? extends PersistableBusinessObjectExtension> extensionClass = getPersistenceStructureService().getBusinessObjectAttributeClass( getClass(), "extension" ); |
333 | 0 | if ( extensionClass != null ) { |
334 | 0 | extension = extensionClass.newInstance(); |
335 | } | |
336 | 0 | } catch ( Exception ex ) { |
337 | 0 | LOG.error( "unable to create extension object", ex ); |
338 | 0 | } |
339 | } | |
340 | 0 | return extension; |
341 | } | |
342 | ||
343 | public void setExtension(PersistableBusinessObjectExtension extension) { | |
344 | 0 | this.extension = extension; |
345 | 0 | } |
346 | ||
347 | /** | |
348 | * @return the persistenceService | |
349 | */ | |
350 | protected static PersistenceService getPersistenceService() { | |
351 | 0 | if ( persistenceService == null ) { |
352 | 0 | persistenceService = KRADServiceLocator.getPersistenceService(); |
353 | } | |
354 | 0 | return persistenceService; |
355 | } | |
356 | ||
357 | protected static PersistenceStructureService getPersistenceStructureService() { | |
358 | 0 | if ( persistenceStructureService == null ) { |
359 | 0 | persistenceStructureService = KRADServiceLocator.getPersistenceStructureService(); |
360 | } | |
361 | 0 | return persistenceStructureService; |
362 | } | |
363 | ||
364 | } |