Skip to content

Commit

Permalink
Add annotations to describe properly data model: issue #777
Browse files Browse the repository at this point in the history
  • Loading branch information
PhantomYdn committed Mar 28, 2020
1 parent 2e8abf6 commit 53c6fab
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
8 changes: 7 additions & 1 deletion orienteer-core/src/main/java/org/orienteer/core/dao/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.lang.reflect.Proxy;

import org.apache.wicket.util.string.Strings;

import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.type.ODocumentWrapper;
Expand All @@ -18,7 +20,11 @@ private DAO() {
}

public static <T> T create(Class<T> interfaceClass, Class<?>... additionalInterfaces) {
return provide(interfaceClass, new ODocumentWrapper(), additionalInterfaces);
DAOOClass daoOClass = interfaceClass.getAnnotation(DAOOClass.class);
ODocumentWrapper docWrapper = daoOClass!=null && !Strings.isEmpty(daoOClass.value())
? new ODocumentWrapper(daoOClass.value())
: new ODocumentWrapper();
return provide(interfaceClass, docWrapper, additionalInterfaces);
}

public static <T> T create(Class<T> interfaceClass, String className, Class<?>... additionalInterfaces) {
Expand Down
16 changes: 16 additions & 0 deletions orienteer-core/src/main/java/org/orienteer/core/dao/DAOField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.orienteer.core.dao;

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

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

/**
* Optional annotation to mark getter/setter methods for ODocumentWrappers
*/
@Retention(RUNTIME)
@Target(METHOD)
public @interface DAOField {
String value() default "";
}
17 changes: 17 additions & 0 deletions orienteer-core/src/main/java/org/orienteer/core/dao/DAOOClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.orienteer.core.dao;

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

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

/**
* Optional annotation to mark interfaces which can be wrapped for DAO
*/
@Retention(RUNTIME)
@Target(TYPE)
public @interface DAOOClass {
String value();
String[] superClasses() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.lang.reflect.Method;

import org.apache.wicket.util.string.Strings;
import org.orienteer.core.dao.DAOField;
import org.orienteer.core.dao.IMethodHandler;
import org.orienteer.core.util.CommonUtils;

Expand All @@ -19,6 +21,8 @@ public ResultHolder handle(ODocumentWrapper target, Object proxy, Method method,
String methodName = method.getName();
if(methodName.startsWith("get")) name = CommonUtils.decapitalize(methodName.substring(3));
if(methodName.startsWith("is")) name = CommonUtils.decapitalize(methodName.substring(2));
DAOField fieldAnnotation = method.getAnnotation(DAOField.class);
if(fieldAnnotation!=null && !Strings.isEmpty(fieldAnnotation.value())) name = fieldAnnotation.value();
if(name!=null) {
Object value = target.getDocument().field(name, method.getReturnType());
return new ResultHolder(prepareForJava(value, method));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.lang.reflect.Method;

import org.apache.wicket.util.string.Strings;
import org.orienteer.core.dao.DAOField;
import org.orienteer.core.dao.IMethodHandler;
import org.orienteer.core.dao.IMethodHandler.ResultHolder;
import org.orienteer.core.util.CommonUtils;

import com.orientechnologies.orient.core.type.ODocumentWrapper;
Expand All @@ -16,7 +17,10 @@ public class ODocumentSetHandler extends AbstractMethodHandler<ODocumentWrapper>
@Override
public ResultHolder handle(ODocumentWrapper target, Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().startsWith("set") && args.length==1) {
target.getDocument().field(CommonUtils.decapitalize(method.getName().substring(3)), args[0]);
String name=CommonUtils.decapitalize(method.getName().substring(3));
DAOField fieldAnnotation = method.getAnnotation(DAOField.class);
if(fieldAnnotation!=null && !Strings.isEmpty(fieldAnnotation.value())) name = fieldAnnotation.value();
target.getDocument().field(name, args[0]);
return returnChained(proxy, method);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void testInjection() {
for (ODocument oDocument : perspectives) {
iOPerspective.fromStream(oDocument);
assertEquals(oDocument.field("alias"), iOPerspective.getAlias());
assertEquals(oDocument.field("alias"), iOPerspective.getAliasSynonymMethod());
assertEquals("test"+oDocument.field("alias"), iOPerspective.getTestAlias());
assertEquals("test2"+oDocument.field("alias"), iOPerspective.getTest2Alias());
assertEquals("test3test"+oDocument.field("alias"), iOPerspective.getTest3Alias());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public interface IOPerspective extends IODocumentWrapper {
public String getAlias();
public Map<String, Object> getName();

@DAOField("alias")
public String getAliasSynonymMethod();

default public String getTestAlias() {
return "test"+getAlias();
}
Expand Down

0 comments on commit 53c6fab

Please sign in to comment.