Hi,
I created some methods commonly useful in DNG context:
package DNG;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.wink.client.ClientResponse;
import org.eclipse.lyo.client.exception.JazzAuthErrorException;
import org.eclipse.lyo.client.exception.JazzAuthFailedException;
import org.eclipse.lyo.client.exception.ResourceNotFoundException;
import org.eclipse.lyo.client.exception.RootServicesException;
import org.eclipse.lyo.client.oslc.OSLCConstants;
import org.eclipse.lyo.client.oslc.jazz.JazzFormAuthClient;
import org.eclipse.lyo.client.oslc.jazz.JazzRootServicesHelper;
import org.eclipse.lyo.client.oslc.resources.OslcQuery;
import org.eclipse.lyo.client.oslc.resources.OslcQueryParameters;
import org.eclipse.lyo.client.oslc.resources.OslcQueryResult;
import org.eclipse.lyo.client.oslc.resources.Requirement;
import org.eclipse.lyo.client.oslc.resources.RmConstants;
import org.eclipse.lyo.client.oslc.resources.RmUtil;
import org.eclipse.lyo.oslc4j.core.model.Link;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import org.w3c.dom.Element;
import net.oauth.OAuthException;
public final class DNG {
static String queryCapability;
static String serviceProviderUrl;
private static JazzRootServicesHelper rootServicesHelper = null;
private static JazzFormAuthClient client = null;
private static String catalogUrl = null;
static Map<String, String> map = new HashMap<String, String>();
//static DNG dngInstance = null;
private DNG(){
throw new RuntimeException();
}
/
Login to DNG
@param username
@param password
@param serverUrl
@param projectArea
@return
*/
public static JazzFormAuthClient loginToDNG(String username, String password, String serverUrl, String projectArea) {
try{
rootServicesHelper = new JazzRootServicesHelper(serverUrl, OSLCConstants.OSLC_RM_V2);
client = rootServicesHelper.initFormClient(username, password);
client.formLogin();
serviceProviderUrl = client.lookupServiceProviderUrl(rootServicesHelper.getCatalogUrl(), projectArea);
queryCapability = client.lookupQueryCapability(serviceProviderUrl,OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
catalogUrl= rootServicesHelper.getCatalogUrl();
if(client.formLogin()==200){
return client;
}
else
throw new RuntimeException("Login Failed");
}
catch(RootServicesException e){
System.out.println(e.getMessage());
}
catch(JazzAuthFailedException e){
System.out.println(e.getMessage());
}
catch(JazzAuthErrorException e){
System.out.println(e.getMessage());
}
catch(ResourceNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
catch(OAuthException e){
System.out.println(e.getMessage());
}
catch(URISyntaxException e){
System.out.println(e.getMessage());
}
return client;
}
/
Prints xml response
@param username
@param password
@param serverUrl
@param projectArea
@param artifactId
@return
/
public static String getDngArtifactXML(String username, String password, String serverUrl, String projectArea, String artifactId) {
String xml=null;
try{
loginToDNG(username, password, serverUrl, projectArea);
String queryCapabilityUrl= client.lookupQueryCapability(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
String requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
OslcQueryParameters queryParams = new OslcQueryParameters();
queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
queryParams.setWhere("dcterms:identifier="+artifactId);
queryParams.setSelect("dcterms:title");
OslcQuery query = new OslcQuery(client, queryCapabilityUrl, 10, queryParams);
OslcQueryResult result = query.submit();
String[] urls= result.getMembersUrls();
String resourceUri=null;
ClientResponse response=null;
for(String s:urls)
{
resourceUri=(String)s;
response = client.getResource(resourceUri , OslcMediaType.APPLICATION_RDF_XML);
xml=response.getEntity(String.class);
return xml;
}
}
catch(ResourceNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
catch(OAuthException e){
System.out.println(e.getMessage());
}
catch(URISyntaxException e){
System.out.println(e.getMessage());
}
return xml;
}
/
Create DNG Artifact
@param username
@param password
@param serverUrl
@param projectArea
@param artifactType
@param artifactDescription
@param artifactTitle
@return
/
public static boolean createArtifact(String username, String password, String serverUrl, String projectArea,String artifactType, String artifactDescription, String artifactTitle) {
try{
loginToDNG(username, password, serverUrl, projectArea);
String requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
Requirement requirement = new Requirement();
ResourceShape featureInstanceShape = RmUtil.lookupRequirementsInstanceShapes( serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE, client, artifactType);
requirement .setInstanceShape(featureInstanceShape.getAbout());
requirement.setDescription(artifactDescription);
requirement.setTitle(artifactTitle);
ClientResponse response= client.createResource(requirementFactory, requirement, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML);
if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
return true;
}
}
catch(ResourceNotFoundException e){
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
catch(OAuthException e){
System.out.println(e.getMessage());
}
catch(URISyntaxException e){
System.out.println(e.getMessage());
}
return false;
}
/
DNG to RTC Linking
@param username
@param password
@param serverUrl
@param projectArea
@param excelFilePath
@return
/
public static boolean dngToRTCLinking(String username, String password, String serverUrl, String projectArea, String excelFilePath){
Map<String, String> mapOut;
boolean result = false;
try {
mapOut = readExcelInputFIle(excelFilePath);
Set<Map.Entry<String, String>> entrySet = mapOut.entrySet();
Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
while(iterator.hasNext()){
Map.Entry<String, String> keyEntrySet = iterator.next();
String dngId = keyEntrySet.getKey();
String rtcUrl = keyEntrySet.getValue();
result = createRtcLink(username, password, serverUrl, projectArea, dngId, rtcUrl);
return result;
}
}
catch (IOException e) {
e.printStackTrace();
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (RootServicesException e) {
e.printStackTrace();
} catch (JazzAuthFailedException e) {
e.printStackTrace();
} catch (JazzAuthErrorException e) {
e.printStackTrace();
} catch (OAuthException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return result;
}
/
Create Links to RTC
@param username
@param password
@param serverUrl
@param projectArea
@param dngId
@param rtcUrl
@return
@throws IOException
@throws OAuthException
@throws URISyntaxException
@throws ResourceNotFoundException
@throws ParserConfigurationException
@throws RootServicesException
@throws JazzAuthFailedException
@throws JazzAuthErrorException
*/
public static boolean createRtcLink(String username, String password, String serverUrl, String projectArea, String dngId, String rtcUrl) throws IOException, OAuthException, URISyntaxException, ResourceNotFoundException, ParserConfigurationException, RootServicesException, JazzAuthFailedException, JazzAuthErrorException {
loginToDNG(username, password, serverUrl, projectArea);
OslcQueryParameters queryParams = new OslcQueryParameters();
queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
queryParams.setWhere("dcterms:identifier="+dngId);
queryParams.setSelect("dcterms:title");
OslcQuery query = new OslcQuery(client, queryCapability, queryParams);
OslcQueryResult result = query.submit();
String resultsUrl[] = result.getMembersUrls();
Link[] link = {new Link(new URI(rtcUrl))};
for(int i=0; i <resultsUrl.length; i++) {
ClientResponse response = client.getResource(resultsUrl[i], OSLCConstants.CT_RDF);
Requirement requirement = response.getEntity(Requirement.class);
requirement.setImplementedBy(link);
String primaryText = "Test Primary Text";
Element obj = RmUtil.convertStringToHTML((String)primaryText);
requirement.getExtendedProperties().put(RmConstants.PROPERTY_PRIMARY_TEXT, obj);
String etag = response.getHeaders().getFirst(OSLCConstants.ETAG);
ClientResponse updateResponse= client.updateResource(resultsUrl[i], (Object)requirement, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML, etag);
if (updateResponse.getStatusCode() == 200 || updateResponse.getStatusCode() == 201) {
return true;
}
else {
return false;
}
}
return false;
}
/
Read Input Excel
@param excelFilePath
@return
@throws IOException
/
public static Map<String, String> readExcelInputFIle(String excelFilePath) throws IOException{
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
String testCaseIdVar;
String requirementIdVar;
DataFormatter df = new DataFormatter();
int c=0;
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Cell cellKey = nextRow.getCell(0);
Cell cellValue = nextRow.getCell(1);
String dngId = df.formatCellValue(cellKey);
String doorsUrl = df.formatCellValue(cellValue);
System.out.println("cellKey "+ dngId);
System.out.println("cellKey "+ doorsUrl);
if(c == 0){
c++;
}
else if (c != 0){
map.put(dngId, doorsUrl);
}
}
return map;
}
/
Main method
@param args
*/
public static void main(String[] args) {
System.out.println("Import the Jar in your Project to use it. Thanks ");
}
}