package eu.eudat.controllers ;
import eu.eudat.entities.Project ;
import eu.eudat.managers.ProjectManager ;
import eu.eudat.models.external.ProjectsExternalSourcesModel ;
import eu.eudat.models.helpers.common.DataTableData ;
import eu.eudat.models.helpers.responses.ResponseItem ;
import eu.eudat.models.project.ProjectCriteriaRequest ;
import eu.eudat.models.project.ProjectListingModel ;
import eu.eudat.models.project.ProjectTableRequest ;
import eu.eudat.models.security.Principal ;
import eu.eudat.proxy.config.exceptions.HugeResultSet ;
import eu.eudat.proxy.config.exceptions.NoURLFound ;
import eu.eudat.services.ApiContext ;
import eu.eudat.types.ApiMessageCode ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.HttpStatus ;
import org.springframework.http.ResponseEntity ;
import org.springframework.web.bind.annotation.* ;
import javax.transaction.Transactional ;
import javax.validation.Valid ;
import java.util.List ;
import java.util.Map ;
@RestController
@CrossOrigin
@RequestMapping ( value = { "/api" } )
public class Projects extends BaseController {
@Autowired
public Projects ( ApiContext apiContext ) {
super ( apiContext ) ;
}
@RequestMapping ( method = RequestMethod . POST , value = { "/projects/getPaged" } , consumes = "application/json" , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < DataTableData < ProjectListingModel > > > getPaged ( @Valid @RequestBody ProjectTableRequest projectTableRequest , Principal principal ) {
try {
DataTableData < eu . eudat . models . project . ProjectListingModel > dataTable = new ProjectManager ( ) . getPaged ( this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , projectTableRequest ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < DataTableData < eu . eudat . models . project . ProjectListingModel > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@RequestMapping ( method = RequestMethod . GET , value = { "/projects/getSingle/{id}" } , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < eu . eudat . models . project . Project > > getSingle ( @PathVariable String id , Principal principal ) {
try {
eu . eudat . models . project . Project project = new ProjectManager ( ) . getSingle ( this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , id ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . payload ( project ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . models . project . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@Transactional
@RequestMapping ( method = RequestMethod . POST , value = { "/projects/createOrUpdate" } , consumes = "application/json" , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < Project > > addProject ( @Valid @RequestBody eu . eudat . models . project . Project project , Principal principal ) {
try {
ProjectManager . createOrUpdate ( this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getUserInfoDao ( ) , project , principal ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . message ( "Created" ) ) ;
} catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@Transactional
@RequestMapping ( method = RequestMethod . DELETE , value = { "/projects/inactivate/{id}" } , consumes = "application/json" , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < Project > > inactivate ( @PathVariable String id , Principal principal ) {
try {
Project project = new ProjectManager ( ) . inactivate ( this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , id ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) ) ;
} catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < eu . eudat . entities . Project > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@RequestMapping ( method = RequestMethod . POST , value = { "/projects/getWithExternal" } , consumes = "application/json" , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < List < eu . eudat . models . project . Project > > > getWithExternal ( @RequestBody ProjectCriteriaRequest projectCriteria , Principal principal ) {
try {
List < eu . eudat . models . project . Project > dataTable = new ProjectManager ( ) . getCriteriaWithExternal ( this . getApiContext ( ) , projectCriteria , this . getApiContext ( ) . getOperationsContext ( ) . getRemoteFetcher ( ) ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@RequestMapping ( method = RequestMethod . POST , value = { "/projects/get" } , consumes = "application/json" , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < List < eu . eudat . models . project . Project > > > get ( @RequestBody ProjectCriteriaRequest projectCriteria , Principal principal ) {
try {
List < eu . eudat . models . project . Project > dataTable = new ProjectManager ( ) . getCriteria ( this . getApiContext ( ) . getOperationsContext ( ) . getDatabaseRepository ( ) . getProjectDao ( ) , projectCriteria , this . getApiContext ( ) . getOperationsContext ( ) . getRemoteFetcher ( ) ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . payload ( dataTable ) . status ( ApiMessageCode . NO_MESSAGE ) ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < eu . eudat . models . project . Project > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
@RequestMapping ( method = RequestMethod . GET , value = { "/external/projects" } , produces = "application/json" )
public @ResponseBody
ResponseEntity < ResponseItem < ProjectsExternalSourcesModel > > listExternalProjects ( @RequestParam ( value = "query" , required = false ) String query , Principal principal ) {
try {
List < Map < String , String > > remoteRepos = this . getApiContext ( ) . getOperationsContext ( ) . getRemoteFetcher ( ) . getProjects ( query ) ;
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel ( ) . fromExternalItem ( remoteRepos ) ;
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . payload ( projectsExternalSourcesModel ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) ) ;
} catch ( NoURLFound ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( "External Url Not Found" ) ) ;
} catch ( HugeResultSet ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( "Huge Result Set" ) ) ;
} catch ( Exception ex ) {
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < ProjectsExternalSourcesModel > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( ex . getMessage ( ) ) ) ;
}
}
}