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