Search in sources :

Example 1 with Persistable

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;
    }
}
Also used : EntityManager(javax.persistence.EntityManager) AssertException(org.olat.core.logging.AssertException) Persistable(org.olat.core.id.Persistable)

Example 2 with 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;
}
Also used : Persistable(org.olat.core.id.Persistable)

Example 3 with Persistable

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);
}
Also used : Persistable(org.olat.core.id.Persistable) PortfolioStructure(org.olat.portfolio.model.structel.PortfolioStructure) AbstractArtefact(org.olat.portfolio.model.artefacts.AbstractArtefact) Test(org.junit.Test)

Example 4 with Persistable

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;
}
Also used : INode(org.olat.core.util.nodes.INode) Persistable(org.olat.core.id.Persistable) MyQuestionsTreeNode(org.olat.modules.qpool.ui.tree.MyQuestionsTreeNode) ControllerTreeNode(org.olat.modules.qpool.ui.tree.ControllerTreeNode) CollectionTreeNode(org.olat.modules.qpool.ui.tree.CollectionTreeNode) TreeNode(org.olat.core.gui.components.tree.TreeNode) MarkedQuestionsTreeNode(org.olat.modules.qpool.ui.tree.MarkedQuestionsTreeNode)

Example 5 with Persistable

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;
    }
}
Also used : EntityManager(javax.persistence.EntityManager) AssertException(org.olat.core.logging.AssertException) Persistable(org.olat.core.id.Persistable)

Aggregations

Persistable (org.olat.core.id.Persistable)10 EntityManager (javax.persistence.EntityManager)2 Test (org.junit.Test)2 TreeNode (org.olat.core.gui.components.tree.TreeNode)2 AssertException (org.olat.core.logging.AssertException)2 INode (org.olat.core.util.nodes.INode)2 CollectionTreeNode (org.olat.modules.qpool.ui.tree.CollectionTreeNode)2 ControllerTreeNode (org.olat.modules.qpool.ui.tree.ControllerTreeNode)2 MarkedQuestionsTreeNode (org.olat.modules.qpool.ui.tree.MarkedQuestionsTreeNode)2 MyQuestionsTreeNode (org.olat.modules.qpool.ui.tree.MyQuestionsTreeNode)2 AbstractArtefact (org.olat.portfolio.model.artefacts.AbstractArtefact)2 PortfolioStructure (org.olat.portfolio.model.structel.PortfolioStructure)2