Skip Headers
Oracle® Content Services Application Developer's Guide
10g Release 1 (10.1.1)

Part Number B14494-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

6 Content Services Document Operations

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:

Creating Folders and Documents

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);
 
    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();
 
    // create document
    Item document = fm.createDocument(
    WsUtility.newNamedValueArray(new Object[][] {
        { Attributes.NAME, docName },
        { Attributes.DESCRIPTION, docDesc },
        { Options.DESTFOLDER, new Long(folder.getId()) } }), null, null);
 
    return document;
}

Copying or Moving a 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
{
    FileManager fm = WsConnection.getFileManager();
 
    // 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);
 
}

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
{
    FileManager fm = WsConnection.getFileManager();
 
    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);
 
}

Deleting Documents and Folders

Deleting documents or 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. 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
{
    CommonManager cm = WsConnection.getCommonManager();
    FileManager fm = WsConnection.getFileManager();
    TrashManager tm = WsConnection.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());
}

Running the Code

To run the code, connect to a 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
    {
        try
        {
            // URL to content services web services servlet
            String serverUrl = "http://yourserver.com:7777/content/ws";
 
            // authenticate to content services
            s_WsCon = WsConnection.login(serverUrl, "jon", "welcome1");
 
            // use jon's Workspace for this example
            FileManager fm = s_WsCon.getFileManager();
            Item workspace = fm.resolvePath("/oracle/users/users-J/jon", 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);
        }
        finally
        {
            s_WsCon.logout();
        }
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
}