Page 1 of 1

Reason to create an interface when extend blc entity

Posted: Tue Nov 27, 2012 12:18 pm
by denis
Hi,
We would like to know for what reason we must create an interface for our custom entities when we extends a broadleaf entity.

In your documention there is this example :

Code: Select all

public class HotSauceImpl extends ProductImpl implements HotSauce


So we have create an interface for each of our custom entities that extends a BLC entity but all of these interfaces are empty and so we wonder where they are useful ?

We tried to extend a BLC entity without create an interface for our custom entity and it works too :

Code: Select all

public class HotSauceImpl extends ProductImpl


Thanks for your answer

Re: Reason to create an interface when extend blc entity

Posted: Tue Nov 27, 2012 1:17 pm
by phillipuniverse
In your code it's not really necessary to have an interface as well. Nothing about that prevents BLC from operating correctly.

The only exception was something that we found when we were there with you all. SkuImpl has some code that depends on your extended SkuImpl having an interface declared for it if you want to use the DynamicSkuPricingService when proxying the Sku (look at getRetailPrice() and getSalePrice() in SkuImpl; there is a line that uses getClass().getInterfaces()). I believe it will end up throwing a ClassCastException if this is not the case.

The other caveat is that in order for your entities to work in the admin datasources, they must implement the Serializable interface.

Re: Reason to create an interface when extend blc entity

Posted: Tue Nov 27, 2012 1:39 pm
by denis
Thanks for your answer.

So we keep the interface for MySkuImpl but we can delete them for the other entities.

Re: Reason to create an interface when extend blc entity

Posted: Tue Nov 27, 2012 4:17 pm
by phillipuniverse
Yup, let me know if you run into any problems with this. If you do, we would like to know about it. Thanks!

Re: Reason to create an interface when extend blc entity

Posted: Sun Feb 03, 2013 3:45 pm
by pccfrmvns
Hi,

I am also trying to extend an BL Sku entity. I've created an interface(MySku) and extended the SkuImpl in MySkuImpl class and implemented MySku.

Code: Select all

public interface MySku {

   public void setRentalPrices(Map<Integer, BigDecimal> rentalPrices);
   
   public Map<Integer, BigDecimal> getRentalPrices();
}


Code: Select all

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="MY_SKU")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="blStandardElements")
public class MySkuImpl extends SkuImpl implements MySku{

   /**
    *
    */
   private static final long serialVersionUID = -518974372567977433L;
   @Transient
   protected static final Log LOG = LogFactory.getLog(TbrSkuImpl.class);
   
   @Resource(name="myRentalEngine")
   @Transient
   protected IRentalEngine rentalEngine;
   
   @Transient
   protected Map<Integer, BigDecimal> rentalPrices = new HashMap<Integer, BigDecimal>();
   

   @Override
   public void setRetailPrice(Money retailPrice) {
      super.setRetailPrice(retailPrice);
      
      //Set Rental price after setting Retail Price
      try {
         rentalPrices = rentalEngine.calculateAllRentals(retailPrice.getAmount());
      } catch (RentalServiceException e) {
         LOG.warn("Error in rental price calculation for retail price: "+retailPrice.getAmount()+" "+e.getMessage());
         //e.printStackTrace();
      }
   }
   
   public void setRentalPrices(Map<Integer, BigDecimal> rentalPrices) {
      this.rentalPrices = rentalPrices;
   }
   
   public Map<Integer, BigDecimal> getRentalPrices(){
      return this.rentalPrices;
   }
   
}


I've added this implementation class in persistence.xml and overrided Sku entity by in application context as

Code: Select all

<bean id="org.broadleafcommerce.core.catalog.domain.Sku" class="com.mycompany.core.catalog.domain.MySkuImpl" scope="prototype"/>


Now I am trying to access the rentalPrices field by Spring EL expression. in listProducItem.html. I get exception as below

Code: Select all

Field or property 'rentalPrices' cannot be found on object of type 'org.broadleafcommerce.core.catalog.domain.SkuImpl'


Please help.

Re: Reason to create an interface when extend blc entity

Posted: Mon Feb 04, 2013 8:52 am
by denis
Hi, I don't know if it can resolve your problem but first of all your class MySku should extends the interface Sku :

Code: Select all

public interface MySku extends Sku {

   public void setRentalPrices(Map<Integer, BigDecimal> rentalPrices);
   
   public Map<Integer, BigDecimal> getRentalPrices();
}

Re: Reason to create an interface when extend blc entity

Posted: Wed Feb 06, 2013 11:31 am
by phillipuniverse
How did you add the Sku to begin with? Via the admin? There might be a problem in the admin when instantiating Skus; it's possible that we don't instantiate the correct subclass. I assume there is no corresponding entry in your MY_SKU table?

Re: Reason to create an interface when extend blc entity

Posted: Thu Feb 07, 2013 6:11 pm
by phillipuniverse
FYI, the second discussion is actively being continued at viewtopic.php?f=11&t=1625