001package org.hl7.fhir.r4.utils;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.util.ArrayList;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Set;
028
029import org.hl7.fhir.r4.model.StructureDefinition;
030
031public class ValidationProfileSet {
032
033  public static class ProfileRegistration {
034    private String profile; 
035    private boolean errorOnMissing;
036    
037    public ProfileRegistration(String profile, boolean errorOnMissing) {
038      super();
039      this.profile = profile;
040      this.errorOnMissing = errorOnMissing;
041    }
042    public String getProfile() {
043      return profile;
044    }
045    public boolean isErrorOnMissing() {
046      return errorOnMissing;
047    }
048    
049    
050  }
051  private List<ProfileRegistration> canonical = new ArrayList<ProfileRegistration>();
052  private List<StructureDefinition> definitions = new ArrayList<StructureDefinition>();
053  
054  public ValidationProfileSet(String profile, boolean isError) {
055    super();
056    canonical.add(new ProfileRegistration(profile, isError));
057  }
058
059  public ValidationProfileSet() {
060    super();
061  }
062
063  public ValidationProfileSet(StructureDefinition profile) {
064    super();
065    definitions.add(profile);
066    canonical.add(new ProfileRegistration(profile.getUrl(), true));
067  }
068
069  public ValidationProfileSet(List<String> profiles, boolean isError) {
070    super();
071    if (profiles != null)
072      for (String p : profiles)
073        canonical.add(new ProfileRegistration(p, isError));
074  }
075
076  public List<String> getCanonicalUrls() {
077    List<String> res = new ArrayList<String>();
078    for (ProfileRegistration c : canonical) {
079      res.add(c.getProfile());
080    }
081    return res;
082  }
083
084  public List<StructureDefinition> getDefinitions() {
085    return definitions;
086  }
087
088  public boolean empty() {
089    return canonical.isEmpty() && definitions.isEmpty();
090  }
091
092  public List<String> getCanonicalAll() {
093    Set<String> res = new HashSet<String>();
094    res.addAll(getCanonicalUrls());
095    for (StructureDefinition sd : definitions)
096      res.add(sd.getUrl());
097    return new ArrayList<String>(res);
098  }
099
100  public List<ProfileRegistration> getCanonical() {
101    return canonical;
102  }
103
104  public StructureDefinition fetch(String effectiveProfile) {
105    for (StructureDefinition sd : definitions) 
106      if (effectiveProfile.equals(sd.getUrl()))
107        return sd;
108    return null;
109  }
110
111}