2024-04-26 12:50:56 +02:00
package org.opencdmp.controllers.publicapi ;
2023-11-27 16:33:24 +01:00
2024-05-02 09:46:36 +02:00
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 ;
2024-04-26 12:50:56 +02:00
import org.opencdmp.authorization.AuthorizationFlags ;
import org.opencdmp.commons.enums.IsActive ;
import org.opencdmp.controllers.publicapi.request.dataset.DatasetPublicTableRequest ;
import org.opencdmp.controllers.publicapi.response.DataTableData ;
2024-05-02 09:46:36 +02:00
import org.opencdmp.controllers.publicapi.types.ApiMessageCode ;
import org.opencdmp.controllers.publicapi.types.ResponseItem ;
2024-04-26 12:50:56 +02:00
import org.opencdmp.data.DescriptionEntity ;
import org.opencdmp.model.* ;
import org.opencdmp.model.builder.DescriptionBuilder ;
import org.opencdmp.model.builder.DmpBuilder ;
import org.opencdmp.model.descriptiontemplatedefinition.* ;
import org.opencdmp.model.descriptiontemplatedefinition.fielddata.BaseFieldData ;
import org.opencdmp.model.mapper.publicapi.DescriptionToPublicApiDatasetListingMapper ;
import org.opencdmp.model.mapper.publicapi.DescriptionToPublicApiDatasetMapper ;
import org.opencdmp.model.mapper.publicapi.DmpToPublicApiDmpListingMapper ;
import org.opencdmp.model.publicapi.listingmodels.DatasetPublicListingModel ;
import org.opencdmp.model.publicapi.overviewmodels.DatasetPublicModel ;
import org.opencdmp.query.DescriptionQuery ;
import org.opencdmp.query.DmpQuery ;
import org.opencdmp.query.lookup.DescriptionLookup ;
import org.opencdmp.query.lookup.DmpLookup ;
import org.opencdmp.service.publicapi.PublicApiProperties ;
2023-11-27 16:33:24 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Autowired ;
2023-11-29 10:30:38 +01:00
import org.springframework.context.MessageSource ;
import org.springframework.context.i18n.LocaleContextHolder ;
2023-11-27 16:33:24 +01:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.* ;
2023-11-29 10:30:38 +01:00
import java.util.EnumSet ;
import java.util.List ;
import java.util.Set ;
import java.util.UUID ;
2023-11-27 16:33:24 +01:00
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE ;
@Tag ( name = " Datasets Description " , description = " Provides Dataset description public API's. " )
@RestController
@CrossOrigin
2024-05-02 09:46:36 +02:00
@RequestMapping ( " /api/public/datasets/ " )
2024-02-08 10:04:38 +01:00
public class PublicDatasetsDescriptionDocumentation {
2023-11-27 16:33:24 +01:00
private static final Logger logger = LoggerFactory . getLogger ( PublicDatasetsDescriptionDocumentation . class ) ;
2023-11-29 10:30:38 +01:00
private final QueryFactory queryFactory ;
private final BuilderFactory builderFactory ;
private final MessageSource messageSource ;
private final DescriptionToPublicApiDatasetMapper descriptionToPublicApiDatasetMapper ;
private final DescriptionToPublicApiDatasetListingMapper descriptionToPublicApiDatasetListingMapper ;
2024-02-08 17:12:47 +01:00
private final PublicApiProperties config ;
2023-11-29 12:45:35 +01:00
private final DmpToPublicApiDmpListingMapper dmpToPublicApiDmpListingMapper ;
2023-11-27 16:33:24 +01:00
@Autowired
2023-11-29 10:30:38 +01:00
public PublicDatasetsDescriptionDocumentation (
2024-02-08 17:12:47 +01:00
QueryFactory queryFactory ,
BuilderFactory builderFactory ,
MessageSource messageSource ,
DescriptionToPublicApiDatasetMapper descriptionToPublicApiDatasetMapper ,
DescriptionToPublicApiDatasetListingMapper descriptionToPublicApiDatasetListingMapper , PublicApiProperties config , DmpToPublicApiDmpListingMapper dmpToPublicApiDmpListingMapper ) {
2023-11-29 10:30:38 +01:00
this . queryFactory = queryFactory ;
this . builderFactory = builderFactory ;
this . messageSource = messageSource ;
this . descriptionToPublicApiDatasetMapper = descriptionToPublicApiDatasetMapper ;
this . descriptionToPublicApiDatasetListingMapper = descriptionToPublicApiDatasetListingMapper ;
2024-02-08 17:12:47 +01:00
this . config = config ;
this . dmpToPublicApiDmpListingMapper = dmpToPublicApiDmpListingMapper ;
2023-11-27 16:33:24 +01:00
}
@Operation ( summary = " This method is used to get a listing of public datasets. " , description = PublicApiStaticHelpers . Description . getPagedNotes )
2024-05-02 09:46:36 +02:00
@io.swagger.v3.oas.annotations.responses.ApiResponses ( @ApiResponse (
responseCode = " 200 " ,
description = " The following example is generated using body: *{ \" criteria \" : {}, \" length \" : 2, \" offset \" : 0, \" orderings \" : { \" fields \" : []} }* " ,
content = @Content ( mediaType = APPLICATION_JSON_VALUE , examples = @ExampleObject ( PublicApiStaticHelpers . Description . getPagedResponseExample ) )
) )
2023-11-27 16:33:24 +01:00
@RequestMapping ( method = RequestMethod . POST , consumes = " application/json " , produces = " application/json " )
2023-11-28 11:54:08 +01:00
public @ResponseBody ResponseEntity < ResponseItem < DataTableData < DatasetPublicListingModel > > > getPaged (
@Valid @RequestBody @io.swagger.v3.oas.annotations.parameters.RequestBody ( description = PublicApiStaticHelpers . Description . getPagedRequestBodyDescription ) DatasetPublicTableRequest datasetTableRequest
) throws Exception {
2023-11-29 10:30:38 +01:00
DescriptionLookup lookup = getDescriptionLookup ( ) ;
DescriptionQuery query = lookup . enrich ( this . queryFactory ) . authorize ( EnumSet . of ( AuthorizationFlags . Public ) ) . isActive ( IsActive . Active ) ;
long count = this . queryFactory . query ( DescriptionQuery . class ) . authorize ( EnumSet . of ( AuthorizationFlags . Public ) ) . isActive ( IsActive . Active ) . count ( ) ;
List < DescriptionEntity > data = query . collectAs ( lookup . getProject ( ) ) ;
List < Description > models = this . builderFactory . builder ( DescriptionBuilder . class ) . build ( lookup . getProject ( ) , data ) ;
DataTableData < DatasetPublicListingModel > dataTableData = new DataTableData < > ( ) ;
2024-02-08 17:12:47 +01:00
dataTableData . setData ( models . stream ( ) . map ( x - > this . descriptionToPublicApiDatasetListingMapper . toPublicListingModel ( x , this . config . getReferenceTypeMap ( ) ) ) . toList ( ) ) ;
2023-11-29 10:30:38 +01:00
dataTableData . setTotalCount ( count ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < DataTableData < DatasetPublicListingModel > > ( ) . status ( ApiMessageCode . NO_MESSAGE ) . payload ( dataTableData ) ) ;
}
2023-11-27 16:33:24 +01:00
@Operation ( summary = " This method is used to get the overview of a public dataset. " , description = PublicApiStaticHelpers . Description . getOverviewSinglePublicNotes )
2024-05-02 09:46:36 +02:00
@io.swagger.v3.oas.annotations.responses.ApiResponses ( @ApiResponse (
responseCode = " 200 " ,
description = " The following example is generated using id: *ef7dfbdc-c5c1-46a7-a37b-c8d8692f1c0e* " ,
content = @Content ( mediaType = APPLICATION_JSON_VALUE , examples = @ExampleObject ( PublicApiStaticHelpers . Description . getOverviewSinglePublicResponseExample ) )
) )
@RequestMapping ( method = RequestMethod . GET , value = " /{id} " , produces = " application/json " )
2023-11-28 11:54:08 +01:00
public @ResponseBody ResponseEntity < ResponseItem < DatasetPublicModel > > getOverviewSinglePublic (
@PathVariable @Parameter ( description = " fetch the dataset with the given id " , example = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx " ) String id
) {
2023-11-29 13:34:37 +01:00
DescriptionLookup lookup = getDescriptionLookup ( ) ;
2023-11-29 10:30:38 +01:00
DescriptionQuery query = this . queryFactory . query ( DescriptionQuery . class ) . authorize ( EnumSet . of ( AuthorizationFlags . Public ) ) . ids ( UUID . fromString ( id ) ) . isActive ( IsActive . Active ) ;
2023-11-29 13:34:37 +01:00
Description model = this . builderFactory . builder ( DescriptionBuilder . class ) . build ( lookup . getProject ( ) , query . firstAs ( lookup . getProject ( ) ) ) ;
2023-11-29 10:30:38 +01:00
if ( model = = null )
2024-05-02 09:46:36 +02:00
throw new MyNotFoundException ( this . messageSource . getMessage ( " General_ItemNotFound " , new Object [ ] { id , Description . class . getSimpleName ( ) } , LocaleContextHolder . getLocale ( ) ) ) ;
2023-11-29 12:45:35 +01:00
2023-12-08 16:21:06 +01:00
DmpQuery dmpQuery = this . queryFactory . query ( DmpQuery . class ) . ids ( model . getDmp ( ) . getId ( ) ) . isActive ( IsActive . Active ) ;
2023-11-29 12:45:35 +01:00
DmpLookup dmpLookup = getDmpLookup ( ) ;
Dmp dmp = this . builderFactory . builder ( DmpBuilder . class ) . build ( dmpLookup . getProject ( ) , dmpQuery . firstAs ( dmpLookup . getProject ( ) ) ) ;
2024-02-08 17:12:47 +01:00
DatasetPublicModel dataset = this . descriptionToPublicApiDatasetMapper . toPublicModel ( model , this . dmpToPublicApiDmpListingMapper . toPublicListingModel ( dmp , this . config . getReferenceTypeMap ( ) ) , this . config . getReferenceTypeMap ( ) ) ;
2023-11-29 10:30:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < DatasetPublicModel > ( ) . status ( ApiMessageCode . NO_MESSAGE ) . payload ( dataset ) ) ;
2023-11-27 16:33:24 +01:00
}
2023-11-29 12:45:35 +01:00
2023-11-29 13:34:37 +01:00
@NotNull
private static DescriptionLookup getDescriptionLookup ( ) {
BaseFieldSet descriptionFieldSet = new BaseFieldSet ( ) ;
Set < String > descriptionFields = Set . of (
Description . _id ,
Description . _label ,
Description . _description ,
Description . _status ,
2023-12-08 16:21:06 +01:00
String . join ( " . " , Description . _dmp , Dmp . _id ) ,
String . join ( " . " , Description . _dmp , Dmp . _label ) ,
2023-11-29 13:34:37 +01:00
String . join ( " . " , Description . _createdBy , User . _id ) ,
String . join ( " . " , Description . _createdBy , User . _name ) ,
2023-12-06 15:14:07 +01:00
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _id ) ,
2023-12-06 16:38:03 +01:00
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _type ) ,
2024-02-08 17:12:47 +01:00
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _type , Reference . _id ) ,
2023-12-06 15:14:07 +01:00
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _reference ) ,
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _label ) ,
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _abbreviation ) ,
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _description ) ,
String . join ( " . " , Description . _descriptionReferences , DescriptionReference . _reference , Reference . _definition ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _status ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _id ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _ordinal ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _title ) ,
2024-01-31 13:33:24 +01:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _id ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _description ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _extendedDescription ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _title ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _numbering ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _ordinal ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _sections ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _id ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _description ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _extendedDescription ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _ordinal ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _additionalInformation ) ,
2024-04-24 13:07:51 +02:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _hasMultiplicity ) ,
2024-01-31 13:33:24 +01:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _multiplicity , Multiplicity . _min ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _multiplicity , Multiplicity . _max ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _multiplicity , Multiplicity . _placeholder ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _multiplicity , Multiplicity . _tableView ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _numbering ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _title ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _id ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _numbering ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _ordinal ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _data , BaseFieldData . _label ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _data , BaseFieldData . _fieldType ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _schematics ) ,
2024-05-02 14:57:19 +02:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _defaultValue , DefaultValue . _dateValue ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _defaultValue , DefaultValue . _booleanValue ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _defaultValue , DefaultValue . _textValue ) ,
2024-01-31 13:33:24 +01:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _includeInExport ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _validations ) ,
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _visibilityRules , Rule . _target ) ,
2024-03-20 17:40:34 +01:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _visibilityRules , Rule . _dateValue ) ,
2024-05-02 09:46:36 +02:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _visibilityRules , Rule . _booleanValue ) ,
2024-03-20 17:40:34 +01:00
String . join ( " . " , Description . _descriptionTemplate , DescriptionTemplate . _definition , Definition . _pages , Page . _sections , Section . _fieldSets , FieldSet . _fields , Field . _visibilityRules , Rule . _textValue ) ,
2023-11-29 13:34:37 +01:00
Description . _createdAt ,
Description . _updatedAt ,
Description . _finalizedAt
) ;
descriptionFieldSet . setFields ( descriptionFields ) ;
DescriptionLookup lookup = new DescriptionLookup ( ) ;
lookup . setProject ( descriptionFieldSet ) ;
return lookup ;
}
2023-11-29 12:45:35 +01:00
@NotNull
private static DmpLookup getDmpLookup ( ) {
BaseFieldSet fieldSet = new BaseFieldSet ( ) ;
Set < String > 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
) ;
fieldSet . setFields ( fields ) ;
DmpLookup lookup = new DmpLookup ( ) ;
lookup . setProject ( fieldSet ) ;
return lookup ;
}
2023-11-27 16:33:24 +01:00
}