2018-01-05 08:47:52 +01:00
package eu.eudat.controllers ;
2018-02-07 10:56:30 +01:00
import eu.eudat.exceptions.security.UnauthorisedException ;
2018-01-05 08:47:52 +01:00
import eu.eudat.managers.InvitationsManager ;
import eu.eudat.models.helpers.responses.ResponseItem ;
import eu.eudat.models.invitation.Invitation ;
2018-01-05 16:40:19 +01:00
import eu.eudat.models.security.Principal ;
2018-01-05 08:47:52 +01:00
import eu.eudat.models.userinfo.UserInfoInvitationModel ;
import eu.eudat.models.userinfo.UserInfoRequestItem ;
import eu.eudat.services.ApiContext ;
2018-01-23 16:21:38 +01:00
import eu.eudat.types.ApiMessageCode ;
2018-01-05 08:47:52 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
2018-01-05 16:40:19 +01:00
import org.springframework.http.HttpStatus ;
2018-01-22 08:41:31 +01:00
import org.springframework.http.ResponseEntity ;
2018-01-05 16:40:19 +01:00
import org.springframework.transaction.annotation.Transactional ;
2018-01-05 08:47:52 +01:00
import org.springframework.web.bind.annotation.* ;
import java.util.List ;
2018-01-08 12:44:48 +01:00
import java.util.UUID ;
2018-01-05 08:47:52 +01:00
2018-02-01 10:08:06 +01:00
2018-01-05 08:47:52 +01:00
@RequestMapping ( " invite/ " )
@RestController
@CrossOrigin
public class UserInvitationController extends BaseController {
@Autowired
public UserInvitationController ( ApiContext apiContext ) {
super ( apiContext ) ;
}
2018-01-05 16:40:19 +01:00
@Transactional
2018-01-05 08:47:52 +01:00
@RequestMapping ( method = RequestMethod . POST , value = { " /users " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < Invitation > > users ( @RequestBody Invitation invitation , Principal principal ) {
2018-01-05 08:47:52 +01:00
try {
2018-01-05 16:40:19 +01:00
InvitationsManager . inviteUsers ( this . getApiContext ( ) , invitation , principal ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < Invitation > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . message ( " Users have beeen invited " ) ) ;
2018-01-05 08:47:52 +01:00
} catch ( Exception e ) {
2018-01-05 16:40:19 +01:00
e . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < Invitation > ( ) . status ( ApiMessageCode . ERROR_MESSAGE ) . message ( e . getMessage ( ) ) ) ;
2018-01-05 08:47:52 +01:00
}
}
2018-01-08 12:44:48 +01:00
@Transactional
@RequestMapping ( method = RequestMethod . GET , value = { " /exchange/{invitationID} " } , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < UUID > > exchange ( @PathVariable UUID invitationID , Principal principal ) {
2018-01-08 12:44:48 +01:00
try {
UUID dmpId = InvitationsManager . assignUserAcceptedInvitation ( this . getApiContext ( ) , invitationID , principal ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < UUID > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . payload ( dmpId ) ) ;
2018-01-08 15:57:21 +01:00
} catch ( UnauthorisedException e ) {
e . printStackTrace ( ) ;
throw e ;
}
catch ( Exception e ) {
2018-01-08 12:44:48 +01:00
e . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < UUID > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . message ( e . getMessage ( ) ) ) ;
2018-01-08 12:44:48 +01:00
}
}
2018-01-05 08:47:52 +01:00
@RequestMapping ( method = RequestMethod . POST , value = { " /getUsers " } , consumes = " application/json " , produces = " application/json " )
2018-01-22 08:41:31 +01:00
public @ResponseBody ResponseEntity < ResponseItem < List < UserInfoInvitationModel > > > getUsers ( @RequestBody UserInfoRequestItem userInfoRequestItem ) {
2018-01-05 08:47:52 +01:00
try {
List < UserInfoInvitationModel > users = InvitationsManager . getUsers ( this . getApiContext ( ) , userInfoRequestItem ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . OK ) . body ( new ResponseItem < List < UserInfoInvitationModel > > ( ) . status ( ApiMessageCode . SUCCESS_MESSAGE ) . payload ( users ) ) ;
2018-01-05 08:47:52 +01:00
} catch ( Exception e ) {
2018-01-05 16:40:19 +01:00
e . printStackTrace ( ) ;
2018-01-23 16:21:38 +01:00
return ResponseEntity . status ( HttpStatus . BAD_REQUEST ) . body ( new ResponseItem < List < UserInfoInvitationModel > > ( ) . status ( ApiMessageCode . DEFAULT_ERROR_MESSAGE ) . message ( e . getMessage ( ) ) ) ;
2018-01-05 08:47:52 +01:00
}
}
}