blob: 42dbc1b2ea87d6a074a6d9f13a64d00e9442857c [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_ID2;
import static org.eclipse.mdm.businessobjects.boundary.ResourceConstants.REQUESTPARAM_ID3;
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 static org.eclipse.mdm.businessobjects.utils.ServiceUtils.SL;
import static org.eclipse.mdm.businessobjects.utils.ServiceUtils.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.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.CatalogAttribute;
import org.eclipse.mdm.api.dflt.model.CatalogComponent;
import org.eclipse.mdm.api.dflt.model.CatalogSensor;
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.EntityService;
import org.eclipse.mdm.businessobjects.utils.ServiceUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.vavr.collection.List;
/**
* {@link CatalogAttribute} resource handling REST requests
*
* @author Philipp Schweinbenz, science+computing AG Tuebingen (Atos SE)
*
*/
@Tag(name = "Catalog")
@Path("/environments/{" + REQUESTPARAM_SOURCENAME + "}/catcomps/testequipment/{" + REQUESTPARAM_ID + "}/catsensors/{"
+ REQUESTPARAM_ID2 + "}/catsensorattrs")
public class CatalogSensorAttributeResource {
@EJB
private EntityService entityService;
/**
* Returns the found {@link CatalogAttribute}.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param catCompId id of the {@link CatalogComponent}
* @param sensorId id of the {@link CatalogSensor}
* @param id id of the {@link CatalogAttribute}
* @return the found {@link CatalogAttribute} as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID3 + "}")
public Response find(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String catCompId, @PathParam(REQUESTPARAM_ID2) String sensorId,
@PathParam(REQUESTPARAM_ID3) String id) {
return entityService
.find(sourceName, CatalogAttribute.class, id, V(ContextType.TESTEQUIPMENT), SL(catCompId, sensorId))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER)
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the (filtered) {@link CatalogAttribute}s.
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param catCompId id of the {@link CatalogComponent}
* @param sensorId id of the {@link CatalogSensor}
* @param filter filter string to filter the {@link CatalogAttribute} result
* @return the (filtered) {@link CatalogAttribute}s as {@link Response}
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findAll(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String catCompId, @PathParam(REQUESTPARAM_ID2) String sensorId,
@QueryParam("filter") String filter) {
return entityService
.find(sourceName, CatalogSensor.class, sensorId, V(ContextType.TESTEQUIPMENT), SL(catCompId))
.map(catSensor -> List.ofAll(catSensor.getCatalogAttributes()))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER)
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the created {@link CatalogAttributeValue}.
*
* @param catCompId id of the {@link CatalogComponent}
* @param sensorId id of the {@link CatalogSensor}
* @param body The {@link CatalogAttribute} to create.
* @return the created {@link CatalogAttribute} as {@link Response}.
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response create(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String catCompId, @PathParam(REQUESTPARAM_ID2) String sensorId,
MDMEntityResponse body) {
return entityService
.create(sourceName, CatalogAttribute.class, L(
decompose(body::getData).<MDMEntity>getAt(0).get(MDMEntity::getName),
entityService.getEnumerationValueSupplier(decompose(body::getData).<MDMEntity>getAt(0)
.get(MDMEntity::getAttributes) //
.get(attrs -> attrs.stream()
.filter(attr -> attr.getName().equals(CatalogAttribute.VATTR_SCALAR_TYPE))
.findFirst().get().getValue())),
entityService.find(sourceName, CatalogSensor.class, sensorId, V(ContextType.TESTEQUIPMENT),
SL(catCompId))))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.CREATED))
.recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER).getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Updates the {@link CatalogAttribute} with all parameters set in the given
* JSON body of the request
*
* @param sourceName name of the source (MDM {@link Environment} name)
* @param catCompId id of the {@link CatalogComponent}
* @param sensorId id of the {@link CatalogSensor}
* @param id the identifier of the {@link CatalogAttribute} to delete.
* @param body the body of the request containing the attributes to update
* @return the updated {@link CatalogAttribute}
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID3 + "}")
public Response patch(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String catCompId, @PathParam(REQUESTPARAM_ID2) String sensorId,
@PathParam(REQUESTPARAM_ID3) String id, MDMEntityResponse body) {
return entityService
.update(sourceName,
entityService.find(sourceName, CatalogAttribute.class, id, V(ContextType.TESTEQUIPMENT),
SL(catCompId, sensorId)),
entityService.removeVirtualAttributes(decompose(body::getData).<MDMEntity>getValueAt(0)))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER)
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Deletes and returns the deleted {@link CatalogAttribute}.
*
* @param catCompId id of the {@link CatalogComponent}
* @param sensorId id of the {@link CatalogSensor}
* @param id The identifier of the {@link CatalogAttribute} to delete.
* @return the deleted {@link CatalogAttribute }s as {@link Response}
*/
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{" + REQUESTPARAM_ID3 + "}")
public Response delete(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName,
@PathParam(REQUESTPARAM_ID) String catCompId, @PathParam(REQUESTPARAM_ID2) String sensorId,
@PathParam(REQUESTPARAM_ID3) String id) {
return entityService
.delete(sourceName,
entityService.find(sourceName, CatalogAttribute.class, id, V(ContextType.TESTEQUIPMENT),
SL(catCompId, sensorId)))
.map(e -> ServiceUtils.buildEntityResponse(e, Status.OK)).recover(ServiceUtils.ERROR_RESPONSE_SUPPLIER)
.getOrElse(ServiceUtils.SERVER_ERROR_RESPONSE);
}
/**
* Returns the search attributes for the {@link CatalogAttribute} 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, CatalogAttribute.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")
public Response localize(@PathParam(REQUESTPARAM_SOURCENAME) String sourceName) {
return ServiceUtils.buildLocalizationResponse(sourceName, CatalogAttribute.class, entityService);
}
}