Documentation enhancements

This commit is contained in:
James Agnew 2014-09-10 14:12:11 -04:00
parent b1b99e1aa2
commit 8a77ac939d
7 changed files with 166 additions and 76 deletions

View File

@ -7,6 +7,7 @@ import ca.uhn.fhir.model.dstu.composite.CodingDt;
import ca.uhn.fhir.model.dstu.composite.HumanNameDt; import ca.uhn.fhir.model.dstu.composite.HumanNameDt;
import ca.uhn.fhir.model.dstu.composite.IdentifierDt; import ca.uhn.fhir.model.dstu.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu.composite.PeriodDt; import ca.uhn.fhir.model.dstu.composite.PeriodDt;
import ca.uhn.fhir.model.dstu.composite.QuantityDt;
import ca.uhn.fhir.model.dstu.resource.Observation; import ca.uhn.fhir.model.dstu.resource.Observation;
import ca.uhn.fhir.model.dstu.resource.Patient; import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum; import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum;
@ -16,88 +17,103 @@ import ca.uhn.fhir.model.primitive.StringDt;
public class FhirDataModel { public class FhirDataModel {
public static void datatypes() { public static void datatypes() {
// START SNIPPET: datatypes // START SNIPPET: datatypes
Observation obs = new Observation(); Observation obs = new Observation();
// These are all equivalent // These are all equivalent
obs.setIssued(new InstantDt(new Date())); obs.setIssued(new InstantDt(new Date()));
obs.setIssued(new Date(), TemporalPrecisionEnum.MILLI); obs.setIssued(new Date(), TemporalPrecisionEnum.MILLI);
obs.setIssuedWithMillisPrecision(new Date()); obs.setIssuedWithMillisPrecision(new Date());
// The InstantDt also lets you work with the instant as a Java Date // The InstantDt also lets you work with the instant as a Java Date
// object or as a FHIR String. // object or as a FHIR String.
Date date = obs.getIssued().getValue(); // A date object Date date = obs.getIssued().getValue(); // A date object
String dateString = obs.getIssued().getValueAsString(); // "2014-03-08T12:59:58.068-05:00" String dateString = obs.getIssued().getValueAsString(); // "2014-03-08T12:59:58.068-05:00"
// END SNIPPET: datatypes // END SNIPPET: datatypes
System.out.println(date); System.out.println(date);
System.out.println(dateString); System.out.println(dateString);
} }
@SuppressWarnings("unused")
public void nonNull() {
// START SNIPPET: nonNull
Observation observation = new Observation();
// None of these calls will return null, but instead create their respective @SuppressWarnings("unused")
// child elements. public void nonNull() {
IdentifierDt identifierDt = observation.getIdentifier(); // START SNIPPET: nonNull
PeriodDt periodDt = observation.getIdentifier().getPeriod(); Observation observation = new Observation();
DateTimeDt activeDt = observation.getIdentifier().getPeriod().getStart();
// DateTimeDt is a FHIR primitive however, so the following will return null // None of these calls will return null, but instead create their
// unless a value has been placed there. // respective
Date active = observation.getIdentifier().getPeriod().getStart().getValue(); // child elements.
// END SNIPPET: nonNull IdentifierDt identifierDt = observation.getIdentifier();
PeriodDt periodDt = observation.getIdentifier().getPeriod();
DateTimeDt activeDt = observation.getIdentifier().getPeriod().getStart();
// DateTimeDt is a FHIR primitive however, so the following will return
// null
// unless a value has been placed there.
Date active = observation.getIdentifier().getPeriod().getStart().getValue();
// END SNIPPET: nonNull
}
}
public static void codes() {
// START SNIPPET: codes
Patient patient = new Patient();
// Coded types can naturally be set using plain Strings public static void codes() {
CodingDt genderCoding = patient.getGender().addCoding(); // START SNIPPET: codes
genderCoding.setSystem("http://hl7.org/fhir/v3/AdministrativeGender"); Patient patient = new Patient();
genderCoding.setCode("M");
// This is equivalent to the three statements above // Coded types can naturally be set using plain Strings
patient.setGender(AdministrativeGenderCodesEnum.M); CodingDt genderCoding = patient.getGender().addCoding();
// END SNIPPET: codes genderCoding.setSystem("http://hl7.org/fhir/v3/AdministrativeGender");
genderCoding.setCode("M");
// This is equivalent to the three statements above
patient.setGender(AdministrativeGenderCodesEnum.M);
// END SNIPPET: codes
}
public static void main(String[] args) {
datatypes();
// START SNIPPET: observation
Observation observation = new Observation();
// Create a quantity datatype
QuantityDt q = new QuantityDt();
q.setValue(185);
q.setSystem("http://unitsofmeasure.org");
q.setCode("lbs");
// Put the datatype in the observation
observation.setValue(q);
// Set the reference range
observation.getReferenceRangeFirstRep().setLow(100);
observation.getReferenceRangeFirstRep().setHigh(200);
// END SNIPPET: observation
}
public void namesHard() {
// START SNIPPET: namesHard
Patient patient = new Patient();
HumanNameDt name = patient.addName();
StringDt family = name.addFamily();
family.setValue("Smith");
StringDt firstName = name.addGiven();
firstName.setValue("Rob");
StringDt secondName = name.addGiven();
secondName.setValue("Bruce");
// END SNIPPET: namesHard
}
public void namesEasy() {
// START SNIPPET: namesEasy
Patient patient = new Patient();
patient.addName().addFamily("Smith").addGiven("Rob").addGiven("Bruce");
// END SNIPPET: namesEasy
}
}
public static void main(String[] args) {
datatypes();
}
public void namesHard() {
// START SNIPPET: namesHard
Patient patient = new Patient();
HumanNameDt name = patient.addName();
StringDt family = name.addFamily();
family.setValue("Smith");
StringDt firstName = name.addGiven();
firstName.setValue("Rob");
StringDt secondName = name.addGiven();
secondName.setValue("Bruce");
//END SNIPPET: namesHard
}
public void namesEasy() {
// START SNIPPET: namesEasy
Patient patient = new Patient();
patient.addName().addFamily("Smith").addGiven("Rob").addGiven("Bruce");
//END SNIPPET: namesEasy
}
} }

View File

@ -1,8 +1,15 @@
package example; package example;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Patient; import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum; import ca.uhn.fhir.model.dstu.valueset.ContactSystemEnum;
import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.DataFormatException;
@ -11,8 +18,7 @@ import ca.uhn.fhir.validation.ValidationFailureException;
public class ValidatorExamples { public class ValidatorExamples {
public static void main(String[] args) throws DataFormatException, IOException { public void validateResource() {
//START SNIPPET: basicValidation //START SNIPPET: basicValidation
// As always, you need a context // As always, you need a context
FhirContext ctx = new FhirContext(); FhirContext ctx = new FhirContext();
@ -35,10 +41,45 @@ public class ValidatorExamples {
System.out.println("Validation failed"); System.out.println("Validation failed");
// The ValidationFailureException which gets thrown by the validator
// will contain an OperationOutcome resource describing the failure
String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome()); String results = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(e.getOperationOutcome());
System.out.println(results); System.out.println(results);
} }
//END SNIPPET: basicValidation //END SNIPPET: basicValidation
}
public static void main(String[] args) throws DataFormatException, IOException {
validateFiles();
}
private static void validateFiles() throws IOException, FileNotFoundException {
//START SNIPPET: validateFiles
FhirContext ctx = new FhirContext();
// Create a validator and configure it
FhirValidator validator = ctx.newValidator();
validator.setValidateAgainstStandardSchema(true);
validator.setValidateAgainstStandardSchematron(true);
// Get a list of files in a given directory
String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt"));
for (String nextFile : fileList) {
// For each file, load the contents into a string
String nextFileContents = IOUtils.toString(new FileReader(nextFile));
// Parse that string (this example assumes JSON encoding)
IResource resource = ctx.newJsonParser().parseResource(nextFileContents);
// Apply the validation. This will throw an exception on the first validation failure
validator.validate(resource);
}
// If we make it here with no exception, all the files validated!
//START SNIPPET: validateFiles
} }
} }

View File

@ -103,7 +103,24 @@
</subsection> </subsection>
</section> </section>
<section name="Examples">
<subsection name="Populating an Observation Resource">
<p>
The following example shows how to create an observation resource containing
a numeric datatype.
</p>
<macro name="snippet">
<param name="id" value="observation" />
<param name="file" value="examples/src/main/java/example/FhirDataModel.java" />
</macro>
</subsection>
</section>
</body> </body>
</document> </document>

View File

@ -23,7 +23,7 @@
<subsection name="Background"> <subsection name="Background">
<p> <p>
FHIR resource definitions are distributed with a set of XML schema files (XDS) FHIR resource definitions are distributed with a set of XML schema files (XSD)
as well as a set of XML Schematron (SCH) files. These two sets of files are as well as a set of XML Schematron (SCH) files. These two sets of files are
complimentary to each other, meaning that in order to claim compliance to the complimentary to each other, meaning that in order to claim compliance to the
FHIR specification, your resources must validate against both sets. FHIR specification, your resources must validate against both sets.
@ -60,6 +60,19 @@
</subsection> </subsection>
<subsection name="Validating a Set of Files">
<p>
The following example shows how to load a set of resources from files
on disk and validate each one.
</p>
<macro name="snippet">
<param name="id" value="validateFiles" />
<param name="file" value="examples/src/main/java/example/ValidatorExamples.java" />
</macro>
</subsection>
</section> </section>
</body> </body>

View File

@ -9,6 +9,7 @@ import org.junit.Test;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.dstu.resource.Patient; import ca.uhn.fhir.model.dstu.resource.Patient;
import ca.uhn.fhir.model.dstu.valueset.AdministrativeGenderCodesEnum;
import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.util.FhirTerser; import ca.uhn.fhir.util.FhirTerser;
@ -18,6 +19,7 @@ public class FhirTerserTest {
public void testGetAllPopulatedChildElementsOfType() { public void testGetAllPopulatedChildElementsOfType() {
Patient p = new Patient(); Patient p = new Patient();
p.setGender(AdministrativeGenderCodesEnum.M);
p.addIdentifier().setSystem("urn:foo"); p.addIdentifier().setSystem("urn:foo");
p.addAddress().addLine("Line1"); p.addAddress().addLine("Line1");
p.addAddress().addLine("Line2"); p.addAddress().addLine("Line2");

View File

@ -1 +1,2 @@
/target/ /target/
/bin/