ItEr41S17CUAsignarUsuarioAProxectoTraballoItEr40S22: implemented a verification to forbid the adition of duplicated authorizations
The existence of a OrderAuthorization with the same User/Profile and OrderAuthorizationType is checked before the addition. In case of duplicated authorizations, the user is informed.
This commit is contained in:
parent
24f4050df5
commit
86be9bf80f
3 changed files with 86 additions and 14 deletions
|
|
@ -246,6 +246,7 @@ public class OrderCRUDController extends GenericForwardComposer {
|
|||
.getFellowIfAny("orderElementAuthorizations");
|
||||
orderAuthorizationController = (OrderAuthorizationController) orderElementAuthorizations
|
||||
.getVariable("orderAuthorizationController", true);
|
||||
orderAuthorizationController.setMessagesForUserComponent(messagesForUser);
|
||||
}
|
||||
|
||||
public List<Order> getOrders() {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@
|
|||
|
||||
package org.navalplanner.web.users;
|
||||
|
||||
import static org.navalplanner.web.I18nHelper._;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -30,6 +32,8 @@ import org.navalplanner.business.users.entities.Profile;
|
|||
import org.navalplanner.business.users.entities.ProfileOrderAuthorization;
|
||||
import org.navalplanner.business.users.entities.User;
|
||||
import org.navalplanner.business.users.entities.UserOrderAuthorization;
|
||||
import org.navalplanner.web.common.IMessagesForUser;
|
||||
import org.navalplanner.web.common.Level;
|
||||
import org.navalplanner.web.common.Util;
|
||||
import org.zkoss.zk.ui.Component;
|
||||
import org.zkoss.zk.ui.util.GenericForwardComposer;
|
||||
|
|
@ -47,6 +51,8 @@ public class OrderAuthorizationController extends GenericForwardComposer{
|
|||
|
||||
private IOrderAuthorizationModel orderAuthorizationModel;
|
||||
|
||||
private IMessagesForUser messagesForUser;
|
||||
|
||||
@Override
|
||||
public void doAfterCompose(Component comp) throws Exception {
|
||||
super.doAfterCompose(comp);
|
||||
|
|
@ -88,12 +94,26 @@ public class OrderAuthorizationController extends GenericForwardComposer{
|
|||
authorizations.add(OrderAuthorizationType.WRITE_AUTHORIZATION);
|
||||
}
|
||||
if (comboItem.getValue() instanceof User) {
|
||||
orderAuthorizationModel.addUserOrderAuthorization(
|
||||
(User)comboItem.getValue(), authorizations);
|
||||
List<OrderAuthorizationType> result =
|
||||
orderAuthorizationModel.addUserOrderAuthorization(
|
||||
(User)comboItem.getValue(), authorizations);
|
||||
if(result != null) {
|
||||
messagesForUser.showMessage(Level.WARNING,
|
||||
_("Cannot add some authorizations for user {0}. " +
|
||||
"Probably they are already present.",
|
||||
((User)comboItem.getValue()).getLoginName()));
|
||||
}
|
||||
}
|
||||
else if (comboItem.getValue() instanceof Profile) {
|
||||
orderAuthorizationModel.addProfileOrderAuthorization(
|
||||
(Profile)comboItem.getValue(), authorizations);
|
||||
List<OrderAuthorizationType> result =
|
||||
orderAuthorizationModel.addProfileOrderAuthorization(
|
||||
(Profile)comboItem.getValue(), authorizations);
|
||||
if(result != null) {
|
||||
messagesForUser.showMessage(Level.WARNING,
|
||||
_("Cannot add some authorizations for profile {0}. " +
|
||||
"Probably they are already present.",
|
||||
((Profile)comboItem.getValue()).getProfileName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Util.reloadBindings(window);
|
||||
|
|
@ -103,4 +123,8 @@ public class OrderAuthorizationController extends GenericForwardComposer{
|
|||
orderAuthorizationModel.removeOrderAuthorization(orderAuthorization);
|
||||
Util.reloadBindings(window);
|
||||
}
|
||||
|
||||
public void setMessagesForUserComponent(IMessagesForUser component) {
|
||||
messagesForUser = component;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,25 +47,43 @@ public class OrderAuthorizationModel implements IOrderAuthorizationModel {
|
|||
@Override
|
||||
public List<OrderAuthorizationType> addProfileOrderAuthorization(
|
||||
Profile profile, List<OrderAuthorizationType> authorizations) {
|
||||
List<OrderAuthorizationType> duplicated =
|
||||
new ArrayList<OrderAuthorizationType>();
|
||||
List<ProfileOrderAuthorization> existingAuthorizations =
|
||||
listAuthorizationsByProfile(profile);
|
||||
for(OrderAuthorizationType type : authorizations) {
|
||||
ProfileOrderAuthorization orderAuthorization =
|
||||
createProfileOrderAuthorization(order, profile);
|
||||
orderAuthorization.setAuthorizationType(type);
|
||||
profileOrderAuthorizationList.add(orderAuthorization);
|
||||
if(listContainsAuthorizationType(existingAuthorizations, type)) {
|
||||
duplicated.add(type);
|
||||
}
|
||||
else {
|
||||
ProfileOrderAuthorization orderAuthorization =
|
||||
createProfileOrderAuthorization(order, profile);
|
||||
orderAuthorization.setAuthorizationType(type);
|
||||
profileOrderAuthorizationList.add(orderAuthorization);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return duplicated.isEmpty()? null : duplicated;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrderAuthorizationType> addUserOrderAuthorization(
|
||||
User user, List<OrderAuthorizationType> authorizations) {
|
||||
List<OrderAuthorizationType> duplicated =
|
||||
new ArrayList<OrderAuthorizationType>();
|
||||
List<UserOrderAuthorization> existingAuthorizations =
|
||||
listAuthorizationsByUser(user);
|
||||
for(OrderAuthorizationType type : authorizations) {
|
||||
UserOrderAuthorization orderAuthorization =
|
||||
createUserOrderAuthorization(order, user);
|
||||
orderAuthorization.setAuthorizationType(type);
|
||||
userOrderAuthorizationList.add(orderAuthorization);
|
||||
if(listContainsAuthorizationType(existingAuthorizations, type)) {
|
||||
duplicated.add(type);
|
||||
}
|
||||
else {
|
||||
UserOrderAuthorization orderAuthorization =
|
||||
createUserOrderAuthorization(order, user);
|
||||
orderAuthorization.setAuthorizationType(type);
|
||||
userOrderAuthorizationList.add(orderAuthorization);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return duplicated.isEmpty()? null : duplicated;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -184,4 +202,33 @@ public class OrderAuthorizationModel implements IOrderAuthorizationModel {
|
|||
this.order = newOrder;
|
||||
}
|
||||
|
||||
private List<UserOrderAuthorization> listAuthorizationsByUser(User user) {
|
||||
List<UserOrderAuthorization> list = new ArrayList<UserOrderAuthorization>();
|
||||
for(UserOrderAuthorization authorization : userOrderAuthorizationList) {
|
||||
if(authorization.getUser().getId().equals(user.getId())) {
|
||||
list.add(authorization);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private List<ProfileOrderAuthorization> listAuthorizationsByProfile(Profile profile){
|
||||
List<ProfileOrderAuthorization> list = new ArrayList<ProfileOrderAuthorization>();
|
||||
for(ProfileOrderAuthorization authorization : profileOrderAuthorizationList) {
|
||||
if(authorization.getProfile().getId().equals(profile.getId())) {
|
||||
list.add(authorization);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private boolean listContainsAuthorizationType(List<? extends OrderAuthorization> list,
|
||||
OrderAuthorizationType authorizationType) {
|
||||
for(OrderAuthorization authorization : list) {
|
||||
if(authorization.getAuthorizationType().equals(authorizationType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue