additions for testability.
[utils] / support / general / src / main / java / org / wamblee / persistence / PersistentFactory.java
index 91fd2b380095a66707aa578b7a0efb29dfb16059..944bd34aa3a028df0543626e86205f70bfb6fcbb 100644 (file)
@@ -18,7 +18,6 @@ package org.wamblee.persistence;
 import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.List;
 import java.util.Map;
@@ -30,7 +29,7 @@ import javax.persistence.Version;
 import org.wamblee.reflection.ReflectionUtils;
 
 /**
- * Factory which creates a {@link Persistent} object for a given entity for
+ * Factory which creates a {@link Persistent} object for a given JPA entity for
  * interfacing with the primary key and version of the entity.
  * 
  * This utility only treats primary keys and fields that are annotated with @Id
@@ -47,7 +46,7 @@ public class PersistentFactory {
      */
     private static Map<String, EntityAccessor> CACHE = new ConcurrentHashMap<String, EntityAccessor>();
 
-    static interface Accessor<T> {
+    public static interface Accessor<T> {
         void set(Object aEntity, T aValue);
 
         T get(Object aEntity);
@@ -64,10 +63,11 @@ public class PersistentFactory {
         @Override
         public T get(Object aEntity) {
             try {
-                return (T) field.get(aEntity);
+                T value = (T) field.get(aEntity);
+                return value;
             } catch (Exception e) {
                 throw new RuntimeException(e);
-            } 
+            }
         }
 
         @Override
@@ -110,7 +110,7 @@ public class PersistentFactory {
                 setter.invoke(aEntity, aValue);
             } catch (Exception e) {
                 throw new RuntimeException(e);
-            } 
+            }
         }
 
         public Method getGetter() {
@@ -155,21 +155,33 @@ public class PersistentFactory {
 
         @Override
         public Serializable getPrimaryKey() {
-            return (Serializable)accessor.getPk().get(entity);
+            if (accessor == null || accessor.getPk() == null) {
+                return null;
+            }
+            return (Serializable) accessor.getPk().get(entity);
         }
 
         @Override
         public void setPrimaryKey(Serializable aKey) {
+            if (accessor == null || accessor.getPk() == null) {
+                return;
+            }
             accessor.getPk().set(entity, aKey);
-        } 
+        }
 
         @Override
         public Number getPersistedVersion() {
-            return (Number)accessor.getVersion().get(entity);
+            if (accessor == null || accessor.getVersion() == null) {
+                return null;
+            }
+            return (Number) accessor.getVersion().get(entity);
         }
 
         @Override
         public void setPersistedVersion(Number aVersion) {
+            if (accessor == null || accessor.getVersion() == null) {
+                return;
+            }
             accessor.getVersion().set(entity, aVersion);
         }
     }
@@ -212,7 +224,8 @@ public class PersistentFactory {
      *            Annotation that must be present.
      * @return Accessor to use or null if the annotation is not present.
      */
-    private static Accessor analyse(Class aClass,
+    // TODO move generic analysis part to the reflection package.
+    public static Accessor analyse(Class aClass,
         Class<? extends Annotation> aAnnotation) {
         List<Field> fields = ReflectionUtils.getAllFields(aClass);
         for (Field field : fields) {
@@ -232,7 +245,8 @@ public class PersistentFactory {
                 }
                 try {
                     Class returnType = method.getReturnType();
-                    Method setter = method.getDeclaringClass().getDeclaredMethod(setterName, returnType);
+                    Method setter = method.getDeclaringClass()
+                        .getDeclaredMethod(setterName, returnType);
                     return new PropertyAccessor(method, setter);
                 } catch (NoSuchMethodException e) {
                     throw new RuntimeException("Error obtaining setter for " +
@@ -244,15 +258,17 @@ public class PersistentFactory {
     }
 
     /**
-     * Creates the {@link Persistent} wrapper for interfacing with primary key and 
-     * version of the entity. 
-     * @param aEntity Entity to use. 
-     * @return Persistent object or null if this is not an entity. 
+     * Creates the {@link Persistent} wrapper for interfacing with primary key
+     * and version of the entity.
+     * 
+     * @param aEntity
+     *            Entity to use.
+     * @return Persistent object or null if this is not an entity.
      */
-    public static Persistent create(Object aEntity) { 
+    public static Persistent create(Object aEntity) {
         EntityAccessor accessor = createEntityAccessor(aEntity.getClass());
-        if ( accessor == null ) { 
-            return null; 
+        if (accessor == null) {
+            return null;
         }
         return new EntityObjectAccessor(aEntity, accessor);
     }