package org.ovirt.engine.core.bll;

import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.action.AttachEntityToTagParameters;
import org.ovirt.engine.core.common.businessentities.LdapGroup;
import org.ovirt.engine.core.common.businessentities.TagsUserGroupMap;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class AttachUserGroupToTagCommand<T extends AttachEntityToTagParameters> extends UserGroupTagMapBase<T> {
    public AttachUserGroupToTagCommand(T parameters) {
        super(parameters);

    }

    @Override
    protected void executeCommand() {
        if (getTagId() != null) {
            for (Guid groupGuid : getGroupList()) {
                LdapGroup group = DbFacade.getInstance().getAdGroupDao().get(groupGuid);
                if (DbFacade.getInstance().getTagDao().getTagUserGroupByGroupIdAndByTagId(getTagId(), groupGuid) == null) {
                    TagsUserGroupMap map = new TagsUserGroupMap(groupGuid, getTagId());
                    DbFacade.getInstance().getTagDao().attachUserGroupToTag(map);
                    noActionDone = false;
                    if (group != null) {
                        AppendCustomValue("AttachGroupsNames", group.getname(), ", ");
                    }
                } else {
                    if (group != null) {
                        AppendCustomValue("AttachGroupsNamesExists", group.getname(), ", ");
                    }
                }
            }
            setSucceeded(true);
        }
    }

    @Override
    public AuditLogType getAuditLogTypeValue() {
        return noActionDone ? AuditLogType.USER_ATTACH_TAG_TO_USER_GROUP_EXISTS
                : getSucceeded() ? AuditLogType.USER_ATTACH_TAG_TO_USER_GROUP
                        : AuditLogType.USER_ATTACH_TAG_TO_USER_GROUP_FAILED;
    }
}
