use of org.olat.ims.cp.objects.CPItem 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.CPItem 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.CPItem 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.CPItem 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;
}
}
use of org.olat.ims.cp.objects.CPItem in project OpenOLAT by OpenOLAT.
the class CPCore method _findReferencesToRes.
/**
* searches recursively for <item>-elements with identifierRef "id" in the
* children-collection of the item "item"
*
* @param item
* @param id
* @return
*/
private CPItem _findReferencesToRes(CPItem item, String id) {
if (item.getIdentifierRef().equals(id))
return item;
for (Iterator<CPItem> itO = item.getItems().iterator(); itO.hasNext(); ) {
CPItem it = itO.next();
CPItem found = _findReferencesToRes(it, id);
if (found != null)
return found;
}
return null;
}
Aggregations