Search in sources :

Example 1 with EPStructureToStructureLink

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

the class EPStructureManager method addStructureToStructure.

/**
 * Add a child structure to the parent structure.
 * @param parentStructure
 * @param childStructure
 * @param destinationPos set to -1 to append at the end!
 */
public void addStructureToStructure(PortfolioStructure parentStructure, PortfolioStructure childStructure, int destinationPos) {
    if (parentStructure == null || childStructure == null)
        throw new NullPointerException();
    if (childStructure instanceof EPStructureElement) {
        // save eventual changes
        dbInstance.updateObject(parentStructure);
        // reconnect to the session (why reconnect? you update it already)
        // parentStructure = (EPStructureElement)dbInstance.loadObject((EPStructureElement)parentStructure);
        EPStructureToStructureLink link = new EPStructureToStructureLink();
        link.setParent(parentStructure);
        link.setChild(childStructure);
        // refresh internal link to its root element
        ((EPStructureElement) childStructure).setRoot((EPStructureElement) parentStructure);
        List<EPStructureToStructureLink> internalChildren = ((EPStructureElement) parentStructure).getInternalChildren();
        if (destinationPos == -1) {
            internalChildren.add(link);
        } else if (destinationPos <= internalChildren.size()) {
            internalChildren.add(destinationPos, link);
        } else {
            internalChildren.add(link);
        }
    }
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Example 2 with EPStructureToStructureLink

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

the class EPStructureManager method copy.

/**
 * Copy/Import structure elements recursively
 * @param refLink
 * @param targetEl
 * @param withArtefacts Copy the artefacts
 * @param importEl Don't load elements from the DB
 * @param cloneRestrictions should the collect-restrictions be applied? you could also do this manually by copyCollectRestriction()
 */
private void copy(EPStructureToStructureLink refLink, EPStructureElement targetEl, boolean withArtefacts, boolean importEl, boolean cloneRestrictions) {
    EPStructureElement childSourceEl = (EPStructureElement) refLink.getChild();
    EPStructureElement clonedChildEl = instantiateClone(refLink.getChild());
    if (clonedChildEl == null) {
        log.warn("Attempt to clone an unsupported structure type: " + refLink.getChild(), null);
    } else {
        OLATResource resource = resourceManager.createOLATResourceInstance(clonedChildEl.getClass());
        clonedChildEl.setOlatResource(resource);
        // set root
        if (targetEl.getRoot() == null) {
            // it's the root element
            clonedChildEl.setRoot(targetEl);
        } else {
            clonedChildEl.setRoot(targetEl.getRoot());
        }
        if (targetEl.getRootMap() == null && targetEl instanceof PortfolioStructureMap) {
            clonedChildEl.setRootMap((PortfolioStructureMap) targetEl);
        } else {
            clonedChildEl.setRootMap(targetEl.getRootMap());
        }
        if (!importEl)
            clonedChildEl.setStructureElSource(childSourceEl.getKey());
        if (cloneRestrictions)
            copyOrUpdateCollectRestriction(childSourceEl, clonedChildEl, true);
        if (importEl) {
            importEPStructureElementRecursively(childSourceEl, clonedChildEl);
        } else {
            copyEPStructureElementRecursively(childSourceEl, clonedChildEl, withArtefacts, cloneRestrictions);
        }
        EPStructureToStructureLink link = new EPStructureToStructureLink();
        link.setParent(targetEl);
        link.setChild(clonedChildEl);
        targetEl.getInternalChildren().add(link);
    }
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) PortfolioStructureMap(org.olat.portfolio.model.structel.PortfolioStructureMap) OLATResource(org.olat.resource.OLATResource) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Example 3 with EPStructureToStructureLink

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

the class EPStructureManager method importEPStructureElementRecursively.

private void importEPStructureElementRecursively(EPStructureElement sourceEl, EPStructureElement targetEl) {
    // clone the links
    List<EPStructureToStructureLink> childLinks = sourceEl.getInternalChildren();
    for (EPStructureToStructureLink childLink : childLinks) {
        EPStructureElement childSourceEl = (EPStructureElement) childLink.getChild();
        // remove source-info on imports.
        childSourceEl.setStructureElSource(null);
        copy(childLink, targetEl, false, true, true);
    }
    savePortfolioStructure(targetEl);
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Example 4 with EPStructureToStructureLink

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

the class EPStructureManager method reOrderStructures.

protected boolean reOrderStructures(PortfolioStructure parent, PortfolioStructure orderSubject, int orderDest) {
    EPStructureElement structureEl = (EPStructureElement) dbInstance.loadObject((EPStructureElement) parent);
    List<EPStructureToStructureLink> structureLinks = structureEl.getInternalChildren();
    int oldPos = indexOf(structureLinks, orderSubject);
    if (oldPos != orderDest && oldPos != -1) {
        EPStructureToStructureLink link = structureLinks.remove(oldPos);
        if (oldPos < orderDest) {
            orderDest--;
        }
        if (orderDest < 0) {
            orderDest = 0;
        } else if (orderDest > structureLinks.size()) {
            // place at end
            orderDest = structureLinks.size() - 1;
        }
        structureLinks.add(orderDest, link);
        dbInstance.updateObject(structureEl);
        return true;
    }
    return false;
}
Also used : EPStructureElement(org.olat.portfolio.model.structel.EPStructureElement) EPStructureToStructureLink(org.olat.portfolio.model.structel.EPStructureToStructureLink)

Example 5 with EPStructureToStructureLink

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

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)

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