2018-07-11 15:47:36 +02:00
package eu.eudat.controllers ;
2020-03-11 10:04:49 +01:00
import eu.eudat.elastic.criteria.DatasetCriteria ;
2018-07-11 15:47:36 +02:00
import eu.eudat.elastic.entities.Dataset ;
2020-03-11 10:04:49 +01:00
import eu.eudat.elastic.entities.Tag ;
2018-07-11 15:47:36 +02:00
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet ;
import eu.eudat.logic.proxy.config.exceptions.NoURLFound ;
import eu.eudat.logic.services.ApiContext ;
2020-03-17 17:03:43 +01:00
import eu.eudat.logic.utilities.helpers.StreamDistinctBy ;
2018-07-11 15:47:36 +02:00
import eu.eudat.models.data.helpers.responses.ResponseItem ;
import eu.eudat.types.ApiMessageCode ;
import org.springframework.beans.factory.annotation.Autowired ;
2019-08-30 17:35:37 +02:00
import org.springframework.core.env.Environment ;
2018-07-11 15:47:36 +02:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.* ;
2018-08-31 16:12:31 +02:00
import java.io.IOException ;
2020-03-11 10:04:49 +01:00
import java.util.Collection ;
2018-07-11 15:47:36 +02:00
import java.util.List ;
2020-03-11 10:04:49 +01:00
import java.util.concurrent.ExecutionException ;
import java.util.stream.Collectors ;
2018-07-11 15:47:36 +02:00
/ * *
* Created by ikalyvas on 7 / 5 / 2018 .
* /
@RestController
@CrossOrigin
@RequestMapping ( value = { " /api " } )
2018-08-31 16:12:31 +02:00
public class TagController extends BaseController {
2018-07-11 15:47:36 +02:00
2020-04-01 17:17:17 +02:00
// private Repository<Dataset, TagCriteria> datasetRepository;
2019-08-30 17:35:37 +02:00
private Environment environment ;
2018-07-11 15:47:36 +02:00
@Autowired
2020-04-01 17:17:17 +02:00
public TagController ( ApiContext apiContext , /*Repository tagRepository, */ Environment environment ) {
2018-07-11 15:47:36 +02:00
super ( apiContext ) ;
2020-04-01 17:17:17 +02:00
// this.datasetRepository = tagRepository;
2019-08-30 17:35:37 +02:00
this . environment = environment ;
2018-07-11 15:47:36 +02:00
}
@RequestMapping ( method = RequestMethod . GET , value = { " /external/tags " } , produces = " application/json " )
public @ResponseBody
2020-03-11 10:04:49 +01:00
ResponseEntity < ResponseItem < List < Tag > > > listExternalTagModel (
@RequestParam ( value = " query " , required = false ) String query , @RequestParam ( value = " type " , required = false ) String type ) throws HugeResultSet , NoURLFound , IOException , ExecutionException , InterruptedException {
//ExternalUrlCriteria externalUrlCriteria = new ExternalUrlCriteria(query);
/ * List < Map < String , String > > remoteRepos = this . getApiContext ( ) . getOperationsContext ( ) . getRemoteFetcher ( ) . getTags ( externalUrlCriteria , type ) ;
TagExternalSourcesModel researchersExternalSourcesModel = new TagExternalSourcesModel ( ) . fromExternalItem ( remoteRepos ) ; * /
2020-09-14 17:09:57 +02:00
if ( this . getApiContext ( ) . getOperationsContext ( ) . getElasticRepository ( ) . getDatasetRepository ( ) . exists ( ) ) {
2021-08-04 17:38:49 +02:00
DatasetCriteria criteria = new DatasetCriteria ( ) ;
criteria . setHasTags ( true ) ;
2021-11-09 13:17:46 +01:00
List < Tag > tags = this . getApiContext ( ) . getOperationsContext ( ) . getElasticRepository ( ) . getDatasetRepository ( ) . query ( criteria ) . stream ( ) . map ( Dataset : : getTags ) . flatMap ( Collection : : stream ) . filter ( StreamDistinctBy . distinctByKey ( Tag : : getId ) ) . filter ( tag - > tag . getName ( ) . toLowerCase ( ) . startsWith ( query . toLowerCase ( ) ) ) . collect ( Collectors . toList ( ) ) ;
2019-05-16 12:54:29 +02:00
2020-09-14 17:09:57 +02:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < Tag > > ( ) . payload ( tags ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
} else {
return ResponseEntity . status ( HttpStatus . INTERNAL_SERVER_ERROR ) . body ( new ResponseItem < List < Tag > > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( " Elastic Services are not available " ) ) ;
}
2018-07-11 15:47:36 +02:00
}
}