use of org.olat.core.id.Persistable in project OpenOLAT by OpenOLAT.
the class DBImpl method loadObject.
/**
* loads an object if needed. this makes sense if you have an object which had
* been generated in a previous hibernate session AND you need to access a Set
* or a attribute which was defined as a proxy.
*
* @param persistable the object which needs to be reloaded
* @param forceReloadFromDB if true, force a reload from the db (e.g. to catch
* up to an object commited by another thread which is still in this
* thread's session cache
* @return the loaded Object
*/
@Override
public Persistable loadObject(Persistable persistable, boolean forceReloadFromDB) {
if (persistable == null)
throw new AssertException("persistable must not be null");
EntityManager em = getCurrentEntityManager();
Class<? extends Persistable> theClass = persistable.getClass();
if (forceReloadFromDB) {
if (contains(persistable)) {
// case b - then we can use evict and load
evict(em, persistable, getData());
return em.find(theClass, persistable.getKey());
} else {
// case a or c - unfortunatelly we can't distinguish these two cases
// and session.refresh(Object) doesn't work.
// the only scenario that works is load/evict/load
Persistable attachedObj = em.find(theClass, persistable.getKey());
evict(em, attachedObj, getData());
return em.find(theClass, persistable.getKey());
}
} else if (!contains(persistable)) {
// therefore the following loadObject can either return it from the cache or load it from the DB
return em.find(theClass, persistable.getKey());
} else {
// nothing to do, return the same object
return persistable;
}
}
use of org.olat.core.id.Persistable in project OpenOLAT by OpenOLAT.
the class PersistenceHelper method removeObjectsFromList.
/**
* Removes a list of persistable objects from another list with
* persistable objects by comparing the hibernate key instead of the
* object identity.
* @param originalList
* @param toBeRemovedObjects
* @return int number of objects removed from the originalList
* After calling this operation the originalList will contain less or the same amount of
* objects
*/
public static int removeObjectsFromList(List<? extends Persistable> originalList, List<? extends Persistable> toBeRemovedObjects) {
int counter = 0;
Iterator<? extends Persistable> removeIter = toBeRemovedObjects.iterator();
while (removeIter.hasNext()) {
Persistable toBeRemoved = removeIter.next();
Iterator<? extends Persistable> originalIter = originalList.iterator();
while (originalIter.hasNext()) {
Persistable fromOriginal = originalIter.next();
if (fromOriginal.getKey().equals(toBeRemoved.getKey())) {
originalList.remove(fromOriginal);
counter++;
break;
}
}
}
return counter;
}
use of org.olat.core.id.Persistable in project openolat by klemens.
the class EPStructureManagerTest method testGetReferencedMapsForArtefact.
@Test
public void testGetReferencedMapsForArtefact() {
PortfolioStructure el = epFrontendManager.createAndPersistPortfolioStructureElement(null, "structure-el", "structure-element");
dbInstance.commitAndCloseSession();
AbstractArtefact artefact = epFrontendManager.createAndPersistArtefact(ident1, "Forum");
epFrontendManager.addArtefactToStructure(ident1, artefact, el);
dbInstance.commitAndCloseSession();
// get the referenced maps
List<PortfolioStructure> mapList = epFrontendManager.getReferencedMapsForArtefact(artefact);
assertTrue(((Persistable) el).equalsByPersistableKey((Persistable) mapList.get(0)));
dbInstance.commitAndCloseSession();
// make the test more complex
// reload the structure element
el = epFrontendManager.loadPortfolioStructureByKey(el.getKey());
// add artefact to substructure (page) and check for the same map
PortfolioStructure childEl = epFrontendManager.createAndPersistPortfolioStructureElement(el, "child-structure-el", "child-structure-element");
el = epFrontendManager.removeArtefactFromStructure(artefact, el);
epFrontendManager.addArtefactToStructure(ident1, artefact, childEl);
dbInstance.commitAndCloseSession();
// get the referenced maps
List<PortfolioStructure> mapList2 = epFrontendManager.getReferencedMapsForArtefact(artefact);
assertTrue(((Persistable) el).equalsByPersistableKey((Persistable) mapList2.get(0)));
dbInstance.commitAndCloseSession();
// add artefact to 3 maps and check to get all of them
PortfolioStructure el2 = epFrontendManager.createAndPersistPortfolioStructureElement(null, "structure-el-2", "structure-element-2");
epFrontendManager.addArtefactToStructure(ident1, artefact, el2);
PortfolioStructure el3 = epFrontendManager.createAndPersistPortfolioStructureElement(null, "structure-el-3", "structure-element-3");
epFrontendManager.addArtefactToStructure(ident1, artefact, el3);
List<PortfolioStructure> mapList3 = epFrontendManager.getReferencedMapsForArtefact(artefact);
assertEquals(3, mapList3.size());
boolean found = false;
for (PortfolioStructure mapValue : mapList3) {
if (((Persistable) mapValue).equalsByPersistableKey((Persistable) el)) {
found = true;
}
}
assertTrue(found);
}
use of org.olat.core.id.Persistable in project openolat by klemens.
the class QuestionPoolMainEditorController method findNodeByPersistableUserObject.
private TreeNode findNodeByPersistableUserObject(TreeNode parentNode, Long id) {
if (parentNode == null || id == null) {
return null;
}
for (int i = parentNode.getChildCount(); i-- > 0; ) {
INode node = parentNode.getChildAt(i);
if (node instanceof TreeNode) {
TreeNode treeNode = (TreeNode) node;
Object userObj = treeNode.getUserObject();
if (userObj instanceof Persistable) {
Persistable obj = (Persistable) userObj;
if (id.equals(obj.getKey())) {
return treeNode;
}
}
}
}
return null;
}
use of org.olat.core.id.Persistable in project openolat by klemens.
the class DBImpl method loadObject.
/**
* loads an object if needed. this makes sense if you have an object which had
* been generated in a previous hibernate session AND you need to access a Set
* or a attribute which was defined as a proxy.
*
* @param persistable the object which needs to be reloaded
* @param forceReloadFromDB if true, force a reload from the db (e.g. to catch
* up to an object commited by another thread which is still in this
* thread's session cache
* @return the loaded Object
*/
@Override
public Persistable loadObject(Persistable persistable, boolean forceReloadFromDB) {
if (persistable == null)
throw new AssertException("persistable must not be null");
EntityManager em = getCurrentEntityManager();
Class<? extends Persistable> theClass = persistable.getClass();
if (forceReloadFromDB) {
if (contains(persistable)) {
// case b - then we can use evict and load
evict(em, persistable, getData());
return em.find(theClass, persistable.getKey());
} else {
// case a or c - unfortunatelly we can't distinguish these two cases
// and session.refresh(Object) doesn't work.
// the only scenario that works is load/evict/load
Persistable attachedObj = em.find(theClass, persistable.getKey());
evict(em, attachedObj, getData());
return em.find(theClass, persistable.getKey());
}
} else if (!contains(persistable)) {
// therefore the following loadObject can either return it from the cache or load it from the DB
return em.find(theClass, persistable.getKey());
} else {
// nothing to do, return the same object
return persistable;
}
}
Aggregations