Title: Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at . http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. # Write Scenario ### How To Guide for building a Sample OData service with the OData Library (Java) This How To Guide shows how to create, update and delete an entry via your Data Provider (`ODataSingleProcessor`). ### Prerequisites This tutorial is based on the [Read Scenario](/doc/odata2/tutorials/basicread.html) - OData Library (Java) tutorial. ### Implementing create, update and delete entry at the single processor ##### Create entry - You already created the `MyODataSingleProcessor` in the basic tutorial - Implement `MyODataSingleProcessor.createEntity(PostUriInfo uriInfo, InputStream content, String requestContentType, String contentType) throws ODataException` by overriding the corresponding method of the `ODataSingleProcessor` **Sample Code** @Override public ODataResponse createEntity(PostUriInfo uriInfo, InputStream content, String requestContentType, String contentType) throws ODataException { //No support for creating and linking a new entry if (uriInfo.getNavigationSegments().size() > 0) { throw new ODataNotImplementedException(); } //No support for media resources if (uriInfo.getStartEntitySet().getEntityType().hasStream()) { throw new ODataNotImplementedException(); } EntityProviderReadProperties properties = EntityProviderReadProperties.init().mergeSemantic(false).build(); ODataEntry entry = EntityProvider.readEntry(requestContentType, uriInfo.getStartEntitySet(), content, properties); //if something goes wrong in deserialization this is managed via the ExceptionMapper //no need for an application to do exception handling here an convert the exceptions in HTTP exceptions Map data = entry.getProperties(); //now one can use the data to create the entry in the backend ... //retrieve the key value after creation, if the key is generated by the server //update the data accordingly data.put("Id", Integer.valueOf(887788675)); //serialize the entry, Location header is set by OData Library return EntityProvider.writeEntry(contentType, uriInfo.getStartEntitySet(), entry.getProperties(), EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build()); } ##### Update entry - You already created the `MyODataSingleProcessor` in the basic tutorial - Implement `MyODataSingleProcessor.updateEntity(PutMergePatchUriInfo uriInfo, InputStream content, String requestContentType, boolean merge, String contentType) throws ODataException` by overriding the corresponding method of the `ODataSingleProcessor` **Sample Code** @Override public ODataResponse updateEntity(PutMergePatchUriInfo uriInfo, InputStream content, String requestContentType, boolean merge, String contentType) throws ODataException { EntityProviderReadProperties properties = EntityProviderReadProperties.init().mergeSemantic(false).build(); ODataEntry entry = EntityProvider.readEntry(requestContentType, uriInfo.getTargetEntitySet(), content, properties); //if something goes wrong in deserialization this is managed via the ExceptionMapper, //no need for an application to do exception handling here an convert the exceptions in HTTP exceptions Map data = entry.getProperties(); if ("Cars".equals(uriInfo.getTargetEntitySet().getName())) { int key = getKeyValue(uriInfo.getKeyPredicates().get(0)); //if there is no entry with this key available, one should return "404 Not Found" //return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build(); //now one can use the data to create the entry in the backend ... String model = (String) data.get("Model"); //... } else if ("Manufacturers".equals(uriInfo.getTargetEntitySet().getName())) { int key = getKeyValue(uriInfo.getKeyPredicates().get(0)); //now one can use the data to create the entry in the backend ... } //we can return Status Code 204 No Content because the URI Parsing already guarantees that //a) only valid URIs are dispatched (also checked against the metadata) //b) 404 Not Found is already returned above, when the entry does not exist return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build(); } ##### Delete entry - You already created the `MyODataSingleProcessor` in the basic tutorial - Implement `MyODataSingleProcessor.deleteEntity(DeleteUriInfo uriInfo, String contentType) throws ODataException` by overriding the corresponding method of the `ODataSingleProcessor` **Sample Code** @Override public ODataResponse deleteEntity(DeleteUriInfo uriInfo, String contentType) throws ODataException { if ("Cars".equals(uriInfo.getTargetEntitySet().getName())) { int key = getKeyValue(uriInfo.getKeyPredicates().get(0)); //if there is no entry with this key available, one should return "404 Not Found" //return ODataResponse.status(HttpStatusCodes.NOT_FOUND).build(); //now one can delete the entry with this particular key in the backend... } else if ("Manufacturers".equals(uriInfo.getTargetEntitySet().getName())) { int key = getKeyValue(uriInfo.getKeyPredicates().get(0)); //now one can delete the entry with this particular key in the backend... } //we can return Status Code 204 No Content because the URI Parsing already guarantees that //a) only valid URIs are dispatched (also checked against the metadata) //b) 404 Not Found is already returned above, when the entry does not exist return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build(); } ### Test your Service After the implementation of the MyODataSingleProcessor the web application can be tested. - Build your project `mvn clean install` - In Eclipse, run the Web Application via Run As -> Run on Server - Create a new Car - Update an existing Car - Delete a Car - Create a new Manufacturer (this is a Media Link Entry), we expect the response 501 Not Implemented - Update a Manufacturer (this is a Media Link Entry)