2017-12-15 11:05:51 +01:00
package eu.eudat.controllers ;
import java.util.Date ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.UUID ;
import javax.transaction.Transactional ;
2018-01-03 11:44:54 +01:00
import javax.validation.Valid ;
2017-12-15 11:05:51 +01:00
2017-12-19 09:38:47 +01:00
import eu.eudat.models.criteria.ProjectCriteria ;
2017-12-22 09:31:05 +01:00
import eu.eudat.models.external.OrganisationsExternalSourcesModel ;
2017-12-19 15:09:49 +01:00
import eu.eudat.models.external.ProjectsExternalSourcesModel ;
import eu.eudat.models.external.RegistriesExternalSourcesModel ;
2017-12-17 22:34:24 +01:00
import eu.eudat.models.helpers.responses.* ;
2017-12-19 09:38:47 +01:00
import eu.eudat.models.project.ProjectCriteriaRequest ;
2018-01-22 08:41:31 +01:00
import eu.eudat.models.project.ProjectListingModel ;
2017-12-20 15:52:09 +01:00
import eu.eudat.models.security.Principal ;
2018-01-04 10:32:39 +01:00
import eu.eudat.services.ApiContext ;
2017-12-15 11:05:51 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2018-01-03 11:44:54 +01:00
import org.springframework.context.ApplicationContext ;
2017-12-15 11:05:51 +01:00
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.CrossOrigin ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RequestMethod ;
import org.springframework.web.bind.annotation.RequestParam ;
import org.springframework.web.bind.annotation.ResponseBody ;
import org.springframework.web.bind.annotation.RestController ;
import eu.eudat.dao.entities.DMPDao ;
import eu.eudat.dao.entities.DMPProfileDao ;
import eu.eudat.dao.entities.DataRepositoryDao ;
import eu.eudat.dao.entities.DatasetDao ;
import eu.eudat.dao.entities.DatasetProfileDao ;
import eu.eudat.dao.entities.DatasetProfileRulesetDao ;
import eu.eudat.dao.entities.DatasetProfileViewstyleDao ;
import eu.eudat.dao.entities.OrganisationDao ;
import eu.eudat.dao.entities.ProjectDao ;
import eu.eudat.dao.entities.RegistryDao ;
import eu.eudat.dao.entities.ResearcherDao ;
import eu.eudat.dao.entities.ServiceDao ;
import eu.eudat.dao.entities.UserInfoDao ;
import eu.eudat.entities.DMP ;
import eu.eudat.entities.Project ;
import eu.eudat.entities.UserInfo ;
import eu.eudat.entities.responses.IDLabelPair ;
import eu.eudat.managers.ProjectManager ;
import eu.eudat.models.helpers.DataTableData ;
import eu.eudat.models.project.ProjectTableRequest ;
import eu.eudat.proxy.config.exceptions.HugeResultSet ;
import eu.eudat.proxy.config.exceptions.NoURLFound ;
import eu.eudat.proxy.fetching.RemoteFetcher ;
@RestController
@CrossOrigin
2018-01-17 13:03:51 +01:00
public class Projects extends BaseController {
2018-01-03 11:44:54 +01:00
2018-01-17 13:03:51 +01:00
@Autowired
public Projects ( ApiContext apiContext ) {
super ( apiContext ) ;
}
2017-12-15 11:05:51 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping ( method = RequestMethod . POST , value = { " /projects/getPaged " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < DataTableData < ProjectListingModel > > > getPaged ( @Valid @RequestBody ProjectTableRequest projectTableRequest , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
DataTableData < eu . eudat . models . project . ProjectListingModel > dataTable = new ProjectManager ( ) . getPaged ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , projectTableRequest ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . payload ( dataTable ) . status ( HttpStatus . OK ) ) ;
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping ( method = RequestMethod . GET , value = { " /projects/getSingle/{id} " } , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < eu . eudat . models . project . Project > > getSingle ( @PathVariable String id , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
eu . eudat . models . project . Project project = new ProjectManager ( ) . getSingle ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , id ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . payload ( project ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@Transactional
@RequestMapping ( method = RequestMethod . POST , value = { " /projects/createOrUpdate " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < Project > > addProject ( @Valid @RequestBody eu . eudat . models . project . Project project , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
ProjectManager . createOrUpdate ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , this . getApiContext ( ) . getDatabaseRepository ( ) . getUserInfoDao ( ) , project , principal ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-22 14:42:47 +01:00
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
@Transactional
@RequestMapping ( method = RequestMethod . DELETE , value = { " /projects/inactivate/{id} " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < Project > > inactivate ( @PathVariable String id , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
Project project = new ProjectManager ( ) . inactivate ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , id ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-19 09:38:47 +01:00
2017-12-15 11:05:51 +01:00
2018-01-17 13:03:51 +01:00
@RequestMapping ( method = RequestMethod . POST , value = { " /projects/getWithExternal " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < List < eu . eudat . models . project . Project > > > getWithExternal ( @RequestBody ProjectCriteriaRequest projectCriteria , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
List < eu . eudat . models . project . Project > dataTable = new ProjectManager ( ) . getCriteriaWithExternal ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , projectCriteria , this . getApiContext ( ) . getRemoteFetcher ( ) ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
@RequestMapping ( method = RequestMethod . POST , value = { " /projects/get " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < List < eu . eudat . models . project . Project > > > get ( @RequestBody ProjectCriteriaRequest projectCriteria , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
List < eu . eudat . models . project . Project > dataTable = new ProjectManager ( ) . getCriteria ( this . getApiContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , projectCriteria , this . getApiContext ( ) . getRemoteFetcher ( ) ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
@RequestMapping ( method = RequestMethod . GET , value = { " /external/projects " } , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < ProjectsExternalSourcesModel > > listExternalProjects ( @RequestParam ( value = " query " , required = false ) String query , Principal principal ) {
2018-01-17 13:03:51 +01:00
try {
List < Map < String , String > > remoteRepos = this . getApiContext ( ) . getRemoteFetcher ( ) . getProjects ( query ) ;
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel ( ) . fromExternalItem ( remoteRepos ) ;
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . payload ( projectsExternalSourcesModel ) . status ( HttpStatus . OK ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( NoURLFound ex ) {
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( " External Url Not Found " ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( HugeResultSet ex ) {
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( " Huge Result Set " ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-22 08:41:31 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( HttpStatus . BAD_REQUEST ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-15 11:05:51 +01:00
}