forked from nedap/archie
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
363 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
opt14/src/main/java/com/nedap/archie/opt14/FlatArchetypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.nedap.archie.opt14; | ||
|
||
import com.nedap.archie.aom.Archetype; | ||
|
||
public interface FlatArchetypeProvider { | ||
|
||
Archetype getFlatArchetype(String archetypeId); | ||
} |
108 changes: 108 additions & 0 deletions
108
opt14/src/main/java/com/nedap/archie/opt14/NodeIdFixer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.nedap.archie.opt14; | ||
|
||
import com.nedap.archie.aom.Archetype; | ||
import com.nedap.archie.aom.ArchetypeModelObject; | ||
import com.nedap.archie.aom.CAttribute; | ||
import com.nedap.archie.aom.CObject; | ||
import com.nedap.archie.aom.Template; | ||
import com.nedap.archie.aom.TemplateOverlay; | ||
import com.nedap.archie.aom.terminology.ArchetypeTerminology; | ||
import com.nedap.archie.aom.utils.AOMUtils; | ||
import com.nedap.archie.aom.utils.NodeIdUtil; | ||
import com.nedap.archie.archetypevalidator.ErrorType; | ||
import com.nedap.archie.flattener.InMemoryFullArchetypeRepository; | ||
import org.openehr.utils.message.I18n; | ||
|
||
public class NodeIdFixer { | ||
|
||
private Archetype archetype; | ||
private Archetype flatParent; | ||
|
||
public void fixNodeIds(Archetype archetype, FlatArchetypeProvider repo) { | ||
this.archetype = archetype; | ||
if(archetype.getParentArchetypeId() != null) { | ||
this.flatParent = repo.getFlatArchetype(archetype.getParentArchetypeId()); | ||
} | ||
fixRootNodeId(archetype); | ||
//fixNodeId(archetype.getDefinition()); | ||
|
||
if(archetype instanceof Template) { | ||
Template template = (Template) archetype; | ||
|
||
for(TemplateOverlay overlay:template.getTemplateOverlays()) { | ||
this.archetype = overlay; | ||
if(archetype.getParentArchetypeId() != null) { | ||
this.flatParent = repo.getFlatArchetype(overlay.getParentArchetypeId()); | ||
} | ||
fixRootNodeId(overlay); | ||
//fixNodeId(overlay.getDefinition()); | ||
} | ||
} | ||
} | ||
|
||
private void fixRootNodeId(Archetype archetype) { | ||
int specDepth = flatParent.specializationDepth()+1; | ||
CObject cObject = archetype.getDefinition(); | ||
if(cObject.isRootNode() && AOMUtils.getSpecializationDepthFromCode(cObject.getNodeId()) != specDepth) { | ||
//create id1.1.1.1.1.....1 one | ||
String newNodeId = "at0000"; | ||
for(int i = 0; i < specDepth; i++) { | ||
newNodeId += ".1"; | ||
} | ||
cObject.setNodeId(newNodeId); | ||
} | ||
} | ||
|
||
private void fixNodeId(CObject cObject) { | ||
if(flatParent == null) { | ||
return; | ||
} | ||
|
||
|
||
if (cObject.isRootNode() || !cObject.getParent().isSecondOrderConstrained()) { | ||
if (AOMUtils.getSpecializationDepthFromCode(cObject.getNodeId()) <= flatParent.specializationDepth() | ||
|| new NodeIdUtil(cObject.getNodeId()).isRedefined()) { | ||
if (!AOMUtils.isPhantomPathAtLevel(cObject.getPathSegments(), flatParent.specializationDepth())) { | ||
String flatPath = AOMUtils.pathAtSpecializationLevel(cObject.getPathSegments(), flatParent.specializationDepth()); | ||
CObject parentCObject = getCObject(flatParent.itemAtPath(flatPath)); | ||
|
||
if (parentCObject != null) { | ||
if (cObject.isProhibited()) { | ||
if (!parentCObject.getNodeId().equals(cObject.getNodeId())) { | ||
// System.out.println("fixing node id " + cObject.getNodeId() + " for archetype " + archetype.getArchetypeId()); | ||
String oldNodeId = cObject.getNodeId(); | ||
cObject.setNodeId(parentCObject.getNodeId()); | ||
ArchetypeTerminology terminology = cObject.getArchetype().getTerminology(); | ||
for(String language:terminology.getTermDefinitions().keySet()) { | ||
terminology.getTermDefinitions().get(language).remove(oldNodeId); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
for(CAttribute attribute:cObject.getAttributes()) { | ||
fixNodeId(attribute); | ||
} | ||
} | ||
|
||
private void fixNodeId(CAttribute cAttribute) { | ||
for(CObject cObject:cAttribute.getChildren()) { | ||
fixNodeId(cObject); | ||
} | ||
} | ||
|
||
private CObject getCObject(ArchetypeModelObject archetypeModelObject) { | ||
if(archetypeModelObject instanceof CAttribute) { | ||
CAttribute attribute = (CAttribute) archetypeModelObject; | ||
if(attribute.getChildren().size() == 1) { | ||
return attribute.getChildren().get(0); | ||
}//TODO: add a numeric identifier to the getPath() method in CObject so this can be deleted and actually works in all cases! | ||
} else if(archetypeModelObject instanceof CObject) { | ||
return (CObject) archetypeModelObject; | ||
} | ||
return null; | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
opt14/src/main/java/com/nedap/archie/opt14/NodeIdSpecializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package com.nedap.archie.opt14; | ||
|
||
import com.nedap.archie.aom.Archetype; | ||
import com.nedap.archie.aom.ArchetypeModelObject; | ||
import com.nedap.archie.aom.CAttribute; | ||
import com.nedap.archie.aom.CComplexObject; | ||
import com.nedap.archie.aom.CObject; | ||
import com.nedap.archie.aom.CPrimitiveObject; | ||
import com.nedap.archie.aom.Template; | ||
import com.nedap.archie.aom.TemplateOverlay; | ||
import com.nedap.archie.aom.primitives.CString; | ||
import com.nedap.archie.aom.terminology.ArchetypeTerm; | ||
import com.nedap.archie.aom.utils.AOMUtils; | ||
import com.nedap.archie.query.AOMPathQuery; | ||
import com.nedap.archie.query.RMPathQuery; | ||
import com.nedap.archie.rminfo.ArchieRMInfoLookup; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Walks the archetype tree, then specializes any node ids it finds with change where required. Can be: | ||
* - different type | ||
* - different text or description | ||
* - different primitive node child (TODO: check how in Differentiator?) | ||
* | ||
* | ||
* TODO: remove simple single constraint name constraints, and put them in the terminology instead first! | ||
* | ||
*/ | ||
public class NodeIdSpecializer { | ||
|
||
private Archetype archetype; | ||
private Archetype flatParent; | ||
|
||
public void specializeNodeIds(Archetype archetype, FlatArchetypeProvider repo) { | ||
if(archetype.getParentArchetypeId() == null) { | ||
return; | ||
} | ||
this.archetype = archetype; | ||
if(archetype.getParentArchetypeId() != null) { | ||
this.flatParent = repo.getFlatArchetype(archetype.getParentArchetypeId()); | ||
} | ||
specializeNodeIds(archetype.getDefinition()); | ||
|
||
if(archetype instanceof Template) { | ||
Template template = (Template) archetype; | ||
|
||
for(TemplateOverlay overlay:template.getTemplateOverlays()) { | ||
this.archetype = overlay; | ||
if(archetype.getParentArchetypeId() != null) { | ||
this.flatParent = repo.getFlatArchetype(overlay.getParentArchetypeId()); | ||
} | ||
|
||
specializeNodeIds(overlay.getDefinition()); | ||
} | ||
} | ||
} | ||
|
||
|
||
private void specializeNodeIds(CObject cObject) { | ||
if(!(cObject instanceof CPrimitiveObject)) { | ||
String flatPath = AOMUtils.pathAtSpecializationLevel(cObject.getPathSegments(), flatParent.specializationDepth()); | ||
CObject parentCObject = getCObject(flatParent.itemAtPath(flatPath)); | ||
|
||
if(parentCObject != null) { | ||
ArchetypeTerm term = archetype.getTerm(cObject, archetype.getOriginalLanguage().getCodeString()); | ||
ArchetypeTerm parentTerm = flatParent.getTerm(parentCObject, archetype.getOriginalLanguage().getCodeString()); | ||
if( cObject.getNodeId().equalsIgnoreCase(parentCObject.getNodeId())) { | ||
if(parentTerm != null && term != null) { | ||
if (!term.getText().equalsIgnoreCase(parentTerm.getText()) || | ||
!term.getDescription().equalsIgnoreCase(parentTerm.getDescription())) { | ||
//term changes. We need a new node id | ||
System.out.println("GOT ONE!"); | ||
} | ||
} | ||
} | ||
|
||
} else { | ||
System.out.println("DID NOT EXPECT THIS"); | ||
// throw new RuntimeException("I did not expect the Spanish inquisition!");//TODO: remove or add proper message | ||
} | ||
for(CAttribute attribute:cObject.getAttributes()) { | ||
specializeNodeIds(attribute); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void specializeNodeIds(CAttribute attribute) { | ||
if(attribute.getRmAttributeName().equalsIgnoreCase("name")) { | ||
//ok a name constraint. If it's a simple constraint, replace it with a terminology entry please. | ||
if(attribute.getChildren().size() == 1 && attribute.getChildren().get(0).getRmTypeName().equalsIgnoreCase("DV_TEXT")) { | ||
CObject cObject = attribute.getChildren().get(0); | ||
Object o = new AOMPathQuery("/value[1]").find((CComplexObject) cObject); | ||
if(o instanceof CString) { | ||
CString nameConstraint = (CString) o; | ||
if (nameConstraint.getConstraint().size() == 1 && !CString.isRegexConstraint(nameConstraint.getConstraint().get(0))) { | ||
//remove the crap out of this attribute. | ||
attribute.setChildren(new ArrayList<>()); | ||
ArchetypeTerm term = archetype.getTerm(attribute.getParent(), archetype.getOriginalLanguage().getCodeString()); | ||
if (term != null) { | ||
term.setText((nameConstraint.getConstraint().get(0))); | ||
} else { | ||
term = new ArchetypeTerm(); | ||
term.setCode(attribute.getParent().getNodeId()); | ||
term.setText(nameConstraint.getConstraint().get(0)); | ||
term.setDescription(nameConstraint.getConstraint().get(0)); | ||
getOrCreateTermDefinitions().put(attribute.getParent().getNodeId(), term); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
for(CObject child:attribute.getChildren()) { | ||
specializeNodeIds(child); | ||
} | ||
} | ||
|
||
private Map<String, ArchetypeTerm> getOrCreateTermDefinitions() { | ||
Map<String, ArchetypeTerm> termDefs = archetype.getTerminology().getTermDefinitions().get(archetype.getOriginalLanguage().getCodeString()); | ||
if(termDefs == null) { | ||
termDefs = new LinkedHashMap<>(); | ||
archetype.getTerminology().getTermDefinitions().put(archetype.getOriginalLanguage().getCodeString(), termDefs); | ||
} | ||
return termDefs; | ||
} | ||
|
||
private CObject getCObject(ArchetypeModelObject archetypeModelObject) { | ||
if(archetypeModelObject instanceof CAttribute) { | ||
CAttribute attribute = (CAttribute) archetypeModelObject; | ||
if(attribute.getChildren().size() == 1) { | ||
return attribute.getChildren().get(0); | ||
}//TODO: add a numeric identifier to the getPath() method in CObject so this can be deleted and actually works in all cases! | ||
} else if(archetypeModelObject instanceof CObject) { | ||
return (CObject) archetypeModelObject; | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.