/*BEGINPROLOGUE****************************************************************
* @copyright(disclaimer) *
* *
* DISCLAIMER OF WARRANTIES. *
* *
* The following IBM Content Manager Enterprise Edition code is sample code *
* created by IBM Corporation. IBM grants you a nonexclusive copyright *
* license to use this sample code example to generate similar function *
* tailored to your own specific needs. This sample code is not part of any *
* standard IBM product and is provided to you solely for the purpose of *
* assisting you in the development of your applications. This example has *
* not been thoroughly tested under all conditions. IBM, therefore cannot *
* guarantee nor may you imply reliability, serviceability, or function of *
* these programs. The code is provided "AS IS", without warranty of any *
* kind. IBM shall not be liable for any damages arising out of your or any *
* other parties use of the sample code, even if IBM has been advised of the *
* possibility of such damages. If you do not agree with these terms, do not *
* use the sample code. *
* *
* Licensed Materials - Property of IBM *
* 5724-B19, 5697-H60 *
* © Copyright IBM Corp. 1994, 2013 All Rights Reserved. *
* *
* US Government Users Restricted Rights - Use, duplication or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
* @endCopyright *
******************************************************************************
Not a sample program. This file is a tool provided as-is.
For reference material, sample code, and official documentation of API
concepts, please refer to the official samples, such as:
- SItemTypeCreationICM
- SItemTypeRetrievalICM
Tool for Item & Component Type Definition Import from XML.
This tool will import an Item Type Definition along with its child
Component Type Definitions described in an XML file that follows the
TXMLItemTypeImportICM.dtd.
Example Item Types File
------------------ -------------------------------------------------
Non-Resource TXMLItemTypeImportICM_Ex_ItemType_NonResource.xml
Resource Lob TXMLItemTypeImportICM_Ex_ItemType_Lob.xml
Resource Text TXMLItemTypeImportICM_Ex_ItemType_Text.xml
Resource Image TXMLItemTypeImportICM_Ex_ItemType_Image.xml
Examlple Usage:
java TXMLItemTypeImportICM -f TXMLItemTypeImportICM_Ex_ItemType_NonResource.xml
*******************************************************************************/
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import com.ibm.mm.sdk.common.*;
import com.ibm.mm.sdk.server.*;
import java.util.Vector;
import java.sql.Date;
import java.io.IOException;
import java.lang.Exception;
import java.lang.*;
/************************************************************************************************
* FILENAME: TXMLItemTypeImportICM.java
* ---------------------------------------------------------------------------
* DESCRIPTION: Tool that imports Item & Component Type Definitions from an XML file that
* follows the TXMLItemTypeImportICM.dtd.
* ---------------------------------------------------------------------------
* DEMONSTRATION: Not a sample program. This file is a tool provided as-is.
* ---------------------------------------------------------------------------
* COMMANDLINE USAGE: java TXMLItemTypeImportICM -f <xml file path> -u <username>
* -d <database> -p <password>
* NOTE: xml file name required.
* NOTE: xml file must follow the TXMLItemTypeImportICM.dtd
* ---------------------------------------------------------------------------
* PREREQUISITES: 1. Classpath must contain file location of xerces.jar.
* 2. Attributes must already be defined. Please refer to TXMLAttrDefImportICM.
* TXMLAttrDefImportICM_Example.xml is required for examples.
* 3. If using Text Searchable Attrs or Item Types, db2text must be started.
* ---------------------------------------------------------------------------
* FILES REQUIRED: SConnectDisconnectICM
* TXMLInternalValidatingParserICM
* TXMLItemTypeImportICM.dtd
************************************************************************************************/
public class TXMLItemTypeImportICM{
// Constants
static final int ADD_ITEMTYPE = 1; // May prevent from adding Item Type by setting to '0'.
static final int LIST_ITEMTYPE = 1; // May prevent from listing Item Type by setting to '0'.
static final short NODE_ELEMENT = 1;
static final short NODE_TEXT = 3;
// Variables
Document _doc;
DKDatastoreICM _ds;
DKDatastoreDefICM _dsDefICM;
String parentName = null;
boolean isDocument = false;
//-------------------------------------------------------------
// Main
//-------------------------------------------------------------
/**
* Run the Tool.
* @param argv[] String Array containing arguments. Optional arguments are -d <databse> -u <userName> -p <password>.
*/
public static void main(String[] argv)throws Exception {
TXMLItemTypeImportICM importXML = null;
TXMLInternalValidatingParserICM vParser = null;
//*** for input parameters
String database = SConnectDisconnectICM.DEFAULT_DATABASE;
String userName = SConnectDisconnectICM.DEFAULT_USERNAME;
String password = SConnectDisconnectICM.DEFAULT_PASSWORD;
String file = "FILE_NOT_SPECIFIED";
int length = argv.length;
String param = null;
//--------------------------------------------------------------
// Checking for input parameters
//--------------------------------------------------------------
if (length < 5) {
System.out.println("Usage: " );
System.out.println(" java TXMLItemTypeImportICM -f <xml file path> -u <username>");
System.out.println(" -d <database> -p <password>");
System.out.println(" NOTE: xml file name required.");
System.out.println(" NOTE: xml file must follow the TXMLItemTypeImportICM.dtd");
System.out.println("");
}
for(int i=0; i<length; i++){
param = argv[i];
if(param.startsWith("-f"))
file = argv[++i];
else if(param.startsWith("-u"))
userName = argv[++i];
else if(param.startsWith("-p"))
password = argv[++i];
else if(param.startsWith("-d"))
database = argv[++i];
}//end for
String ver = SConnectDisconnectICM.VERSION;
System.out.println("===========================================");
System.out.println("IBM DB2 Content Manager v"+ver);
System.out.println("Tool Program: TXMLItemTypeImportICM");
System.out.println("-------------------------------------------");
System.out.println(" Database: "+database);
System.out.println(" UserName: "+userName);
System.out.println(" FileName: "+file);
System.out.println("===========================================\n");
try{
//-------------------------------------------------------------
// Connect to datastore
//-------------------------------------------------------------
// See Sample SConnectDisconnectICM for more information
System.out.println("Connecting to datastore (Database '"+database+"', UserName '"+userName+"')...");
DKDatastoreICM dsICM = new DKDatastoreICM(); // Create new datastore object.
dsICM.connect(database,userName,password,""); // Connect to the datastore.
System.out.println("Connected to datastore (Database '"+dsICM.datastoreName()+"', UserName '"+dsICM.userName()+"').");
//-------------------------------------------------------------
// Run Tool
//-------------------------------------------------------------
System.out.println("Running Tool...");
importXML = new TXMLItemTypeImportICM(dsICM);
vParser = new TXMLInternalValidatingParserICM();
importXML._doc = vParser.open(file); // Opening xml file and validating it
Element root = (Element)importXML._doc.getDocumentElement(); // Get root element
importXML.traverseItemTypes(root); // Start creating itemtypes
System.out.println("Tool Completed.");
//-------------------------------------------------------------
// Disconnect from datastore & Destroy Reference
//-------------------------------------------------------------
System.out.println("Disconnecting from datastore & destroying reference...");
dsICM.disconnect();
dsICM.destroy();
System.out.println("Disconnected from datastore & destroying reference.");
//-------------------------------------------------------------
// Tool program completed without exception
//-------------------------------------------------------------
System.out.println("\n==========================================");
System.out.println("Tool program completed.");
System.out.println("==========================================\n");
}
//------------------------------------------------------------
// Catch & Print Exceptions
//------------------------------------------------------------
catch (DKException exc){
SConnectDisconnectICM.printException(exc); // Print the exception using the function from the connection sample.
throw(exc);
} catch (Exception exc) {
SConnectDisconnectICM.printException(exc); // Print the exception using the function from the connection sample.
throw(exc);
}
}//end main
/**
* Constructor.
* @param dsICM Connected DKDatastoreICM.
**/
TXMLItemTypeImportICM(DKDatastoreICM dsICM) throws DKException, Exception {
_doc = null;
_ds = dsICM;
_dsDefICM = (DKDatastoreDefICM) dsICM.datastoreDef(); // Get the Datastore Definition Object
}
/**
* Traverse Nodes.
* @param node Node to traverse.
**/
private void traverseItemTypes (Node node) throws DKException, Exception{
if(node.getNodeName().equals("ItemTypes"))//doc must be itemtypes
{
int type = node.getNodeType();
NodeList children = ((Element)node).getElementsByTagName("ItemType");//get all Itemtype nodes
if (children != null) {//if children exists continue
for (int i=0; i< children.getLength(); i++) {
traverseRootItem(children.item(i)); //traverse each itemtype
}
}
}
}
/**
* Traverse the Root Item Type Node.
* @param itemType Node for the root (Item Type).
**/
public void traverseRootItem(Node itemtype) throws DKException, Exception{//traverses parent components
DKItemTypeDefICM xmlItemType;
Node child;
int type;
int size;
boolean exists = false;
NamedNodeMap attrs = itemtype.getAttributes();//get attributes of node itemtype
xmlItemType = new DKItemTypeDefICM(_ds);
type = itemtype.getNodeType();
Node n2 = null;
int xdoid;
DKXDOClassificationDefICM xdoClassDef;
if (type == Node.ELEMENT_NODE){//verify that this itemtype node is an element
if ((attrs != null)) {
size = attrs.getLength();
String nodeValue;
Node n ;
for (int j = 0; j < size; j++) {//traverse attributes of itemtype and set them
n = attrs.item(j);
nodeValue = getValue(n);
if(n.getNodeName().equals("name")){
System.out.println("");
System.out.println("Determining If ItemType "+getValue(n)+" Already Exists...");
if(SItemTypeRetrievalICM.doesEntityExist(_ds,getValue(n))){//check if name+suffix itemtype already exists
exists = true;
System.out.println("ItemType Already Exists.");
//break;
}
else{
System.out.println("---------------------------------");
System.out.println("Creating ItemType #"+j+" - "+getValue(n)+"...");
xmlItemType.setName(getValue(n));
parentName = getValue(n);
}
}
else if(n.getNodeName().equals("description"))
{
xmlItemType.setDescription(getValue(n));
}
else if(n.getNodeName().equals("versioning"))
{
String value = getValue(n).toUpperCase();
System.out.println("versioning value = "+value);
if(value.equals("OPTIMIZED")){
xmlItemType.setVersioningType(DKConstantICM.DK_ICM_ITEM_VERSIONING_OPTIMIZED);
}
else if(value.equals("FULL")){
xmlItemType.setVersioningType(DKConstantICM.DK_ICM_ITEM_VERSIONING_FULL);
}
} //setVersionControl(short versionControl)
else if(n.getNodeName().equals("versionControl")){
xmlItemType.setVersionControl((new Short(getValue(n))).shortValue());
}
else if(n.getNodeName().equals("classification"))
{
System.out.println("Classification = "+nodeValue);
if(nodeValue.equals("non-resource")){
xmlItemType.setClassification(DKConstantICM.DK_ICM_ITEMTYPE_CLASS_ITEM);
}
else if(nodeValue.equals("document")){
xmlItemType.setClassification(DKConstantICM.DK_ICM_ITEMTYPE_CLASS_DOC_MODEL);
isDocument = true;
}
else if(nodeValue.equals("resource")){
xmlItemType.setClassification(DKConstantICM.DK_ICM_ITEMTYPE_CLASS_RESOURCE_ITEM);
n2 = attrs.getNamedItem("xdoclassid");//get xdoclassid
xdoid = (new Integer(getValue(n2))).intValue();
xdoClassDef = _dsDefICM.retrieveXdoClassification(xdoid);//get xdoclassificationdef
if(xdoClassDef !=null){
xmlItemType.setXDOClassName(xdoClassDef.getName());//set name of resource type
System.out.println("Resource = "+xdoClassDef.getName());//
xmlItemType.setXDOClassID(xdoid);//set xdoclass id
}
else{
throw new DKUsageError("XDO ID specified is not supported");//if not found in LS
}
}
}
else if(n.getNodeName().equals("defaultCollCode"))
{
xmlItemType.setDefaultCollCode((new Short(getValue(n))).shortValue());
}
else if(n.getNodeName().equals("defaultPrefetchCode"))
{
xmlItemType.setDefaultPrefchCollCode((new Short(getValue(n))).shortValue());
}
else if(n.getNodeName().equals("defaultRMCode"))
{
xmlItemType.setDefaultRMCode((new Short(getValue(n))).shortValue());
}
}
if(!exists){
traverseChildrenNodes(xmlItemType, itemtype);//get itemtype after it has been added
if(isDocument){
addParts(itemtype, xmlItemType);//if document add parts
System.out.println("Adding Item Type...");
//xmlItemType.setAutoLinkEnable(false);
//xmlItemType.setVersionControl((short)DKConstantICM.DK_ICM_VERSION_CONTROL_ALWAYS);
xmlItemType.setVersioningType(DKConstantICM.DK_ICM_ITEM_VERSIONING_FULL);
xmlItemType.add();
System.out.println("Complete.");
isDocument = false;//reset for next itemtype
System.out.println("---------------------------------");
}
}
else{
isDocument = false;
}
}//if(attrs !=null
}
}
/**
* Add Parts to an Item Type.
**/
private void addParts(Node itemtype, DKItemTypeDefICM rootItemType) throws Exception{
NodeList children = ((Element)itemtype).getElementsByTagName("part");//get part nodes
NamedNodeMap attrs;
Node n;
String partName;
DKItemTypeDefICM partIT;
int partID;
DKItemTypeRelationDefICM itemTypeRel;
System.out.println("Creating Parts...");
int size;
String nodeValue;
for (int i=0; i< children.getLength(); i++) {//traverse through each part node
attrs = children.item(i).getAttributes();//get attributes of a part node
if (attrs != null) {
n = attrs.getNamedItem("name");//get the name
partName = getValue(n);
partIT = (DKItemTypeDefICM) _dsDefICM.retrieveEntity(partName.toUpperCase());//retrieve DKItemTypeDefICM part
partID = partIT.getItemTypeId();
itemTypeRel = addItemTypeRelation(partID);//create IT relation
size = attrs.getLength();
for (int j = 0; j < size; j++) {//traverse attributes of itemtype and set them
n = attrs.item(j);
nodeValue = getValue(n);
if(n.getNodeName().equals("defaultCollCode"))
{
itemTypeRel.setDefaultCollCode((new Short(nodeValue)).shortValue());
}
else if(n.getNodeName().equals("defaultPrefetchCode"))
{
itemTypeRel.setDefaultPrefetchCollCode((new Short(nodeValue)).shortValue());
}
else if(n.getNodeName().equals("defaultRMCode"))
{
itemTypeRel.setDefaultRMCode((new Short(nodeValue)).shortValue());
}
}
itemTypeRel.setSourceItemTypeID(rootItemType.getIntId());
rootItemType.addItemTypeRelation(itemTypeRel);//add the IT relation to root component
System.out.println(partName);
}
}
}
/**
* Add an Item Type Relation (Document Model).
**/
public DKItemTypeRelationDefICM addItemTypeRelation(int targetITId) throws DKException, Exception
{
DKItemTypeRelationDefICM itRel = new DKItemTypeRelationDefICM(_ds);//create new one
itRel.setTargetItemTypeID(targetITId);
itRel.setDefaultRMCode((short)0);
itRel.setDefaultACLCode(1);
itRel.setDefaultCollCode((short)0);
itRel.setDefaultPrefetchCollCode((short)0);
itRel.setVersionControl((short) 0);
System.out.print("Successfully created the itemtype relation - ");
return itRel;
}
/**
* Add a Child Component.
* NOTE: For information on ItemTypes & Component Types, please refer to SItemTypeCreationICM
* and SItemTypeRetrievalICM.
**/
public dkEntityDef addChildComponent(Node child) throws DKException, Exception{//traverses parent components
NamedNodeMap attrs = child.getAttributes();
DKComponentTypeDefICM component = new DKComponentTypeDefICM(_ds);//create new component to be child
int type = child.getNodeType();
if (type == Node.ELEMENT_NODE){
if ((attrs != null)) {
int size = attrs.getLength();
Node n;
for (int j = 0; j < size; j++) {//set attributes of child component
n = attrs.item(j);
if(n.getNodeName().equals("name")){
System.out.println("Creating Child component "+getValue(n)+"...");
component.setName(getValue(n));
}
else if(n.getNodeName().equals("CardinalityMin"))
{
component.setCardinalityMin((new Short(getValue(n))).shortValue());
}
else if(n.getNodeName().equals("CardinalityMax"))
{
component.setCardinalityMax((new Short(getValue(n))).shortValue());
}
else if(n.getNodeName().equals("description"))
{
component.setDescription(getValue(n));
}
}
}//if(attrs !=null
dkEntityDef subcomp = addChildComponentChildrenNodes(component, child);
return(subcomp);
}
return null;
}
/**
* Add Child Component Nodes.
* NOTE: For information on ItemTypes & Component Types, please refer to SItemTypeCreationICM
* and SItemTypeRetrievalICM.
**/
public dkEntityDef addChildComponentChildrenNodes(DKComponentTypeDefICM newChild, Node component) throws DKException, Exception{
DKAttrDefICM attr;
DKAttrGroupDefICM attrGroup;
NodeList children = component.getChildNodes();
int type;
String attrname = null;
String attrGroupName = null;
String node_type ;
if ((children != null) ) {
for (int i=0; i< children.getLength(); i++) //traversing attributes of child component
{
attrname = null;
node_type = children.item(i).getNodeName();
type = children.item(i).getNodeType();
if ((type == Node.ELEMENT_NODE)&&(node_type.equals("component"))){//add child component
dkEntityDef subcomp = addChildComponent(children.item(i));
if(subcomp != null){
newChild.addSubEntity(subcomp);
}
}
else if ((type == Node.ELEMENT_NODE)&&(node_type.equals("attribute"))){//add attribute
attr = new DKAttrDefICM(_ds);
NamedNodeMap attrs = children.item(i).getAttributes();
if (attrs != null) {
Node n = attrs.getNamedItem("name");
attrname = getValue(n);
if(getAttr(attrname)!=null){
attr=setAttrInfo(attrs,getAttr(attrname), children.item(i));
}
else{
throw new DKUsageError("Attribute "+attrname+" does not exist");
}
newChild.addAttr(attr);
}//if attr!=NULL
}//if type == element
else if ((type == Node.ELEMENT_NODE)&&(node_type.equals("attrGroup"))){//add attribute group
attrGroup = new DKAttrGroupDefICM(_ds);
NamedNodeMap attrs = children.item(i).getAttributes();
if (attrs != null) {
Node n = attrs.getNamedItem("name");
attrGroupName = getValue(n);
attrGroup = getAttrGroup(attrGroupName);
if(attrGroup==null){
throw new DKUsageError("Attribute Group "+attrGroupName+" does not exist");
}
newChild.addAttrGroup(attrGroup);
}//if attr!=NULL
}//if type == element
}//forloop
}//if children !=NULL
return newChild;
}
/**
* Traverse Children Nodes of an Item Type Node.
**/
public dkEntityDef traverseChildrenNodes(DKComponentTypeDefICM xmlItemType, Node itemtype) throws DKException, Exception{
DKAttrDefICM attr;
DKAttrGroupDefICM attrGroup;
NodeList children = itemtype.getChildNodes();//go through parent components
String attrname;
String attrGroupName;
String node_type;
int type ;
if ((children != null) ) {
for (int i=0; i< children.getLength(); i++) //traversing attributes of itemtype
{
attrname = null;
node_type = children.item(i).getNodeName();
type = children.item(i).getNodeType();
if ((type == Node.ELEMENT_NODE)&&(node_type.equals("component"))){//add child component
dkEntityDef subcomp = addChildComponent(children.item(i));
if(subcomp != null){
xmlItemType.addSubEntity(subcomp);
}
}
else if ((type == Node.ELEMENT_NODE)&&(node_type.equals("attribute"))){//add attribute
attr = new DKAttrDefICM(_ds);
NamedNodeMap attrs = children.item(i).getAttributes();
if (attrs != null) {
Node n = attrs.getNamedItem("name");
attrname = getValue(n);
if(getAttr(attrname)!=null){
attr=setAttrInfo(attrs,getAttr(attrname), children.item(i));
}
else{
throw new DKUsageError("Attribute "+attrname+" does not exist");
}
if(ADD_ITEMTYPE==1){
xmlItemType.addAttr(attr);
}
}//if attr!=NULL
}//if type == element
else if ((type == Node.ELEMENT_NODE)&&(node_type.equals("attrGroup"))){//add attribute group
attrGroup = new DKAttrGroupDefICM(_ds);
NamedNodeMap attrs = children.item(i).getAttributes();
if (attrs != null) {
Node n = attrs.getNamedItem("name");
attrGroupName = getValue(n);
attrGroup = getAttrGroup(attrGroupName);
if(attrGroup==null){
throw new DKUsageError("Attribute Group "+attrGroupName+" does not exist");
}
if(ADD_ITEMTYPE==1){
xmlItemType.addAttrGroup(attrGroup);
}
}//if attr!=NULL
}//if type == element
}//forloop
if((ADD_ITEMTYPE==1) && (!isDocument)){
System.out.println("Adding Item Type...");
xmlItemType.add();
System.out.println("Complete.");
System.out.println("---------------------------------");
}
}//if children !=NULL
return xmlItemType;
}
/**
* Set the Attribute Information for the node.
* NOTE: For information on Adding Attributes to an Item Type or Component Types, please
* refer to SItemTypeCreationICM.
**/
DKAttrDefICM setAttrInfo(NamedNodeMap attrs, DKAttrDefICM attr, Node node)
{
int size = attrs.getLength();
NodeList children = node.getChildNodes();
Node n ;
for (int j = 0; j < size; j++) {//setting attributes of attrs
n = attrs.item(j);
if(n.getNodeName().equals("nullable")) {
if(getValue(n).equals("yes")){
attr.setNullable(true);
}
else{
attr.setNullable(false);
}
}
else if(n.getNodeName().equals("unique")) {
if(getValue(n).equals("yes")){
attr.setUnique(true);
}
else{
attr.setUnique(false);
}
}
}//for loop
if ((children != null) ) {
int childrenType ;
for (int i=0; i< children.getLength(); i++)
{
childrenType = children.item(i).getNodeType();
if (childrenType == NODE_ELEMENT){
attr=setMoreAttrInfo(attr,children.item(i));
break;
}
}
}
return attr;
}
/**
* Set More Attribute Information for the node.
* NOTE: For information on Adding Attributes to an Item Type or Component Types, please
* refer to SItemTypeCreationICM.
**/
DKAttrDefICM setMoreAttrInfo(DKAttrDefICM attr, Node node)
{
NamedNodeMap attrs = node.getAttributes();
int size = attrs.getLength();
Node n ;
for (int j = 0; j < size; j++) {//setting attributes of attrs
n = attrs.item(j);
if(n.getNodeName().equals("searchable"))
{
if(getValue(n).equals("yes"))
{
attr.setTextSearchable(true);
}
else
{
attr.setTextSearchable(false);
}
}
}//for loop
return attr;
}
/**
* Get the Attribute Definition.
* @param name Name of the Attribute.
* NOTE: For information on Obtaining Attribute Definitions by Name, please refer to
* SAttributeDefinitionRetrievalICM and SItemTypeCreationICM.
**/
public DKAttrDefICM getAttr(String name) throws DKException, Exception {
return (DKAttrDefICM)_dsDefICM.retrieveAttr(name);
}
/**
* Get the Attribute Group Definition.
* @param name Name of the Attribute Group.
* NOTE: For information on Obtaining Attribute Group Definitions by Name, please refer to
* SAttributeGroupDefRetrievalICM and SItemTypeCreationICM.
**/
public DKAttrGroupDefICM getAttrGroup(String name) throws DKException, Exception {
return (DKAttrGroupDefICM)_dsDefICM.retrieveAttrGroup(name);
}
/**
* Method to get text from node. Get the first subnode from the
* node and extract nodeValue from it. Text is usually the first child value
* in a node.
**/
private String getValue(Node n) {
String result = null;
if (n.hasChildNodes()) {
Node firstchild = n.getFirstChild();
if (firstchild.getNodeType() == NODE_TEXT)
result = firstchild.getNodeValue();
if (result != null) {
result = result.trim();
if (!result.equals(""))
return result;
}
}
return null;
}
}//end TXMLItemTypeImportICM