@@ -1210,44 +1210,44 @@ public boolean updateInvitation(final long projectId, String accountName, Long u
12101210 throw new InvalidParameterValueException ("Invitation is expired for account id=" + accountName + " to the project id=" + projectId );
12111211 } else {
12121212 final ProjectInvitationVO inviteFinal = invite ;
1213- final Long accountIdFinal = accountId ;
1213+ final Long accountIdFinal = invite . getAccountId () != - 1 ? invite . getAccountId () : accountId ;
12141214 final String accountNameFinal = accountName ;
1215- final User finalUser = user ;
1216- ProjectInvitationVO finalInvite = invite ;
1215+ final User finalUser = getFinalUser (user , invite );
12171216 result = Transaction .execute (new TransactionCallback <Boolean >() {
12181217 @ Override
12191218 public Boolean doInTransaction (TransactionStatus status ) {
12201219 boolean result = true ;
12211220
1222- ProjectInvitation .State newState = accept ? ProjectInvitation .State .Completed : ProjectInvitation .State .Declined ;
1223-
1224- //update invitation
1225- s_logger .debug ("Marking invitation " + inviteFinal + " with state " + newState );
1226- inviteFinal .setState (newState );
1227- result = _projectInvitationDao .update (inviteFinal .getId (), inviteFinal );
1228-
1229- if (result && accept ) {
1230- //check if account already exists for the project (was added before invitation got accepted)
1231- if (finalInvite .getForUserId () == -1 ) {
1232- ProjectAccount projectAccount = _projectAccountDao .findByProjectIdAccountId (projectId , accountIdFinal );
1233- if (projectAccount != null ) {
1234- s_logger .debug ("Account " + accountNameFinal + " already added to the project id=" + projectId );
1235- } else {
1236- assignAccountToProject (project , accountIdFinal , finalInvite .getAccountRole (), null , finalInvite .getProjectRoleId ());
1237- }
1238- } else {
1239- ProjectAccount projectAccount = _projectAccountDao .findByProjectIdUserId (projectId , finalUser .getAccountId (), finalUser .getId ());
1240- if (projectAccount != null ) {
1241- s_logger .debug ("User " + finalUser .getId () + "has already been added to the project id=" + projectId );
1221+ ProjectInvitation .State newState = accept ? ProjectInvitation .State .Completed : ProjectInvitation .State .Declined ;
1222+
1223+ //update invitation
1224+ s_logger .debug ("Marking invitation " + inviteFinal + " with state " + newState );
1225+ inviteFinal .setState (newState );
1226+ result = _projectInvitationDao .update (inviteFinal .getId (), inviteFinal );
1227+
1228+ if (result && accept ) {
1229+ //check if account already exists for the project (was added before invitation got accepted)
1230+ if (inviteFinal .getForUserId () == -1 ) {
1231+ ProjectAccount projectAccount = _projectAccountDao .findByProjectIdAccountId (projectId , accountIdFinal );
1232+ if (projectAccount != null ) {
1233+ s_logger .debug ("Account " + accountNameFinal + " already added to the project id=" + projectId );
1234+ } else {
1235+ assignAccountToProject (project , accountIdFinal , inviteFinal .getAccountRole (), null , inviteFinal .getProjectRoleId ());
1236+ }
1237+ } else {
1238+ ProjectAccount projectAccount = _projectAccountDao .findByProjectIdUserId (projectId , finalUser .getAccountId (), finalUser .getId ());
1239+ if (projectAccount != null ) {
1240+ s_logger .debug ("User " + finalUser .getId () + "has already been added to the project id=" + projectId );
1241+ } else {
1242+ assignUserToProject (project , inviteFinal .getForUserId (), finalUser .getAccountId (), inviteFinal .getAccountRole (), inviteFinal .getProjectRoleId ());
1243+ }
1244+ }
12421245 } else {
1243- assignUserToProject ( project , finalInvite . getForUserId (), finalUser . getAccountId (), finalInvite . getAccountRole (), finalInvite . getProjectRoleId () );
1246+ s_logger . warn ( "Failed to update project invitation " + inviteFinal + " with state " + newState );
12441247 }
1248+ return result ;
12451249 }
1246- } else {
1247- s_logger .warn ("Failed to update project invitation " + inviteFinal + " with state " + newState );
1248- }
1249- return result ;
1250- }});
1250+ });
12511251 }
12521252 } else {
12531253 throw new InvalidParameterValueException ("Unable to find invitation for account name=" + accountName + " to the project id=" + projectId );
@@ -1256,6 +1256,14 @@ public Boolean doInTransaction(TransactionStatus status) {
12561256 return result ;
12571257 }
12581258
1259+ private User getFinalUser (User user , ProjectInvitationVO invite ) {
1260+ User returnedUser = user ;
1261+ if (invite .getForUserId () != -1 && invite .getForUserId () != user .getId ()) {
1262+ returnedUser = userDao .getUser (invite .getForUserId ());
1263+ }
1264+ return returnedUser ;
1265+ }
1266+
12591267 @ Override
12601268 public List <Long > listPermittedProjectAccounts (long accountId ) {
12611269 return _projectAccountDao .listPermittedAccountIds (accountId );
0 commit comments