Search in sources :

Example 11 with CPResource

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

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 12 with CPResource

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

the class CPCore method findReferencesToResource.

/**
 * Searches for <item>-elements or <dependency>-elements which references to
 * the resource with id "resourceIdentifier"
 *
 * if an element is found, search is aborted and the found element is returned
 *
 * @param resourceIdentifier
 * @return the found element or null
 */
public DefaultElement findReferencesToResource(String resourceIdentifier) {
    // search for <item identifierref="resourceIdentifier" >
    for (Iterator<CPOrganization> it = rootNode.getOrganizations().getOrganizationIterator(); it.hasNext(); ) {
        CPOrganization org = it.next();
        for (Iterator<CPItem> itO = org.getItems().iterator(); itO.hasNext(); ) {
            CPItem item = itO.next();
            CPItem found = _findReferencesToRes(item, resourceIdentifier);
            if (found != null)
                return found;
        }
    }
    // search for <dependency identifierref="resourceIdentifier" >
    for (Iterator<CPResource> itRes = rootNode.getResources().getResourceIterator(); itRes.hasNext(); ) {
        CPResource res = itRes.next();
        for (Iterator<CPDependency> itDep = res.getDependencyIterator(); itDep.hasNext(); ) {
            CPDependency dep = itDep.next();
            if (dep.getIdentifierRef().equals(resourceIdentifier))
                return dep;
        }
    }
    return null;
}
Also used : CPResource(org.olat.ims.cp.objects.CPResource) CPOrganization(org.olat.ims.cp.objects.CPOrganization) CPItem(org.olat.ims.cp.objects.CPItem) CPDependency(org.olat.ims.cp.objects.CPDependency)

Example 13 with CPResource

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

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 14 with CPResource

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

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 15 with CPResource

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

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