Search in sources :

Example 1 with CPResource

use of org.olat.ims.cp.objects.CPResource 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 2 with CPResource

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

the class CPCore method getPageByItemID.

/**
 * Gets the linked page for the <item> element with given id if no resource
 * (page) is referenced, null is returned
 *
 * @param id
 * @return
 */
public String getPageByItemID(String id) {
    DefaultElement ele = getElementByIdentifier(id);
    if (ele instanceof CPItem) {
        CPItem item = (CPItem) ele;
        if (item.getIdentifierRef() == null || item.getIdentifierRef().equals("")) {
            return null;
        }
        DefaultElement resElement = getElementByIdentifier(item.getIdentifierRef());
        if (resElement instanceof CPResource) {
            CPResource res = (CPResource) resElement;
            return res.getFullHref();
        } else {
            Logger log = Logger.getLogger(this.getClass().getName());
            log.info("method: getPageByItemID(" + id + ") :  invalid manifest.. identifierred of <item> must point to a <resource>-element");
            return null;
        }
    } else {
        return null;
    }
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) CPResource(org.olat.ims.cp.objects.CPResource) Logger(java.util.logging.Logger) CPItem(org.olat.ims.cp.objects.CPItem)

Example 3 with CPResource

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

the class CPCore method cloneResourceOfItem.

/**
 * Clones the resource of an item. If the resource is not editable, i.e. it is
 * not an html, Word or Excel file, there's no need to clone it and nothing
 * will be done. Editable resources are cloned and the single referenced file
 * is copied.
 *
 * @param item
 */
private void cloneResourceOfItem(CPItem item) {
    DefaultElement ref = getElementByIdentifier(item.getIdentifierRef());
    if (ref != null && ref instanceof CPResource) {
        CPResource resource = (CPResource) ref;
        // Clone the resource if the linked file is editable (i.e. it is an html,
        // Word or Excel file)
        String href = resource.getFullHref();
        if (href != null) {
            String extension = href.substring(href.lastIndexOf(".") + 1);
            if ("htm".equals(extension) || "html".equals(extension) || "doc".equals(extension) || "xls".equals(extension)) {
                CPResource clonedResource = (CPResource) resource.clone();
                addElement(clonedResource);
                item.setIdentifierRef(clonedResource.getIdentifier());
            }
        }
    }
}
Also used : DefaultElement(org.dom4j.tree.DefaultElement) CPResource(org.olat.ims.cp.objects.CPResource)

Example 4 with CPResource

use of org.olat.ims.cp.objects.CPResource 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 CPResource

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

the class ContentPackage method updatePage.

protected void updatePage(CPPage page) {
    DefaultElement ele = cpcore.getElementByIdentifier(page.getIdentifier());
    if (ele instanceof CPItem) {
        CPItem item = (CPItem) ele;
        item.setTitle(page.getTitle());
        item.setMetadata(page.getMetadata());
        String itemIdentifierRef = item.getIdentifierRef();
        if (itemIdentifierRef == null || itemIdentifierRef.equals("")) {
            // This item has no linked resource yet. Add one if there is a page file
            // attached.
            VFSLeaf pageFile = page.getPageFile();
            if (pageFile != null) {
                CPResource res = new CPResource();
                CPFile file = new CPFile(pageFile);
                res.addFile(file);
                // TODO:GW Set type according to file
                res.setType("text/html");
                res.setHref(file.getHref());
                item.setIdentifierRef(res.getIdentifier());
                cpcore.getRootNode().getResources().addResource(res);
            }
        } else {
        // this item has already a linked resource
        // this is not supported, we don't change linked resources...
        }
    } else if (ele instanceof CPOrganization) {
        CPOrganization organization = (CPOrganization) ele;
        organization.setTitle(page.getTitle());
    } else {
        // ERROR: this shouldn't be
        throw new OLATRuntimeException("Error while updating manifest with new Page-Data. Invalid identifier " + page.getIdentifier(), null);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) CPFile(org.olat.ims.cp.objects.CPFile) 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)

Aggregations

CPResource (org.olat.ims.cp.objects.CPResource)16 DefaultElement (org.dom4j.tree.DefaultElement)14 CPItem (org.olat.ims.cp.objects.CPItem)10 CPOrganization (org.olat.ims.cp.objects.CPOrganization)8 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)6 CPManager (org.olat.ims.cp.CPManager)4 CPDependency (org.olat.ims.cp.objects.CPDependency)4 CPFile (org.olat.ims.cp.objects.CPFile)4 Logger (java.util.logging.Logger)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2 CPMetadata (org.olat.ims.cp.objects.CPMetadata)2 CPOrganizations (org.olat.ims.cp.objects.CPOrganizations)2 CPResources (org.olat.ims.cp.objects.CPResources)2