blob: 664d5662a2e6c93e394eb0fccb83bbe3088ecef0 [file] [log] [blame]
/********************************************************************************
* Copyright (c) 2015-2018 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_NAME;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.REQUESTPARAM_SOURCENAME;
import static org.eclipse.mdm.businessobjects.utils.Decomposer.decompose;
import static org.eclipse.mdm.businessobjects.utils.ServiceUtils.L;
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.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.ContextType;
import org.eclipse.mdm.api.base.model.Environment;
import org.eclipse.mdm.api.dflt.model.TemplateRoot;
import org.eclipse.mdm.api.dflt.model.TemplateTestStep;
import org.eclipse.mdm.businessobjects.entity.MDMEntity;
import org.eclipse.mdm.businessobjects.entity.MDMEntityResponse;
import org.eclipse.mdm.businessobjects.entity.SearchAttribute;
import org.eclipse.mdm.businessobjects.service.TemplateService;
import org.eclipse.mdm.businessobjects.utils.ServiceUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
* {@link TemplateTestStep} resource handling REST requests
*
* @author Alexander Nehmer, science+computing AG Tuebingen (Atos SE)
*
*/
@Tag(name = "Template")
@Path("/environments/{" + REQUESTPARAM_SOURCENAME + "}/tplteststeps")
public class TemplateTestStepResource {
@EJB
private TemplateService tplService;
/**
* Returns the found {@link TemplateTestStep}.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param contextType {@link ContextType} of the {@link TemplateTestStep} to
* load
* @param id id of the {@link TemplateTestStep}
* @return the found {@link TemplateTestStep} as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
public Response find(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, @PathParam(REQUESTPARAM_ID) String id) {
return tplService.find(sourceName, TemplateTestStep.class, id)
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the (filtered) {@link TemplateTestStep}s.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param contextType {@link ContextType} of the {@link TemplateTestStep} to
* load
* @param filter filter string to filter the {@link TemplateTestStep}
* result
* @return the (filtered) {@link TemplateTestStep}s as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findAll(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@QueryParam("filter") String filter) {
return tplService.findAll(sourceName, TemplateTestStep.class, filter)
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the created {@link TemplateTestStepValue}.
*
* @param body The {@link TemplateTestStep} to create.
* @return the created {@link TemplateTestStep} as {@link Response}.
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response create(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, MDMEntityResponse body) {
return tplService
.create(sourceName, TemplateTestStep.class,
L(decompose(body::getData).<MDMEntity>getAt(0).get(MDMEntity::getName)))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.CREATED)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Updates the {@link TemplateTestStep} 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 TemplateTestStep} to delete.
* @param body the body of the request containing the attributes to update
* @return the updated {@link TemplateTestStep}
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
public Response patch(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName, @PathParam(REQUESTPARAM_ID) String id,
MDMEntityResponse body) {
return tplService.update(sourceName, tplService.find(sourceName, TemplateTestStep.class, id),
decompose(body::getData).<MDMEntity>getValueAt(0))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Creates new version of given {@link TemplateTestStep}.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param id the identifier of the {@link TemplateRoot} to create new
* version of
* @return a new version of given {@link TemplateTestStep}
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_NAME + "}/versions")
public Response createNewVersion(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_NAME) String name) {
return tplService.createTplTestStepVersion(sourceName, name)
.map(e -> ServiceUtils.buildEntityResponse(e, Status.CREATED)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Deletes and returns the deleted {@link TemplateTestStep}.
*
* @param id The identifier of the {@link TemplateTestStep} to delete.
* @return the deleted {@link TemplateTestStep }s as {@link Response}
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID + "}")
public Response delete(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String id) {
return tplService.delete(sourceName, tplService.find(sourceName, TemplateTestStep.class, id))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)) //
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER) //
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the search attributes for the {@link TemplateTestStep} 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")
public Response getSearchAttributes(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName) {
return ServiceUtils.buildSearchAttributesResponse(sourceName, TemplateTestStep.class, tplService);
}
/**
* 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")
public Response localize(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName) {
return ServiceUtils.buildLocalizationResponse(sourceName, TemplateTestStep.class, tplService);
}
}