获取所需的模块并使用 OSCL + JAVA 在其中添加工件

Get desired Module and add artifacts in it using OSCL + JAVA

我是 OSLC + JAVA 框架的新手。目前我正在尝试在特定模块中添加一些工件。

所以我需要获取该模块并在其中添加新工件。

作为 POC 的一部分,目前我可以通过将文件夹路径设置为父文件夹来在某个文件夹中添加基础工件。

是否有任何资源可用于在特定模块中添加工件?

如果我们得到一些示例代码,那将非常有帮助。

在此处为 OLSC 用户发布答案:

package csvImport.poc;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.namespace.QName;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.wink.client.ClientResponse;
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.RequirementCollection;
import org.eclipse.lyo.client.oslc.resources.RmUtil;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;

import net.oauth.OAuthException;

public class OSLCHelper {

    String user;
    String pwd;
    String projectArea;
    String serverUrl;

    String catalogUrl;
    String serviceProviderUrl;
    String queryCapability;
    String requirementFactory;

    private static JazzRootServicesHelper rootServicesHelper = null;
    private static JazzFormAuthClient client = null;

    public boolean logIn() throws Exception {
        if (client.login() == HttpStatus.SC_OK) {
            return true;
        }
        return false;
    }

    public OSLCHelper(String user, String pwd, String projectArea, String serverUrl) {
        this.user = user;
        this.pwd = pwd;
        this.projectArea = projectArea;
        this.serverUrl = serverUrl;
        try {
            rootServicesHelper = new JazzRootServicesHelper(serverUrl, OSLCConstants.OSLC_RM_V2);
        } catch (RootServicesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        client = rootServicesHelper.initFormClient(user, pwd);
    }

    public void updateURLs() throws Exception {
        // STEP 4: Get the URL of the OSLC ChangeManagement catalog
        catalogUrl = rootServicesHelper.getCatalogUrl();

        // STEP 5: Find the OSLC Service Prsovider for the project area we want to work
        // with
        serviceProviderUrl = client.lookupServiceProviderUrl(catalogUrl, projectArea);

        // STEP 6: Get the Query Capabilities URL so that we can run some OSLC queries
        queryCapability = client.lookupQueryCapability(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
                OSLCConstants.RM_REQUIREMENT_TYPE);
        // STEP 7: Create base requirements
        // Get the Creation Factory URL for change requests so that we can create one

        requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
                OSLCConstants.RM_REQUIREMENT_TYPE);
    }

    public String[] getAllRequirementURLS() {
        OslcQueryParameters queryParams = new OslcQueryParameters();
        queryParams.setPrefix("rdf=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
        queryParams.setWhere("rdf:type=<http://open-services.net/ns/rm#Requirement>");
        OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
        OslcQueryResult result = query.submit();
        for (String iterable_element : result.getMembersUrls()) {
            System.out.println(iterable_element);
        }
        return result.getMembersUrls();
    }
    public String[] getAllRequirementCollectionsURLS() {
        OslcQueryParameters queryParams = new OslcQueryParameters();
        queryParams.setPrefix("rdf=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>");
        queryParams.setWhere("rdf:type=<http://open-services.net/ns/rm#RequirementCollection>");
        OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
        OslcQueryResult result = query.submit();
        for (String iterable_element : result.getMembersUrls()) {
            System.out.println(iterable_element);
        }
        return result.getMembersUrls();
    }

    public String getRequirementById(String id) {
        OslcQueryParameters queryParams = new OslcQueryParameters();
        String url = "";
        queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
        queryParams.setWhere("dcterms:identifier="+id);
        OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
        OslcQueryResult result = query.submit();
        String[] membersUrls = result.getMembersUrls();
        if (membersUrls.length>0) {
            url = membersUrls[0];
        }
        return url ;
    }

    public String createRequirement(String requirementType, String title, String description) throws Exception {
        // Get Feature Requirement Type URL
        
        serviceProviderUrl = client.lookupServiceProviderUrl(catalogUrl, projectArea);

        // STEP 6: Get the Query Capabilities URL so that we can run some OSLC queries
        queryCapability = client.lookupQueryCapability(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
                OSLCConstants.RM_REQUIREMENT_TYPE);
        // STEP 7: Create base requirements
        // Get the Creation Factory URL for change requests so that we can create one

        requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
                OSLCConstants.RM_REQUIREMENT_TYPE);
        
        ResourceShape featureInstanceShape = RmUtil.lookupRequirementsInstanceShapes(serviceProviderUrl,
                OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE, client, requirementType);
        Requirement requirement = new Requirement();
        requirement.setInstanceShape(featureInstanceShape.getAbout());
        requirement.setTitle(title);
        requirement.setDescription(description);
        // Create the Requirement
        ClientResponse creationResponse = client.createResource(requirementFactory, requirement,
                OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML);
        String req04URL = creationResponse.getHeaders().getFirst(HttpHeaders.LOCATION);
        creationResponse.consumeContent();
        return req04URL;
    }

    public boolean createRequirementCollection(String requirementType, String title, String description)
            throws Exception {
        // Get Feature Requirement Type URL
        boolean isAdded = true;

        ResourceShape collectionInstanceShape = RmUtil.lookupRequirementsInstanceShapes(serviceProviderUrl,
                OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_COLLECTION_TYPE, client,
                "Diagnostics Specification");
        RequirementCollection collection = new RequirementCollection();
//      collection.addUses(new URI(req04URL));
        collection.setInstanceShape(collectionInstanceShape.getAbout());
        collection.setTitle("All DTCs");
        collection.setDescription("Created By EclipseLyo");
        // Create the collection
        ClientResponse creationResponse = client.createResource(requirementFactory, collection,
                OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML);
        creationResponse.consumeContent();

        String reqcoll01URL = creationResponse.getHeaders().getFirst(HttpHeaders.LOCATION);
        creationResponse.consumeContent();
        if (reqcoll01URL != null) {
            isAdded = false;
        }
        return isAdded;
    }
    
    public Requirement updateRequirement(Map<String,Object> dataMap , String url) throws Exception{
        boolean isUpdated=false;
        ClientResponse getResponse = client.getResource(url,OslcMediaType.APPLICATION_RDF_XML);
        Requirement requirement = getResponse.getEntity(Requirement.class);
        // Get the eTAG, we need it to update
        String etag = getResponse.getHeaders().getFirst(OSLCConstants.ETAG);
        getResponse.consumeContent();
        Map<QName, Object> extendedProperties = requirement.getExtendedProperties();
        Map<QName, Object> removeCurrentProperty = new HashMap<QName, Object>(extendedProperties); 
        for (Entry<String, Object> entry : dataMap.entrySet()) {
            String qName = entry.getKey();
            Object value = entry.getValue();
            removeCurrentProperty = removeCurrentProperty(removeCurrentProperty,qName);
            removeCurrentProperty.put(new QName(qName), value);
            requirement.setExtendedProperties(removeCurrentProperty);
        }
        // Update the requirement with the proper etag
        ClientResponse updateResponse = client.updateResource(url,
                requirement, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML, etag);
        updateResponse.consumeContent();
        if (updateResponse.getStatusCode()==200) {
            isUpdated=true;
        }
        
        return requirement;
    }
    
    private Map<QName, Object> removeCurrentProperty(Map<QName, Object> extendedProperties, String qName) {
        String comapre = qName.substring(qName.lastIndexOf("/") + 1).trim();
        Map<QName, Object> returnMap= new HashMap<QName, Object>(extendedProperties);
        for (Entry<QName, Object> entry : extendedProperties.entrySet()) {
            QName key = entry.getKey();
            if (key.getLocalPart().equalsIgnoreCase(comapre)) {
                returnMap.remove(key);
            }
            
        }
        return returnMap;
    }

    public Requirement getRequirementByUrl(String url) throws Exception {
        ClientResponse getResponse = null;
        Requirement requirement =null;
        if (url!=null) {
            getResponse = client.getResource(url,OslcMediaType.APPLICATION_RDF_XML);
            requirement = getResponse.getEntity(Requirement.class);
        }
        return requirement;
    }
    
    public RequirementCollection getRequirementCollectionByUrl(String url) throws Exception {
        ClientResponse getResponse = null;
        RequirementCollection requirement =null;
        if (url!=null) {
            getResponse = client.getResource(url,OslcMediaType.APPLICATION_RDF_XML);
            requirement = getResponse.getEntity(RequirementCollection.class);
        }
        return requirement;
    }
    


    public String getRequirementCollectionByTitle(String title) {
        OslcQueryParameters queryParams = new OslcQueryParameters();
        String url = "";
        queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
        queryParams.setWhere("dcterms:title=\""+title+"\"");
        OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
        OslcQueryResult result = query.submit();
        String[] membersUrls = result.getMembersUrls();
        if (membersUrls.length>0) {
            url = membersUrls[0];
        }
        return url ;
    }
    private String getRequirementCollectionByIdentifier(String id) {
        OslcQueryParameters queryParams = new OslcQueryParameters();
        String url = "";
        queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
        queryParams.setWhere("dcterms:identifier="+id);
        OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
        OslcQueryResult result = query.submit();
        String[] membersUrls = result.getMembersUrls();
        if (membersUrls.length>0) {
            url = membersUrls[0];
        }
        return url ;
    }

    public void updateSpecificRequirementCollectionWithRequiremtns(List<Requirement> requiremtns, String title) throws Exception {
        String url = getRequirementCollectionByTitle(title);
        ClientResponse getResponse = null;
        RequirementCollection requirement =null;
        if (url!=null) {
                getResponse = client.getResource(url,OslcMediaType.APPLICATION_RDF_XML);
            String etag = getResponse.getHeaders().getFirst(OSLCConstants.ETAG);
            requirement = getResponse.getEntity(RequirementCollection.class);
            if (requirement!=null) {
                for (Requirement requirement2 : requiremtns) {
                    requirement.addUses(new  URI(requirement2.getAbout().toString()));
                    break;
                }
            }
            ClientResponse updateResponse = client.updateResource(requirement.getAbout().toString(),
                    requirement, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML, etag);
            System.out.println("");
        }
    }
    
     

}