<commit_before>package org.ovirt.engine.core.bll;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.EventNotificationMethod;
import org.ovirt.engine.core.common.action.EventSubscriptionParametesBase;
import org.ovirt.engine.core.common.businessentities.EventSubscriber;
import org.ovirt.engine.core.common.businessentities.aaa.DbUser;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class AddEventSubscriptionCommand<T extends EventSubscriptionParametesBase> extends
        EventSubscriptionCommandBase<T> {
    public AddEventSubscriptionCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected boolean canDoAction() {
        boolean retValue;
        // check if user is not already subscribed to this event with same
        // method and address
        Guid subscriberId = getParameters().getEventSubscriber().getSubscriberId();
        String eventName = getParameters().getEventSubscriber().getEventUpName();
        EventNotificationMethod eventNotificationMethod =
                getParameters().getEventSubscriber().getEventNotificationMethod();
        List<EventSubscriber> subscriptions = DbFacade.getInstance()
                .getEventDao().getAllForSubscriber(subscriberId);
        if (isAlreadySubscribed(subscriptions, subscriberId, eventName, eventNotificationMethod)) {
            addCanDoActionMessage(EngineMessage.EN_ALREADY_SUBSCRIBED);
            retValue = false;
        } else if (!eventExists(eventName)) {
            addCanDoActionMessage(EngineMessage.EN_UNSUPPORTED_NOTIFICATION_EVENT);
            retValue = false;
        } else {
            // get notification method
            if (eventNotificationMethod != null) {
                // Validate user
                DbUser user = DbFacade.getInstance().getDbUserDao().get(subscriberId);
                if (user == null) {
                    addCanDoActionMessage(EngineMessage.USER_MUST_EXIST_IN_DB);
                    retValue = false;
                } else {
                    retValue = validateAdd(eventNotificationMethod, getParameters().getEventSubscriber(), user);
                }
            } else {
                addCanDoActionMessage(EngineMessage.EN_UNKNOWN_NOTIFICATION_METHOD);
                retValue = false;
            }
        }
        return retValue;
    }

    private boolean eventExists(String eventName) {
        boolean exists = false;
        try {
            AuditLogType.valueOf(eventName);
            exists = true;
        } catch (Exception ex) {
        }
        return exists;
    }

    /**
     * Determines whether [is already subscribed] [the specified subscriptions].
     *
     * @param subscriptions           The subscriptions.
     * @param subscriberId            The subscriber id.
     * @param eventName               Name of the event.
     * @param eventNotificationMethod The notification method.
     * @return <c>true</c> if [is already subscribed] [the specified
     * subscriptions]; otherwise, <c>false</c>.
     */
    private static boolean isAlreadySubscribed(Iterable<EventSubscriber> subscriptions, Guid subscriberId,
            String eventName, EventNotificationMethod eventNotificationMethod) {
        boolean retval = false;
        for (EventSubscriber eventSubscriber : subscriptions) {
            if (subscriberId.equals(eventSubscriber.getSubscriberId())
                    && StringUtils.equals(eventSubscriber.getEventUpName(), eventName)
                    && eventSubscriber.getEventNotificationMethod() == eventNotificationMethod) {
                retval = true;
                break;
            }
        }
        return retval;
    }

    @Override
    protected void executeCommand() {
        if (getParameters().getEventSubscriber().getTagName() == null) {<commit_msg>core:java8 stream API in AddEventSubscription...<commit_after>package org.ovirt.engine.core.bll;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.common.AuditLogType;
import org.ovirt.engine.core.common.EventNotificationMethod;
import org.ovirt.engine.core.common.action.EventSubscriptionParametesBase;
import org.ovirt.engine.core.common.businessentities.EventSubscriber;
import org.ovirt.engine.core.common.businessentities.aaa.DbUser;
import org.ovirt.engine.core.common.errors.EngineMessage;
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;

public class AddEventSubscriptionCommand<T extends EventSubscriptionParametesBase> extends
        EventSubscriptionCommandBase<T> {
    public AddEventSubscriptionCommand(T parameters) {
        super(parameters);
    }

    @Override
    protected boolean canDoAction() {
        boolean retValue;
        // check if user is not already subscribed to this event with same
        // method and address
        Guid subscriberId = getParameters().getEventSubscriber().getSubscriberId();
        String eventName = getParameters().getEventSubscriber().getEventUpName();
        EventNotificationMethod eventNotificationMethod =
                getParameters().getEventSubscriber().getEventNotificationMethod();
        List<EventSubscriber> subscriptions = DbFacade.getInstance()
                .getEventDao().getAllForSubscriber(subscriberId);
        if (isAlreadySubscribed(subscriptions, subscriberId, eventName, eventNotificationMethod)) {
            addCanDoActionMessage(EngineMessage.EN_ALREADY_SUBSCRIBED);
            retValue = false;
        } else if (!eventExists(eventName)) {
            addCanDoActionMessage(EngineMessage.EN_UNSUPPORTED_NOTIFICATION_EVENT);
            retValue = false;
        } else {
            // get notification method
            if (eventNotificationMethod != null) {
                // Validate user
                DbUser user = DbFacade.getInstance().getDbUserDao().get(subscriberId);
                if (user == null) {
                    addCanDoActionMessage(EngineMessage.USER_MUST_EXIST_IN_DB);
                    retValue = false;
                } else {
                    retValue = validateAdd(eventNotificationMethod, getParameters().getEventSubscriber(), user);
                }
            } else {
                addCanDoActionMessage(EngineMessage.EN_UNKNOWN_NOTIFICATION_METHOD);
                retValue = false;
            }
        }
        return retValue;
    }

    private boolean eventExists(String eventName) {
        boolean exists = false;
        try {
            AuditLogType.valueOf(eventName);
            exists = true;
        } catch (Exception ex) {
        }
        return exists;
    }

    /**
     * Determines whether [is already subscribed] [the specified subscriptions].
     *
     * @param subscriptions           The subscriptions.
     * @param subscriberId            The subscriber id.
     * @param eventName               Name of the event.
     * @param eventNotificationMethod The notification method.
     * @return <c>true</c> if [is already subscribed] [the specified
     * subscriptions]; otherwise, <c>false</c>.
     */
    private static boolean isAlreadySubscribed(List<EventSubscriber> subscriptions, Guid subscriberId,
            String eventName, EventNotificationMethod eventNotificationMethod) {
        return subscriptions.stream()
                .anyMatch(subscription -> subscriberId.equals(subscription.getSubscriberId())
                    && StringUtils.equals(subscription.getEventUpName(), eventName)
                    && subscription.getEventNotificationMethod() == eventNotificationMethod);
    }

    @Override
    protected void executeCommand() {
        if (getParameters().getEventSubscriber().getTagName() == null) {
{'fraction_non_alphanumeric': 0.08709921736935117, 'fraction_numerical': 0.00012623074981065388, 'mean_word_length': 2.629409070087036, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3234045', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1982, 'n_words': 396}