blob: 6515830a36642e2f719d4cdfa5ce169d52773086 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
package org.eclipse.mdm.businessobjects.boundary;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.ENTITYATTRIBUTE_NAME;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.ENTITYATTRIBUTE_PHYSICALDIMENSION_ID;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.REQUESTPARAM_ID;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.REQUESTPARAM_SOURCENAME;
import static org.eclipse.mdm.businessobjects.service.EntityService.L;
import static org.eclipse.mdm.businessobjects.service.EntityService.V;
import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.mdm.api.base.model.Environment;
import org.eclipse.mdm.api.base.model.PhysicalDimension;
import org.eclipse.mdm.api.base.model.Unit;
import org.eclipse.mdm.businessobjects.entity.I18NResponse;
import org.eclipse.mdm.businessobjects.entity.MDMEntityResponse;
import org.eclipse.mdm.businessobjects.entity.SearchAttribute;
import org.eclipse.mdm.businessobjects.entity.SearchAttributeResponse;
import org.eclipse.mdm.businessobjects.service.EntityService;
import org.eclipse.mdm.businessobjects.utils.RequestBody;
import org.eclipse.mdm.businessobjects.utils.ServiceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
* {@link Unit} resource handling REST requests
*
* @author Alexander Nehmer, science+computing AG Tuebingen (Atos SE)
*
*/
@Tag(name = "Unit")
@Path("/environments/{" + REQUESTPARAM_SOURCENAME + "}/units")
public class UnitResource {
@EJB
private EntityService entityService;
/**
* Returns the found {@link Unit}
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param id id of the {@link Unit}
* @return the found {@link Unit} as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
@Operation(summary = "Find a Unit by ID", description = "Returns Unit based on ID", responses = {
@ApiResponse(description = "The Unit", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response find(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, @PathParam(REQUESTPARAM_ID) String id) {
return entityService.find(V(sourceName), Unit.class, V(id))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get();
}
/**
* Returns the (filtered) {@link Unit}s.
*
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param filter filter string to filter the {@link Unit} result
* @return the (filtered) {@link Unit}s as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Find Units by filter", description = "Get a list of Units matched by an optional filter", responses = {
@ApiResponse(description = "The Unit", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response findAll(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@QueryParam("filter") String filter) {
return entityService.findAll(V(sourceName), Unit.class, filter)
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get();
}
/**
* Returns the created {@link Unit}.
*
*
* @param body The {@link Unit} to create.
* @return the created {@link Unit} as {@link Response}.
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Operation(summary = "Create a new Unit", responses = {
@ApiResponse(description = "The Unit", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response create(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, String body) {
RequestBody requestBody = RequestBody.create(body);
return entityService
.create(V(sourceName), Unit.class,
L(requestBody.getStringValueSupplier(ENTITYATTRIBUTE_NAME),
entityService.find(V(sourceName), PhysicalDimension.class,
requestBody.getStringValueSupplier(ENTITYATTRIBUTE_PHYSICALDIMENSION_ID))))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.CREATED)).get();
}
/**
* Updates the {@link Unit} with all parameters set in the given JSON body of
* the request.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param id the identifier of the {@link Unit} to update.
* @param body the body of the request containing the attributes to update
* @return the updated {@link Unit}
*/
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
@Operation(summary = "Updates the Unit with all parameters set in the body of the request", responses = {
@ApiResponse(description = "The Unit", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response update(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, @PathParam(REQUESTPARAM_ID) String id,
String body) {
RequestBody requestBody = RequestBody.create(body);
return entityService
.update(V(sourceName), entityService.find(V(sourceName), Unit.class, V(id)),
requestBody.getValueMapSupplier())
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get();
}
/**
* Deletes and returns the deleted {@link Unit}. on error.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param id The identifier of the {@link Unit} to delete.
* @return the deleted {@link Unit }s as {@link Response}
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
@Operation(summary = "Delete an existing Unit", responses = {
@ApiResponse(description = "The deleted Unit", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response delete(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String id) {
return entityService.delete(V(sourceName), entityService.find(V(sourceName), Unit.class, V(id)))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get();
}
/**
* Returns the search attributes for the {@link Unit} type.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @return the {@link SearchAttribute}s as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/searchattributes")
@Operation(summary = "Get the search attributes", description = "Get a list of search attributes", responses = {
@ApiResponse(description = "The search attributes", content = @Content(schema = @Schema(implementation = SearchAttributeResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
public Response getSearchAttributes(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName) {
return ServiceUtils.buildSearchAttributesResponse(V(sourceName), Unit.class, entityService);
}
/**
* Returns a map of localization for the entity type and the attributes.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @return the I18N as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/localizations")
@Operation(summary = "Get the Unit localizations", description = "Returns Unit localizations", responses = {
@ApiResponse(description = "The Unit localizations", content = @Content(schema = @Schema(implementation = I18NResponse.class))),
@ApiResponse(responseCode = "500", description = "Error") })
@Deprecated
public Response localize(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName) {
return ServiceUtils.buildLocalizationResponse(V(sourceName), Unit.class, entityService);
}
}