package edu.indiana.dde.catalog.catalogtester;
import edu.indiana.dde.metadata.catalog.types.*;
import edu.indiana.dde.catalog.catalogclient.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
//Classes from the XML Bean for the LEAD Metadata Schema
import edu.indiana.extreme.lead.metadata.*;
/**
* This class can be used to test adding metadata for files to the XMC Cat metadata catalog
* that includes metadata for the application used to generate the file. Currently this includes
* the application name and version, with the version also broken into its major version,
* minor version and revision components. The code for adding the application metadata to a
* LEADresource document is in the static addApplication method below.
* This code can be lifted out and used in other code - but also include the static constants at
* the top of the class. The imports for the Matcher and Pattern also
* need to be included.
*
* @author Scott Jensen scjensen@cs.indiana.edu
*
*/
public class TestAddApplicationMetadata extends TesterBase {
// The APP_VERSION_PATTERN is used to parse out the components of the version number.
// This pattern has the following components: (note: separator is a dot, dash or underscore)
// 1 = starting with a numeric value of any length (the major version) up to a separator
// The rest is optional
// 2 = the minor version and revision
// 3 = the minor version, which is a separator followed by a numeric value of any length until possibly a
// separator or alpha is encountered.
// 4 = numeric portion of the minor version.
// 5 = revision without the optional suffix. A separator followed by a numeric value of any length.
// 6 = numeric portion of the revision.
// 7 = a suffix starting with a non-numeric character
// Following are the groups for this pattern:
// 1 1 2 3 4 4 3 5 6 6 5 2 7 7
// "^(\d+?) ( ([\.\-\_]{1} (\d+?) ) ([\.\-\_]{1} (\d+?) )? )? (\D+.*)? $"
protected static final Pattern APP_VERSION_PATTERN = Pattern.compile("^(\\d+?)(([\\.\\-\\_]{1}(\\d+?))([\\.\\-\\_]{1}(\\d+?))?)?(\\D+.*)?$");
protected static final int VER_GROUP_MAJOR = 1; //numeric portion of the major version
protected static final int VER_GROUP_MINOR = 4; //numeric portion of the minor version (if there is one)
protected static final int VER_GROUP_REVISION = 6; //numeric portion of the revision (if there is one)
protected static final String APP_SOURCE = "LEAD"; //same for all of the properties and elements in application metadata
protected static final String APP_PROPERTY_NAME = "application"; //name of the application property
protected static final String VERSION_SUBPROPERTY_NAME = "applicationVersion"; //the sub-property within application
protected static final String APP_NAME_ELEMENT = "applicationName"; //name of the metadata element for the application name
protected static final String VERSION_STR_ELEMENT = "version"; //name of the element containing the full version string
protected static final String VERSION_MAJOR_ELEMENT = "majorVersion"; //name of the integer element for the major version
protected static final String VERSION_MINOR_ELEMENT = "minorVersion"; //name of the integer element for the minor version
protected static final String VERSION_REV_ELEMENT = "revision"; //name of the integer element for the revision
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int option = 0;
String guid = null;
String hostName = "localhost";
String portNum = "8080";
String myDn = null;
String delivery = "DIRECT";
String fileName = null;
String parentGuid = null;
String appName = null;
String appVersion = null;
for (int j = 0; j < args.length; j++) {
String argVal = args[j];
if ( argVal.compareToIgnoreCase("--help") == 0 || argVal.compareToIgnoreCase("-help") == 0) {
usage();
return;
} else if (argVal.startsWith("-")) {
if (argVal.compareToIgnoreCase("-g") == 0)
option = 2; //guid
else if (argVal.compareToIgnoreCase("-dn") == 0)
option = 3; //distinguished name
else if (argVal.compareToIgnoreCase("-h") == 0)
option = 4; //host name
else if (argVal.compareToIgnoreCase("-p") == 0)
option = 5; //port
else if (argVal.compareToIgnoreCase("-d") == 0)
option = 6; //error delivery method
else if (argVal.compareToIgnoreCase("-an") == 0)
option = 7; //application name
else if (argVal.compareToIgnoreCase("-av") == 0)
option = 8; //application version e.g., 5.2.9-Beta or 5.0
else if (argVal.compareToIgnoreCase("-f") == 0)
option = 9;//file name
else if (argVal.compareToIgnoreCase("-pg") == 0)
option = 10; //parent guid
else //invalid
option = 0;
} else if (option > 0) {
if (option == 2)
guid = argVal;
else if (option == 3)
myDn = argVal;
else if (option == 4)
hostName = argVal;
else if (option == 5)
portNum = argVal;
else if (option == 6) {
String priorMethod = delivery;
if (argVal.compareToIgnoreCase("STREAMING") == 0)
delivery = "STREAMING";
else if(argVal.compareToIgnoreCase("DIRECT") == 0)
delivery = "DIRECT";
else {
delivery = priorMethod;
String msg = "The delivery method specified (" + argVal +
") is not valid. Restored prior setting: " + priorMethod;
System.out.println(msg);
}
}
else if (option == 7)
appName = argVal;
else if (option == 8)
appVersion = argVal;
else if (option == 9)
fileName = argVal;
else if (option == 10)
parentGuid = argVal;
option = 0; //reset
} //end of option setting
} //loop through parameters
if (fileName == null || appName == null || appVersion == null || myDn == null || guid == null || parentGuid == null) {
usage();
return;
}
CatalogServiceStub stub = getStub(hostName, portNum, myDn);
CreateObjectRequestDocument doc = CreateObjectRequestDocument.Factory.newInstance();
CreateObjectRequestDocument.CreateObjectRequest request = doc.addNewCreateObjectRequest();
request.setErrorDeliveryMethod(CatalogDeliveryType.Enum.forString("STREAMING") );
//Open the file and load the LEADresource document
File dataFile = new File(fileName);
LEADresourceDocument myLeadDoc = null;
try {
myLeadDoc = LEADresourceDocument.Factory.parse(dataFile);
} catch (Exception e) {
System.out.println("An error occurred in parsing the bean: " + e);
return;
}
//put in the replacement GUID
myLeadDoc.getLEADresource().setResourceID(guid);
// **** Add The Application Metadata ****
addApplication(myLeadDoc, appName, appVersion);
CatalogDataType myData = request.addNewCatalogData();
myData.setCatalogType(CatalogAggregationType.FILE);
myData.setParentId(parentGuid);
myData.setLEADresource(myLeadDoc.getLEADresource() );
System.out.println(doc.toString() );
//invoke service
CreateObjectResponseDocument myResponse = stub.createObject(doc);
System.out.println(myResponse.getCreateObjectResponse().getCatalogOperationStatus().getStatus().toString() );
System.out.println("\nResponse Document:\n******************\n" + myResponse.toString() );
stub.cleanup();
return;
} //end of main
/**
* This method takes an XML Bean instance of the root document node for a LMS document
* and adds application metadata to that bean so it can be added as metadata to the
* XMC Cat metadata catalog. If either the name or version parameters are null, then
* only the other value will be added as application metadata. If both are null, then
* no metadata is added.
*
* @param myLeadDoc LEADresourceDocument which is the root document class for a
* metadata instance conforming to the LEAD Metadata Schema (LMS).
* @param appName String with the name of the application - if null it will not be added.
* @param appVersion String with the version of the application - possibly including a major
* version, minor version (optional), revision (also optional) and possibly
* a suffix.
* For example: 5.0.29-beta
* The separators (a dot here) can be a dot, underscore, or dash. The suffix can
* be anything as long as it starts with a non-numeric value, such as 5-0_29*beta007
* would extract the same major version, minor version, and revision.
*/
private static void addApplication(LEADresourceDocument myLeadDoc, String appName, String appVersion) {
String majorVersion = null;
String minorVersion = null;
String revision = null;
// Parse out the version components
if (appVersion != null) {
Matcher verMatcher = APP_VERSION_PATTERN.matcher(appVersion);
if (verMatcher.matches() ) {
majorVersion = verMatcher.group(VER_GROUP_MAJOR);
minorVersion = verMatcher.group(VER_GROUP_MINOR); //null returned if there is no minor version
revision = verMatcher.group(VER_GROUP_REVISION); //null returned if there is no revision
}
} else if (appName == null) {
// both the name and version of the application
// are null, so there is no metadata to add.
return;
}
// If here, there must be some application metadata to add. We need to
// add a detailed element for the application property.
DataType myData = myLeadDoc.getLEADresource().getData();
// Determine if there is an eainfo element or we need to add one
EainfoType myEainfo = null;
if (myData.isSetEainfo() )
myEainfo = myData.getEainfo();
else
myEainfo = myData.addNewEainfo();
// Add the application property as a detailed element
DetailedType appProperty = myEainfo.addNewDetailed();
EnttypType appPropertyEntity = appProperty.addNewEnttyp(); // the property name and source are stored in the enttyp element
appPropertyEntity.setEnttypl(APP_PROPERTY_NAME);
appPropertyEntity.setEnttypds(APP_SOURCE);
// if the application name is not null, add a metadata element for it (attr element in LMS)
if (appName != null) {
AttrType appNameElement = appProperty.addNewAttr();
appNameElement.setAttrlabl(APP_NAME_ELEMENT);
appNameElement.setAttrdefs(APP_SOURCE);
appNameElement.addAttrv(appName);
} //end of adding application name
if (appVersion != null) {
AttrType versionSubproperty = appProperty.addNewAttr();
versionSubproperty.setAttrlabl(VERSION_SUBPROPERTY_NAME);
versionSubproperty.setAttrdefs(APP_SOURCE);
// add the full version as a string
AttrType fullVersion = versionSubproperty.addNewAttr();
fullVersion.setAttrlabl(VERSION_STR_ELEMENT);
fullVersion.setAttrdefs(APP_SOURCE);
fullVersion.addAttrv(appVersion);
// major version
if (majorVersion != null) {
AttrType majorVer = versionSubproperty.addNewAttr();
majorVer.setAttrlabl(VERSION_MAJOR_ELEMENT);
majorVer.setAttrdefs(APP_SOURCE);
majorVer.addAttrv(majorVersion);
}
//minor version
if (minorVersion != null) {
AttrType minorVer = versionSubproperty.addNewAttr();
minorVer.setAttrlabl(VERSION_MINOR_ELEMENT);
minorVer.setAttrdefs(APP_SOURCE);
minorVer.addAttrv(minorVersion);
}
//revision
if (revision != null) {
AttrType rev = versionSubproperty.addNewAttr();
rev.setAttrlabl(VERSION_REV_ELEMENT);
rev.setAttrdefs(APP_SOURCE);
rev.addAttrv(revision);
}
} //end of adding version
return;
} //end of addApplication
private static void usage() {
System.out.println("Usage: TestAddApplicationMetadata -f -g -pg -dn -an -av -d -h -p \n" +
"Defaults:\n" +
"delivery = DIRECT" +
"host = localhost\n" +
"port = 8080\n" +
"delivery method options for errors:\n" +
"DIRECT - included in response\n" +
"STREAMING - response includes URL for retrieving errors\n" +
"The file pointed to by the -f parameter should contain a LEADresource document");
return;
} //end of usage
} //end of class TestAddApplicationMetadata