Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.pbm.ampl.fhir.unmarshall.dstu2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.uhn.fhir.model.dstu2.resource.Bundle;
import gov.va.med.pbm.ampl.fhir.client.Dstu2Client;
import gov.va.med.pbm.ampl.fhir.unmarshall.AbstractUnmarshaller;
import gov.va.med.pbm.ampl.fhir.unmarshall.dstu2.unbundle.UnbundleProblem;
import gov.va.med.pbm.ampl.model.Comment;
import gov.va.med.pbm.ampl.model.Problem;
import gov.va.med.pbm.ampl.utility.operations.SortOperation;
import gov.va.med.pbm.ampl.utility.ESAPIValidationType;
import gov.va.med.pbm.ampl.utility.ESAPIValidator;
import gov.va.med.pbm.ampl.utility.operations.SearchOperation;
import gov.va.med.pbm.ampl.utility.operations.criteria.SearchCriteria;

/**
* The UnmarshallCondition class marshals the FHIR Condition resource to a client readable JSON format.
*
* @author Ian Meinert
* @author Pavani Mukthipudi
*
*/
public class UnmarshallCondition extends AbstractUnmarshaller<Problem> {

/**
* The application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(UnmarshallCondition.class);

private Optional<String> searchTerm;

/**
* The overloaded constructor accepting the Dstu2Client, patient Id, search term, and sort term.
* <p>
* The constructor also passes these parameters to the super class, along with the PatientDetails class definition.
*
* @param client the DSTU2 client
* @param patientId the patient id
* @param searchTerm the search field, operation, and value
* @param sortTerm the field and direction to sort
*/
public UnmarshallCondition(Dstu2Client client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm) {
super(client, patientId, searchTerm, sortTerm);
this.searchTerm = searchTerm;
}

/**
* The overloaded constructor accepting the Dstu2Client, patient Id, search term, sort term, size, and page.
* <p>
* The constructor also passes these parameters to the super class, along with the class definition.
*
* @param client the DSTU2 client
* @param patientId the patient id
* @param searchTerm the search field, operation, and value
* @param sortTerm the field and direction to sort
* @param size the limit of the results per page
* @param page the page number for the results
*/
public UnmarshallCondition(Dstu2Client client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm,
Optional<Integer> page, Optional<Integer> size) {
super(client, patientId, searchTerm, sortTerm, page, size);
this.searchTerm = searchTerm;
}

/**
* This method queries the FHIR server for all patients that match the patient Id.
*/
@Override
public void getAllPatientsById() {

// the collection of progress notes to be returned
Collection<Problem> problemList = new ArrayList<Problem>();

Optional<Bundle> optBundle = Optional
.ofNullable(super.getClient().searchProblemListByPatientId(ESAPIValidator.validateStringInput(this.getPatientId().toString(),ESAPIValidationType.CROSS_SITE_SCRIPTING_REFLECTED)));

if (optBundle.isPresent()) {
List<ca.uhn.fhir.model.dstu2.resource.Bundle.Entry> entries = optBundle.get().getEntry();
UnbundleProblem unbundleProblem = new UnbundleProblem(ESAPIValidator.validateStringInput(this.getPatientId(),ESAPIValidationType.CROSS_SITE_SCRIPTING_REFLECTED), entries);

problemList = unbundleProblem.getEntries();

}

LOGGER.info("Storing {} record(s)", problemList.size());
super.setCollection(problemList);
}

/**
* This method overrides the super by checking the dot notation first for queries against the {@link Problem} comments.
*/
@Override
public void search() {
String searchParams = searchTerm.isPresent() ? searchTerm.get() : new String();

// set up the Problem search operation
SearchOperation<Problem> opProblem = new SearchOperation<Problem>();
Collection<Problem> problems = super.getCollection();

opProblem.setSearchParams(searchParams);

// now that the search params are set, lets see if there is one for the comments
Iterator<Entry<String, List<SearchCriteria>>> it = opProblem.getSearchParams().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, List<SearchCriteria>> pair = (Map.Entry<String, List<SearchCriteria>>) it.next();
List<SearchCriteria> criterias = pair.getValue();

for (int i = 0; i < pair.getValue().size(); i++) {
SearchCriteria criteria = pair.getValue().get(i);

String[] keys = criteria.getKey().split("\\.");

if (criteria.getKey().contains(".") && keys[0].equalsIgnoreCase("comments")) {
// Instantiate the operation and set the found search parameter
SearchOperation<Comment> opComment = new SearchOperation<Comment>();
criteria.setKey(keys[1]);
opComment.setSearchParams(criteria.toString());

Iterator<Problem> probIt = problems.iterator();
// Apply the filter to each of the Problems comments.
while (probIt.hasNext()) {
Collection<Comment> c = new ArrayList<Comment>();
Problem p = probIt.next();
c.addAll(p.getComments());

if (opComment.search(c).isEmpty()) {
probIt.remove();
}
}

// the criteria was already applied, remove it
criterias.remove(criteria);
}
}

// update the search params
opProblem.getSearchParams().put(pair.getKey(), criterias);
}

super.setCollection(opProblem.search(problems));
}

/**
* This method implements a business rule which sorts the problem list comments by date desc by default.
*/
public void sortCommentsByDateDesc() {
List<Problem> problemCollection = new ArrayList<>(this.getCollection());
SortOperation<Comment> sortOperation = new SortOperation<Comment>();
sortOperation.setSortParams("signature.signDate[DESC]");

for (Problem item : problemCollection) {
if (!item.getComments().isEmpty()) {
Collection<Comment> temp = sortOperation.sort(item.getComments());
item.setComments(temp);
}

}
super.setCollection(problemCollection);
}
}