package org.ovirt.engine.core.bll;

import java.util.Collections;
import java.util.List;

import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.PermissionSubject;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.QuotaCRUDParameters;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
import org.ovirt.engine.core.dao.QuotaDAO;

public class UpdateQuotaCommand<T extends QuotaCRUDParameters> extends CommandBase<T> {

    /**
     * Generated serialization UUID.
     */
    private static final long serialVersionUID = 8037593564998496651L;

    public UpdateQuotaCommand(T parameters) {
        super(parameters);
        setStoragePoolId(getParameters().getQuota() != null ? getParameters().getQuota().getStoragePoolId() : null);
        setQuota(getParameters().getQuota());
    }

    @Override
    protected boolean canDoAction() {
        if (!QuotaHelper.getInstance().checkQuotaValidationForAddEdit(getParameters().getQuota(),
                getReturnValue().getCanDoActionMessages())) {
            return false;
        } else if (getParameters().getQuota().getId() == null) {
            addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_IS_NOT_VALID);
            return false;
        } else if (getQuotaDAO().getById(getParameters().getQuota().getId()) == null) {
            addCanDoActionMessage(VdcBllMessages.ACTION_TYPE_FAILED_QUOTA_NOT_EXIST);
            return false;
        }
        return true;
    }

    @Override
    protected void executeCommand() {
        QuotaDAO dao = DbFacade.getInstance().getQuotaDAO();
        dao.update(getParameters().getQuota());
        getReturnValue().setSucceeded(true);
    }

    @Override
    public List<PermissionSubject> getPermissionCheckSubjects() {
        return Collections.singletonList(new PermissionSubject(getQuotaId() == null ? null
                : getQuotaId().getValue(),
                VdcObjectType.Quota, getActionType().getActionGroup()));
    }

    protected void setActionMessageParameters() {
        addCanDoActionMessage(VdcBllMessages.VAR__ACTION__UPDATE);
        addCanDoActionMessage(VdcBllMessages.VAR__TYPE__QUOTA);
    }

    @Override
    public AuditLogType getAuditLogTypeValue() {
          return getSucceeded() ? AuditLogType.USER_UPDATE_QUOTA : AuditLogType.USER_FAILED_UPDATE_QUOTA;
    }
}

@@ -303,16 +303,253 @@
 meters;%0A
+import org.ovirt.engine.core.common.businessentities.Quota;%0Aimport org.ovirt.engine.core.common.businessentities.QuotaStorage;%0Aimport org.ovirt.engine.core.common.businessentities.QuotaVdsGroup;%0Aimport org.ovirt.engine.core.compat.Guid;%0A
 import o
@@ -589,16 +589,16 @@
 ssages;%0A
-
 import o
@@ -1846,24 +1846,53 @@
 Command() %7B%0A
+        setQuotaParameter();%0A
         Quot
@@ -2581,24 +2581,24 @@
 peValue() %7B%0A
-
           re
@@ -2696,11 +2696,934 @@
 A;%0A    %7D
+%0A%0A    /**%0A     * Set quota from the parameter%0A     *%0A     * @param parameters%0A     * @return%0A     */%0A    private void setQuotaParameter() %7B%0A        Quota quotaParameter = getParameters().getQuota();%0A        setStoragePoolId(quotaParameter.getStoragePoolId());%0A        setQuotaName(quotaParameter.getQuotaName());%0A        if (quotaParameter.getQuotaStorages() != null) %7B%0A            for (QuotaStorage quotaStorage : quotaParameter.getQuotaStorages()) %7B%0A                quotaStorage.setQuotaId(getQuotaId());%0A                quotaStorage.setQuotaStorageId(Guid.NewGuid());%0A            %7D%0A        %7D%0A        if (quotaParameter.getQuotaVdsGroups() != null) %7B%0A            for (QuotaVdsGroup quotaVdsGroup : quotaParameter.getQuotaVdsGroups()) %7B%0A                quotaVdsGroup.setQuotaId(getQuotaId());%0A                quotaVdsGroup.setQuotaVdsGroupId(Guid.NewGuid());%0A            %7D%0A        %7D%0A        setQuota(quotaParameter);%0A    %7D
 %0A%7D%0A

Fix quota update command