Search in sources :

Example 26 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException 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 27 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException 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 28 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException 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 29 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class CPManagerImpl method load.

/**
 * @see org.olat.ims.cp.CPManager#load(org.olat.core.util.vfs.VFSContainer)
 */
public ContentPackage load(VFSContainer directory, OLATResourceable ores) {
    XMLParser parser = new XMLParser();
    ContentPackage cp;
    VFSLeaf file = (VFSLeaf) directory.resolve("imsmanifest.xml");
    if (file != null) {
        try {
            DefaultDocument doc = (DefaultDocument) parser.parse(file.getInputStream(), false);
            cp = new ContentPackage(doc, directory, ores);
            // identifier.
            if (cp.getLastError() == null) {
                if (cp.isOLATContentPackage() && CPCore.OLAT_ORGANIZATION_IDENTIFIER.equals(cp.getFirstOrganizationInManifest().getIdentifier())) {
                    setUniqueOrgaIdentifier(cp);
                }
            }
        } catch (OLATRuntimeException e) {
            cp = new ContentPackage(null, directory, ores);
            logError("Reading imsmanifest failed. Dir: " + directory.getName() + ". Ores: " + ores.getResourceableId(), e);
            cp.setLastError("Exception reading XML for IMS CP: invalid xml-file ( " + directory.getName() + ")");
        }
    } else {
        cp = new ContentPackage(null, directory, ores);
        cp.setLastError("Exception reading XML for IMS CP: IMS-Manifest not found in " + directory.getName());
        logError("IMS manifiest xml couldn't be found in dir " + directory.getName() + ". Ores: " + ores.getResourceableId(), null);
        throw new OLATRuntimeException(CPManagerImpl.class, "The imsmanifest.xml file was not found.", new IOException());
    }
    return cp;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) DefaultDocument(org.dom4j.tree.DefaultDocument) IOException(java.io.IOException) XMLParser(org.olat.core.util.xml.XMLParser)

Example 30 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException 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

OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)268 IOException (java.io.IOException)104 File (java.io.File)50 ModuleConfiguration (org.olat.modules.ModuleConfiguration)26 ArrayList (java.util.ArrayList)22 AssertException (org.olat.core.logging.AssertException)22 FileOutputStream (java.io.FileOutputStream)20 OutputStream (java.io.OutputStream)20 Properties (java.util.Properties)20 FileInputStream (java.io.FileInputStream)18 HashMap (java.util.HashMap)18 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)18 QTIItemObject (org.olat.ims.qti.export.helper.QTIItemObject)18 DefaultElement (org.dom4j.tree.DefaultElement)16 Element (org.jdom.Element)16 InputStream (java.io.InputStream)14 BufferedInputStream (java.io.BufferedInputStream)12 List (java.util.List)12 Document (org.dom4j.Document)12 CPItem (org.olat.ims.cp.objects.CPItem)12