}
public static ClinicalDocument createDocument(NonCodedCDACommonFields commonFields) throws MissingMandatoryFieldException {
ClinicalDocument template = new ClinicalDocument();
MissingMandatoryFieldException missingFields = new MissingMandatoryFieldException();
DateValue currentDateTime = new DateValue(new Date(), DatePrecision.Minutes);
// ==== We will assume some things and set them accordingly ====
template.setDocumentId(CDAUUID.generateUUIDString());
template.setConfidentialityCode(x_BasicConfidentialityKind._N);
// Title
if (commonFields.getDocumentTitle() != null) {
template.setDocumentTitle(commonFields.getDocumentTitle());
} else {
missingFields.addMissingField("documentTitle", "The document title must be provided");
}
// Document Type
if (commonFields.getDocumentType() != null) {
template.setDocumentType(commonFields.getDocumentType());
} else {
missingFields.addMissingField("documentType", "The document type must be provided");
}
// If no record effective date/time specified, assume the current date/time
if (commonFields.getDocumentEffectiveTime() == null) {
template.setEffectiveTime(currentDateTime);
} else {
template.setEffectiveTime(commonFields.getDocumentEffectiveTime());
}
// If no document set ID provided, generate a new one
if (commonFields.getDocumentSetID() != null) {
template.setDocumentSetId(commonFields.getDocumentSetID());
} else {
template.setDocumentSetId(CDAUUID.generateUUIDString());
}
// Version defaults to 1 unless set to a different integer value
template.setDocumentVersionNumber(String.valueOf(commonFields.getDocumentVersionNumber()));
// Patient
try {
PatientUniversal patient = createPatient(commonFields);
template.setPatient(patient);
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
// Author
if (commonFields.getTimeAuthored() == null) {
template.setTimeAuthored(currentDateTime);
} else {
template.setTimeAuthored(commonFields.getTimeAuthored());
}
try {
AuthorPersonUniversal author = createAuthor(commonFields);
template.setAuthor(author);
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
// Data Enterer (optional)
if (commonFields.getDataEntererName() != null) {
try {
template.setDataEnterer(createDataEnterer(commonFields));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
// Custodian
try {
template.setCustodianOrganisation(createCustodian(commonFields));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
// Recipients
// Having at least one recipient is mandatory
if (commonFields.getRecipients()==null) {
missingFields.addMissingField("recipients", "At least one recipient must be provided");
} else if (commonFields.getRecipients().size()==0) {
missingFields.addMissingField("recipients", "At least one recipient must be provided");
} else {
// Primary Recipients
for (DocumentRecipient recipient : commonFields.getRecipients()) {
try {
Recipient r = createRecipient(recipient);
template.addPrimaryRecipients(
new PrimaryRecipient().setRecipient(r));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
// Copy Recipients
if (commonFields.getCopyRecipients() != null) {
for (DocumentRecipient recipient : commonFields.getCopyRecipients()) {
try {
Recipient r = createRecipient(recipient);
template.addInformationOnlyRecipients(
new InformationOnlyRecipient().setRecipient(r));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
}
}
// Authenticator
if (commonFields.getAuthenticatorName() != null) {
if (commonFields.getAuthenticatedTime() == null) {
missingFields.addMissingField("authenticatedTime", "The time the document was authenticated must be provided");
} else {
template.setTimeAuthenticated(commonFields.getAuthenticatedTime());
}
try {
template.setAuthenticator(createAuthenticator(commonFields));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
// Participants
if (commonFields.getParticipants() != null) {
for (NonCodedCDAParticipant participant : commonFields.getParticipants()) {
if (participant.getParticipantType() == null) {
missingFields.addMissingField("participantType", "The participant type must be provided");
}
try {
Participant p = createParticipant(participant);
template.addParticipant((
new DocumentParticipant()
.setParticipant(p)
.setParticipantTypeCode(participant.getParticipantType().code)));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
}
// DocumentationOf
if (commonFields.getEventCode() != null) {
try {
template.addDocumentationOf(new DocumentationOf().setServiceEvent(createServiceEvent(commonFields)));
} catch (MissingMandatoryFieldException e) {
missingFields.addMissingFields(e);
}
}
// Consent
if (commonFields.getConsent() != null) {
template.setAuthorizingConsent(new Consent()
.setConsentCode(commonFields.getConsent())
.addID(new ConsentID(CDAUUID.generateUUIDString())));
}
// Encompassing Encounter
if (commonFields.getEncounterType() != null) {
template.setEncompassingEncounter(createEncompassingEncounter(commonFields));
}
// We have done all the checks on mandatory fields, so if there are any
// errors, throw them up to the caller
if (missingFields.hasEntries()) {
throw missingFields;
}
return template;
}