2017-12-15 11:05:51 +01:00
package eu.eudat.controllers ;
import java.util.List ;
import java.util.Map ;
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 15:09:49 +01:00
import eu.eudat.models.external.ProjectsExternalSourcesModel ;
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 ;
2018-01-23 16:21:38 +01:00
import eu.eudat.types.ApiMessageCode ;
2017-12-15 11:05:51 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
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.entities.Project ;
import eu.eudat.managers.ProjectManager ;
2018-01-25 16:24:21 +01:00
import eu.eudat.models.helpers.common.DataTableData ;
2017-12-15 11:05:51 +01:00
import eu.eudat.models.project.ProjectTableRequest ;
import eu.eudat.proxy.config.exceptions.HugeResultSet ;
import eu.eudat.proxy.config.exceptions.NoURLFound ;
@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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
2017-12-19 09:38:47 +01:00
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . payload ( project ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . message ( " Created " ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . 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-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . payload ( projectsExternalSourcesModel ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( NoURLFound ex ) {
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( " External Url Not Found " ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( HugeResultSet ex ) {
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( " Huge Result Set " ) ) ;
2018-01-17 13:03:51 +01:00
} catch ( Exception ex ) {
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
2018-01-17 13:03:51 +01:00
}
}
2017-12-15 11:05:51 +01:00
}