Skip to content

Commit

Permalink
Minor bug fixes in OCM for handling null cases. Cleaned up debug mess…
Browse files Browse the repository at this point in the history
…ages.
  • Loading branch information
johnament committed Aug 4, 2011
1 parent 02b62a4 commit 3211b93
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
Expand Up @@ -16,13 +16,15 @@
package org.jboss.seam.jcr.annotations.ocm;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Target({FIELD})
@Target({FIELD, METHOD})
@Retention(RUNTIME)
@Documented
public @interface JcrProperty {
Expand Down
7 changes: 3 additions & 4 deletions api/src/main/java/org/jboss/seam/jcr/ocm/OCMHandler.java
Expand Up @@ -73,7 +73,7 @@ public Object handle(InvocationContext ctx) {
Object result = returnType.newInstance();
ConvertToObject event = new ConvertToObject(foundNode,result);
ctoEvent.fire(event);
System.out.println("Returning the result "+event);
logger.debug("Returning the result "+event);
return result;
} else if (method.isAnnotationPresent(JcrQuery.class)) {
JcrQuery jcrQuery = method.getAnnotation(JcrQuery.class);
Expand Down Expand Up @@ -127,8 +127,7 @@ public Object handle(InvocationContext ctx) {
}
}
} catch (Exception e) {
System.out.println("Unable to handle message");
e.printStackTrace();
logger.error("Unable to handle conversion request",e);
} finally {
if(session != null) {
try {
Expand All @@ -139,7 +138,7 @@ public Object handle(InvocationContext ctx) {
session.logout();
}
}
System.out.println("Returning null");
logger.debug("Returning null from OCMHandler.");
return null;
}
}
43 changes: 25 additions & 18 deletions impl/src/main/java/org/jboss/seam/jcr/ocm/NodeConverter.java
Expand Up @@ -52,24 +52,28 @@ public <T> void convertNodeToObject(javax.jcr.Node node, Class<?> nodeType, Obje
if(field != null && jcrProperty.equalsIgnoreCase("uuid")) {
value = node.getIdentifier();
} else {
Property property = node.getProperty(jcrProperty);
if(field != null && property != null) {
Class<?> fieldType = field.getType();
if(fieldType.equals(java.util.Calendar.class)) {
value = property.getDate();
} else if(fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
value = property.getBoolean();
} else if(fieldType.equals(double.class) || fieldType.equals(Double.class)) {
value = property.getDouble();
} else if(fieldType.equals(BigDecimal.class)) {
value = property.getDecimal();
} else if(fieldType.equals(Long.class) || fieldType.equals(long.class)) {
value = property.getLong();
} else if(fieldType.equals(String.class)) {
value = property.getString();
} else {
logger.warnf("invalid field type %s",field);
try{
Property property = node.getProperty(jcrProperty);
if(field != null && property != null) {
Class<?> fieldType = field.getType();
if(fieldType.equals(java.util.Calendar.class)) {
value = property.getDate();
} else if(fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
value = property.getBoolean();
} else if(fieldType.equals(double.class) || fieldType.equals(Double.class)) {
value = property.getDouble();
} else if(fieldType.equals(BigDecimal.class)) {
value = property.getDecimal();
} else if(fieldType.equals(Long.class) || fieldType.equals(long.class)) {
value = property.getLong();
} else if(fieldType.equals(String.class)) {
value = property.getString();
} else {
logger.warnf("invalid field type %s",field);
}
}
} catch (RepositoryException e) {
logger.debug("No property found "+jcrProperty,e);
}
}
if(value != null) {
Expand All @@ -84,10 +88,13 @@ public <T> void objectToNode(T object, javax.jcr.Node node) throws RepositoryExc
Class<?> nodeType = object.getClass();
OCMMapping mapping = ocmExtension.getOCMMappingStore().findMapping(nodeType);
Set<String> jcrProperties = mapping.getPropertiesToFields().keySet();
logger.debug("The properties: "+jcrProperties);
for(String jcrProperty : jcrProperties) {
Field field = mapping.getPropertiesToFields().get(jcrProperty);
logger.debugf("Searched for property [%s] and retrieved field [%s]", jcrProperty, field);
String getterMethodName = "get"+field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1);
Method method = Reflections.findDeclaredMethod(nodeType, getterMethodName);
logger.debug("Found method : "+method);
Object value = Reflections.invokeMethod(method, object);
if(field != null && jcrProperty.equalsIgnoreCase("uuid")) {
//don't set UUID
Expand All @@ -105,7 +112,7 @@ public <T> void objectToNode(T object, javax.jcr.Node node) throws RepositoryExc
} else if(fieldType.equals(Long.class) || fieldType.equals(long.class)) {

This comment has been minimized.

Copy link
@gastaldi

gastaldi Aug 4, 2011

Member

I don´t think this line may work. Better use Long.TYPE instead of long.class

This comment has been minimized.

Copy link
@johnament

johnament Aug 4, 2011

Author Member

You mean:

} else if(fieldType.equals(Long.class) || fieldType.equals(Long.TYPE)) {

Never had to do that before.

This comment has been minimized.

Copy link
@gastaldi

gastaldi via email Aug 4, 2011

Member
node.setProperty(jcrProperty, (Long)value);
} else if(fieldType.equals(String.class)) {
node.setProperty(jcrProperty, value.toString());
node.setProperty(jcrProperty, (String)value);
} else {
logger.warnf("invalid field type %s",field);
}
Expand Down
18 changes: 18 additions & 0 deletions impl/src/main/java/org/jboss/seam/jcr/ocm/OCMMappingStore.java
Expand Up @@ -16,7 +16,10 @@
package org.jboss.seam.jcr.ocm;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.inject.spi.AnnotatedType;
Expand Down Expand Up @@ -52,19 +55,34 @@ void map(AnnotatedType<?> annotatedType, JcrNode jcrNode) {
mapping.setNodeType(jcrNode.value());
Class<?> clazz = (Class<?>)annotatedType.getBaseType();
Field[] fields = clazz.getDeclaredFields();
Method[] methods = clazz.getMethods();
mapping.setNodeClass(clazz);
Map<String,JcrProperty> properties = new HashMap<String, JcrProperty>();
for(Method method : methods) {
if(method.isAnnotationPresent(JcrProperty.class)) {
String fieldName = getterToFieldName(method.getName());
properties.put(fieldName, method.getAnnotation(JcrProperty.class));
}
}
for(Field field : fields) {
logger.debugf("field name: %s",field.getName());
String fieldName = field.getName();
String prop = field.getName();
JcrProperty property = field.getAnnotation(JcrProperty.class);
if(property != null) {
prop = property.value();
} else if(properties.containsKey(prop)) {
prop = properties.get(prop).value();
}
logger.debugf("fieldName: %s prop: %s\n", fieldName,prop);
mapping.getPropertiesToFields().put(prop,field);
mapping.getFieldsToProperties().put(fieldName,prop);
}
addMapping(mapping);
}

private static String getterToFieldName(String methodName) {
String fieldName = methodName.replace("get", "");
return fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);
}
}
Expand Up @@ -23,6 +23,7 @@ public class BasicNode implements java.io.Serializable {
@JcrProperty("myvalue")
private String value;
private String uuid;
private String lordy;

public String getValue() {
return value;
Expand All @@ -39,5 +40,13 @@ public String getUuid() {
public void setUuid(String uuid) {
this.uuid = uuid;
}
@JcrProperty("notaproperty")
public String getLordy() {
return lordy;
}

public void setLordy(String lordy) {
this.lordy = lordy;
}

}
Expand Up @@ -66,11 +66,13 @@ public void testRun() {
OCMMappingStore ocmMappingStore = extension.getOCMMappingStore();
OCMMapping mapping = ocmMappingStore.findMapping(BasicNode.class);
Assert.assertNotNull(mapping);
Assert.assertEquals(2,mapping.getFieldsToProperties().size());
Assert.assertEquals(3,mapping.getFieldsToProperties().size());
String result = mapping.getFieldsToProperties().get("value");
Assert.assertEquals("myvalue", result);
String uuid = mapping.getFieldsToProperties().get("uuid");
Assert.assertEquals("uuid", uuid);
String notaproperty = mapping.getFieldsToProperties().get("lordy");
Assert.assertEquals("notaproperty", notaproperty);
}

@Test
Expand All @@ -80,10 +82,12 @@ public void testCreateNodeAndOCM() throws Exception {
Node root = session.getRootNode();
Node hello = root.addNode("ocmnode1","nt:unstructured");
hello.setProperty("myvalue", "Hello, World!");
hello.setProperty("notaproperty", "this was saved.");

Node hello2 = root.getNode("ocmnode1");
BasicNode bn = nodeConverter.nodeToObject(hello2, BasicNode.class);
Assert.assertEquals("Hello, World!", bn.getValue());
Assert.assertEquals("this was saved.", bn.getLordy());

Node hello3 = root.addNode("ocmnode3", "nt:unstructured");
session.save();
Expand Down

0 comments on commit 3211b93

Please sign in to comment.