Search in sources :

Example 1 with CPOrganization

use of org.olat.ims.cp.objects.CPOrganization in project OpenOLAT by OpenOLAT.

the class CPCore method addElementAfter.

/**
 * adds an element to the cp. Adds it after the item with identifier "id"
 *
 * @param newElement
 * @param id
 * @return
 */
public boolean addElementAfter(DefaultElement newElement, String id) {
    DefaultElement beforeElement = rootNode.getElementByIdentifier(id);
    if (beforeElement == null)
        return false;
    if (beforeElement instanceof CPItem) {
        // beforeElement is a <item>
        // ==> newElement has to be an <item>
        CPItem beforeItem = (CPItem) beforeElement;
        DefaultElement parent = beforeItem.getParentElement();
        if (!(newElement instanceof CPItem)) {
            throw new OLATRuntimeException(CPOrganizations.class, "only <item> element allowed", new Exception());
        }
        if (parent instanceof CPItem) {
            CPItem p = (CPItem) parent;
            p.addItemAt((CPItem) newElement, beforeItem.getPosition() + 1);
        } else if (parent instanceof CPOrganization) {
            CPOrganization o = (CPOrganization) parent;
            o.addItemAt((CPItem) newElement, beforeItem.getPosition() + 1);
        } else {
            throw new OLATRuntimeException(CPOrganizations.class, "you cannot add an <item> element to a " + parent.getName() + " element", new Exception());
        }
    }
    return true;
}
Also used : CPOrganizations(org.olat.ims.cp.objects.CPOrganizations) DefaultElement(org.dom4j.tree.DefaultElement) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPItem(org.olat.ims.cp.objects.CPItem) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPOrganization(org.olat.ims.cp.objects.CPOrganization)

Example 2 with CPOrganization

use of org.olat.ims.cp.objects.CPOrganization in project OpenOLAT by OpenOLAT.

the class CPCore method removeElement.

/**
 * removes an element with identifier "identifier" from the manifest
 *
 * @param identifier the identifier if the element to remove
 * @param booleanFlag indicates whether to remove linked resources as well...!
 *          (needed for moving elements)
 */
public void removeElement(String identifier, boolean resourceFlag) {
    DefaultElement el = rootNode.getElementByIdentifier(identifier);
    if (el != null) {
        if (el instanceof CPItem) {
            // element is CPItem
            CPItem item = (CPItem) el;
            // first remove resources
            if (resourceFlag) {
                // Delete children (depth first search)
                removeChildElements(item, resourceFlag);
                // remove referenced resource
                CPResource res = (CPResource) rootNode.getElementByIdentifier(item.getIdentifierRef());
                if (res != null && referencesCount(res) == 1) {
                    res.removeFromManifest();
                }
            }
            // then remove item
            item.removeFromManifest();
        } else if (el instanceof CPOrganization) {
            // element is organization
            CPOrganization org = (CPOrganization) el;
            org.removeFromManifest(resourceFlag);
        } else if (el instanceof CPMetadata) {
            // element is <metadata>
            CPMetadata md = (CPMetadata) el;
            md.removeFromManifest();
        }
    } else {
        throw new OLATRuntimeException(CPOrganizations.class, "couldn't remove element with id \"" + identifier + "\"! Element not found in manifest ", new Exception());
    }
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPResource(org.olat.ims.cp.objects.CPResource) CPItem(org.olat.ims.cp.objects.CPItem) CPOrganization(org.olat.ims.cp.objects.CPOrganization) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPMetadata(org.olat.ims.cp.objects.CPMetadata)

Example 3 with CPOrganization

use of org.olat.ims.cp.objects.CPOrganization in project OpenOLAT by OpenOLAT.

the class CPCore method referencesCount.

/**
 * Checks how many item-elements link to the given resource element.
 *
 * @param resource
 * @return
 */
protected int referencesCount(CPResource resource) {
    int linkCount = 0;
    Vector<CPItem> items = new Vector<CPItem>();
    for (Iterator<CPOrganization> it = rootNode.getOrganizations().getOrganizationIterator(); it.hasNext(); ) {
        CPOrganization org = it.next();
        items.addAll(org.getAllItems());
    }
    for (CPItem item : items) {
        if (item.getIdentifierRef().equals(resource.getIdentifier()))
            linkCount++;
    }
    Vector<CPDependency> dependencies = rootNode.getResources().getAllDependencies();
    for (CPDependency dependency : dependencies) {
        if (dependency.getIdentifierRef().equals(resource.getIdentifier()))
            linkCount++;
    }
    return linkCount;
}
Also used : Vector(java.util.Vector) CPItem(org.olat.ims.cp.objects.CPItem) CPOrganization(org.olat.ims.cp.objects.CPOrganization) CPDependency(org.olat.ims.cp.objects.CPDependency)

Example 4 with CPOrganization

use of org.olat.ims.cp.objects.CPOrganization in project OpenOLAT by OpenOLAT.

the class CPCore method addElement.

// *** CP manipulation ***
/**
 * adds an element as a child to the element with id parentId if the element
 * with parentId is not found, it returns false
 *
 * if adding was successful, it returns true
 */
public boolean addElement(DefaultElement newElement, String parentId, int position) {
    DefaultElement parentElement = rootNode.getElementByIdentifier(parentId);
    if (parentElement == null) {
        throw new OLATRuntimeException(CPOrganizations.class, "Parent-element with identifier:\"" + parentId + "\" not found!", new Exception());
    }
    if (parentElement instanceof CPItem) {
        // parent is a <item>
        if (newElement instanceof CPItem) {
            // only CPItems can be added to CPItems
            CPItem item = (CPItem) parentElement;
            item.addItemAt((CPItem) newElement, position);
            return true;
        } else {
            throw new OLATRuntimeException(CPOrganizations.class, "you can only add <item>  elements to an <item>-element", new Exception());
        }
    } else if (parentElement instanceof CPOrganization) {
        // parent is a <organization>
        if (newElement instanceof CPItem) {
            // add a new item to organization element
            CPOrganization org = (CPOrganization) parentElement;
            org.addItemAt((CPItem) newElement, position);
            return true;
        } else {
            throw new OLATRuntimeException(CPOrganizations.class, "you can only add <item>  elements to an <organization>-element", new Exception());
        }
    } else if (parentElement instanceof CPResource) {
        // parent is a <resource>
        CPResource resource = (CPResource) parentElement;
        if (newElement instanceof CPFile) {
            resource.addFile((CPFile) newElement);
        } else if (newElement instanceof CPDependency) {
            resource.addDependency((CPDependency) newElement);
        } else {
            throw new OLATRuntimeException(CPOrganizations.class, "you can only add <dependency> or <file> elements to a Resource", new Exception());
        }
        return true;
    } else if (parentElement instanceof CPResources) {
        // parent is <resources> !!see the "s" at the end ;)
        if (newElement instanceof CPResource) {
            CPResources resources = (CPResources) parentElement;
            resources.addResource((CPResource) newElement);
            return true;
        } else {
            throw new OLATRuntimeException(CPOrganizations.class, "you can only add <resource>elements to the <resources> element", new Exception());
        }
    }
    return false;
}
Also used : CPOrganizations(org.olat.ims.cp.objects.CPOrganizations) CPFile(org.olat.ims.cp.objects.CPFile) DefaultElement(org.dom4j.tree.DefaultElement) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPResources(org.olat.ims.cp.objects.CPResources) CPResource(org.olat.ims.cp.objects.CPResource) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPItem(org.olat.ims.cp.objects.CPItem) CPOrganization(org.olat.ims.cp.objects.CPOrganization) CPDependency(org.olat.ims.cp.objects.CPDependency)

Example 5 with CPOrganization

use of org.olat.ims.cp.objects.CPOrganization in project OpenOLAT by OpenOLAT.

the class CPManagerImpl method createNewCP.

/**
 * @see org.olat.ims.cp.CPManager#createNewCP(org.olat.core.id.OLATResourceable)
 */
public ContentPackage createNewCP(OLATResourceable ores, String initalPageTitle) {
    // copy template cp to new repo-location
    if (copyTemplCP(ores)) {
        File cpRoot = FileResourceManager.getInstance().unzipFileResource(ores);
        logDebug("createNewCP: cpRoot=" + cpRoot);
        logDebug("createNewCP: cpRoot.getAbsolutePath()=" + cpRoot.getAbsolutePath());
        LocalFolderImpl vfsWrapper = new LocalFolderImpl(cpRoot);
        ContentPackage cp = load(vfsWrapper, ores);
        // Modify the copy of the template to get a unique identifier
        CPOrganization orga = setUniqueOrgaIdentifier(cp);
        setOrgaTitleToRepoEntryTitle(ores, orga);
        // Also set the translated title of the inital page.
        orga.getItems().get(0).setTitle(initalPageTitle);
        writeToFile(cp);
        // set the default settings for file delivery
        DeliveryOptions defOptions = DeliveryOptions.defaultWithGlossary();
        CPPackageConfig config = new CPPackageConfig();
        config.setDeliveryOptions(defOptions);
        setCPPackageConfig(ores, config);
        return cp;
    } else {
        logError("CP couldn't be created. Error when copying template. Ores: " + ores.getResourceableId(), null);
        throw new OLATRuntimeException("ERROR while creating new empty cp. an error occured while trying to copy template CP", null);
    }
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) CPPackageConfig(org.olat.ims.cp.ui.CPPackageConfig) File(java.io.File) DeliveryOptions(org.olat.core.gui.control.generic.iframe.DeliveryOptions) CPOrganization(org.olat.ims.cp.objects.CPOrganization) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl)

Aggregations

CPOrganization (org.olat.ims.cp.objects.CPOrganization)30 CPItem (org.olat.ims.cp.objects.CPItem)22 DefaultElement (org.dom4j.tree.DefaultElement)20 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)12 CPResource (org.olat.ims.cp.objects.CPResource)8 CPDependency (org.olat.ims.cp.objects.CPDependency)6 CPOrganizations (org.olat.ims.cp.objects.CPOrganizations)6 GenericTreeNode (org.olat.core.gui.components.tree.GenericTreeNode)4 TreeNode (org.olat.core.gui.components.tree.TreeNode)4 CPFile (org.olat.ims.cp.objects.CPFile)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 Vector (java.util.Vector)2 Element (org.dom4j.Element)2 JSONException (org.json.JSONException)2 Test (org.junit.Test)2 DeliveryOptions (org.olat.core.gui.control.generic.iframe.DeliveryOptions)2 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2