Code: Select all
Company.svc/Departaments(x)/Employees(x)/BusinessTrips
Im Beispiel werden diese beiden (insbesondere EntitySet) als Parameter in der Methode benötigt, die die zugehörige Entitätssammlung abruft aus dem Speicher.
In meinem Beispiel gibt es
Code: Select all
Company.svc/UriResourceEntitySet/UriResourceNavigation/UriResourceNavigation
Ich habe keine Ahnung, wie das geht. Sollte ich die Methode ändern oder irgendwie erzwingen, dass das vorletzte Segment EntitySet ist?.
Vielen Dank für Ihr Interesse und ich warte auf Antworten.
Code: Select all
@Override
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
throws ODataApplicationException, ODataLibraryException {
String lastUri = "lastUriResource";
String sourceNavigationUri = "sourceUriResource";
EdmEntitySet responseEntitySet = null;
EntityCollection responseEntityCollection = null;
List resourcePaths = uriInfo.getUriResourceParts();
UriResource uriResource = resourcePaths.get(0);
if (!(uriResource instanceof UriResourceEntitySet)) {
throw new ODataApplicationException("Only EntitySet is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) uriResource;
EdmEntitySet startEntitySet = uriResourceEntitySet.getEntitySet();
if (resourcePaths.size() == 1) {
responseEntitySet = startEntitySet;
responseEntityCollection = storage.readEntitySetData(responseEntitySet);
} else {
startEntitySet = Util.getNavigationTargetEntitySet(uriInfo);
HashMap uriResourceHashMap = Util.getLastNavigationAndItsSource(uriInfo);
UriResource lastUriResource = uriResourceHashMap.get(lastUri);
UriResource sourceUriResource = uriResourceHashMap.get(sourceNavigationUri);
EdmNavigationProperty edmNavigationProperty = null;
if (!(lastUriResource instanceof UriResourceNavigation)) {
throw new ODataApplicationException("Only navigation is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
edmNavigationProperty = ((UriResourceNavigation) lastUriResource).getProperty();
if (!(sourceUriResource instanceof UriResourceEntitySet)) {
throw new ODataApplicationException("Only Entity Set is supported", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
}
uriResourceEntitySet = (UriResourceEntitySet) sourceUriResource;
startEntitySet = uriResourceEntitySet.getEntitySet();
responseEntitySet = Util.getNavigationTargetEntitySet(uriInfo);
EdmEntityType targetEntityType = edmNavigationProperty.getType();
List keyParameters = uriResourceEntitySet.getKeyPredicates();
Entity sourceEntity = storage.readEntityData(startEntitySet, keyParameters);
if (sourceEntity == null) {
throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ROOT);
}
responseEntityCollection = storage.getRelatedEntityCollection(sourceEntity, targetEntityType);
}
ContextURL contextUrl = ContextURL.with().entitySet(responseEntitySet).build();
final String id = request.getRawBaseUri() + "/" + responseEntitySet.getName();
EntityCollectionSerializerOptions options = EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
EdmEntityType edmEntityType = responseEntitySet.getEntityType();
ODataSerializer serializer = odata.createSerializer(responseFormat);
SerializerResult serializerResult = serializer.entityCollection(serviceMetaData, edmEntityType, responseEntityCollection, options);
InputStream inputStream = serializerResult.getContent();
response.setContent(inputStream);
response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
}