Skip to content

Commit

Permalink
Merge pull request #1855 from preethi29/TRUNK-4963
Browse files Browse the repository at this point in the history
Reload parent obs from db instead of manipulating hibernate session object - TRUNK-4963
  • Loading branch information
wluyima committed Oct 13, 2016
2 parents 4fba636 + 3c1ecc4 commit 6e98a8c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
10 changes: 10 additions & 0 deletions api/src/main/java/org/openmrs/api/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,16 @@ public static boolean isSessionOpen() {
return userContextHolder.get() != null;
}

/**
* Used to re-read the state of the given instance from the underlying database.
* @since 2.0
* @param obj The object to refresh from the database in the session
*/
public static void refreshEntity(Object obj) {
log.trace("refreshing object: "+obj);
getContextDAO().refreshEntity(obj);
}

/**
* Used to clear a cached object out of a session in the middle of a unit of work. Future
* updates to this object will not be saved. Future gets of this object will not fetch this
Expand Down
11 changes: 10 additions & 1 deletion api/src/main/java/org/openmrs/api/db/ContextDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ public interface ContextDAO {
* @see org.openmrs.api.context.Context#evictFromSession(Object)
*/
public void evictFromSession(Object obj);


/**
* Used to re-read the state of the given instance from the underlying database.
* @since 2.0
*
* @param obj The object to refresh from the database in the session
* @see org.openmrs.api.context.Context#refreshEntity(Object)
*/
public void refreshEntity(Object obj);

/**
* Starts the OpenMRS System
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ public void evictFromSession(Object obj) {
sessionFactory.getCurrentSession().evict(obj);
}

/**
* @see org.openmrs.api.db.ContextDAO#refreshEntity(Object)
*/
public void refreshEntity(Object obj) {
sessionFactory.getCurrentSession().refresh(obj);
}

/**
* @see org.openmrs.api.db.ContextDAO#flushSession()
*/
Expand Down
20 changes: 5 additions & 15 deletions api/src/main/java/org/openmrs/api/impl/ObsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -180,28 +179,19 @@ private Obs saveObsNotDirty(Obs obs, String changeMessage) {
}

ObsService os = Context.getObsService();
List<Obs> toRemove = new ArrayList<>();
List<Obs> toAdd = new ArrayList<>();
boolean refreshNeeded = false;
for (Obs o : obs.getGroupMembers(true)) {
if (o.getId() == null) {
os.saveObs(o, null);
} else {
Obs replacement = os.saveObs(o, changeMessage);
if(!replacement.equals(o)){
toRemove.add(o);
toAdd.add(os.getObs(o.getId()));
toAdd.add(replacement);
}
Obs newObs = os.saveObs(o, changeMessage);
refreshNeeded = !newObs.equals(o) || refreshNeeded;
}
}

for (Obs o : toRemove) {
obs.removeGroupMember(o);
if(refreshNeeded) {
Context.refreshEntity(obs);
}
for (Obs o : toAdd) {
obs.addGroupMember(o);
}

return obs;
}

Expand Down
15 changes: 15 additions & 0 deletions api/src/test/java/org/openmrs/api/EncounterServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ public void saveEncounter_shouldUpdateExistingEncounterWhenAChildObsIsEdited() t
assertEquals("Obs value updated", o3.getValueText());
}

@Test
@Verifies(value = "should update leaf obs and not duplicate at encounter level", method = "saveEncounter(Encounter)")
public void saveEncounter_shouldUpdateValueOfLeafObsAndNotDuplicateAtEncounterLevel() throws Exception {
executeDataSet(ENC_OBS_HIERARCHY_DATA_XML);
Encounter encounter = Context.getEncounterService().getEncounter(100);
assertEquals(1, encounter.getObsAtTopLevel(true).size());
Obs topLevelObs = encounter.getObsAtTopLevel(true).iterator().next();
topLevelObs.getGroupMembers().iterator().next().setValueText("editing first obs");
encounter.addObs(topLevelObs);

Encounter savedEncounter = Context.getEncounterService().saveEncounter(encounter);

assertEquals(1, savedEncounter.getObsAtTopLevel(true).size());
}

@Test
@Verifies(value = "should update existing encounter correctly when a new obs is added to parent obs", method = "saveEncounter(Encounter)")
public void saveEncounter_shouldUpdateExistingEncounterWhenNewObsIsAddedToParentObs() throws Exception {
Expand Down

0 comments on commit 6e98a8c

Please sign in to comment.