<commit_before>/*
 * JBILLING CONFIDENTIAL
 * _____________________
 *
 * [2003] - [2012] Enterprise jBilling Software Ltd.
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Enterprise jBilling Software.
 * The intellectual and technical concepts contained
 * herein are proprietary to Enterprise jBilling Software
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden.
 */

package com.sapienter.jbilling.server.pricing.strategy;

import com.sapienter.jbilling.server.item.PricingField;
import com.sapienter.jbilling.server.item.tasks.PricingResult;
import com.sapienter.jbilling.server.order.OrderBL;
import com.sapienter.jbilling.server.order.OrderHelper;
import com.sapienter.jbilling.server.order.Usage;
import com.sapienter.jbilling.server.order.UsageBL;
import com.sapienter.jbilling.server.order.db.OrderDTO;
import com.sapienter.jbilling.server.pricing.db.AttributeDefinition;
import com.sapienter.jbilling.server.pricing.db.ChainPosition;
import com.sapienter.jbilling.server.pricing.db.PriceModelDTO;
import com.sapienter.jbilling.server.pricing.util.AttributeUtils;
import com.sapienter.jbilling.server.user.db.UserDTO;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.sapienter.jbilling.server.pricing.db.AttributeDefinition.Type.*;

/**
 * PlanSelectorStrategy
 *
 * @author <NAME>
 * @since 12/07/11
 */
public class ItemSelectorStrategy extends AbstractPricingStrategy {

    private static final Logger LOG = Logger.getLogger(ItemSelectorStrategy.class);

    public ItemSelectorStrategy() {
        setAttributeDefinitions(
                new AttributeDefinition("typeId", INTEGER, true),
                new AttributeDefinition("1", INTEGER, false)
        );

        setChainPositions(
                ChainPosition.START
        );

        setRequiresUsage(false);
    }

    public void applyTo(OrderDTO pricingOrder, PricingResult result, List<PricingField> fields,
                        PriceModelDTO planPrice, BigDecimal quantity, Usage usage, boolean singlePurchase) {

        // price of selector trigger item will be the set $/unit rate
        result.setPrice(planPrice.getRate());

        if (pricingOrder != null) {
            // parse item selection tiers
            SortedMap<Integer, Integer> tiers = getTiers(planPrice.getAttributes());
            LOG.debug("Item selector tiers: " + tiers);

            // items used for selection
            Integer typeId =  AttributeUtils.getInteger(planPrice.getAttributes(), "typeId");
            Usage typeUsage = new UsageBL(result.getUserId(), pricingOrder).getItemTypeUsage(typeId);
            LOG.debug("Selecting tier for usage level " + typeUsage.getQuantity());

            // find matching tier
            Integer selectedItemId = tiers.get(1);
            for (Integer tier : tiers.keySet()) {
                if (typeUsage.getQuantity().compareTo(new BigDecimal(tier)) >= 0) {
                    selectedItemId = tiers.get(tier);
                }
            }

            // add item
            if (selectedItemId != null) {
                addIfNotExists(pricingOrder, tiers, selectedItemId);
            } else {
                LOG.debug("No tier for usage level " + typeUsage.getQuantity());
            }

        } else {
            LOG.debug("No pricing order given (simple price fetch), skipping tiered item selection.");
        }<commit_msg>Bugs #2954 Item selector' pricing feature is not working correctly.<commit_after>/*
 * JBILLING CONFIDENTIAL
 * _____________________
 *
 * [2003] - [2012] Enterprise jBilling Software Ltd.
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Enterprise jBilling Software.
 * The intellectual and technical concepts contained
 * herein are proprietary to Enterprise jBilling Software
 * and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden.
 */

package com.sapienter.jbilling.server.pricing.strategy;

import com.sapienter.jbilling.server.item.PricingField;
import com.sapienter.jbilling.server.item.tasks.PricingResult;
import com.sapienter.jbilling.server.order.OrderBL;
import com.sapienter.jbilling.server.order.OrderHelper;
import com.sapienter.jbilling.server.order.Usage;
import com.sapienter.jbilling.server.order.UsageBL;
import com.sapienter.jbilling.server.order.db.OrderDTO;
import com.sapienter.jbilling.server.pricing.db.AttributeDefinition;
import com.sapienter.jbilling.server.pricing.db.ChainPosition;
import com.sapienter.jbilling.server.pricing.db.PriceModelDTO;
import com.sapienter.jbilling.server.pricing.util.AttributeUtils;
import com.sapienter.jbilling.server.user.db.UserDTO;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.sapienter.jbilling.server.pricing.db.AttributeDefinition.Type.*;

/**
 * PlanSelectorStrategy
 *
 * @author <NAME>
 * @since 12/07/11
 */
public class ItemSelectorStrategy extends AbstractPricingStrategy {

    private static final Logger LOG = Logger.getLogger(ItemSelectorStrategy.class);

    public ItemSelectorStrategy() {
        setAttributeDefinitions(
                new AttributeDefinition("typeId", INTEGER, true),
                new AttributeDefinition("1", INTEGER, false)
        );

        setChainPositions(
                ChainPosition.START
        );

        setRequiresUsage(true);
    }

    public void applyTo(OrderDTO pricingOrder, PricingResult result, List<PricingField> fields,
                        PriceModelDTO planPrice, BigDecimal quantity, Usage usage, boolean singlePurchase) {

        // price of selector trigger item will be the set $/unit rate
        result.setPrice(planPrice.getRate());

        if (pricingOrder != null) {
            // parse item selection tiers
            SortedMap<Integer, Integer> tiers = getTiers(planPrice.getAttributes());
            LOG.debug("Item selector tiers: " + tiers);

            LOG.debug("Selecting tier for usage level " + usage.getQuantity());

            // find matching tier
            Integer selectedItemId = tiers.get(1);
            for (Integer tier : tiers.keySet()) {
                if (usage.getQuantity().compareTo(new BigDecimal(tier)) >= 0) {
                    selectedItemId = tiers.get(tier);
                }
            }

            // add item
            if (selectedItemId != null) {
                addIfNotExists(pricingOrder, tiers, selectedItemId);
            } else {
                LOG.debug("No tier for usage level " + usage.getQuantity());
            }

        } else {
            LOG.debug("No pricing order given (simple price fetch), skipping tiered item selection.");
        }
{'fraction_non_alphanumeric': 0.09525807805287453, 'fraction_numerical': 0.005595188138201147, 'mean_word_length': 2.994413407821229, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6747177', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1952, 'n_words': 472}