2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache 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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.persistence;
18 import java.io.Serializable;
19 import java.lang.annotation.Annotation;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.Method;
22 import java.util.List;
24 import java.util.concurrent.ConcurrentHashMap;
26 import javax.persistence.Id;
27 import javax.persistence.Version;
29 import org.wamblee.reflection.ReflectionUtils;
32 * Factory which creates a {@link Persistent} object for a given JPA entity for
33 * interfacing with the primary key and version of the entity.
35 * This utility only treats primary keys and fields that are annotated with @Id
36 * and @Version. In case ORM files are used for the definition of primary key
37 * and or version, then those fields are ignored.
39 * @author Erik Brakkee
42 public class PersistentFactory {
45 * Cache of a mapping of class names to entity accessors.
47 private static Map<String, EntityAccessor> CACHE = new ConcurrentHashMap<String, EntityAccessor>();
49 static class EntityAccessor {
51 private Accessor version;
53 public EntityAccessor(Accessor<?> aPk, Accessor<?> aVersion) {
58 public Accessor getPk() {
62 public Accessor getVersion() {
67 public static class EntityObjectAccessor implements Persistent {
68 private EntityAccessor accessor;
69 private Object entity;
71 public EntityObjectAccessor(Object aEntity, EntityAccessor aAccessor) {
76 public EntityAccessor getAccessor() {
81 public Serializable getPrimaryKey() {
82 if (accessor == null || accessor.getPk() == null) {
85 return (Serializable) accessor.getPk().get(entity);
89 public void setPrimaryKey(Serializable aKey) {
90 if (accessor == null || accessor.getPk() == null) {
93 accessor.getPk().set(entity, aKey);
97 public Number getPersistedVersion() {
98 if (accessor == null || accessor.getVersion() == null) {
101 return (Number) accessor.getVersion().get(entity);
105 public void setPersistedVersion(Number aVersion) {
106 if (accessor == null || accessor.getVersion() == null) {
109 accessor.getVersion().set(entity, aVersion);
114 * Create the entity accessor for a given class or returns a cached instance
115 * if one already exists.
119 * @return Entity accessor for the given class or null of the given object
122 public static EntityAccessor createEntityAccessor(Class aClass) {
123 EntityAccessor accessor = CACHE.get(aClass.getName());
124 if (accessor == null) {
125 accessor = analyse(aClass);
126 if (accessor != null) {
127 CACHE.put(aClass.getName(), accessor);
133 private static EntityAccessor analyse(Class aClass) {
134 Accessor<Serializable> pk = analyse(aClass, Id.class);
135 Accessor<Integer> version = analyse(aClass, Version.class);
136 if (pk != null || version != null) {
137 return new EntityAccessor(pk, version);
143 * Returns the accessor for a given annotation.
148 * Annotation that must be present.
149 * @return Accessor to use or null if the annotation is not present.
151 // TODO move generic analysis part to the reflection package.
152 public static Accessor analyse(Class aClass,
153 Class<? extends Annotation> aAnnotation) {
154 List<Field> fields = ReflectionUtils.getAllFields(aClass);
155 for (Field field : fields) {
156 if (field.isAnnotationPresent(aAnnotation)) {
157 return new FieldAccessor(field);
160 List<Method> methods = ReflectionUtils.getAllMethods(aClass,
162 for (Method method : methods) {
163 if (method.isAnnotationPresent(aAnnotation)) {
164 String setterName = null;
165 if (method.getName().startsWith("get")) {
166 setterName = method.getName().replaceFirst("get", "set");
167 } else if (method.getName().startsWith("is")) {
168 setterName = method.getName().replaceFirst("is", "set");
171 Class returnType = method.getReturnType();
172 Method setter = method.getDeclaringClass()
173 .getDeclaredMethod(setterName, returnType);
174 return new PropertyAccessor(method, setter);
175 } catch (NoSuchMethodException e) {
176 throw new RuntimeException("Error obtaining setter for " +
177 method.getName() + " in class " + aClass.getName(), e);
185 * Creates the {@link Persistent} wrapper for interfacing with primary key
186 * and version of the entity.
190 * @return Persistent object or null if this is not an entity.
192 public static Persistent create(Object aEntity) {
193 EntityAccessor accessor = createEntityAccessor(aEntity.getClass());
194 if (accessor == null) {
197 return new EntityObjectAccessor(aEntity, accessor);