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;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import org.owasp.esapi.ESAPI;
import org.owasp.esapi.Encoder;
import org.owasp.esapi.errors.EncodingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gov.va.med.pbm.ampl.fhir.client.IFhirClient;
import gov.va.med.pbm.ampl.utility.ESAPIValidator;
import gov.va.med.pbm.ampl.utility.operations.PagingOperation;
import gov.va.med.pbm.ampl.utility.operations.SearchOperation;
import gov.va.med.pbm.ampl.utility.operations.SortOperation;
/**
* AbstractUnMarshaller is an abstract class using generics. The class implements {@link IUnmarshaller}
*
* @author Ian Meinert
*
* @param <T> The Type definition
*/
public abstract class AbstractUnmarshaller<T> implements IUnmarshaller<T> {
/**
* The FHIR client interface used to communicate with the server.
*/
private IFhirClient client;
/**
* application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(AbstractUnmarshaller.class);
/**
* The patient Id.
*/
private String patientId;
/**
* The searchTerm.
*/
private Optional<String> searchTerm;
/**
* The sortTerm.
*/
private Optional<String> sortTerm;
/**
* The size.
*/
private Optional<Integer> size;
/**
* The page.
*/
private Optional<Integer> page;
/**
* The collection.
*/
private Collection<T> collection;
/**
* The default constructor for the AbstractUnMarshaler class.
*/
public AbstractUnmarshaller() {
}
/**
* The constructor which accepts multiple parameters for marshaling data.
*
* @param client The {@link IFhirClient} used to communicate with the server
* @param patientId The patient Id
* @param searchTerm String value representing the search term and property values
* @param sortTerm String value representing the sort property and direction values
*/
public AbstractUnmarshaller(IFhirClient client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm) {
this.client = client;
this.patientId = patientId;
this.searchTerm = searchTerm;
this.sortTerm = sortTerm;
this.collection = new ArrayList<T>();
}
/**
* The constructor which accepts multiple parameters for marshaling data.
*
* @param client The {@link IFhirClient} used to communicate with the server
* @param patientId The patient Id
* @param searchTerm String value representing the search term and property values
* @param sortTerm String value representing the sort property and direction values
* @param page the page number for the results
* @param size the limit of the results per page
*/
public AbstractUnmarshaller(IFhirClient client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm,
Optional<Integer> page, Optional<Integer> size) {
this.client = client;
this.patientId = patientId;
this.searchTerm = searchTerm;
this.sortTerm = sortTerm;
this.page = page.isPresent() ? page : Optional.of(1);
this.size = size.isPresent() ? size : Optional.of(0);
this.collection = new ArrayList<T>();
}
/**
* The template for sorting records.
*/
@Override
public void search() {
SearchOperation<T> operation = new SearchOperation<T>();
Collection<T> collectionToFilter = collection;
String decodedSearch = new String();
try {
if (this.searchTerm.isPresent()) {
Encoder encoder = ESAPI.encoder();
decodedSearch = encoder.decodeFromURL(searchTerm.get());
}
} catch (EncodingException e) {
LOGGER.error("unable to decode search term: {}", ESAPIValidator.validateLogParam(searchTerm.get()), e);
}
operation.setSearchParams(decodedSearch);
this.collection = operation.search(collectionToFilter);
}
@Override
public final void sort() {
SortOperation<T> operation = new SortOperation<T>();
Collection<T> collectionToFilter = collection;
String decodedSort = new String();
try {
if (sortTerm.isPresent()) {
Encoder encoder = ESAPI.encoder();
decodedSort = encoder.decodeFromURL(sortTerm.get());
}
} catch (EncodingException e) {
LOGGER.error("Error in decoding URL: {}", ESAPIValidator.validateLogParam(sortTerm.get()), e);
}
operation.setSortParams(decodedSort);
this.collection = operation.sort(collectionToFilter);
}
@Override
public final void paginate() {
PagingOperation<T> operation = new PagingOperation<T>();
Collection<T> collectionToPaginate = collection;
operation.setSize(size.get());
operation.setPage(page.get());
this.collection = operation.paginate(collectionToPaginate);
}
/**
* The getter for the collection.
*
* @return the collection
*/
public Collection<T> getCollection() {
return this.collection;
}
/**
* The setter for the collection.
*
* @param collection A collection of Generic type T
*/
public void setCollection(Collection<T> collection) {
this.collection = collection;
}
/**
* The getter for the client.
*
*
* @return The IFhirClient in context
*/
public IFhirClient getClient() {
return client;
}
/**
* The setter for the client.
*
* @param client the FHIR client
*/
public void setClient(IFhirClient client) {
this.client = client;
}
/**
* The getter for the patientId.
*
*
* @return the patient Id
*/
public String getPatientId() {
return patientId;
}
/**
* The setter for the patientId.
*
* @param patientId the patient id
*/
public void setPatientId(String patientId) {
this.patientId = patientId;
}
/**
* The getter for the sortTerm.
*
* @return the sortTerm
*/
public Optional<String> getSortTerm() {
return sortTerm;
}
/**
* The setter for the sortTerm.
*
* @param sortTerm the sortTerm to set
*/
public void setSortTerm(Optional<String> sortTerm) {
this.sortTerm = sortTerm;
}
/**
* The getter for the searchTerm.
*
* @return the searchTerm
*/
public Optional<String> getSearchTerm() {
return searchTerm;
}
/**
* The setter for the searchTerm.
*
* @param searchTerm the searchTerm to set
*/
public void setSearchTerm(Optional<String> searchTerm) {
this.searchTerm = searchTerm;
}
/**
* The getter for the size.
*
* @return the size
*/
public Optional<Integer> getSize() {
return size;
}
/**
* The setter for the size.
*
* @param size the size to set
*/
public void setSize(Optional<Integer> size) {
this.size = size;
}
/**
* The getter for the page.
*
* @return the page
*/
public Optional<Integer> getPage() {
return page;
}
/**
* The setter for the page.
*
* @param page the page to set
*/
public void setPage(Optional<Integer> page) {
this.page = page;
}
}