@Override
 protected boolean validate() {
   boolean returnValue = true;
   Role oldRole = roleDao.get(getRole().getId());
   if (oldRole == null) {
     addValidationMessage(EngineMessage.ERROR_CANNOT_UPDATE_ROLE_ID);
     returnValue = false;
   } else {
     if (checkIfRoleIsReadOnly(getReturnValue().getValidationMessages())) {
       returnValue = false;
       addValidationMessage(EngineMessage.VAR__ACTION__UPDATE);
     } else if (!StringUtils.equals(getRole().getName(), oldRole.getName())
         && roleDao.getByName(getRole().getName()) != null) {
       addValidationMessage(EngineMessage.ACTION_TYPE_FAILED_NAME_ALREADY_USED);
       returnValue = false;
     } // changing role type isn't allowed
     else if (getRole().getType() != oldRole.getType()) {
       addValidationMessage(EngineMessage.ERROR_CANNOT_UPDATE_ROLE_TYPE);
       returnValue = false;
     }
   }
   if (!returnValue) {
     addValidationMessage(EngineMessage.VAR__TYPE__ROLE);
     addValidationMessage(EngineMessage.VAR__ACTION__UPDATE);
   }
   return returnValue;
 }
 private void updateAdminStatus(Permissions perm) {
   // if the role of the permission is of type admin update the user
   // lastAdminCheckStatus to true
   Role role = getRoleDao().get(perm.getrole_id());
   if (role.getType() == RoleType.ADMIN) {
     MultiLevelAdministrationHandler.setIsAdminGUIFlag(perm.getad_element_id(), true);
   }
 }
  @Override
  protected boolean canDoAction() {
    Permissions perm = getParameters().getPermission();
    if (perm == null) {
      addCanDoActionMessage(VdcBllMessages.PERMISSION_ADD_FAILED_PERMISSION_NOT_SENT);
      return false;
    }

    Role role = getRoleDao().get(perm.getrole_id());
    Guid adElementId = perm.getad_element_id();

    if (role == null) {
      addCanDoActionMessage(VdcBllMessages.PERMISSION_ADD_FAILED_INVALID_ROLE_ID);
      return false;
    }

    if (perm.getObjectType() == null || getVdcObjectName() == null) {
      addCanDoActionMessage(VdcBllMessages.PERMISSION_ADD_FAILED_INVALID_OBJECT_ID);
      return false;
    }

    // if user and group not sent check user/group is in the db in order to
    // give permission
    if (getParameters().getUser() == null
        && getParameters().getGroup() == null
        && getDbUserDAO().get(adElementId) == null
        && getAdGroupDAO().get(adElementId) == null) {
      getReturnValue()
          .getCanDoActionMessages()
          .add(VdcBllMessages.USER_MUST_EXIST_IN_DB.toString());
      return false;
    }

    // only system super user can give permissions with admin roles
    if (!isSystemSuperUser() && role.getType() == RoleType.ADMIN) {
      addCanDoActionMessage(
          VdcBllMessages.PERMISSION_ADD_FAILED_ONLY_SYSTEM_SUPER_USER_CAN_GIVE_ADMIN_ROLES);
      return false;
    }

    // don't allow adding permissions to vms from pool externally
    if (!isInternalExecution() && perm.getObjectType() == VdcObjectType.VM) {
      VM vm = getVmDAO().get(perm.getObjectId());
      if (vm != null && vm.getVmPoolId() != null) {
        addCanDoActionMessage(VdcBllMessages.PERMISSION_ADD_FAILED_VM_IN_POOL);
        return false;
      }
    }

    return true;
  }
  private void onAttach() {
    AdElementListModel model = (AdElementListModel) getWindow();

    if (model.getProgress() != null) {
      return;
    }

    if (model.getSelectedItems() == null) {
      cancel();
      return;
    }

    ArrayList<DbUser> items = new ArrayList<DbUser>();
    for (Object item : model.getItems()) {
      EntityModel entityModel = (EntityModel) item;
      if (entityModel.getIsSelected()) {
        items.add((DbUser) entityModel.getEntity());
      }
    }

    Role role = model.getRole().getSelectedItem();

    ArrayList<VdcActionParametersBase> list = new ArrayList<VdcActionParametersBase>();
    for (DbUser user : items) {
      Permission perm = new Permission(user.getId(), role.getId(), null, null);

      if (user.isGroup()) {
        DbGroup group = new DbGroup();
        group.setId(user.getId());
        group.setName(user.getFirstName());
        group.setDomain(user.getDomain());
        group.setExternalId(user.getExternalId());
        group.setNamespace(user.getNamespace());
        PermissionsOperationsParameters tempVar2 = new PermissionsOperationsParameters();
        tempVar2.setPermission(perm);
        tempVar2.setGroup(group);
        list.add(tempVar2);
      } else {
        PermissionsOperationsParameters tempVar3 = new PermissionsOperationsParameters();
        tempVar3.setPermission(perm);
        tempVar3.setUser(user);
        list.add(tempVar3);
      }
    }

    model.startProgress(null);

    Frontend.getInstance()
        .runMultipleAction(
            VdcActionType.AddSystemPermission,
            list,
            new IFrontendMultipleActionAsyncCallback() {
              @Override
              public void executed(FrontendMultipleActionAsyncResult result) {

                AdElementListModel localModel = (AdElementListModel) result.getState();
                localModel.stopProgress();
                cancel();
              }
            },
            model);
  }
  private void onAdd() {
    AdElementListModel model = (AdElementListModel) getWindow();

    if (model.getProgress() != null) {
      return;
    }

    if (!model.getIsEveryoneSelected() && model.getSelectedItems() == null) {
      cancel();
      return;
    }

    ArrayList<DbUser> items = new ArrayList<DbUser>();
    if (model.getIsEveryoneSelected()) {
      DbUser tempVar = new DbUser();
      tempVar.setId(ApplicationGuids.everyone.asGuid());
      items.add(tempVar);
    } else {
      for (Object item : model.getItems()) {
        EntityModel entityModel = (EntityModel) item;
        if (entityModel.getIsSelected()) {
          items.add((DbUser) entityModel.getEntity());
        }
      }
    }

    Role role = (Role) model.getRole().getSelectedItem();
    // adGroup/user

    ArrayList<VdcActionParametersBase> list = new ArrayList<VdcActionParametersBase>();
    for (DbUser user : items) {
      Permissions tempVar2 = new Permissions();
      tempVar2.setad_element_id(user.getId());
      tempVar2.setrole_id(role.getId());
      Permissions perm = tempVar2;
      perm.setObjectId(getEntityGuid());
      perm.setObjectType(this.getObjectType());

      if (user.isGroup()) {
        DbGroup group = new DbGroup();
        group.setId(user.getId());
        group.setExternalId(user.getExternalId());
        group.setName(user.getFirstName());
        group.setDomain(user.getDomain());
        group.setNamespace(user.getNamespace());
        PermissionsOperationsParameters tempVar3 = new PermissionsOperationsParameters();
        tempVar3.setPermission(perm);
        tempVar3.setGroup(group);
        list.add(tempVar3);
      } else {
        PermissionsOperationsParameters tempVar4 = new PermissionsOperationsParameters();
        tempVar4.setPermission(perm);
        tempVar4.setUser(user);
        list.add(tempVar4);
      }
    }

    model.startProgress(null);

    Frontend.getInstance()
        .runMultipleAction(
            VdcActionType.AddPermission,
            list,
            new IFrontendMultipleActionAsyncCallback() {
              @Override
              public void executed(FrontendMultipleActionAsyncResult result) {

                AdElementListModel localModel = (AdElementListModel) result.getState();
                localModel.stopProgress();
                cancel();
              }
            },
            model);
  }