* @throws Exception
*/
public static Encounter trimEncounterToMatchForm(Encounter e, HtmlForm htmlform) throws Exception {
//this should move existing obs from session to tag handlers.
FormEntrySession session = new FormEntrySession(e.getPatient(), e, FormEntryContext.Mode.VIEW, htmlform, null); // session gets a null HttpSession
session.getHtmlToDisplay();
if (log.isDebugEnabled()){
Map<Concept, List<Obs>> map = session.getContext().getExistingObs();
if (map != null){
for (Map.Entry<Concept, List<Obs>> existingObs : map.entrySet()){
List<Obs> oList = existingObs.getValue();
for (Obs oInner : oList)
log.debug("Obs in existingObs " + existingObs.getKey() + " " + oInner.getConcept());
}
}
Map<Obs, Set<Obs>> map2 = session.getContext().getExistingObsInGroups();
if (map2 != null){
for (Map.Entry<Obs, Set<Obs>> existingObsInGroups : map2.entrySet()){
Set<Obs> oList = existingObsInGroups.getValue();
for (Obs oInner : oList)
log.debug("Obs in existingObsInGroups " + existingObsInGroups.getKey().getConcept() + " " + oInner.getConcept());
}
}
}
Encounter ret = new Encounter();
ret.setCreator(e.getCreator());
ret.setEncounterDatetime(e.getEncounterDatetime());
ret.setProvider(e.getProvider());
ret.setLocation(e.getLocation());
ret.setDateCreated(e.getDateCreated());
ret.setPatient(e.getPatient());
//renders new encounter unsave-able:
ret.setEncounterId(e.getEncounterId());
for (Obs oTest : e.getAllObs()){
boolean found = false;
if (session.getContext().getExistingObs() != null && !oTest.isObsGrouping()){
List<Obs> obsList = session.getContext().getExistingObs().get(oTest.getConcept());
if (obsList != null && obsList.size() > 0){
for (Obs o : obsList){
if (o.getObsId().equals(oTest.getObsId())){
found = true;
continue;
}
}
}
}
if (!found && session.getContext().getExistingObsInGroups() != null){
for (Map.Entry<Obs, Set<Obs>> mapEntry : session.getContext().getExistingObsInGroups().entrySet()){
if (mapEntry.getKey().equals(oTest)){
found = true;
continue;
} else {
Set<Obs> oSet = mapEntry.getValue();