| /******************************************************************************** |
| * 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.REQUESTPARAM_ID; |
| import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.REQUESTPARAM_SOURCENAME; |
| import static org.eclipse.mdm.businessobjects.service.EntityService.V; |
| |
| import java.util.Map; |
| |
| 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.adapter.Attribute; |
| import org.eclipse.mdm.api.base.adapter.EntityType; |
| import org.eclipse.mdm.api.base.model.Environment; |
| import org.eclipse.mdm.api.dflt.model.Pool; |
| import org.eclipse.mdm.api.dflt.model.Project; |
| import org.eclipse.mdm.api.dflt.model.ValueList; |
| import org.eclipse.mdm.businessobjects.entity.I18NResponse; |
| import org.eclipse.mdm.businessobjects.entity.MDMEntityResponse; |
| 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.Parameter; |
| 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; |
| import io.vavr.collection.List; |
| import io.vavr.control.Try; |
| |
| /** |
| * {@link Pool} resource |
| * |
| * @author Matthias Koller, Peak Solution GmbH |
| * |
| */ |
| @Tag(name = "Pool") |
| @Path("/environments/{" + REQUESTPARAM_SOURCENAME + "}/pools") |
| public class PoolResource { |
| |
| @EJB |
| private PoolService poolService; |
| |
| @EJB |
| private EntityService entityService; |
| |
| /** |
| * delegates the request to the {@link PoolService} |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @param filter filter string to filter the {@link Pool} result |
| * @return the result of the delegated request as {@link Response} |
| */ |
| |
| @GET |
| @Operation(summary = "Find Pools by filter", description = "Get list of pools", responses = { |
| @ApiResponse(description = "The pools", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "400", description = "Error") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| public Response getPools(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, |
| @Parameter(description = "Filter expression", required = false) @QueryParam("filter") String filter) { |
| |
| return Try.of(() -> poolService.getPools(sourceName, filter)).map(List::ofAll) |
| .map(e -> ServiceUtils.buildEntityResponse(e, Status.OK, Pool.class)).get(); |
| } |
| |
| /** |
| * delegates the request to the {@link PoolService} |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @return the result of the delegated request as {@link Response} |
| */ |
| @GET |
| @Operation(summary = "Get the search attributes", description = "Get list of search attributes", responses = { |
| @ApiResponse(description = "The search attributes", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "400", description = "Error") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Path("/searchattributes") |
| public Response getSearchAttributes( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName) { |
| |
| return Try.of(() -> poolService.getSearchAttributes(sourceName)) |
| .map(attrs -> ServiceUtils.toResponse(new SearchAttributeResponse(attrs), Status.OK)).get(); |
| } |
| |
| /** |
| * delegates the request to the {@link PoolService} |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @param id id of the {@link Pool} |
| * @return the result of the delegated request as {@link Response} |
| */ |
| @GET |
| @Operation(summary = "Find a Pool by ID", description = "Returns Pool based on ID", responses = { |
| @ApiResponse(description = "The project", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "400", description = "Invalid ID supplied") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Path("/{" + REQUESTPARAM_ID + "}") |
| public Response findPool( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName, |
| @Parameter(description = "ID of the Pool", required = true) @PathParam(REQUESTPARAM_ID) String id) { |
| return entityService.find(V(sourceName), Pool.class, V(id)) |
| .map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get(); |
| } |
| |
| /** |
| * delegates the request to the {@link PoolService} |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @return the result of the delegated request as {@link Response} |
| */ |
| @GET |
| @Operation(summary = "Get the Pool localizations", description = "Returns Pool localizations", responses = { |
| @ApiResponse(description = "The project localizations", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "500", description = "Error") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Path("/localizations") |
| @Deprecated |
| public Response localize( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName) { |
| |
| Map<Attribute, String> localizedAttributeMap = this.poolService.localizeAttributes(sourceName); |
| Map<EntityType, String> localizedEntityTypeMap = this.poolService.localizeType(sourceName); |
| return ServiceUtils.toResponse(new I18NResponse(localizedEntityTypeMap, localizedAttributeMap), Status.OK); |
| |
| } |
| |
| /** |
| * Returns the created {@link Pool}. |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @param body The {@link Pool} to create. |
| * @return the created {@link Pool} as {@link Response}. |
| */ |
| @POST |
| @Operation(summary = "Create a new Pool", responses = { |
| @ApiResponse(description = "The created Pool", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "500", description = "Error") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Consumes(MediaType.APPLICATION_JSON) |
| public Response create( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName, |
| String body) { |
| |
| return entityService |
| .create(V(sourceName), Pool.class, |
| entityService.extractRequestBody(body, sourceName, io.vavr.collection.List.of(Project.class))) |
| .map(e -> ServiceUtils.buildEntityResponse(e, Status.CREATED)).get(); |
| } |
| |
| /** |
| * Updates the {@link Pool} 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 PoolValue} to update. |
| * @param body the body of the request containing the attributes to update |
| * @return the updated {@link Pool} |
| */ |
| @PUT |
| @Operation(summary = "Update an existing Pool", description = "Updates the Pool with all parameters set in the given body of the request.", responses = { |
| @ApiResponse(description = "The updated Pool", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "400", description = "Invalid ID supplied") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Consumes(MediaType.APPLICATION_JSON) |
| @Path("/{" + REQUESTPARAM_ID + "}") |
| public Response update( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName, |
| @Parameter(description = "ID of the Pool", required = true) @PathParam(REQUESTPARAM_ID) String id, |
| String body) { |
| RequestBody requestBody = RequestBody.create(body); |
| |
| return entityService |
| .update(V(sourceName), entityService.find(V(sourceName), Pool.class, V(id)), |
| requestBody.getValueMapSupplier()) |
| .map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get(); |
| } |
| |
| /** |
| * Deletes and returns the deleted {@link Pool}. |
| * |
| * @param sourceName name of the source (MDM {@link Environment} name) |
| * @param id The identifier of the {@link Pool} to delete. |
| * @return the deleted {@link ValueList }s as {@link Response} |
| */ |
| @DELETE |
| @Operation(summary = "Delete an existing Pool", responses = { |
| @ApiResponse(description = "The deleted Pool", content = @Content(schema = @Schema(implementation = MDMEntityResponse.class))), |
| @ApiResponse(responseCode = "400", description = "Invalid ID supplied") }) |
| @Produces(MediaType.APPLICATION_JSON) |
| @Path("/{" + REQUESTPARAM_ID + "}") |
| public Response delete( |
| @Parameter(description = "Name of the MDM datasource", required = true) @PathParam(REQUESTPARAM_SOURCENAME) String sourceName, |
| @Parameter(description = "ID of the Pool", required = true) @PathParam(REQUESTPARAM_ID) String id) { |
| return entityService.delete(V(sourceName), entityService.find(V(sourceName), Pool.class, V(id))) |
| .map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).get(); |
| } |
| } |