2013-06-28 58 views
0

任何人都可以提供示例OSLC代碼,其中包含服務類的where子句? 我剛剛開始使用OSLC。使用「where」子句的OSLC代碼示例


這裏是我試過的代碼(開不工作):我只是想我會發布的代碼工作片

@GET 
@Produces({OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_XML, OslcMediaType.APPLICATION_JSON}) 
public Project[] getChangeRequests(@QueryParam("oslc.where") final String where, 
        @QueryParam("oslc.prefix") final String prefix) 
{  
    final List< Project> results = new ArrayList<Project>();   
    Map<String, String> prefixMap; 
    try 
    {  
     QueryUtils.parseSearchTerms(where); 
     prefixMap = QueryUtils.parsePrefixes(prefix); 
     WhereClause whereClause = QueryUtils.parseWhere(where, prefixMap); 

    } 
    catch (ParseException e) 
    { 
     e.printStackTrace(); 
    } 

    final Project[] changeRequests = Persistence.getChangeRequestsForProject(); 
    for (final Project changeRequest : changeRequests) 
    { 
     changeRequest.setServiceProvider(ServiceProviderSingleton.getServiceProviderURI()); 
     results.add(changeRequest); 
    } 
    return results.toArray(new Project[results.size()]); 
} 
+0

請問有人可以在downvoting時對問題的改進發表評論嗎? – eeerahul

回答

2

。這裏是:

prefixMap = QueryUtils.parsePrefixes(prefix); 
WhereClause whereClause = QueryUtils.parseWhere(where, prefixMap); 

PName property = null; 
String value =""; 
for (SimpleTerm term : whereClause.children()) 
{ 
    ComparisonTerm comparison = (ComparisonTerm)term; 
    String operator; 

    switch (comparison.operator()) 
    { 
     case EQUALS: 
      operator = "equals"; 
      break; 
     case NOT_EQUALS: 
      operator = "notequals"; 
      break; 
     case LESS_THAN: 
      operator = "lessthan"; 
      break; 
     case LESS_EQUALS: 
      operator = "lessthaneq"; 
      break; 
     case GREATER_THAN: 
      operator = "greaterthan"; 
      break; 
     default: 
     case GREATER_EQUALS: 
      operator = "greaterhaneq"; 
      break; 
    }   
    property = comparison.property();  
    Value operand = comparison.operand(); 
    value = operand.toString(); 
    switch (operand.type()) 
    { 
     case STRING: 
     case URI_REF: 
      value = value.substring(1, value.length() - 1); 
      break; 
     case BOOLEAN: 
     case DECIMAL: 
      break; 
     default: 
     throw new WebApplicationException 
     (new UnsupportedOperationException("Unsupported oslc.where comparison operand: " + value),Status.BAD_REQUEST);     
    } 
} 
String compareString=property.toString().substring(property.toString().indexOf(":")+1); 
String val="get" + compareString.substring(0,1).toUpperCase() +compareString.substring(1); 
final Project[] changeRequests = Persistence.getChangeRequestsForProject(); 
for (final Project changeRequest : changeRequests) 
{ 
    changeRequest.setServiceProvider(ServiceProviderSingleton.getServiceProviderURI()); 
    Method m=changeRequest.getClass().getMethod(val, null); 
    if((m.invoke(changeRequest).toString().equalsIgnoreCase(value))) 
    { 
     results.add(changeRequest); 
    } 
}