use of org.olat.ims.cp.objects.CPItem 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.CPItem 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);
}
}
use of org.olat.ims.cp.objects.CPItem in project OpenOLAT by OpenOLAT.
the class CPTreeController method getParentIdentifier.
/**
* Retrieves the parent identifier of the current page
*
* @return The identifier of the current page's parent
*/
private String getParentIdentifier() {
DefaultElement currentElem = CPManager.getInstance().getElementByIdentifier(cp, currentPage.getIdentifier());
// Get the parent node to be displayed after deletion.
String parentIdentifier = null;
if (currentElem instanceof CPItem) {
Element parent = ((CPItem) currentElem).getParentElement();
if (parent instanceof CPItem) {
CPItem parentItem = (CPItem) parent;
parentIdentifier = parentItem.getIdentifier();
} else if (parent instanceof CPOrganization) {
CPOrganization parentItem = (CPOrganization) parent;
parentIdentifier = parentItem.getIdentifier();
}
}
return parentIdentifier;
}
use of org.olat.ims.cp.objects.CPItem in project OpenOLAT by OpenOLAT.
the class CPTreeDataModel method getChildrenFor.
public List<TreeNode> getChildrenFor(String nodeId) {
List<TreeNode> nodeList = new ArrayList<TreeNode>();
nodeId = getIdentifierForNodeID(nodeId);
DefaultElement el = cp.getElementByIdentifier(nodeId);
if (el == null) {
log.info("element not found (id " + nodeId + ")");
return nodeList;
}
try {
if (el.getName().equals(CPCore.ORGANIZATION)) {
CPOrganization org = (CPOrganization) el;
for (Iterator<CPItem> it = org.getItemIterator(); it.hasNext(); ) {
CPItem item = it.next();
addItem(nodeList, item);
}
} else if (el.getName().equals(CPCore.ITEM)) {
CPItem pItem = (CPItem) el;
for (Iterator<CPItem> it = pItem.getItemIterator(); it.hasNext(); ) {
CPItem item = it.next();
addItem(nodeList, item);
}
} else {
// element is not item nor orga -> ergo wrong element
log.info("unknown element while building treemodel for gui (id: " + nodeId + ")");
}
} catch (JSONException e) {
log.error("error while building treemodel");
}
return nodeList;
}
use of org.olat.ims.cp.objects.CPItem in project OpenOLAT by OpenOLAT.
the class CPManagerTest method testLoad.
@Test
public void testLoad() {
ContentPackage relodedCP = mgr.load(cp.getRootDir(), cp.getResourcable());
assertNotNull(relodedCP);
CPOrganization orga = relodedCP.getFirstOrganizationInManifest();
assertNotNull(orga);
CPItem item = orga.getFirstItem();
assertEquals(PAGE_TITLE, item.getTitle());
}
Aggregations