195 lines
11 KiB
Java
195 lines
11 KiB
Java
package eu.eudat.controllers.publicapi;
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
import eu.eudat.commons.enums.IsActive;
|
|
import eu.eudat.controllers.BaseController;
|
|
import eu.eudat.controllers.publicapi.request.dmp.DataManagmentPlanPublicTableRequest;
|
|
import eu.eudat.controllers.publicapi.response.DataTableData;
|
|
import eu.eudat.data.DmpEntity;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.model.*;
|
|
import eu.eudat.model.builder.DmpBuilder;
|
|
import eu.eudat.model.builder.DmpDescriptionTemplateBuilder;
|
|
import eu.eudat.model.mapper.publicapi.DmpToPublicApiDmpListingMapper;
|
|
import eu.eudat.model.mapper.publicapi.DmpToPublicApiDmpMapper;
|
|
import eu.eudat.model.publicapi.listingmodels.DataManagementPlanPublicListingModel;
|
|
import eu.eudat.model.publicapi.overviewmodels.DataManagementPlanPublicModel;
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
|
import eu.eudat.query.DmpDescriptionTemplateQuery;
|
|
import eu.eudat.query.DmpQuery;
|
|
import eu.eudat.query.EntityDoiQuery;
|
|
import eu.eudat.query.lookup.DmpLookup;
|
|
import eu.eudat.types.ApiMessageCode;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
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.ExampleObject;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import jakarta.validation.Valid;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.MessageSource;
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.EnumSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
|
|
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
|
|
@Tag(name = "DMPs", description = "Provides DMP public API's.")
|
|
@RestController
|
|
@CrossOrigin
|
|
@RequestMapping(value = {"/api/public/dmps"})
|
|
public class PublicDmpsDocumentation extends BaseController {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PublicDmpsDocumentation.class);
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
private final MessageSource messageSource;
|
|
|
|
private final DmpToPublicApiDmpMapper dmpToPublicApiDmpMapper;
|
|
|
|
private final DmpToPublicApiDmpListingMapper dmpToPublicApiDmpListingMapper;
|
|
|
|
@Autowired
|
|
public PublicDmpsDocumentation(
|
|
ApiContext apiContext,
|
|
QueryFactory queryFactory,
|
|
BuilderFactory builderFactory,
|
|
MessageSource messageSource,
|
|
DmpToPublicApiDmpMapper dmpToPublicApiDmpMapper,
|
|
DmpToPublicApiDmpListingMapper dmpToPublicApiDmpListingMapper) {
|
|
super(apiContext);
|
|
this.queryFactory = queryFactory;
|
|
this.builderFactory = builderFactory;
|
|
this.messageSource = messageSource;
|
|
this.dmpToPublicApiDmpMapper = dmpToPublicApiDmpMapper;
|
|
this.dmpToPublicApiDmpListingMapper = dmpToPublicApiDmpListingMapper;
|
|
}
|
|
|
|
@Operation(summary = "This method is used to get a listing of public dmps.", description = PublicApiStaticHelpers.Dmp.getPagedNotes)
|
|
@io.swagger.v3.oas.annotations.responses.ApiResponses(value = {@ApiResponse(
|
|
responseCode = "200",
|
|
description = """
|
|
The following example is generated using:
|
|
a) body: *{"criteria": {},"length": 2,"offset": 0,"orderings": {"fields": []} }*
|
|
b) fieldsGroup: listing""",
|
|
content = @Content(mediaType = APPLICATION_JSON_VALUE, examples = {@ExampleObject(
|
|
value = PublicApiStaticHelpers.Dmp.getPagedResponseExample
|
|
)})
|
|
)})
|
|
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
|
|
public @ResponseBody ResponseEntity<ResponseItem<DataTableData<DataManagementPlanPublicListingModel>>> getPaged(
|
|
@Valid @RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody(description = PublicApiStaticHelpers.Dmp.getPagedRequestBodyDescription) DataManagmentPlanPublicTableRequest dmpTableRequest,
|
|
@RequestParam @Parameter(description = PublicApiStaticHelpers.Dmp.getPagedRequestParamDescription, example = "listing") String fieldsGroup
|
|
) throws Exception {
|
|
DmpLookup lookup = getDmpLookup(fieldsGroup, dmpTableRequest);
|
|
DmpQuery query = lookup.enrich(this.queryFactory).authorize(EnumSet.of(AuthorizationFlags.Public)).isActive(IsActive.Active);
|
|
long count = this.queryFactory.query(DmpQuery.class).authorize(EnumSet.of(AuthorizationFlags.Public)).isActive(IsActive.Active).count();
|
|
|
|
List<DmpEntity> data = query.collectAs(lookup.getProject());
|
|
List<Dmp> models = this.builderFactory.builder(DmpBuilder.class).build(lookup.getProject(), data);
|
|
DataTableData<DataManagementPlanPublicListingModel> dataTableData = new DataTableData<>();
|
|
dataTableData.setData(models.stream().map(this.dmpToPublicApiDmpListingMapper::toPublicListingModel).toList());
|
|
dataTableData.setTotalCount(count);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<DataManagementPlanPublicListingModel>>().status(ApiMessageCode.NO_MESSAGE).payload(dataTableData));
|
|
}
|
|
|
|
@NotNull
|
|
private static DmpLookup getDmpLookup(String fieldsGroup, DataManagmentPlanPublicTableRequest request) {
|
|
BaseFieldSet fieldSet = new BaseFieldSet();
|
|
Set<String> fields;
|
|
if (fieldsGroup.equals("listing")) {
|
|
fields = Set.of(
|
|
Dmp._id,
|
|
Dmp._label,
|
|
Dmp._description,
|
|
Dmp._version,
|
|
Dmp._groupId,
|
|
String.join(".", Dmp._dmpUsers, String.join(".", DmpUser._user, User._id)),
|
|
String.join(".", Dmp._dmpUsers, String.join(".", DmpUser._user, User._name)),
|
|
String.join(".", Dmp._dmpUsers, DmpUser._role),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._id)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._reference)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._label)),
|
|
Dmp._dmpReferences,
|
|
Dmp._createdAt,
|
|
Dmp._updatedAt,
|
|
Dmp._finalizedAt
|
|
);
|
|
} else {
|
|
fields = Set.of(
|
|
Dmp._id,
|
|
Dmp._label,
|
|
Dmp._groupId,
|
|
Dmp._createdAt
|
|
);
|
|
}
|
|
fieldSet.setFields(fields);
|
|
DmpLookup lookup = new DmpLookup();
|
|
lookup.setProject(fieldSet);
|
|
return lookup;
|
|
}
|
|
|
|
@Operation(summary = "This method is used to get the overview of a public dmp.", description = PublicApiStaticHelpers.Dmp.getOverviewSinglePublicNotes)
|
|
@io.swagger.v3.oas.annotations.responses.ApiResponses(value = {@ApiResponse(
|
|
responseCode = "200",
|
|
description = "The following example is generated using id: *e9a73d77-adfa-4546-974f-4a4a623b53a8*",
|
|
content = @Content(mediaType = APPLICATION_JSON_VALUE, examples = {@ExampleObject(
|
|
value = PublicApiStaticHelpers.Dmp.getOverviewSinglePublicResponseExample
|
|
)})
|
|
)})
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/{id}"}, produces = "application/json")
|
|
public @ResponseBody ResponseEntity<ResponseItem<DataManagementPlanPublicModel>> getOverviewSinglePublic(
|
|
@PathVariable @Parameter(description = "fetch the dmp with the given id", example = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") String id
|
|
) {
|
|
BaseFieldSet dmpFieldSet = new BaseFieldSet();
|
|
Set<String> dmpFields = Set.of(
|
|
Dmp._id,
|
|
Dmp._label,
|
|
Dmp._description,
|
|
Dmp._version,
|
|
Dmp._groupId,
|
|
String.join(".", Dmp._dmpUsers, String.join(".", DmpUser._user, User._id)),
|
|
String.join(".", Dmp._dmpUsers, String.join(".", DmpUser._user, User._name)),
|
|
String.join(".", Dmp._dmpUsers, DmpUser._role),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._id)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._reference)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._label)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._abbreviation)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._description)),
|
|
String.join(".", Dmp._dmpReferences, String.join(".", DmpReference._reference, Reference._definition)),
|
|
Dmp._createdAt,
|
|
Dmp._updatedAt,
|
|
Dmp._finalizedAt
|
|
);
|
|
dmpFieldSet.setFields(dmpFields);
|
|
DmpQuery query = this.queryFactory.query(DmpQuery.class).authorize(EnumSet.of(AuthorizationFlags.Public)).ids(UUID.fromString(id)).isActive(IsActive.Active);
|
|
Dmp model = this.builderFactory.builder(DmpBuilder.class).build(dmpFieldSet, query.firstAs(dmpFieldSet));
|
|
if (model == null)
|
|
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Dmp.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
EntityDoiQuery entityDoiQuery = this.queryFactory.query(EntityDoiQuery.class).entityIds(UUID.fromString(id)).isActive(IsActive.Active);
|
|
BaseFieldSet templateFieldSet = new BaseFieldSet();
|
|
DmpDescriptionTemplateQuery dmpDescriptionTemplateQuery = this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(UUID.fromString(id)).isActive(IsActive.Active);
|
|
List<DmpDescriptionTemplate> descriptionTemplates = this.builderFactory.builder(DmpDescriptionTemplateBuilder.class).build(templateFieldSet, dmpDescriptionTemplateQuery.collectAs(templateFieldSet));
|
|
DataManagementPlanPublicModel dataManagementPlan = this.dmpToPublicApiDmpMapper.toPublicModel(model, entityDoiQuery.collect(), descriptionTemplates);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataManagementPlanPublicModel>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlan));
|
|
}
|
|
|
|
}
|