Page 1 of 1

Extend ProductImpl

Posted: Wed Apr 22, 2015 6:29 am
by turner2448
Hi

I have successfully setup the Heat Clinic Demo application running on an external Tomcat (version 8) with MYSQL.

I would like to extend a product to include a latitude and longitude value.

I have followed the tutorials for extending entities. The steps followed are below.

I don't see a latitude and longitude field if I attempt to create a product of the new type, called GeoProduct. I'm unsure how these fields will appear in the admin console and the tutorials aren't clear what I should expect.

1. I have created an interface for my new product type in core.

Code: Select all

package com.mycompany.catalog.entity;

import org.broadleafcommerce.core.catalog.domain.Product;

public interface GeoProduct extends Product {
   public Double getLat();

    public Double getLon();
   
    public void setLon(Double lon);
   
    public void setLat(Double lat);
}


2. I have created the implementation:

Code: Select all

package com.mycompany.catalog.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.broadleafcommerce.core.catalog.domain.ProductImpl;

@Entity
@Table(name = "blc_geoproduct")
public class GeoProductImpl extends ProductImpl implements GeoProduct {

    private static final long serialVersionUID = 1L;

    @Column(name = "LAT")
    protected Double lat;

    @Column(name = "LON")
    protected Double lon;
   
    @Override
   public Double getLat() {
      return lat;
   }
   
    @Override
   public void setLat(Double lat) {
      this.lat = lat;
   }
   
    @Override
   public Double getLon() {
      return lon;
   }
   
    @Override
   public void setLon(Double lon) {
      this.lon = lon;
   }

}


3. I have created a table in MYSQL called blc_geoproduct.

I have tried two tables (the two articles about extending entities seem to contradict themselves):

Table 1:
PRODUCT_ID BIGINT(20)
LAT Double
LON Double

Table 2:
GEOPRODUCT_ID BIGINT(20)
LAT Double
LON Double
PRODUCT_ID BIGINT(20)

It wasn't clear whether the foreign key constraint should be on a key other than the primary key (as is usually the case) or whether the primary key would also act at the foreign key.

4. I have amended persistence-core.xml:

<persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/web</non-jta-data-source>
<class>com.mycompany.catalog.entity.GeoProductImpl</class>
<exclude-unlisted-classes/>
</persistence-unit>

5. I have amended applicationContext-entity.xml:

<bean id="org.broadleafcommerce.core.catalog.domain.Product"
class="org.broadleafcommerce.core.catalog.domain.ProductImpl"
scope="prototype"/>
</beans>

The foreign key constraint gets created and from the admin console/create new product I am able to select type GeoProductImpl. I do however not see the two fields, though I don't know where they should appear in the console.

Any help would be appreciated.

Re: Extend ProductImpl

Posted: Wed Apr 22, 2015 9:10 am
by arunmrao
I think in Step 5, you need to point to your new Product as follows:

Code: Select all

<bean id="org.broadleafcommerce.core.catalog.domain.Product"
class="com.mycompany.catalog.entity.GeoProductImpl"
scope="prototype"/>
</beans>

Re: Extend ProductImpl

Posted: Wed Apr 22, 2015 9:46 am
by turner2448
Hi arunmrao

You're right, that was indeed a silly mistake, however I still can't get the blc_geoproduct table to get populated or the LAT, LON values to appear in the admin.

Do you know where the fields will appear in the admin? And do you have an idea as to how the table should look?

Thanks

Re: Extend ProductImpl

Posted: Wed Apr 22, 2015 10:06 am
by turner2448
Actually, the geoproduct database is being populated correctly, just the fields are not appearing in the admin. I'm not sure if I need to make additional changes in the admin project to make this happen.

Re: Extend ProductImpl

Posted: Wed Apr 22, 2015 10:33 am
by arunmrao
for your fields to show up in admin, I believe that you have to annotate your fields with the @AdminPresentation - see examples in the the source code

Re: Extend ProductImpl

Posted: Wed Apr 22, 2015 12:13 pm
by turner2448
Thanks arunmrao, I can now see the fields in the admin console.

In addition, the reason I could not see the fields in the site page is because I needed to declare the instance variables in the implementation to be public - despite the fact that there ware getters and setters it seems that the BL code is referencing the fields directly.

Thanks again.

Re: Extend ProductImpl

Posted: Thu Apr 23, 2015 10:40 am
by phillipuniverse
Sounds like you got this worked out.

To your point on the instance variables, you do not need them to be public but the Broadleaf admin code does reference those fields directly. It just uses `field.setAccessible(true)` with the reflection code in order to access protected/private fields.