001package org.hl7.fhir.utilities.npm; 002 003import java.io.File; 004import java.io.IOException; 005import java.text.ParseException; 006import java.time.Instant; 007import java.util.ArrayList; 008import java.util.HashSet; 009import java.util.List; 010import java.util.Set; 011 012import org.hl7.fhir.utilities.FhirPublication; 013import org.hl7.fhir.utilities.TextFile; 014import org.hl7.fhir.utilities.Utilities; 015import org.hl7.fhir.utilities.json.JsonException; 016import org.hl7.fhir.utilities.json.model.JsonArray; 017import org.hl7.fhir.utilities.json.model.JsonObject; 018import org.hl7.fhir.utilities.json.parser.JsonParser; 019 020public class PackageList { 021 022 public static class PackageListEntry { 023 private JsonObject json; 024 025 private PackageListEntry(JsonObject json) { 026 super(); 027 this.json = json; 028 } 029 030 public String version() { 031 return json.asString("version"); 032 } 033 034 public String path() { 035 return json.asString("path"); 036 } 037 038 public boolean current() { 039 return json.has("current") && json.asBoolean("current"); 040 } 041 042 public boolean cibuild() { 043 return "ci-build".equals(json.asString("status")); 044 } 045 046 public String sequence() { 047 return json.asString("sequence"); 048 } 049 050 public String fhirVersion() { 051 if (json.has("fhir-version")) // legacy 052 return json.asString("fhir-version"); 053 return json.asString("fhirversion"); 054 } 055 056 public String status() { 057 return json.asString("status"); 058 } 059 060 public String desc() { 061 return json.asString("desc"); 062 } 063 064 public String date() { 065 return json.asString("date"); 066 } 067 068 public void setDate(String date) { 069 json.set("date", date); 070 } 071 072 public String name() { 073 return json.asString("name"); 074 } 075 076 public List<String> subPackages() { 077 List<String> list = new ArrayList<>(); 078 if (json.has("sub-packages")) { 079 list.addAll(json.getStrings("sub-packages")); 080 } 081 return list; 082 } 083 084 public void clearSubPackages() { 085 json.remove("sub-packages"); 086 } 087 public void addSubPackage(String s) { 088 json.forceArray("sub-packages").add(s); 089 } 090 091 private boolean isPartofMainSpec() { 092 return Utilities.startsWithInList(path(), "http://hl7.org/fhir/DSTU2", "http://hl7.org/fhir/2015Sep", "http://hl7.org/fhir/2015May"); 093 } 094 095 /** 096 * For a variety of reasons in the past, the path is not always under the canonical 097 * this method determines the path *if it exists* 098 * 099 * @param root 100 * @param v 101 * @throws IOException 102 */ 103 public String determineLocalPath(String url, String root) throws IOException { 104 if (!isPartofMainSpec() && path().startsWith(url+"/")) { 105 String tail = path().substring(url.length()+1); 106 return Utilities.path(root, tail); 107 } else { 108 return null; 109 } 110 } 111 112 public void setCurrent(boolean b) { 113 if (b) { 114 json.set("current", true); 115 } else { 116 if (json.has("current")) { 117 json.remove("current"); 118 } 119 } 120 } 121 122 public void init(String version, String path, String status, String sequence, FhirPublication fhirVersion) { 123 json.set("version", version); 124 json.set("path", path); 125 json.set("status", status); 126 json.set("sequence", sequence); 127 if (fhirVersion != null) { 128 json.set("fhirversion", fhirVersion.toCode()); 129 } 130 } 131 132 public void describe(String desc, String descMD, String changes) { 133 if (!Utilities.noString(desc)) { 134 json.set("desc", desc); 135 } 136 if (!Utilities.noString(descMD)) { 137 json.set("descmd", descMD); 138 } 139 if (!Utilities.noString(changes)) { 140 json.set("changes", changes); 141 } 142 } 143 144 public JsonObject json() { 145 return json; 146 } 147 148 public Instant instant() throws ParseException { 149 return json.asInstant("date"); 150 } 151 152 /** 153 * only used for a technical correction. tcPath is the name of archive file (web reference) containing the content before correction 154 * 155 * @param asString 156 * @param webpath 157 * @param asString2 158 * @param asString3 159 * @param fhirVersion 160 * @param tcName 161 */ 162 public void update(String version, String path, String status, String sequence, FhirPublication fhirVersion, String tcPath, String date) { 163 JsonObject tc = new JsonObject(); 164 json.forceArray("corrections").add(tc); 165 tc.set("version", json.asString("version")); 166 tc.set("path", tcPath); 167 tc.set("date", date); 168 169 json.set("version", version); 170 json.set("path", path); 171 json.set("status", status); 172 json.set("sequence", sequence); 173 if (fhirVersion != null) { 174 json.set("fhirversion", fhirVersion.toCode()); 175 } 176 setDate(date); 177 } 178 179 } 180 181 private String source; 182 private JsonObject json; 183 private List<PackageListEntry> list = new ArrayList<>(); 184 private List<PackageListEntry> versions = new ArrayList<>(); 185 private PackageListEntry cibuild; 186 187 public PackageList() { 188 super(); 189 json = new JsonObject(); 190 json.add("list", new JsonArray()); 191 } 192 193 public PackageList(JsonObject json) { 194 this.json = json; 195 for (JsonObject o : json.getJsonObjects("list")) { 196 PackageListEntry e = new PackageListEntry(o); 197 list.add(e); 198 if ("current".equals(o.asString("version"))) { 199 cibuild = e; 200 } else { 201 versions.add(e); 202 } 203 } 204 } 205 206 public static PackageList fromFile(File f) throws JsonException, IOException { 207 return new PackageList(JsonParser.parseObject(f)).setSource(f.getAbsolutePath()); 208 } 209 210 public PackageList setSource(String src) { 211 this.source = src; 212 return this; 213 } 214 215 public static PackageList fromFile(String f) throws JsonException, IOException { 216 return new PackageList(JsonParser.parseObjectFromFile(f)).setSource(f); 217 } 218 219 public static PackageList fromContent(byte[] cnt) throws JsonException, IOException { 220 return new PackageList(JsonParser.parseObject(cnt)).setSource("unknown"); 221 } 222 223 public static PackageList fromUrl(String url) throws JsonException, IOException { 224 return new PackageList(JsonParser.parseObjectFromUrl(url)).setSource(url); 225 } 226 227 public String source() { 228 return source; 229 } 230 231 public String canonical() { 232 return json.asString("canonical"); 233 } 234 235 public String pid() { 236 return json.asString("package-id"); 237 } 238 239 public boolean hasPid() { 240 return json.has("package-id"); 241 } 242 243 public String category() { 244 return json.asString("category"); 245 } 246 247 public List<PackageListEntry> list() { 248 return list; 249 } 250 251 public List<PackageListEntry> versions() { 252 return versions; 253 } 254 255 public PackageListEntry ciBuild() { 256 return cibuild; 257 } 258 259 public String toJson() { 260 return JsonParser.compose(json, true); 261 } 262 263 public PackageListEntry newVersion(String version, String path, String status, String sequence, FhirPublication fhirVersion) { 264 JsonObject o = new JsonObject(); 265 PackageListEntry e = new PackageListEntry(o); 266 versions.add(0, e); 267 if (cibuild != null) { 268 json.getJsonArray("list").add(1, o); 269 list.add(1, e); 270 } else { 271 json.getJsonArray("list").add(0, o); 272 list.add(0, e); 273 } 274 e.init(version, path, status, sequence, fhirVersion); 275 return e; 276 } 277 278 public void init(String name, String canonical, String title, String category, String introduction) { 279 json.add("package-id", name); 280 json.add("canonical", canonical); 281 json.add("title", title); 282 json.add("category", category); 283 json.add("introduction", introduction); 284 285 } 286 287 public void addCIBuild(String version, String path, String desc, String status) { 288 if (cibuild != null) { 289 json.getJsonArray("list").remove(cibuild.json); 290 } 291 cibuild = new PackageListEntry(new JsonObject()); 292 cibuild.init(version, path, status, status, null); 293 cibuild.describe(desc, null, null); 294 json.getJsonArray("list").add(0, cibuild.json); 295 } 296 297 public PackageListEntry findByVersion(String version) { 298 for (PackageListEntry p : versions) { 299 if (version.equals(p.version())) { 300 return p; 301 } 302 } 303 return null; 304 } 305 306 public void save(String filepath) throws IOException { 307 TextFile.stringToFile(toJson(), filepath); 308 } 309 310 public String determineLocalPath(String url, String root) throws IOException { 311 if (canonical().startsWith(url+"/")) { 312 String tail = canonical().substring(url.length()+1); 313 return Utilities.path(root, tail); 314 } else { 315 return null; 316 } 317 } 318 319 public String title() { 320 return json.asString("title"); 321 } 322 323 public PackageListEntry current() { 324 for (PackageListEntry e : list) { 325 if (e.current() && !"ci-build".equals(e.status())) { 326 return e; 327 } 328 } 329 return null; 330 } 331 332 public PackageListEntry latest() { 333 for (PackageListEntry e : list) { 334 if (!"ci-build".equals(e.status())) { 335 return e; 336 } 337 } 338 return null; 339 } 340 341 public String intro() { 342 return json.asString("introduction"); 343 } 344 345 public List<PackageListEntry> milestones() { 346 List<PackageListEntry> list = new ArrayList<>(); 347 for (PackageListEntry t : versions) { 348 if (t.name() != null) { 349 list.add(t); 350 } 351 } 352 return list; 353 } 354 355 public boolean hasPath(String pathVer) { 356 for (PackageListEntry t : versions) { 357 if (t.path() != null && t.path().equals(pathVer)) { 358 return true; 359 } 360 } 361 return false; 362 } 363 364 365}