001package org.hl7.fhir.convertors.misc;
002
003import java.io.BufferedReader;
004import java.io.FileNotFoundException;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.nio.file.Files;
008import java.nio.file.Paths;
009
010import org.hl7.fhir.exceptions.FHIRFormatError;
011import org.hl7.fhir.r5.formats.IParser.OutputStyle;
012import org.hl7.fhir.r5.formats.JsonParser;
013import org.hl7.fhir.r5.model.CodeSystem;
014import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent;
015import org.hl7.fhir.r5.model.CodeSystem.PropertyType;
016import org.hl7.fhir.r5.model.ContactDetail;
017import org.hl7.fhir.r5.model.ContactPoint;
018import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem;
019import org.hl7.fhir.r5.model.DateTimeType;
020import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
021import org.hl7.fhir.r5.model.StringType;
022import org.hl7.fhir.utilities.Utilities;
023
024public class HCPCSImporter {
025
026  // https://www.cms.gov/medicare/coding-billing/healthcare-common-procedure-system/quarterly-update
027
028  public static void main(String[] args) throws FHIRFormatError, IOException {
029    new HCPCSImporter().execute(args[0], args[1], args[2], args[3]);
030
031  }
032
033  private void execute(String source, String version, String date, String dest) throws FileNotFoundException, IOException {
034    CodeSystem cs = new CodeSystem();
035    cs.setId("hcpcs");
036    cs.setUrl("http://www.cms.gov/Medicare/Coding/HCPCSReleaseCodeSets");
037    cs.setVersion(version);
038
039    cs.setName("HCPCSLevelII");
040    cs.setTitle("Healthcare Common Procedure Coding System (HCPCS) level II alphanumeric codes");
041    cs.setStatus(PublicationStatus.ACTIVE);
042    cs.setExperimental(false);
043    cs.setDateElement(new DateTimeType(date));
044    cs.setPublisher("U.S. Centers for Medicare & Medicaid Services (CMS)");
045    ContactDetail cd = cs.addContact();
046    cd.setName("U.S. Centers for Medicare & Medicaid Services (CMS)");
047    cd.addTelecom().setSystem(ContactPointSystem.URL).setValue("https://www.cms.gov/");
048    cd.addTelecom().setSystem(ContactPointSystem.EMAIL).setValue("hcpcs@cms.hhs.gov");
049    cs.setDescription("The Level II HCPCS codes, which are established by CMS's Alpha-Numeric Editorial Panel, primarily represent items and supplies and non-physician services not covered by the American Medical Association's Current Procedural Terminology-4 (CPT-4) codes; Medicare, Medicaid, and private health insurers use HCPCS procedure and modifier codes for claims processing.  Level II alphanumeric procedure and modifier codes comprise the A to V range.");
050    cs.setCaseSensitive(true);
051
052    cs.addProperty().setCode("seqnum").setType(PropertyType.STRING).setDescription("Code to identify record type");
053
054    try (BufferedReader reader = Files.newBufferedReader(Paths.get(source))) {
055      String line;
056      while ((line = reader.readLine()) != null) {
057        if (!Utilities.noString(line.trim())) {
058          String code = field(line, 1, 5);
059          String shortD = field(line, 92, 119);
060          String longD = field(line, 12, 91); 
061          String seqNum = field(line, 6, 10); 
062          ConceptDefinitionComponent cc = cs.addConcept();
063          cc.setCode(code);
064          cc.setDisplay(shortD);
065          cc.addDesignation().setValue(longD);
066          if (!Utilities.noString(seqNum)) {
067            cc.addProperty().setCode("seqNum").setValue(new StringType(seqNum));
068          }
069 
070        }
071      }
072    } catch (IOException e) {
073      e.printStackTrace();
074    }
075    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(dest), cs);
076  }
077
078  private String field(String line, int i, int j) {
079    return line.substring(i-1, j).trim();
080  }
081}