‘Spring cleaning …. ‘

This commit is contained in:
jkv 2017-03-07 22:45:49 +01:00
parent fb80c79103
commit 2cb3aaa2a1
2 changed files with 43 additions and 60 deletions

View File

@ -16,12 +16,13 @@ import org.glassfish.jersey.media.sse.EventOutput;
import org.glassfish.jersey.media.sse.OutboundEvent;
import org.glassfish.jersey.media.sse.SseBroadcaster;
import org.glassfish.jersey.media.sse.SseFeature;
import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.Patient;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsResourceProvider;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.annotation.ConditionalUrlParam;
import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.RequiredParam;
@ -44,42 +45,17 @@ public class JaxRsPatientProvider extends AbstractJaxRsResourceProvider<Patient>
@Inject
public JaxRsPatientProvider() {
super(FhirContext.forDstu3(), JaxRsPatientProvider.class);
new Thread() {
@Override
public void run() {
try {
while (true) {
Thread.sleep(1000);
final int id = patients.size() + 1;
final Patient p = new Patient();
final Identifier i = new Identifier();
i.setValue(id + "");
p.addName().setFamily("John " + i);
p.getIdentifier().add(i);
p.setId(new IdType(id));
patients.put(id + "", p);
broadcaster.broadcast(new OutboundEvent.Builder().name("patients").data(String.class, patients.size() + "").build());
}
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
@Search
public List<Patient> search(@RequiredParam(name = Patient.SP_NAME) final StringParam name) {
final List<Patient> result = new LinkedList<Patient>();
for (final Patient patient : patients.values()) {
Patient single = null;
if (name == null || patient.getName().get(0).getFamilyElement().getValueNotNull()
.equals(name.getValueNotNull())) {
single = patient;
}
if (name == null
|| patient.getName().get(0).getFamilyElement().getValueNotNull().equals(name.getValueNotNull())) {
single = patient;
}
if (single != null) {
result.add(single);
}
@ -91,13 +67,42 @@ public class JaxRsPatientProvider extends AbstractJaxRsResourceProvider<Patient>
public MethodOutcome create(@ResourceParam final Patient patient, @ConditionalUrlParam final String theConditional)
throws Exception {
patients.put(patient.getIdentifierFirstRep().getId(), patient);
storePatient(patient);
final MethodOutcome result = new MethodOutcome().setCreated(true);
result.setResource(patient);
result.setId(new IdType(patient.getId()));
return result;
}
// Conceptual wrapper for storing in a db
private void storePatient(final Patient patient) {
try {
patients.put(patient.getIdentifierFirstRep().getValue(), patient);
// if storing is successful the notify the listeners that listens on
// any patient => patient/*
final String bundleToString = currentPatientsAsJsonString();
broadcaster
.broadcast(new OutboundEvent.Builder().name("patients").data(String.class, bundleToString).build());
} catch (final Exception e) {
e.printStackTrace();
}
}
private String currentPatientsAsJsonString() {
final IParser jsonParser = this.getFhirContext().newJsonParser().setPrettyPrint(true);
final org.hl7.fhir.dstu3.model.Bundle bundle = new org.hl7.fhir.dstu3.model.Bundle();
for (final Patient p : patients.values())
bundle.addEntry(new BundleEntryComponent().setResource(p));
final String bundleToString = jsonParser.encodeResourceToString(bundle);
return bundleToString;
}
@Override
public ETagSupportEnum getETagSupport() {
return ETagSupportEnum.DISABLED;
@ -108,14 +113,16 @@ public class JaxRsPatientProvider extends AbstractJaxRsResourceProvider<Patient>
return Patient.class;
}
@GET
@Path("listen")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput listenToBroadcast() throws IOException {
final EventOutput eventOutput = new EventOutput();
final String bundleToString = currentPatientsAsJsonString();
eventOutput.write(
new OutboundEvent.Builder().name("patient count").data(String.class, patients.size() + "").build());
new OutboundEvent.Builder().name("patients").data(String.class, bundleToString).build());
this.broadcaster.add(eventOutput);
return eventOutput;
}

View File

@ -21,25 +21,18 @@ import java.awt.Desktop;
import java.net.URI;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.DispatcherType;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.hl7.fhir.dstu3.model.HumanName;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Patient;
import org.slf4j.bridge.SLF4JBridgeHandler;
import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.Stage;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceFilter;
import com.google.inject.servlet.GuiceServletContextListener;
@ -52,28 +45,11 @@ public class GuiceJersey2ServletContextListener extends GuiceServletContextListe
final List<Module> modules = Lists.newArrayList();
modules.add(new AbstractModule() {
@Override
protected void configure() {
final ConcurrentHashMap<String, List<Patient>> patients = new ConcurrentHashMap<String, List<Patient>>();
for (int i = 0; i < 20; i++) {
final Patient patient = new Patient();
patient.getName().add(new HumanName().setFamily("Random Patient " + i));
patient.setId(new IdType("Patient", "" + i, "" + 1));
patients.put(String.valueOf(i), Lists.newArrayList(patient));
}
}
});
modules.add(new GuiceHk2Helper() {
@Override
protected void configureServlets() {
bind(String.class).annotatedWith(Names.named("1")).toInstance("sad");
bind(JaxRsPatientProvider.class).in(Scopes.SINGLETON);
// bind(JaxRsPatientProvider.class).in(Scopes.SINGLETON);
rest("/*").packages(JaxRsPatientProvider.class);
}
});