Skip to content

Commit

Permalink
Merge pull request #86 from openEHR/fix_rm_annotations_55
Browse files Browse the repository at this point in the history
Add annotations to ignore properties or override property names.
  • Loading branch information
pieterbos authored Mar 5, 2019
2 parents 7f796a6 + f1370eb commit d0d0a03
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 47 deletions.
5 changes: 4 additions & 1 deletion aom/src/main/java/com/nedap/archie/aom/AuthoredResource.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.nedap.archie.aom;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.nedap.archie.base.terminology.TerminologyCode;
import com.nedap.archie.rminfo.RMPropertyIgnore;
import com.nedap.archie.xml.adapters.ResourceDescriptionAdapter;
import com.nedap.archie.xml.adapters.TranslationDetailsAdapter;

Expand Down Expand Up @@ -87,6 +87,7 @@ public void setOriginalLanguage(TerminologyCode originalLanguage) {
}

@XmlTransient
@Nullable
public Map<String, TranslationDetails> getTranslations() {
if(content == null) {
return null;
Expand All @@ -104,6 +105,7 @@ public void setTranslations(Map<String, TranslationDetails> translations) {
@XmlElement(name="translations")
@XmlJavaTypeAdapter(TranslationDetailsAdapter.class)
@JsonIgnore
@RMPropertyIgnore
public List<TranslationDetails> getTranslationList() { return new ArrayList(content.getTranslations().values());}

public void setTranslationList(List<TranslationDetails> translationList) {
Expand All @@ -129,6 +131,7 @@ public void setAnnotations(ResourceAnnotations annotations) {
*/
@JsonIgnore
@XmlTransient
@RMPropertyIgnore
public LanguageSection getAuthoredResourceContent() {
return content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,25 @@ public String getTypeName(Class clazz) {
}

@Override
public String getAttributeName(Method method) {
public String getAttributeName(Field field, Method method) {
if(field != null) {
RMProperty annotation = field.getDeclaredAnnotation(RMProperty.class);
if (annotation != null) {
return annotation.value();
}
}

if(method != null) {
RMProperty annotation = method.getDeclaredAnnotation(RMProperty.class);
if(annotation != null) {
return annotation.value();
}
}

if(field != null) {
return snakeCaseStrategy.translate(field.getName());
}

if(method.getName().startsWith("get") ||
method.getName().startsWith("set") ||
method.getName().startsWith("add") ) {
Expand All @@ -50,9 +68,4 @@ public String getAttributeName(Method method) {
}
return method.getName();
}

@Override
public String getAttributeName(Field field) {
return snakeCaseStrategy.translate(field.getName());
}
}
15 changes: 15 additions & 0 deletions aom/src/main/java/com/nedap/archie/rminfo/RMProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nedap.archie.rminfo;

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

/**
* Annotation to override name of RM property.
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RMProperty {
String value();
}
15 changes: 15 additions & 0 deletions aom/src/main/java/com/nedap/archie/rminfo/RMPropertyIgnore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.nedap.archie.rminfo;

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

/**
* Mark methods as to be ignored by ReflectionModelInfoLookup, because they are not specified as a property in the model specification (eg openehr RM),
* but just an extra method
*/
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RMPropertyIgnore {
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,33 @@ private void addAttributeInfo(Class clazz, RMTypeInfo typeInfo) {
}
if(addAttributesWithoutField) {
Set<Method> getters = ReflectionUtils.getAllMethods(clazz, (method) -> method.getName().startsWith("get") || method.getName().startsWith("is"));
for (Method method : getters) {
if(shouldAdd(method)) {
addRMAttributeInfo(clazz, typeInfo, typeToken, method, fieldsByName);
for (Method getMethod : getters) {
if(shouldAdd(getMethod)) {
addRMAttributeInfo(clazz, typeInfo, typeToken, getMethod, fieldsByName);
}
}
}
}

protected boolean shouldAdd(Method method) {
if(method == null) {
return true;
}
//do not add private or protected properties, they will result in errors
return Modifier.isPublic(method.getModifiers());
return Modifier.isPublic(method.getModifiers()) && method.getAnnotation(RMPropertyIgnore.class) == null;
}

protected void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken typeToken, Method getMethod, Map<String, Field> fieldsByName) {
String attributeName = namingStrategy.getAttributeName(getMethod);
String javaFieldName = null;
if(getMethod.getName().startsWith("is")) {
javaFieldName = lowerCaseFirstChar(getMethod.getName().substring(2));
} else {
javaFieldName = lowerCaseFirstChar(getMethod.getName().substring(3));
}
Field field = fieldsByName.get(javaFieldName);
if(field == null) {
field = fieldsByName.get(getMethod.getName());
}
String javaFieldNameUpperCased = upperCaseFirstChar(javaFieldName);
Method setMethod = null, addMethod = null;

Expand All @@ -184,11 +189,12 @@ protected void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken ty
}
if (getMethod != null) {
setMethod = getMethod(clazz, "set" + javaFieldNameUpperCased, getMethod.getReturnType());
addMethod = getAddMethod(clazz, typeToken, attributeName, javaFieldNameUpperCased, getMethod);
addMethod = getAddMethod(clazz, typeToken, javaFieldNameUpperCased, getMethod);
} else {
logger.debug("No get method found for attribute {} on class {}", attributeName, clazz.getSimpleName());
logger.debug("No get method found for attribute {} on class {}", javaFieldName, clazz.getSimpleName());
}

String attributeName = namingStrategy.getAttributeName(field, getMethod);

TypeToken fieldType = typeToken.resolveType(getMethod.getGenericReturnType());;

Expand Down Expand Up @@ -220,7 +226,6 @@ protected boolean isNullable(Class clazz, Method getMethod, Field field) {


private void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken typeToken, Field field) {
String attributeName = namingStrategy.getAttributeName(field);
String javaFieldName = field.getName();
String javaFieldNameUpperCased = upperCaseFirstChar(javaFieldName);
Method getMethod = getMethod(clazz, "get" + javaFieldNameUpperCased);
Expand All @@ -230,12 +235,12 @@ private void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken type
}
if (getMethod != null) {
setMethod = getMethod(clazz, "set" + javaFieldNameUpperCased, getMethod.getReturnType());
addMethod = getAddMethod(clazz, typeToken, attributeName, javaFieldNameUpperCased, getMethod);
addMethod = getAddMethod(clazz, typeToken, javaFieldNameUpperCased, getMethod);
} else {
logger.debug("No get method found for field {} on class {}", field.getName(), clazz.getSimpleName());
}

if(attributeName.startsWith("is")) {
if(javaFieldName.startsWith("is")) {
//special case
String fieldNameWithoutPrefix = javaFieldName.substring(2);
String withoutPrefixUpperCased = upperCaseFirstChar(fieldNameWithoutPrefix);
Expand All @@ -247,13 +252,14 @@ private void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken type
setMethod = getMethod(clazz, "set" + withoutPrefixUpperCased, getMethod.getReturnType());
}
if(addMethod == null) {
addMethod = getAddMethod(clazz, typeToken, attributeName, withoutPrefixUpperCased, getMethod);
addMethod = getAddMethod(clazz, typeToken, withoutPrefixUpperCased, getMethod);
}
} else {
logger.debug("No get method found for attribute {} on class {}", attributeName, clazz.getSimpleName());
logger.debug("No get method found for attribute {} on class {}", javaFieldName, clazz.getSimpleName());
}

}
String attributeName = namingStrategy.getAttributeName(field, getMethod);

TypeToken fieldType = null;
if (getMethod != null) {
Expand All @@ -264,7 +270,7 @@ private void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken type

Class rawFieldType = fieldType.getRawType();
Class typeInCollection = getTypeInCollection(fieldType);
if (setMethod != null && shouldAdd(setMethod)) {
if (setMethod != null && (shouldAdd(setMethod) && shouldAdd(getMethod))) {
RMAttributeInfo attributeInfo = new RMAttributeInfo(
attributeName,
field,
Expand Down Expand Up @@ -301,7 +307,7 @@ private Class getTypeInCollection(TypeToken fieldType) {
return rawFieldType;
}

private Method getAddMethod(Class clazz, TypeToken typeToken, String name, String javaFieldNameUpperCased, Method getMethod) {
private Method getAddMethod(Class clazz, TypeToken typeToken, String javaFieldNameUpperCased, Method getMethod) {
Method addMethod = null;
if (Collection.class.isAssignableFrom(getMethod.getReturnType())) {
Type[] typeArguments = ((ParameterizedType) getMethod.getGenericReturnType()).getActualTypeArguments();
Expand All @@ -316,7 +322,7 @@ private Method getAddMethod(Class clazz, TypeToken typeToken, String name, Strin
if (allAddMethods.size() == 1) {
addMethod = allAddMethods.iterator().next();
} else {
logger.debug("strange number of add methods for field {} on class {}", name, clazz.getSimpleName());
logger.debug("strange number of add methods for field {} on class {}", javaFieldNameUpperCased, clazz.getSimpleName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nedap.archie.paths.PathSegment;
import com.nedap.archie.rm.support.identification.UIDBasedId;
import com.nedap.archie.rm.datavalues.DvText;
import com.nedap.archie.rminfo.RMPropertyIgnore;

import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -97,6 +98,7 @@ public void addLink(Link link) {

@Override
@JsonIgnore
@RMPropertyIgnore
public List<PathSegment> getPathSegments() {
Pathable parent = getParent();
if(parent == null) {
Expand All @@ -109,6 +111,7 @@ public List<PathSegment> getPathSegments() {
}

@JsonIgnore
@RMPropertyIgnore
public String getNameAsString() {
return name == null ? null : name.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.nedap.archie.query.RMPathQuery;
import com.nedap.archie.rm.RMObject;
import com.nedap.archie.rminfo.ArchieRMInfoLookup;
import com.nedap.archie.rminfo.RMPropertyIgnore;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

Expand Down Expand Up @@ -86,19 +88,7 @@ protected String getParentAttributeName() {
return parentAttributeName;
}

/**
* Return the path of an item relative to this item
* @param item
* @return
*/
// public String pathOfItem(Pathable item) {
//
// }
//
// public String path() {
//
// }

@RMPropertyIgnore
public List<PathSegment> getPathSegments() {
Pathable parent = getParent();
if(parent == null) {
Expand All @@ -117,6 +107,7 @@ public List<PathSegment> getPathSegments() {
* API subject to change in the future!
* @return
*/
@RMPropertyIgnore
public final String getPath() {
return PathUtil.getPath(getPathSegments());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nedap.archie.rm.datavalues.DvCodedText;
import com.nedap.archie.rm.generic.Attestation;
import com.nedap.archie.rm.support.identification.ObjectVersionId;
import com.nedap.archie.rminfo.RMProperty;


import javax.annotation.Nullable;
Expand Down Expand Up @@ -65,6 +66,7 @@ public String getCanonicalForm() {
}

@Override
@RMProperty("is_branch")
public boolean isBranch() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.nedap.archie.rm.datavalues.DvCodedText;
import com.nedap.archie.rm.generic.AuditDetails;
import com.nedap.archie.rm.support.identification.ObjectVersionId;
import com.nedap.archie.rminfo.RMProperty;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -60,6 +61,7 @@ public void setCommitAudit(AuditDetails commitAudit) {
//
// }

@RMProperty("is_branch")
public abstract boolean isBranch();


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nedap.archie.rm.datastructures;

import com.google.common.collect.Lists;
import com.nedap.archie.rminfo.RMPropertyIgnore;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -28,6 +29,7 @@ public void setItem(Element item) {
}

@Override
@RMPropertyIgnore
public List<Element> getItems() {
return Lists.newArrayList(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nedap.archie.rm.datastructures;

import com.nedap.archie.rminfo.RMPropertyIgnore;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
Expand All @@ -13,7 +15,7 @@
public abstract class ItemStructure<Type extends Item> extends DataStructure {

/** In the default model it's in the subclasses, but defined here as well because it has a lot of uses */
//TODO: add a @MetaModelIgnore annotation - this should be ignored by the ModelInfoLookup
@RMPropertyIgnore
public abstract List<Type> getItems();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nedap.archie.rm.datastructures;

import com.nedap.archie.rminfo.RMPropertyIgnore;

import javax.annotation.Nullable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -37,6 +39,7 @@ public void addItem(Cluster row) {

/** This is a bit of a strange one - returns all elements present in the table. Use getRows instead*/
@Override
@RMPropertyIgnore
public List<Element> getItems() {
if(rows == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nedap.archie.rm.datavalues;

import com.nedap.archie.rminfo.RMProperty;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
Expand All @@ -16,6 +18,7 @@
public class DvState extends DataValue implements SingleValuedDataValue<DvCodedText> {

@XmlElement(name = "is_terminal")
@RMProperty("is_terminal")
private boolean isTerminal;
private DvCodedText value;

Expand Down
Loading

0 comments on commit d0d0a03

Please sign in to comment.