The following sample Java API
code populates the metadata attributes and adds a content to repository
by using the CMSManager and CMSInstance Java interfaces. Note: The following
two statements should not be called repeatedly in a Java API. CMSManager CMSMgr = ctx.getCMSManager();
CMSInstance CMSInst = CMSMgr.getCMSInstance();
Otherwise, this might
lead to OutOfMemory or an increase in the number
of applications that are connecting to the Content Management System
from collaborative MDM. The following sample code uses these statements
correctly.
Context ctx = null;
try
{
ctx = PIMContextFactory.getContext("Admin", "xxx", "MyCompany");
CMSManager CMSMgr = ctx.getCMSManager();
CMSInstance CMSInst = CMSMgr.getCMSInstance();
String repositoryName = "DB2CM";
String fileToBeAdded = Configuration.getValue("tmp_dir") + "/test.xml";
String displayFileName = "test.xml";
HashMap<String, String> metadataAttributesHM = new HashMap<String, String>();
Map<String, String> metaProps =
CMSInst. getMetaDataAttributes(repositoryName);
Iterator<String> it = metaProps.keySet().iterator();
while (it.hasNext())
{
// meta data attribute
property = (String) it.next();
// meta data attribute value
value = "val1";
metadataAttributesHM.put(property, value);
}
File file1 = new File(fileToBeAdded);
String cmsURN = CMSInst.addContent(repositoryName, fileToBeAdded, displayFileName, metaDataAttribsValuesHM);
String addedContentReference = cmsURN.getURN();
System.out.println(" The content reference of the added file is : \n" + addedContentReference);
}
catch (PIMAuthorizationException ae)
{
// Expected a failure
System.out.println("Authorization Failure");
return;
}
catch(IllegalArgumentException iae)
{
System.out.println(" Passed argument is null or empty ");
return;
}
catch(PIMInternalException ie)
{
System.out.println(" Internal Error ");
return;
}
To provide metadata information programmatically
through Java API, set the item-attribute
values on the Add Content screen. Note: If the selected content management
system is set as your default system during configuration, the metadata
attributes are already set when the Add Content screen displays. For
more information, see content_management_system_properties.xml File.
The
following sample Java API code
checks to see whether a specified URN is a valid one syntactically
and also checks the URN's existence by using the CMSContentURN Java interface. Context ctx = null;
try
{
ctx = PIMContextFactory.getContext("Admin", "xxx", "MyCompany");
CMSManager CMSMgr = ctx.getCMSManager();
CMSInstance CMSInst = CMSMgr.getCMSInstance();
// login to all the repositories through single sign on
CMSInst.logon();
System.out.println("\n\n ------------ Displaying cases for validation of URN ------------ ");
// sample for APIs on validation of URN
String URN1 = "vbr:/firstItemType.A1001001A09B24B35145B46524.A09B24B35145B46524.1022/1/CONTENT"; // invalid URN
String URN2 = "vbr:/DB2PAL/firstItemType.A4B35145B46524.A09B24B35145B46524.1022/1/CONTENT"; // valid but non-existing URN
String URN3 = "vbr:/DB2PAL/firstItemType.A1001001A09B24B35145B46524.A09B24B35145B46524.1022/1/CONTENT"; // valid and existing URN
CMSContentURN urnObj1 = CMSInst.getCMSContentURN(URN1);
System.out.println(" The URN1 " + URN1 + " is valid? : " + urnObj1.isValid());
CMSContentURN urnObj2 = CMSInst.getCMSContentURN(URN2);
System.out.println(" The URN2 " + URN2 + " is valid? : " + urnObj2.isValid());
System.out.println(" The URN2 " + URN2 + " is existing? : " + urnObj2.isExisting());
CMSContentURN urnObj1 = CMSInst.getCMSContentURN(URN1);
System.out.println(" The URN1 " + URN1 + " is valid? : " + urnObj1.isValid());
CMSContentURN urnObj2 = CMSInst.getCMSContentURN(URN2);
System.out.println(" The URN2 " + URN2 + " is valid? : " + urnObj2.isValid());
System.out.println(" The URN2 " + URN2 + " is existing? : " + urnObj2.isExisting());
CMSContentURN urnObj3 = CMSInst.getCMSContentURN(URN3);
System.out.println(" The URN3 " + URN3 + " is valid? : " + urnObj3.isValid());
System.out.println(" The URN3 " + URN3 + " is existing? : " + urnObj3.isExisting());
}
catch (Throwable e)
{
throw new PIMInternalException(e.getLocalizedMessage(), e);
}
The following sample Java API code retrieves an already uploaded
document from its URL through Java API.
Assume that URN is the string URL for the content management system
object that is stored within the content management system. CMSContentURN URNobj = CMSInst.getCMSContentURN(“urn”);
/*content object holds the reference of that object in CMS */
InputStream content = CMSInst.getURNContentAsStream(URNobj);
/* one can use this stream to redirect the contents to a file */
|