Sie sind auf Seite 1von 8

package com.bluefish.dfc.

test;

import junit.framework.TestCase;

import com.documentum.fc.client.DfClient;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfLoginInfo;
import com.documentum.fc.common.IDfLoginInfo;

public class Dfc5BaseTest extends TestCase {

// TODO: refactor to pull from a properties file


private static final String DOCBASE = "YOUR DOCBASE";
private static final String USERNAME = "YOUR USERNAME";
private static final String PASSWORD = "YOUR PASSWORD";

private IDfSessionManager sessionMgr = null;


protected IDfSession session = null;

protected void setUp() throws Exception {


super.setUp();

IDfClient client = DfClient.getLocalClient();


sessionMgr = client.newSessionManager();

// Setup login details.


IDfLoginInfo login = new DfLoginInfo();
login.setUser(USERNAME);
login.setPassword(PASSWORD);
login.setDomain(null);
sessionMgr.setIdentity(DOCBASE, login);

session = sessionMgr.newSession(DOCBASE);
}

protected void tearDown() throws Exception {


super.tearDown();
if (session != null) {
sessionMgr.release(session);
}
}

protected void log(String message) {


System.out.println(message);
}

And then I tested my login code with the following subclass:

package com.bluefish.dfc.test;

public class LoginTest extends Dfc5BaseTest {


public void testLogin() throws Exception {
// login happens in setUp(), so nothing to do here
assertNotNull("session is null", session);
}

There are a couple of important points regarding the above code samples:

• Be sure always to clean up your IDfSessions when you're finished with them.

• If available, always use the session manager to access and release sessions.
Prior to DFC 5.x, there wasn't an IDfSessionManager and the developer was required to
call IDfSession.disconnect() whenever a session was no longer needed. However, the
IDfSessionManager supports session pooling, so it is critical that any session acquired
through a session manager is released through that session manager as well. Otherwise,
bad things can happen and probably will. This is typical Object/Relational Mapper
design, so those familiar with a similar persistence framework should find the transition
rather painless.

Overview of Documentum Foundation


Classes (DFC)
DFC's object model for managing docbase objects is a deep and complex hierarchy, but
we can get started with the basics by looking at only a small subset of these classes:
*Arrows represent object inheritance levels.
IDfClient
IDfSessionManager
IDfSession
IDfQuery
IDfTypedObject --> IDfCollection
IDfPersistentObject --> IDfSysObject --> IDfFolder
IDfDocum
ent
We've already been introduced to IDfClient, IDfSessionManager, and IDfSession in the
previous section. So what are the remaining classes used for? The DFC Javadoc describes
them as follows:
Class Description
This interface provides functionality to establish and manage sessions
IDfClient with a Documentum server, and provides information about the server
before a session is established.
IDfCollect
This interface provides access to collection objects.
ion
IDfDocum This class provides the functionality for the client to interact with
ent "dm_document" objects in the repository.
This interface provides access to folder-related data stored in folder
IDfFolder
objects.
IDfPersist This interface extends IDfTypedObject and is the base class for all
entObject Documentum persistent objects.
This interface provides functionality for running queries against a
IDfQuery
repository.
IDfSession This interface encapsulates a session with a Documentum repository.
IDfSession
Manages identities, pooled sessions, and transactions.
Manager
IDfSysObj This class provides the functionality for the client to interact with
ect "dm_sysobject" objects in the repository.
IDfTyped
This interface provides basic operations for all typed objects.
Object
We'll get a better understanding once we see them in action, so let's put them to use.

CRUD: Create, Read, Update, and Delete


Finally, it's time to do what we all love: Write code. Let's revisit our chosen exercises:

• Create folder

• Create file

• Link file to folder

• Modify file

• Fetch folder contents

• Query files by attribute (name and author)

• Delete file

• Delete folder
I've created a single test case class, DfcCrudTest.java, with test methods present for each
of our exercises. For some of our exercises, there turned out to be more than one viable
way of accomplishing our goal. For example, to obtain a folder's contents, you can
perform a simple DQL query, or if you have a handle on the IDfFolder object, you can
call the getContents(..) method on the folder object. To demonstrate this, I included both
options within my testFolderContents() method.
Please keep in mind that these tests are written for clarify, not for optimal design.

package com.bluefish.dfc.test;

import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfDocument;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.common.IDfId;

public class DfcCrudTest extends Dfc5BaseTest {

private static String DIR_NAME = "Subdir";


private static String DIR_PATH = "/Temp/" + DIR_NAME;
private static String FILE_NAME = "Getting Started with DFC
and DQL.txt";
private static String FILE_PATH = DIR_PATH + "/" +
FILE_NAME;
private static String DOC_AUTHOR = "Steve McMichael";

private IDfFolder folder;


private IDfDocument document;

public void testSimpleDfc() throws Exception {

initialize();

// tests are order dependent

createFolder();
createFile();
linkFileToFolder();
modifyFile();
fetchFolderContents();
queryFiles();
deleteFile();
deleteFolder();

private void createFolder() throws Exception {


log("** Testing folder creation");

folder = (IDfFolder) session.newObject("dm_folder");


folder.setObjectName(DIR_NAME);
folder.link("/Temp");
folder.save();

log("created folder " + folder.getId("r_object_id"));


assertEquals("unexpected folder path", DIR_PATH,
folder.getFolderPath(0));
}

private void createFile() throws Exception {


log("** Testing file creation");

document = (IDfDocument)
session.newObject("dm_document");
document.setObjectName(FILE_NAME);
document.setContentType("crtext");
document.setFile("E:/clipboard.txt"); // add content to
this dm_document
document.save();

log("created file" + document.getId("r_object_id"));


}

private void linkFileToFolder() throws Exception {


log("** Testing file linking to folder");

document.link(DIR_PATH);
document.save();

log(FILE_PATH);
assertNotNull("unexpected folder path",
session.getObjectByPath( FILE_PATH));
}

private void modifyFile() throws Exception {


log("** Testing file modification");

document.checkout();
int numAuthors = document.getAuthorsCount();
document.setAuthors(numAuthors, DOC_AUTHOR);
//doc.checkin(false, "Prevents promotion to CURRENT");
document.checkin(false, null); // When a null version
label is provided,
// DFC automatically gives
the new version
// an implicit version
label (1.1, 1.2, etc.)
// and the symbolic label
"CURRENT".
}

private void fetchFolderContents() throws Exception {


log("** Testing folder contents");

// (1) Fetch using IDfFolder object

IDfFolder folder = session.getFolderByPath(DIR_PATH);


assertNotNull("folder is null", folder);
IDfCollection collection = null;
IDfDocument doc = null;
int count = 0;
try {
collection = folder.getContents("r_object_id");
while (collection.next()) {
count++;
IDfId id = collection.getId("r_object_id");
doc = (IDfDocument) session.getObject(id);
log(id + ": " + doc.getObjectName());
}
} finally {
// ALWAYS! clean up your collections
if (collection != null) {
collection.close();
}
}

assertEquals("wrong number of files in folder", 1,


count);
assertEquals("unexpected doc name", FILE_NAME,
doc.getObjectName());

// (2) Fetch using DQL folder(..)

String dql = "SELECT r_object_id, object_name from


dm_document where folder('"+DIR_PATH+"');";

// Or we can fetch the contents of our folder and all


of its subfolders using
//
// folder('/Temp/Subdir', descend)
//
// But since we haven't added any subfolders, this will
return the same set of dm_documents.
//
// String dql = "SELECT r_object_id, object_name from
dm_document where folder('"+DIR_PATH+"', descend);";

IDfQuery query = new DfQuery();


query.setDQL(dql);
collection = null;
String docName = null;
count = 0;
try {
collection = query.execute(session,
IDfQuery.DF_READ_QUERY);
while (collection.next()) {
count++;
String id = collection.getString("r_object_id");
docName = collection.getString("object_name");
log(id + ": " + docName);
}
} finally {
// ALWAYS! clean up your collections
if (collection != null) {
collection.close();
}
}
assertEquals("wrong number of files in folder", 1,
count);
assertEquals("unexpected doc name", FILE_NAME, docName);

private void queryFiles() throws Exception {


log("** Testing file query");

// (1) load by path

IDfDocument doc = (IDfDocument)


session.getObjectByPath(FILE_PATH);
assertNotNull("null doc returned", doc);
assertEquals("unexpected doc name", FILE_NAME,
doc.getObjectName());

// (2) load by query

// NOTE: Authors is a "repeating attribute" in


Documentum terminology,
// meaning it is multi-valued. So we need to use the
ANY DQL keyword here.
doc = null;
String dql = "SELECT r_object_id"
+ " FROM dm_document"
+ " WHERE object_name = '" + FILE_NAME + "'"
+ " AND ANY authors = '" + DOC_AUTHOR +
"'";

IDfQuery query = new DfQuery();


query.setDQL(dql);
IDfCollection collection = query.execute(session,
IDfQuery.DF_READ_QUERY);
try {
assertTrue("query did not return any results",
collection.next());
doc = (IDfDocument)
session.getObject(collection.getId("r_object_id"));
} finally {
// ALWAYS! clean up your collections
if (collection != null) {
collection.close();
}
}

assertNotNull("null doc returned", doc);


assertEquals("unexpected doc name", FILE_NAME,
doc.getObjectName());
}

private void deleteFile() throws Exception {


if (document != null) {
log("** Testing file deletion");
document.destroyAllVersions();
}
}

private void deleteFolder() throws Exception {


if (folder != null) {
log("** Testing folder deletion");
folder.destroyAllVersions();
}
}

private void initialize() {


// If something bad happened during the previous run,
this will
// make sure we're back in a good state for this test
run.
try {
session.getObjectByPath(FILE_PATH).destroy();
} catch (Exception e) {
// ignore
}
try {
session.getObjectByPath(DIR_PATH).destroy();
} catch (Exception e) {
// ignore
}
}
}

Das könnte Ihnen auch gefallen