Search in sources :

Example 16 with EPStructureToStructureLink

use of org.olat.portfolio.model.structel.EPStructureToStructureLink in project OpenOLAT by OpenOLAT.

the class EPStructureManagerTest method testChildrenBetweenSeveralSessions.

@Test
public void testChildrenBetweenSeveralSessions() {
    // test save parent and child
    PortfolioStructure parentEl = epFrontendManager.createAndPersistPortfolioStructureElement(null, "parent-structure-el", "parent-structure-element");
    PortfolioStructure childEl1 = epFrontendManager.createAndPersistPortfolioStructureElement(parentEl, "multi-session-structure-el-1", "child-structure-element");
    dbInstance.commitAndCloseSession();
    PortfolioStructure childEl2 = epFrontendManager.createAndPersistPortfolioStructureElement(parentEl, "multi-session-structure-el-2", "child-structure-element");
    dbInstance.commitAndCloseSession();
    PortfolioStructure childEl3 = epFrontendManager.createAndPersistPortfolioStructureElement(parentEl, "multi-session-structure-el-3", "child-structure-element");
    ((EPStructureElement) parentEl).setTitle("parent-structure-el-prime");
    epStructureManager.savePortfolioStructure(parentEl);
    dbInstance.commitAndCloseSession();
    // test if all children are saved
    List<PortfolioStructure> retrievedChilrenEl = epFrontendManager.loadStructureChildren(parentEl);
    assertNotNull(retrievedChilrenEl);
    assertEquals(3, retrievedChilrenEl.size());
    // test if they are ordered
    assertEquals(childEl1.getKey(), retrievedChilrenEl.get(0).getKey());
    assertEquals(childEl2.getKey(), retrievedChilrenEl.get(1).getKey());
    assertEquals(childEl3.getKey(), retrievedChilrenEl.get(2).getKey());
    // test the title too (why not?)
    assertEquals("multi-session-structure-el-1", ((EPStructureElement) retrievedChilrenEl.get(0)).getTitle());
    assertEquals("multi-session-structure-el-2", ((EPStructureElement) retrievedChilrenEl.get(1)).getTitle());
    assertEquals("multi-session-structure-el-3", ((EPStructureElement) retrievedChilrenEl.get(2)).getTitle());
    // test if the change to the parent was not lost
    PortfolioStructure retrievedParentEl = epFrontendManager.loadPortfolioStructureByKey(parentEl.getKey());
    assertEquals("parent-structure-el-prime", ((EPStructureElement) retrievedParentEl).getTitle());
    dbInstance.commitAndCloseSession();
    // test that the children are not always loaded
    PortfolioStructure retrievedParentEl2 = epFrontendManager.loadPortfolioStructureByKey(parentEl.getKey());
    dbInstance.commitAndCloseSession();
    boolean failedToLazyLoadChildren;
    try {
        List<EPStructureToStructureLink> children = ((EPStructureElement) retrievedParentEl2).getInternalChildren();
        failedToLazyLoadChildren = (children == null || children.isEmpty());
    } catch (Exception e) {
        failedToLazyLoadChildren = true;
    }
    assertTrue(failedToLazyLoadChildren);
    dbInstance.commitAndCloseSession();
    // test load parent
    PortfolioStructure retrievedParentEl3 = epFrontendManager.loadStructureParent(childEl1);
    assertNotNull(retrievedParentEl3);
    assertEquals(parentEl.getKey(), retrievedParentEl3.getKey());
    PortfolioStructure retrievedParentEl4 = epFrontendManager.loadStructureParent(childEl2);
    assertNotNull(retrievedParentEl4);
    assertEquals(parentEl.getKey(), retrievedParentEl4.getKey());
    PortfolioStructure retrievedParentEl5 = epFrontendManager.loadStructureParent(childEl3);
    assertNotNull(retrievedParentEl5);
    assertEquals(parentEl.getKey(), retrievedParentEl5.getKey());
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) PortfolioStructure(org.olat.portfolio.model.structel.PortfolioStructure) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink) Test(org.junit.Test)

Example 17 with EPStructureToStructureLink

use of org.olat.portfolio.model.structel.EPStructureToStructureLink in project openolat by klemens.

the class EPStructureManager method copyEPStructureElementRecursively.

private void copyEPStructureElementRecursively(EPStructureElement sourceEl, EPStructureElement targetEl, boolean withArtefacts, boolean cloneRestrictions) {
    // needed if the sourceEl come from a link. Hibernate doesn't initialize the list properly
    sourceEl = (EPStructureElement) dbInstance.loadObject(sourceEl);
    if (withArtefacts) {
        List<EPStructureToArtefactLink> artefactLinks = sourceEl.getInternalArtefacts();
        for (EPStructureToArtefactLink artefactLink : artefactLinks) {
            EPStructureToArtefactLink link = instantiateClone(artefactLink);
            // make the pseudo
            link.setStructureElement(targetEl);
            // bidirectional relations
            targetEl.getInternalArtefacts().add(link);
        }
    }
    // clone the links
    List<EPStructureToStructureLink> childLinks = sourceEl.getInternalChildren();
    for (EPStructureToStructureLink childLink : childLinks) {
        copy(childLink, targetEl, withArtefacts, false, cloneRestrictions);
    }
    savePortfolioStructure(targetEl);
}
Also used : EPStructureToArtefactLink(org.olat.portfolio.model.structel.EPStructureToArtefactLink) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Example 18 with EPStructureToStructureLink

use of org.olat.portfolio.model.structel.EPStructureToStructureLink in project openolat by klemens.

the class EPStructureManager method move.

private void move(PortfolioStructure parentStructure, PortfolioStructure childStructure, boolean up) {
    if (childStructure == null || parentStructure == null)
        throw new NullPointerException();
    if (parentStructure instanceof EPStructureElement) {
        // save eventual changes
        dbInstance.updateObject(parentStructure);
        // reconnect to the session
        EPStructureElement structureEl = (EPStructureElement) dbInstance.loadObject((EPStructureElement) parentStructure);
        List<EPStructureToStructureLink> structureLinks = structureEl.getInternalChildren();
        int index = indexOf(structureLinks, childStructure);
        if (up && index > 0) {
            // swap the link with the previous link in the list
            Collections.swap(structureLinks, index, index - 1);
            dbInstance.updateObject(structureEl);
        } else if (!up && (index >= 0 && index < (structureLinks.size() - 1))) {
            // swap the link with the next link in the list
            Collections.swap(structureLinks, index, index + 1);
            dbInstance.updateObject(structureEl);
        }
    }
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Aggregations

EPStructureToStructureLink (org.olat.portfolio.model.structel.EPStructureToStructureLink)18 EPStructureElement (org.olat.portfolio.model.structel.EPStructureElement)16 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 EPStructureToArtefactLink (org.olat.portfolio.model.structel.EPStructureToArtefactLink)2 PortfolioStructure (org.olat.portfolio.model.structel.PortfolioStructure)2 PortfolioStructureMap (org.olat.portfolio.model.structel.PortfolioStructureMap)2 OLATResource (org.olat.resource.OLATResource)2