001package org.hl7.fhir.r5.comparison;
002
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
007
008import org.hl7.fhir.r5.comparison.CanonicalResourceComparer.CanonicalResourceComparison;
009import org.hl7.fhir.r5.model.Base;
010import org.hl7.fhir.r5.model.CanonicalResource;
011
012public class VersionComparisonAnnotation {
013
014  public enum AnotationType {
015    NoChange, Added, Changed, Deleted;
016  }
017
018  public static final String USER_DATA_NAME = "version-annotation";
019
020  private AnotationType type;
021  private String original;  
022  private Map<String, List<Base>> deletedChildren;
023  private CanonicalResourceComparison<? extends CanonicalResource> comp;
024
025  public VersionComparisonAnnotation(AnotationType type) {
026    super();
027    this.type = type;
028  }
029
030  public void added() {
031    assert type == AnotationType.NoChange;
032    type = AnotationType.Added;    
033  }
034
035  public void changed(Base orig) {
036    assert type == AnotationType.NoChange;
037    type = AnotationType.Changed;
038    if (orig != null && orig.isPrimitive() && orig.primitiveValue().length() < 120) { // arbitrary, but we don't a whack of markdown 
039      this.original = orig.primitiveValue();
040    }
041  }
042
043  public void deleted() {    
044    assert type == AnotationType.NoChange;
045    type = AnotationType.Deleted;
046
047
048  }
049
050  public void deleted(String name, Base other) {
051    if (deletedChildren == null) {
052      deletedChildren = new HashMap<>();
053    }
054    if (!deletedChildren.containsKey(name)) {
055      deletedChildren.put(name, new ArrayList<>());
056    }
057    deletedChildren.get(name).add(other);    
058  }
059
060  public void comp(CanonicalResourceComparison<? extends CanonicalResource> comp) {
061    assert this.comp == null;
062    // TODO Auto-generated method stub
063    if (!comp.noUpdates()) {
064      type = AnotationType.Changed;
065    }
066    this.comp = comp;
067  }
068
069  public static String getUserDataName() {
070    return USER_DATA_NAME;
071  }
072
073  public AnotationType getType() {
074    return type;
075  }
076
077  public String getOriginal() {
078    return original;
079  }
080
081  public Map<String, List<Base>> getDeletedChildren() {
082    return deletedChildren;
083  }
084
085  public CanonicalResourceComparison<? extends CanonicalResource> getComp() {
086    return comp;
087  }
088
089
090  public static boolean hasDeleted(Base base, String... names) {
091    boolean result = false;
092    if (base.hasUserData(USER_DATA_NAME)) {
093      VersionComparisonAnnotation self = (VersionComparisonAnnotation) base.getUserData(USER_DATA_NAME);
094      for (String name : names) {
095        if (self.deletedChildren != null && self.deletedChildren.containsKey(name))  {
096          result = true;
097        }
098      }
099    }
100    return result;
101  }
102
103  public static List<Base> getDeleted(Base base, String... names) {
104    List<Base> result = new ArrayList<>();
105    if (base.hasUserData(USER_DATA_NAME)) {
106      VersionComparisonAnnotation self = (VersionComparisonAnnotation) base.getUserData(USER_DATA_NAME);
107      for (String name : names) {
108        if (self.deletedChildren != null && self.deletedChildren.containsKey(name))  {
109          result.addAll(self.deletedChildren.get(name));
110        }
111      }
112    }
113    return result;
114  }
115
116  public static Base getDeletedItem(Base base, String name) {
117    List<Base> result = new ArrayList<>();
118    if (base.hasUserData(USER_DATA_NAME)) {
119      VersionComparisonAnnotation self = (VersionComparisonAnnotation) base.getUserData(USER_DATA_NAME);
120      if (self.deletedChildren != null && self.deletedChildren.containsKey(name))  {
121        result.addAll(self.deletedChildren.get(name));
122      }
123    }
124    return result.isEmpty() ? null : result.get(0);
125  }
126
127
128
129
130  public static CanonicalResourceComparison<? extends CanonicalResource> artifactComparison(Base base) {
131    if (base.hasUserData(USER_DATA_NAME)) {
132      VersionComparisonAnnotation self = (VersionComparisonAnnotation) base.getUserData(USER_DATA_NAME);
133      return self.comp;
134    } else {
135      return null;
136    }
137  }
138
139  
140}