| Oracle® Content Services Application Developer's Guide 10g Release 1 (10.1.2.2) Part Number B25277-02 |
|
|
View PDF |
Oracle Content Services provides a set of Managers for document creation, deletion, and manipulation. Documents are created within workspaces, and can be copied, moved, deleted, or moved to the trash.
For these examples, you will need:
FileManager - for creating files and folders
TrashManager - for deleting documents
CommonManager - for retrieving common Oracle Content Services items, namely the Trash
To create a folder, you will need a folder name, a description, and the ID of a parent folder. Pass the ID to the FileManager, along with the folder definition, and call the createFolder() method.
/**
* Creates a folder in the specified destination folder.
*/
public static Item createFolder(String folderName, String folderDesc,
Item parent) throws RemoteException, FdkException
{
FileManager fm = WsConnection.getFileManager();
// create folder definition
NamedValue[] folderDef =
WsUtility.newNamedValueArray(new Object[][] {
{ Attributes.NAME, folderName },
{ Attributes.DESCRIPTION, folderDesc } });
Item folder = fm.createFolder(parent.getId(), folderDef, null);
// log the created folder
WsUtility.log(WsUtility.INDENT, folder);
return folder;
}
To create a document, the process is nearly identical, except you should call the createDocument() method. Also, instead of passing the parent folder ID directly to the method, you should include it as the value for the Options.DESTFOLDER constant in the document definition.
/**
* Creates a document.
*/
public static Item createDocument(String docName, String docDesc, Item folder)
throws RemoteException, FdkException
{
FileManager fm = WsConnection.getFileManager();
// document attribute request
AttributeRequest[] doc_attr = new AttributeRequest[] {
WsUtility.newAttributeRequest(Attributes.PATH),
WsUtility.newAttributeRequest(Attributes.DESCRIPTION),
WsUtility.newAttributeRequest(Attributes.VERSIONS) };
// create document
Item document = fm.createDocument(
WsUtility.newNamedValueArray(new Object[][] {
{ Attributes.NAME, docName },
{ Attributes.DESCRIPTION, docDesc },
{ Options.DESTFOLDER, new Long(folder.getId()) } }), null, doc_attr);
WsUtility.log("document attributes");
WsUtility.log(WsUtility.INDENT, document);
return document;
}
To copy a document to another folder, create a "copy definition" by putting the DESTFOLDER option and its value into a NamedValueArray. Then, use the FileManager's copy() method.
/**
* Copies a Document to a destination folder.
*/
public static void copyDocument(Item document, Item destFolder)
throws RemoteException, FdkException
{
// get the Manager instances
FileManager fm = s_WsCon.getFileManager();
CommonManager cm = s_WsCon.getCommonManager();
// copy document
NamedValue[] copyDef = WsUtility.newNamedValueArray(new Object[][] { {
Options.DESTFOLDER, new Long(destFolder.getId()) } });
NamedValueSet[] copyDefs = new NamedValueSet[] {
WsUtility.newNamedValueSet(copyDef) };
Item[] doc = fm.copy(new long[] { document.getId() }, null, copyDefs);
// document attribute request
AttributeRequest[] doc_attr = new AttributeRequest[] {
WsUtility.newAttributeRequest(Attributes.PATH) };
WsUtility.log("copy document attributes");
WsUtility.log(WsUtility.INDENT, cm.getItem(doc[0].getId(), doc_attr));
}
To move a document the process is identical, except you will need to use the move() method.
/**
* Moves a Document to a destination folder.
*/
public static void moveDocument(Item document, Item destFolder)
throws RemoteException, FdkException
{
// get the Manager instances
FileManager fm = s_WsCon.getFileManager();
CommonManager cm = s_WsCon.getCommonManager();
NamedValue[] moveDef = WsUtility.newNamedValueArray(new Object[][] { {
Options.DESTFOLDER, new Long(destFolder.getId()) } });
NamedValueSet[] moveDefs = new NamedValueSet[] {
WsUtility.newNamedValueSet(moveDef) };
Item[] doc = fm.move(new long[] { document.getId() }, null, moveDefs);
// document attribute request
AttributeRequest[] doc_attr = new AttributeRequest[] {
WsUtility.newAttributeRequest(Attributes.PATH) };
WsUtility.log("move document attributes");
WsUtility.log(WsUtility.INDENT, cm.getItem(doc[0].getId(), doc_attr));
}
Deleting documents and folders involves moving them to the trash, and then emptying the trash. The trash is simply a folder with a special designation as the TRASH_FOLDER, so moving a document or folder only requires the use of the FileManager. The trash belongs to the Library containing the folder or document. In order to empty the trash and thus delete the folders or documents permanently, you will need to use the TrashManager. To empty the trash, call the emptyTrash() method, and pass in the ID of the trash folder.
This example also uses the CommonManager, which allows you to retrieve any kind of item, provided the logged-in user has permission, using the getItem() method. Using the CommonManager, you can retrieve the Item corresponding to the document to delete. The getItem() call is also combined with an AttributeRequest to retrieve the Item corresponding to the trash folder.
/**
* Deletes a Document and empties the Trash.
*/
public static void deleteDocument(Item document)
throws RemoteException, FdkException
{
// get the Manager instances
CommonManager cm = s_WsCon.getCommonManager();
FileManager fm = s_WsCon.getFileManager();
TrashManager tm = s_WsCon.getTrashManager();
// TRASH_FOLDER AttributeRequest array
AttributeRequest[] trash_attr =
WsUtility.newAttributeRequestArray(Attributes.TRASH_FOLDER);
// get TRASH_FOLDER attribute
document = cm.getItem(document.getId(), trash_attr);
NamedValue[] attrs = document.getRequestedAttributes();
Map attrMap = ClientUtils.namedValuesToMap(attrs);
Item trashItem = (Item) attrMap.get(Attributes.TRASH_FOLDER);
// delete the Document and empty the Trash
fm.delete(new long[] { document.getId() }, null, null);
tm.emptyTrash(trashItem.getId());
}
To run the code, connect to a Oracle Content Services instance and call the document and folder methods. Remember to log out of the Web Services instance using the logout() method of the WsConnection class.
private static WsConnection s_WsCon;
public static void main(String[] args)
{
try
{
// get property object
Properties prop = WsUtility.getProperty(args[0]);
// URL to content services web services servlet
String serverUrl = "http://" + prop.getProperty("hostname") + ":"
+ prop.getProperty("port") + "/content/ws";
// authenticate to content services
s_WsCon = WsConnection.login(serverUrl, prop.getProperty("user"),
prop.getProperty("password"));
try
{
// log-in to user's personal workspace
FileManager fm = s_WsCon.getFileManager();
Item workspace = fm.resolvePath(prop.getProperty("userhome"), null);
// create folders
Item folder1 =
createFolder("firstFolder", "This is the 1st folder", workspace);
Item folder2 =
createFolder("secondFolder", "This is the 2nd folder", workspace);
// create document
Item document =
createDocument("myDoc", "This is an example doc", folder1);
// move document to second folder
moveDocument(document, folder2);
// copy document to first folder
copyDocument(document, folder1);
// delete document
deleteDocument(document);
// delete objects
cleanup(folder1, folder2);
}
finally
{
s_WsCon.logout();
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
/**
* Delete created objects
*/
public static void cleanup(Item folder, Item folder1)
throws FdkException, RemoteException
{
// get the Manager instances
CommonManager cm = s_WsCon.getCommonManager();
FileManager fm = s_WsCon.getFileManager();
TrashManager tm = s_WsCon.getTrashManager();
// TRASH_FOLDER AttributeRequest array
AttributeRequest[] trash_attr =
WsUtility.newAttributeRequestArray(Attributes.TRASH_FOLDER);
// get TRASH_FOLDER attribute for folder
folder = cm.getItem(folder.getId(), trash_attr);
NamedValue[] attrs = folder.getRequestedAttributes();
Map attrMap = ClientUtils.namedValuesToMap(attrs);
Item trashItem = (Item) attrMap.get(Attributes.TRASH_FOLDER);
// delete the folder and empty the Trash
fm.delete(new long[] { folder.getId() }, null, null);
tm.emptyTrash(trashItem.getId());
// get TRASH_FOLDER attribute for folder1
folder1 = cm.getItem(folder1.getId(), trash_attr);
attrs = folder.getRequestedAttributes();
attrMap = ClientUtils.namedValuesToMap(attrs);
trashItem = (Item) attrMap.get(Attributes.TRASH_FOLDER);
// delete the folder and empty the Trash
fm.delete(new long[] { folder1.getId() }, null, null);
tm.emptyTrash(trashItem.getId());
}