gt:                     aPrice = price.getPrice();
context: /*
   * 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.item;
  
  import java.sql.SQLException;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  
  import com.sapienter.jbilling.server.customer.CustomerSQL;
  import com.sapienter.jbilling.server.item.event.ItemDeletedEvent;
  import com.sapienter.jbilling.server.item.event.ItemUpdatedEvent;
  import com.sapienter.jbilling.server.item.event.NewItemEvent;
  import com.sapienter.jbilling.server.order.db.OrderDTO;
  import com.sapienter.jbilling.server.system.event.EventManager;
  import org.apache.log4j.Logger;
  
  import org.springmodules.cache.provider.CacheProviderFacade;
  import org.springmodules.cache.CachingModel;
  import org.springmodules.cache.FlushingModel;
  
  import com.sapienter.jbilling.common.SessionInternalError;
  import com.sapienter.jbilling.server.item.db.ItemDAS;
  import com.sapienter.jbilling.server.item.db.ItemDTO;
  import com.sapienter.jbilling.server.item.db.ItemPriceDAS;
  import com.sapienter.jbilling.server.item.db.ItemPriceDTO;
  import com.sapienter.jbilling.server.item.db.ItemTypeDTO;
  import com.sapienter.jbilling.server.item.tasks.IPricing;
  import com.sapienter.jbilling.server.list.ResultList;
  import com.sapienter.jbilling.server.order.db.OrderLineDAS;
  import com.sapienter.jbilling.server.pluggableTask.admin.PluggableTaskManager;
  import com.sapienter.jbilling.server.user.EntityBL;
  import com.sapienter.jbilling.server.user.UserBL;
  import com.sapienter.jbilling.server.user.db.CompanyDAS;
  import com.sapienter.jbilling.server.user.db.CompanyDTO;
  import com.sapienter.jbilling.server.util.Constants;
  import com.sapienter.jbilling.server.util.Context;
  import com.sapienter.jbilling.server.util.audit.EventLogger;
  import com.sapienter.jbilling.server.util.db.CurrencyDAS;
  import com.sapienter.jbilling.server.util.db.CurrencyDTO;
  import java.math.BigDecimal;
  import java.util.ArrayList;
  
  public class ItemBL extends ResultList{
  
      private static final Logger LOG = Logger.getLogger(ItemBL.class);
  
      private ItemDAS itemDas = null;
      private ItemDTO item = null;
      private EventLogger eLogger = null;
      private String priceCurrencySymbol = null;
      private List<PricingField> pricingFields = null;
  
      // item price cache for getPrice()
      private CacheProviderFacade cache;
      private CachingModel cacheModel;
      private FlushingModel flushModel;
  
      public ItemBL(Integer itemId) 
              throws SessionInternalError {
          try {
              init();
              set(itemId);
          } catch (Exception e) {
              throw new SessionInternalError("Setting item", ItemBL.class, e);
          } 
      }
      
      public ItemBL() {
          init();
      }
      
      public ItemBL(ItemDTO item) {
          this.item = item;
          init();
      }
      
      public void set(Integer itemId) {
          item = itemDas.find(itemId);
      }
      
      private void init() {
          eLogger = EventLogger.getInstance();        
          itemDas = new ItemDAS();
          cache = (CacheProviderFacade) Context.getBean(Context.Name.CACHE);
          cacheModel = (CachingModel) Context.getBean(
                  Context.Name.CACHE_MODEL_ITEM_PRICE);
          flushModel = (FlushingModel) Context.getBean(
                  Context.Name.CACHE_FLUSH_MODEL_ITEM_PRICE);
      }
      
      public ItemDTO getEntity() {
          return item;
      }
  
      public Integer create(ItemDTO dto, Integer languageId) {
          EntityBL entity = new EntityBL(dto.getEntityId());
          if (languageId == null) {
              languageId = entity.getEntity().getLanguageId();
          }
  
          if (dto.getHasDecimals() != null) {
              dto.setHasDecimals(dto.getHasDecimals());
          } else {
              dto.setHasDecimals(0);
          }
  
          dto.setDeleted(0);
  
          item = itemDas.save(dto);
  
          item.setDescription(dto.getDescription(), languageId);
          updateTypes(dto);
          updateExcludedTypes(dto);
          updateCurrencies(dto);
          
          // trigger internal event
          EventManager.process(new NewItemEvent(item));
  
          return item.getId();
      }
      
      public void update(Integer executorId, ItemDTO dto, Integer languageId)  {
          eLogger.audit(executorId, null, Constants.TABLE_ITEM, item.getId(),
                  EventLogger.MODULE_ITEM_MAINTENANCE, 
                  EventLogger.ROW_UPDATED, null, null, null);
          
          item.setNumber(dto.getNumber());
          item.setPriceManual(dto.getPriceManual());
          item.setDescription(dto.getDescription(), languageId);
          item.setPercentage(dto.getPercentage());
          item.setHasDecimals(dto.getHasDecimals());
          
          updateTypes(dto);
          updateExcludedTypes(dto);
  
          if (item.getPercentage() == null) {
              // update price currencies
              updateCurrencies(dto);
          } else {
              // percentage items shouldn't have a price, remove all old prices
              for (ItemPriceDTO  price : item.getItemPrices()) {
                  new ItemPriceDAS().delete(price);
              }
              item.getItemPrices().clear();
          }
  
          itemDas.save(item);
          invalidateCache();
  
          // trigger internal event
          EventManager.process(new ItemUpdatedEvent(item));
      }
      
  
      private void updateTypes(ItemDTO dto)
              {
          // update the types relationship        
          Collection types = item.getItemTypes();
          types.clear();
          ItemTypeBL typeBl = new ItemTypeBL();
          // TODO verify that all the categories belong to the same
          // order_line_type_id
          for (int f=0; f < dto.getTypes().length; f++) {
              typeBl.set(dto.getTypes()[f]);
              types.add(typeBl.getEntity());
          }
      }
  
      private void updateExcludedTypes(ItemDTO dto) {
          item.getExcludedTypes().clear();
  
          ItemTypeBL itemType = new ItemTypeBL();
          for (Integer typeId : dto.getExcludedTypeIds()) {
              itemType.set(typeId);
              item.getExcludedTypes().add(itemType.getEntity());
          }
      }
      
      private void updateCurrencies(ItemDTO dto) {
          LOG.debug("updating prices. prices " + (dto.getPrices() != null) + 
                  " price = " + dto.getPrice());
          ItemPriceDAS itemPriceDas = new ItemPriceDAS();
          // may be there's just one simple price
          if (dto.getPrices() == null) {
              if (dto.getPrice() != null) {
                  List prices = new ArrayList();
                  // get the defualt currency of the entity
                  CurrencyDTO currency = new CurrencyDAS().findNow(
                          dto.getCurrencyId());
                  if (currency == null) {
                      EntityBL entity = new EntityBL(dto.getEntityId());
                      currency = entity.getEntity().getCurrency();
                  }
                  ItemPriceDTO price = new ItemPriceDTO(null, dto, dto.getPrice(),
                          currency);
                  prices.add(price);
                  dto.setPrices(prices);
              } else {
                  LOG.warn("updatedCurrencies was called, but this " +
                          "item has no price");
                  return;
              }
          }
          
          // a call to clear() would simply set item_price.entity_id = null
          // instead of removing the row
          for (int f = 0; f < dto.getPrices().size(); f++) {
              ItemPriceDTO price = (ItemPriceDTO) dto.getPrices().get(f);
              ItemPriceDTO priceRow = null;
  
              priceRow = itemPriceDas.find(dto.getId(), 
                          price.getCurrencyId());
                      
              if (price.getPrice() != null) {
                  if (priceRow != null) {
                      // if there one there already, update it
                      priceRow.setPrice(price.getPrice());
                      
                  } else {
                      // nothing there, create one
                      ItemPriceDTO itemPrice= new ItemPriceDTO(null, item, 
                              price.getPrice(), price.getCurrency());
                      item.getItemPrices().add(itemPrice);
                  }
              } else {
                  // this price should be removed if it is there
                  if (priceRow != null) {
                      itemPriceDas.delete(priceRow);
                  }
              }
          }    
          // invalidate item/price cache
          invalidateCache();
      }
      
      public void delete(Integer executorId) {
          item.setDeleted(new Integer(1));
          
          eLogger.audit(executorId, null, Constants.TABLE_ITEM, item.getId(),
                  EventLogger.MODULE_ITEM_MAINTENANCE, 
                  EventLogger.ROW_DELETED, null, null, null);
  
          // trigger internal event
          EventManager.process(new ItemDeletedEvent(item));
  
          itemDas.flush();
          itemDas.clear();
      }
  
      public boolean validateDecimals( Integer hasDecimals ){
          if( hasDecimals == 0 ){
              if(new OrderLineDAS().findLinesWithDecimals(item.getId()) > 0) {
                  return false;
              }
          }
          return true;
      }
  
      /**
       * This is the basic price, without any plug-ins applied.
       * It only takes into account the currency and makes the necessary
       * conversions.
       * It uses a cache to avoid repeating this look-up too often
       * @return The price in the requested currency
       */
      public BigDecimal getPriceByCurrency(ItemDTO item, Integer entityId, Integer currencyId)  {
          BigDecimal retValue = null;
  
          // try to get cached item price for this currency
          retValue = (BigDecimal) cache.getFromCache(item.getId() +
                  currencyId.toString(), cacheModel);
  
          if (retValue != null) {
              return retValue;
          }
  
          // get the item's defualt price
          int prices = 0;
          BigDecimal aPrice = null;
          Integer aCurrency = null;
          // may be the item has a price in this currency
          for (Iterator it = item.getItemPrices().iterator(); it.hasNext(); ) {
              prices++;
              ItemPriceDTO price = (ItemPriceDTO) it.next();
              if (price.getCurrencyId().equals(currencyId)) {
                  // it is there!
                  retValue = price.getPrice();
                  break;
              } else {
                  // the pivot has priority, for a more accurate conversion
                  if (aCurrency == null || aCurrency.intValue() != 1) { 
  