Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions tools/src/main/java/com/nedap/archie/FixedValueFiller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.nedap.archie;

import com.nedap.archie.aom.CAttribute;
import com.nedap.archie.aom.CObject;
import com.nedap.archie.aom.CPrimitiveObject;
import com.nedap.archie.aom.primitives.COrdered;
import com.nedap.archie.aom.primitives.CString;
import com.nedap.archie.base.Interval;
import com.nedap.archie.query.RMObjectWithPath;
import com.nedap.archie.rminfo.ModelInfoLookup;
import com.nedap.archie.rminfo.RMAttributeInfo;
import com.nedap.archie.rmobjectvalidator.APathQueryCache;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;

/**
* Fills fixed values from the archetype if the corresponding values are missing in the RM objects
*/
public class FixedValueFiller {

private final ModelInfoLookup lookup;

private final APathQueryCache queryCache = new APathQueryCache();

Check warning on line 25 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L25

Added line #L25 was not covered by tests

public FixedValueFiller(ModelInfoLookup lookup) {
this.lookup = lookup;
}

Check warning on line 29 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L27-L29

Added lines #L27 - L29 were not covered by tests

/**
* Recursively fill fixed values.
*
* @param rmObject RM object to fill fixed values in.
* @param cObject CObject to retrieve the fixed values from.
*/
public void fillFixedValues(Object rmObject, CObject cObject) {
// Do not process the constraint if the constraint type is not assignable from the data type
// Examples:
// - DV_CODED_TEXT constraint with DV_TEXT data: do not continue
// - DV_TEXT constraint with DV_TEXT data: continue
// - DV_TEXT constraint with DV_CODED_TEXT data: continue
Class<?> rmClassOfCObject = lookup.getClass(cObject.getRmTypeName());

Check warning on line 43 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L43

Added line #L43 was not covered by tests
if(rmClassOfCObject != null && !rmClassOfCObject.isAssignableFrom(rmObject.getClass())) {
return; //stop filling fixed values, the types do not match

Check warning on line 45 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L45

Added line #L45 was not covered by tests
}

for (CAttribute cAttribute : cObject.getAttributes()) {
fillFixedValues(rmObject, cAttribute);
}
}

Check warning on line 51 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L49-L51

Added lines #L49 - L51 were not covered by tests

/**
* Recursively fill fixed values for an attribute.
*
* @param rmObject RM object to fill fixed values in.
* @param cAttribute CAttribute to retrieve the fixed values from.
*/
private void fillFixedValues(Object rmObject, CAttribute cAttribute) {
if(cAttribute.getChildren() == null) {
return;

Check warning on line 61 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L61

Added line #L61 was not covered by tests
}

String rmAttributeName = cAttribute.getRmAttributeName();
RMAttributeInfo attributeInfo = lookup.getAttributeInfo(rmObject.getClass(), rmAttributeName);

Check warning on line 65 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L64-L65

Added lines #L64 - L65 were not covered by tests

if(!attributeInfo.isMultipleValued() && getValue(rmObject, attributeInfo) == null && cAttribute.getChildren().size() == 1) {
// Fill fixed value (if any) if the attribute is singular, empty and has only one constraint.
CObject cObject = cAttribute.getChildren().get(0);

Check warning on line 69 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L69

Added line #L69 was not covered by tests
if (cObject instanceof CPrimitiveObject) {
Object fixedValue = getPrimitiveFixedValue((CPrimitiveObject<?, ?>) cObject);

Check warning on line 71 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L71

Added line #L71 was not covered by tests
if (fixedValue != null) {
setValue(rmObject, attributeInfo, fixedValue);

Check warning on line 73 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L73

Added line #L73 was not covered by tests
}
}
} else {

Check warning on line 76 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L76

Added line #L76 was not covered by tests
// Recursively fill fixed values for the exising values
for (CObject cObject : cAttribute.getChildren()) {
List<RMObjectWithPath> values;
values = queryCache.getApathQuery("/" + rmAttributeName + "[" + cObject.getNodeId() + "]").findList(lookup, rmObject);

Check warning on line 80 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L80

Added line #L80 was not covered by tests

for (RMObjectWithPath rmObjectWithPath : values) {
fillFixedValues(rmObjectWithPath.getObject(), cObject);
}
}

Check warning on line 85 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L83-L85

Added lines #L83 - L85 were not covered by tests
}
}

Check warning on line 87 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L87

Added line #L87 was not covered by tests

private Object getPrimitiveFixedValue(CPrimitiveObject<?, ?> cobject) {
Object fixedValue = getFixedValue(cobject);

Check warning on line 90 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L90

Added line #L90 was not covered by tests
if(fixedValue != null) {
return lookup.convertConstrainedPrimitiveToRMObject(fixedValue);

Check warning on line 92 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L92

Added line #L92 was not covered by tests
} else {
return null;

Check warning on line 94 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L94

Added line #L94 was not covered by tests
}
}

private static Object getFixedValue(CPrimitiveObject<?, ?> cObject) {
if(cObject instanceof COrdered) {
return getFixedValueForOrdered((COrdered<?>) cObject);

Check warning on line 100 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L100

Added line #L100 was not covered by tests
} else if (cObject instanceof CString) {
return getFixedValueForString((CString) cObject);

Check warning on line 102 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L102

Added line #L102 was not covered by tests
}
return null;

Check warning on line 104 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L104

Added line #L104 was not covered by tests
}

private static Object getFixedValueForString(CString cString) {
List<String> constraint = cString.getConstraint();

Check warning on line 108 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L108

Added line #L108 was not covered by tests
if(constraint != null && constraint.size() == 1) {
String singleConstraint = constraint.get(0);

Check warning on line 110 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L110

Added line #L110 was not covered by tests
if(!CString.isRegexConstraint(singleConstraint)) {
return singleConstraint;

Check warning on line 112 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L112

Added line #L112 was not covered by tests
}
}
return null;

Check warning on line 115 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L115

Added line #L115 was not covered by tests
}

private static <T> Object getFixedValueForOrdered(COrdered<T> cOrdered) {
List<Interval<T>> constraint = cOrdered.getConstraint();

Check warning on line 119 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L119

Added line #L119 was not covered by tests
if(constraint != null && constraint.size() == 1) {
Interval<T> interval = constraint.get(0);

Check warning on line 121 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L121

Added line #L121 was not covered by tests
if(!interval.isLowerUnbounded() && !interval.isUpperUnbounded() &&
interval.isLowerIncluded() && interval.isUpperIncluded()
&& Objects.equals(interval.getLower(), interval.getUpper())) {
return interval.getLower();

Check warning on line 125 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L125

Added line #L125 was not covered by tests
}
}
return null;

Check warning on line 128 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L128

Added line #L128 was not covered by tests
}

private Object getValue(Object rmObject, RMAttributeInfo attributeInfo) throws IllegalArgumentException {
try {
return attributeInfo.getGetMethod().invoke(rmObject);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);

Check warning on line 135 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L133-L135

Added lines #L133 - L135 were not covered by tests
}
}

private void setValue(Object rmObject, RMAttributeInfo attributeInfo, Object value) throws IllegalArgumentException {
try {
attributeInfo.getSetMethod().invoke(rmObject, value);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}

Check warning on line 145 in tools/src/main/java/com/nedap/archie/FixedValueFiller.java

View check run for this annotation

Codecov / codecov/patch

tools/src/main/java/com/nedap/archie/FixedValueFiller.java#L141-L145

Added lines #L141 - L145 were not covered by tests
}