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;
}
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());
}
}
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;
}
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;
}
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);
}
}
Aggregations