001package org.hl7.fhir.r5.utils;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r5
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.io.BufferedOutputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.File;
027import java.io.IOException;
028import java.io.UnsupportedEncodingException;
029import java.nio.charset.Charset;
030import java.text.SimpleDateFormat;
031import java.util.ArrayList;
032import java.util.Calendar;
033import java.util.Date;
034import java.util.GregorianCalendar;
035import java.util.HashMap;
036import java.util.HashSet;
037import java.util.List;
038import java.util.Locale;
039import java.util.Map;
040import java.util.Set;
041import java.util.TimeZone;
042import java.util.UUID;
043
044import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
045import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
046import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
047import org.hl7.fhir.exceptions.FHIRException;
048import org.hl7.fhir.r5.model.ContactDetail;
049import org.hl7.fhir.r5.model.ContactPoint;
050import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem;
051import org.hl7.fhir.r5.model.Enumeration;
052import org.hl7.fhir.r5.model.Enumerations.FHIRVersion;
053import org.hl7.fhir.r5.model.ImplementationGuide;
054import org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideDependsOnComponent;
055import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
056import org.hl7.fhir.utilities.TextFile;
057import org.hl7.fhir.utilities.Utilities;
058import org.hl7.fhir.utilities.cache.PackageGenerator.PackageType;
059import org.hl7.fhir.utilities.cache.NpmPackageIndexBuilder;
060import org.hl7.fhir.utilities.cache.ToolsVersion;
061import org.hl7.fhir.utilities.json.JsonTrackingParser;
062
063import com.google.gson.Gson;
064import com.google.gson.GsonBuilder;
065import com.google.gson.JsonArray;
066import com.google.gson.JsonObject;
067import com.google.gson.JsonPrimitive;
068
069public class NPMPackageGenerator {
070
071  public enum Category {
072    RESOURCE, EXAMPLE, OPENAPI, SCHEMATRON, RDF, OTHER, TOOL, TEMPLATE, JEKYLL;
073
074    private String getDirectory() {
075      switch (this) {
076      case RESOURCE: return "package/";
077      case EXAMPLE: return "example/";
078      case OPENAPI: return "openapi/";
079      case SCHEMATRON: return "xml/";
080      case RDF: return "rdf/";      
081      case OTHER: return "other/";      
082      case TEMPLATE: return "other/";      
083      case JEKYLL: return "jekyll/";      
084      case TOOL: return "bin/";      
085      }
086      return "/";
087    }
088  }
089
090  private String destFile;
091  private Set<String> created = new HashSet<String>();
092  private TarArchiveOutputStream tar;
093  private ByteArrayOutputStream OutputStream;
094  private BufferedOutputStream bufferedOutputStream;
095  private GzipCompressorOutputStream gzipOutputStream;
096  private JsonObject packageJ;
097  private JsonObject packageManifest;
098  private NpmPackageIndexBuilder indexer;
099
100
101  public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date) throws FHIRException, IOException {
102    super();
103    this.destFile = destFile;
104    start();
105    List<String> fhirVersion = new ArrayList<>();
106    for (Enumeration<FHIRVersion> v : ig.getFhirVersion())
107      fhirVersion.add(v.asStringValue());
108    buildPackageJson(canonical, kind, url, date, ig, fhirVersion);
109  }
110
111  public static NPMPackageGenerator subset(NPMPackageGenerator master, String destFile, String id, String name, Date date) throws FHIRException, IOException {
112    JsonObject p = master.packageJ.deepCopy();
113    p.remove("name");
114    p.addProperty("name", id);
115    p.remove("type");
116    p.addProperty("type", PackageType.SUBSET.getCode());    
117    p.remove("title");
118    p.addProperty("title", name);
119
120    return new NPMPackageGenerator(destFile, p, date);
121  }
122
123  public NPMPackageGenerator(String destFile, String canonical, String url, PackageType kind, ImplementationGuide ig, Date date, List<String> fhirVersion) throws FHIRException, IOException {
124    super();
125    this.destFile = destFile;
126    start();
127    buildPackageJson(canonical, kind, url, date, ig, fhirVersion);
128  }
129
130  public NPMPackageGenerator(String destFile, JsonObject npm, Date date) throws FHIRException, IOException {
131    super();
132    String dt = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
133    packageJ = npm;
134    packageManifest = new JsonObject();
135    packageManifest.addProperty("version", npm.get("version").getAsString());
136    packageManifest.addProperty("date", dt);
137    npm.addProperty("date", dt);
138    packageManifest.addProperty("name", npm.get("name").getAsString());
139    this.destFile = destFile;
140    start();
141    Gson gson = new GsonBuilder().setPrettyPrinting().create();
142    String json = gson.toJson(npm);
143    try {
144      addFile(Category.RESOURCE, "package.json", json.getBytes("UTF-8"));
145    } catch (UnsupportedEncodingException e) {
146    }
147  }
148
149  private void buildPackageJson(String canonical, PackageType kind, String web, Date date, ImplementationGuide ig, List<String> fhirVersion) throws FHIRException, IOException {
150    String dtHuman = new SimpleDateFormat("EEE, MMM d, yyyy HH:mmZ", new Locale("en", "US")).format(date);
151    String dt = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
152
153    CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
154    if (!ig.hasPackageId())
155      b.append("packageId");
156    if (!ig.hasVersion())
157      b.append("version");
158    if (!ig.hasFhirVersion())
159      b.append("fhirVersion");
160    if (!ig.hasLicense())
161      b.append("license");
162    for (ImplementationGuideDependsOnComponent d : ig.getDependsOn()) {
163      if (!d.hasVersion()) {
164        b.append("dependsOn.version("+d.getUri()+")");
165      }
166    }
167
168    JsonObject npm = new JsonObject();
169    npm.addProperty("name", ig.getPackageId());
170    npm.addProperty("version", ig.getVersion());
171    npm.addProperty("tools-version", ToolsVersion.TOOLS_VERSION);
172    npm.addProperty("type", kind.getCode());
173    npm.addProperty("date", dt);
174    if (ig.hasLicense())
175      npm.addProperty("license", ig.getLicense().toCode());
176    npm.addProperty("canonical", canonical);
177    npm.addProperty("url", web);
178    if (ig.hasTitle())
179      npm.addProperty("title", ig.getTitle());
180    if (ig.hasDescription())
181      npm.addProperty("description", ig.getDescription()+ " (built "+dtHuman+timezone()+")");
182    JsonArray vl = new JsonArray();
183    
184    npm.add("fhirVersions", vl);
185    for (String v : fhirVersion) { 
186      vl.add(new JsonPrimitive(v));
187    }
188    
189    if (kind != PackageType.CORE) {
190      JsonObject dep = new JsonObject();
191      npm.add("dependencies", dep);
192      for (String v : fhirVersion) { 
193        String vp = packageForVersion(v);
194        if (vp != null ) {
195          dep.addProperty(vp, v);
196        }
197      }
198      for (ImplementationGuideDependsOnComponent d : ig.getDependsOn()) {
199        dep.addProperty(d.getPackageId(), d.getVersion());
200      }
201    }
202    if (ig.hasPublisher())
203      npm.addProperty("author", ig.getPublisher());
204    JsonArray m = new JsonArray();
205    for (ContactDetail t : ig.getContact()) {
206      String email = email(t.getTelecom());
207      String url = url(t.getTelecom());
208      if (t.hasName() & (email != null || url != null)) {
209        JsonObject md = new JsonObject();
210        m.add(md);
211        md.addProperty("name", t.getName());
212        if (email != null)
213          md.addProperty("email", email);
214        if (url != null)
215          md.addProperty("url", url);
216      }
217    }
218    if (m.size() > 0)
219      npm.add("maintainers", m);
220    if (ig.getManifest().hasRendering())
221      npm.addProperty("homepage", ig.getManifest().getRendering());
222    JsonObject dir = new JsonObject();
223    npm.add("directories", dir);
224    dir.addProperty("lib", "package");
225    dir.addProperty("example", "example");
226    Gson gson = new GsonBuilder().setPrettyPrinting().create();
227    String json = gson.toJson(npm);
228    try {
229      addFile(Category.RESOURCE, "package.json", json.getBytes("UTF-8"));
230    } catch (UnsupportedEncodingException e) {
231    }
232    packageJ = npm;
233
234    packageManifest = new JsonObject();
235    packageManifest.addProperty("version", ig.getVersion());
236    packageManifest.addProperty("fhirVersion", fhirVersion.toString());
237    packageManifest.addProperty("date", dt);
238    packageManifest.addProperty("name", ig.getPackageId());
239
240  }
241
242
243  private String packageForVersion(String v) {
244    if (v == null)
245      return null;
246    if (v.startsWith("1.0"))
247      return "hl7.fhir.r2.core";
248    if (v.startsWith("1.4"))
249      return "hl7.fhir.r2b.core";
250    if (v.startsWith("3.0"))
251      return "hl7.fhir.r3.core";
252    if (v.startsWith("4.0"))
253      return "hl7.fhir.r4.core";
254    return null;
255  }
256
257  private String timezone() {
258    TimeZone tz = TimeZone.getDefault();  
259    Calendar cal = GregorianCalendar.getInstance(tz);
260    int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
261
262    String offset = String.format("%02d:%02d", Math.abs(offsetInMillis / 3600000), Math.abs((offsetInMillis / 60000) % 60));
263    offset = (offsetInMillis >= 0 ? "+" : "-") + offset;
264
265    return offset;
266  }
267
268
269  private String url(List<ContactPoint> telecom) {
270    for (ContactPoint cp : telecom) {
271      if (cp.getSystem() == ContactPointSystem.URL)
272        return cp.getValue();
273    }
274    return null;
275  }
276
277
278  private String email(List<ContactPoint> telecom) {
279    for (ContactPoint cp : telecom) {
280      if (cp.getSystem() == ContactPointSystem.EMAIL)
281        return cp.getValue();
282    }
283    return null;
284  }
285
286  private void start() throws IOException {
287    OutputStream = new ByteArrayOutputStream();
288    bufferedOutputStream = new BufferedOutputStream(OutputStream);
289    gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
290    tar = new TarArchiveOutputStream(gzipOutputStream);
291    indexer = new NpmPackageIndexBuilder();
292    indexer.start();
293  }
294
295
296  public void addFile(Category cat, String name, byte[] content) throws IOException {
297    String path = cat.getDirectory()+name;
298    if (!path.startsWith("package/")) {
299      path = "package/" +path;
300    }
301    if (path.length() > 100) {
302      name = name.substring(0, name.indexOf("-"))+"-"+UUID.randomUUID().toString();
303      path = cat.getDirectory()+name;
304      if (!path.startsWith("package/")) {
305        path = "package/" +path;
306      }
307      
308    }
309      
310    if (created.contains(path)) {
311      System.out.println("Duplicate package file "+path);
312    } else {
313      created.add(path);
314      TarArchiveEntry entry = new TarArchiveEntry(path);
315      entry.setSize(content.length);
316      tar.putArchiveEntry(entry);
317      tar.write(content);
318      tar.closeArchiveEntry();
319      if(cat == Category.RESOURCE) {
320        indexer.seeFile(name, content);
321      }
322    }
323  }
324
325  public void finish() throws IOException {
326    buildIndexJson();
327    tar.finish();
328    tar.close();
329    gzipOutputStream.close();
330    bufferedOutputStream.close();
331    OutputStream.close();
332    TextFile.bytesToFile(OutputStream.toByteArray(), destFile);
333    // also, for cache management on current builds, generate a little manifest
334    Gson gson = new GsonBuilder().setPrettyPrinting().create();
335    String json = gson.toJson(packageManifest);
336    TextFile.stringToFile(json, Utilities.changeFileExt(destFile, ".manifest.json"), false);
337  }
338
339  private void buildIndexJson() throws IOException {
340    byte[] content = TextFile.stringToBytes(indexer.build(), false);
341    addFile(Category.RESOURCE, ".index.json", content); 
342  }
343
344  public String filename() {
345    return destFile;
346  }
347
348  public void loadDir(String rootDir, String name) throws IOException {
349    loadFiles(rootDir, new File(Utilities.path(rootDir, name)));
350  }
351
352  public void loadFiles(String root, File dir, String... noload) throws IOException {
353    for (File f : dir.listFiles()) {
354      if (!Utilities.existsInList(f.getName(), noload)) {
355        if (f.isDirectory()) {
356          loadFiles(root, f);
357        } else {
358          String path = f.getAbsolutePath().substring(root.length()+1);
359          byte[] content = TextFile.fileToBytes(f);
360          if (created.contains(path)) 
361            System.out.println("Duplicate package file "+path);
362          else {
363            created.add(path);
364            TarArchiveEntry entry = new TarArchiveEntry(path);
365            entry.setSize(content.length);
366            tar.putArchiveEntry(entry);
367            tar.write(content);
368            tar.closeArchiveEntry();
369          }
370        }
371      }
372    }
373  }
374
375
376}