0

任何人都可以提供带有where服务类子句的示例 OSLC 代码吗?我最近才开始使用 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()]);
}
4

1 回答 1

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);
     }
 }
于 2013-07-02T06:48:57.480 回答