<commit_before>/*
 * Copyright 2010-2012 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.billing.analytics;

import javax.inject.Inject;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ning.billing.account.api.Account;
import com.ning.billing.account.api.AccountApiException;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.analytics.dao.BusinessAccountTagSqlDao;
import com.ning.billing.analytics.dao.BusinessInvoicePaymentTagSqlDao;
import com.ning.billing.analytics.dao.BusinessInvoiceTagSqlDao;
import com.ning.billing.analytics.dao.BusinessSubscriptionTransitionTagSqlDao;
import com.ning.billing.entitlement.api.user.EntitlementUserApi;
import com.ning.billing.entitlement.api.user.EntitlementUserApiException;
import com.ning.billing.entitlement.api.user.SubscriptionBundle;
import com.ning.billing.util.dao.ObjectType;

public class BusinessTagRecorder {
    private static final Logger log = LoggerFactory.getLogger(BusinessTagRecorder.class);

    private final BusinessAccountTagSqlDao accountTagSqlDao;
    private final BusinessInvoiceTagSqlDao invoiceTagSqlDao;
    private final BusinessInvoicePaymentTagSqlDao invoicePaymentTagSqlDao;
    private final BusinessSubscriptionTransitionTagSqlDao subscriptionTransitionTagSqlDao;
    private final AccountUserApi accountApi;
    private final EntitlementUserApi entitlementUserApi;

    @Inject
    public BusinessTagRecorder(final BusinessAccountTagSqlDao accountTagSqlDao,
                               final BusinessInvoicePaymentTagSqlDao invoicePaymentTagSqlDao,
                               final BusinessInvoiceTagSqlDao invoiceTagSqlDao,
                               final BusinessSubscriptionTransitionTagSqlDao subscriptionTransitionTagSqlDao,
                               final AccountUserApi accountApi,
                               final EntitlementUserApi entitlementUserApi) {
        this.accountTagSqlDao = accountTagSqlDao;
        this.invoicePaymentTagSqlDao = invoicePaymentTagSqlDao;
        this.invoiceTagSqlDao = invoiceTagSqlDao;
        this.subscriptionTransitionTagSqlDao = subscriptionTransitionTagSqlDao;
        this.accountApi = accountApi;
        this.entitlementUserApi = entitlementUserApi;
    }

    public void tagAdded(final ObjectType objectType, final UUID objectId, final String name) {
        if (objectType.equals(ObjectType.ACCOUNT)) {
            tagAddedForAccount(objectId, name);
        } else if (objectType.equals(ObjectType.BUNDLE)) {
            tagAddedForBundle(objectId, name);
        } else if (objectType.equals(ObjectType.INVOICE)) {
            tagAddedForInvoice(objectId, name);
        } else if (objectType.equals(ObjectType.PAYMENT)) {
            tagAddedForPayment(objectId, name);
        } else {
            log.info("Ignoring tag addition of {} for object id {} (type {})", new Object[]{name, objectId.toString(), objectType.toString()});
        }
    }

    public void tagRemoved(final ObjectType objectType, final UUID objectId, final String name) {
        if (objectType.equals(ObjectType.ACCOUNT)) {
            tagRemovedForAccount(objectId, name);
        } else if (objectType.equals(ObjectType.BUNDLE)) {
            tagRemovedForBundle(objectId, name);
        } else if (objectType.equals(ObjectType.INVOICE)) {
            tagRemovedForInvoice(objectId, name);
        } else if (objectType.equals(ObjectType.PAYMENT)) {
            tagRemovedForPayment(objectId, name);
        } else {
            log.info("Ignoring tag removal of {} for object id {} (type {})", new Object[]{name, objectId.toString(), objectType.toString()});
        }
    }

    private void tagAddedForAccount(final UUID objectId, final String name) {
        final Account account;
        try {
            account = accountApi.getAccountById(objectId);
        } catch (AccountApiException e) {
            log.warn("Ignoring tag addition of {} for account id {} (account does not exist)", name, objectId.toString());
            return;
        }

        final String accountKey = account.getExternalKey();
        accountTagSqlDao.addTag(accountKey, name);
    }

    private void tagRemovedForAccount(final UUID objectId, final String name) {
        final Account account;
        try {
            account = accountApi.getAccountById(objectId);
        } catch (AccountApiException e) {
            log.warn("Ignoring tag removal of {} for account id {} (account does not exist)", name, objectId.toString());
            return;
        }

        final String accountKey = account.getExternalKey();
        accountTagSqlDao.removeTag(accountKey, name);
    }

    private void tagAddedForBundle(final UUID objectId, final String name) {
        final SubscriptionBundle bundle;
        try {
            bundle = entitlementUserApi.getBundleFromId(objectId);
        } catch (EntitlementUserApiException e) {
            log.warn("Ignoring tag addition of {} for bundle id {} (bundle does not exist)", name, objectId.toString());
            return;
        }

        // TODO - this stores tags for bundles?
        final String externalKey = bundle.getKey();
        subscriptionTransitionTagSqlDao.addTag(externalKey, name);
    }

    private void tagRemovedForBundle(final UUID objectId, final String name) {
        final SubscriptionBundle bundle;
        try {
            bundle = entitlementUserApi.getBundleFromId(objectId);
        } catch (EntitlementUserApiException e) {
            log.warn("Ignoring tag removal of {} for bundle id {} (bundle does not exist)", name, objectId.toString());
            return;
        }

        // TODO - this stores tags for bundles?
        final String externalKey = bundle.getKey();
        subscriptionTransitionTagSqlDao.removeTag(externalKey, name);
    }

    private void tagAddedForInvoice(final UUID objectId, final String name) {
        invoiceTagSqlDao.addTag(objectId.toString(), name);
    }

    private void tagRemovedForInvoice(final UUID objectId, final String name) {
        invoiceTagSqlDao.removeTag(objectId.toString(), name);
    }

    private void tagAddedForPayment(final UUID objectId, final String name) {
        invoicePaymentTagSqlDao.addTag(objectId.toString(), name);
    }
<commit_msg>analytics: update comment in BusinessTagRecorder<commit_after>/*
 * Copyright 2010-2012 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.billing.analytics;

import javax.inject.Inject;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ning.billing.account.api.Account;
import com.ning.billing.account.api.AccountApiException;
import com.ning.billing.account.api.AccountUserApi;
import com.ning.billing.analytics.dao.BusinessAccountTagSqlDao;
import com.ning.billing.analytics.dao.BusinessInvoicePaymentTagSqlDao;
import com.ning.billing.analytics.dao.BusinessInvoiceTagSqlDao;
import com.ning.billing.analytics.dao.BusinessSubscriptionTransitionTagSqlDao;
import com.ning.billing.entitlement.api.user.EntitlementUserApi;
import com.ning.billing.entitlement.api.user.EntitlementUserApiException;
import com.ning.billing.entitlement.api.user.SubscriptionBundle;
import com.ning.billing.util.dao.ObjectType;

public class BusinessTagRecorder {
    private static final Logger log = LoggerFactory.getLogger(BusinessTagRecorder.class);

    private final BusinessAccountTagSqlDao accountTagSqlDao;
    private final BusinessInvoiceTagSqlDao invoiceTagSqlDao;
    private final BusinessInvoicePaymentTagSqlDao invoicePaymentTagSqlDao;
    private final BusinessSubscriptionTransitionTagSqlDao subscriptionTransitionTagSqlDao;
    private final AccountUserApi accountApi;
    private final EntitlementUserApi entitlementUserApi;

    @Inject
    public BusinessTagRecorder(final BusinessAccountTagSqlDao accountTagSqlDao,
                               final BusinessInvoicePaymentTagSqlDao invoicePaymentTagSqlDao,
                               final BusinessInvoiceTagSqlDao invoiceTagSqlDao,
                               final BusinessSubscriptionTransitionTagSqlDao subscriptionTransitionTagSqlDao,
                               final AccountUserApi accountApi,
                               final EntitlementUserApi entitlementUserApi) {
        this.accountTagSqlDao = accountTagSqlDao;
        this.invoicePaymentTagSqlDao = invoicePaymentTagSqlDao;
        this.invoiceTagSqlDao = invoiceTagSqlDao;
        this.subscriptionTransitionTagSqlDao = subscriptionTransitionTagSqlDao;
        this.accountApi = accountApi;
        this.entitlementUserApi = entitlementUserApi;
    }

    public void tagAdded(final ObjectType objectType, final UUID objectId, final String name) {
        if (objectType.equals(ObjectType.ACCOUNT)) {
            tagAddedForAccount(objectId, name);
        } else if (objectType.equals(ObjectType.BUNDLE)) {
            tagAddedForBundle(objectId, name);
        } else if (objectType.equals(ObjectType.INVOICE)) {
            tagAddedForInvoice(objectId, name);
        } else if (objectType.equals(ObjectType.PAYMENT)) {
            tagAddedForPayment(objectId, name);
        } else {
            log.info("Ignoring tag addition of {} for object id {} (type {})", new Object[]{name, objectId.toString(), objectType.toString()});
        }
    }

    public void tagRemoved(final ObjectType objectType, final UUID objectId, final String name) {
        if (objectType.equals(ObjectType.ACCOUNT)) {
            tagRemovedForAccount(objectId, name);
        } else if (objectType.equals(ObjectType.BUNDLE)) {
            tagRemovedForBundle(objectId, name);
        } else if (objectType.equals(ObjectType.INVOICE)) {
            tagRemovedForInvoice(objectId, name);
        } else if (objectType.equals(ObjectType.PAYMENT)) {
            tagRemovedForPayment(objectId, name);
        } else {
            log.info("Ignoring tag removal of {} for object id {} (type {})", new Object[]{name, objectId.toString(), objectType.toString()});
        }
    }

    private void tagAddedForAccount(final UUID objectId, final String name) {
        final Account account;
        try {
            account = accountApi.getAccountById(objectId);
        } catch (AccountApiException e) {
            log.warn("Ignoring tag addition of {} for account id {} (account does not exist)", name, objectId.toString());
            return;
        }

        final String accountKey = account.getExternalKey();
        accountTagSqlDao.addTag(accountKey, name);
    }

    private void tagRemovedForAccount(final UUID objectId, final String name) {
        final Account account;
        try {
            account = accountApi.getAccountById(objectId);
        } catch (AccountApiException e) {
            log.warn("Ignoring tag removal of {} for account id {} (account does not exist)", name, objectId.toString());
            return;
        }

        final String accountKey = account.getExternalKey();
        accountTagSqlDao.removeTag(accountKey, name);
    }

    private void tagAddedForBundle(final UUID objectId, final String name) {
        final SubscriptionBundle bundle;
        try {
            bundle = entitlementUserApi.getBundleFromId(objectId);
        } catch (EntitlementUserApiException e) {
            log.warn("Ignoring tag addition of {} for bundle id {} (bundle does not exist)", name, objectId.toString());
            return;
        }

        /*
         * Note: we store tags associated to bundles, not to subscriptions.
         * Subscriptions are in the core of killbill and not exposed in Analytics to avoid a hard dependency
         * (i.e. dashboards should not rely on killbill ids).
         */
        final String externalKey = bundle.getKey();
        subscriptionTransitionTagSqlDao.addTag(externalKey, name);
    }

    private void tagRemovedForBundle(final UUID objectId, final String name) {
        final SubscriptionBundle bundle;
        try {
            bundle = entitlementUserApi.getBundleFromId(objectId);
        } catch (EntitlementUserApiException e) {
            log.warn("Ignoring tag removal of {} for bundle id {} (bundle does not exist)", name, objectId.toString());
            return;
        }

        final String externalKey = bundle.getKey();
        subscriptionTransitionTagSqlDao.removeTag(externalKey, name);
    }

    private void tagAddedForInvoice(final UUID objectId, final String name) {
        invoiceTagSqlDao.addTag(objectId.toString(), name);
    }

    private void tagRemovedForInvoice(final UUID objectId, final String name) {
        invoiceTagSqlDao.removeTag(objectId.toString(), name);
    }

    private void tagAddedForPayment(final UUID objectId, final String name) {
        invoicePaymentTagSqlDao.addTag(objectId.toString(), name);
    }

{'fraction_non_alphanumeric': 0.0820225526107879, 'fraction_numerical': 0.002011060834590246, 'mean_word_length': 3.12807589682775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6442315', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3712, 'n_words': 949}