Page 1 of 1

fetch custom datas

Posted: Fri Jan 25, 2013 5:40 am
by denis
Hi,

I have 3 problems :

1/ I would like to display some ProductOption fields into an EntitySearchDialog which contains ProductOptionValues :

Code: Select all

getPresenterSequenceSetupManager().addOrReplaceItem(new PresenterSetupItem("subscriptionProductOptionValuesDS", new SubscriptionProductOptionValueDataSourceFactory(), new AsyncCallbackAdapter() {
               @Override
               public void onSetupSuccess(DataSource result) {
                  CustomCriteriaListGridDataSource productOptionValueSearchDataSource = (CustomCriteriaListGridDataSource)result;
                   subscriptionProductOptionValuesDS = productOptionValueSearchDataSource;
                   productOptionValueSearchDataSource.resetPermanentFieldVisibility(
                         "attributeValue",
                                        //DISPLAY PRODUCT OPTION LABEL HERE
                         "productOption.label"
                   );
 
                  EntitySearchDialog productOptionValueSearchView = new EntitySearchDialog(productOptionValueSearchDataSource);
                  library.put("productOptionValueSearchView", productOptionValueSearchView);
               }
           }));


But the class ProductOptionValueImpl doesn't have the attribute "populateToOneFields=PopulateToOneFieldsEnum.TRUE" so i can't display productOption attributes (like label). Can i override it by xml or is there another way to display theses attributes ?

2/ The second problem is to retrieve productOptionValues (but with a custom query) so i have a CustomCriteriaListGridDataSource :

Code: Select all

public class SubscriptionProductOptionValueDataSourceFactory implements DataSourceFactory {

    protected CustomCriteriaListGridDataSource dataSource = null;
   
    public void createDataSource(String name, OperationTypes operationTypes, Object[] additionalItems, AsyncCallback<DataSource> cb) {
        if (dataSource == null) {
            operationTypes = new OperationTypes(OperationType.BASIC, OperationType.BASIC, OperationType.BASIC, OperationType.BASIC, OperationType.BASIC);
            PersistencePerspective persistencePerspective = new PersistencePerspective(operationTypes, new String[]{}, new ForeignKey[]{});
            persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, new ForeignKey("productOption", EntityImplementations.PRODUCT_OPTION, null));
            DataSourceModule[] modules = new DataSourceModule[]{
                new BasicClientEntityModule(CeilingEntities.PRODUCT_OPTION_VALUE, persistencePerspective, AppServices.DYNAMIC_ENTITY)
            };
            CustomCriteriaListGridDataSource dataSource = new CustomCriteriaListGridDataSource(name, persistencePerspective, AppServices.DYNAMIC_ENTITY, modules, true, false, false, false, false);
            dataSource.setCustomCriteria(new String[]{"searchSubscriptionProductOptionValuesList"});       
            dataSource.buildFields(null, false, cb);
        } else {
            if (cb != null) {
                cb.onSuccess(dataSource);
            }
        }
    }
}


A custom persistence handler, with an override on the fetch method :

Code: Select all

 @Override
       public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
           String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
           try {
               PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
               Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(ProductOptionValue.class.getName(), persistencePerspective);
               //Pull back the productOptionValues based on the criteria from the client
               BaseCtoConverter ctoConverter = helper.getCtoConverter(persistencePerspective, cto,
                                                                       ceilingEntityFullyQualifiedClassname, originalProps);
              
               String subscriptionId = persistencePackage.getCustomCriteria()[1];
              //HERE MY CUSTOM REQUEST TO RETRIEVE productOptionValues
              
               Entity[] entities = helper.getRecords(originalProps, productOptionvalues);
               return new DynamicResultSet(entities, entities.length);       
           } catch (Exception e) {
               LOG.error("Unable to execute persistence activity", e);
               throw new ServiceException("Unable to perform fetch for entity: "+ceilingEntityFullyQualifiedClassname, e);
           }
       }


It works but if i filter on a column, fetch method is called again and retrieve all productOptionValues...It's normal because i don't use the dynamicDao with queryCriteria:

Code: Select all

 List<Serializable> records = dynamicEntityDao.query(queryCriteria,
                                               Class.forName(persistencePackage.getCeilingEntityFullyQualifiedClassname()));


But is it possible to use my custom query and use queryCriteria to filter on? Because by default your dynamicEntityDao retrieve all datas of a given class.For instance i can't retrieve only productOptionValues of all ProductOptions of a given product by using only your dynamicEntityDao.

3/ When i select a ProductOptionValue, the field in the popup is still empty :
Image
however i believe that the annotation on my attribute is correct :

Code: Select all

@ManyToOne(targetEntity=ProductOptionValueImpl.class)
   @JoinColumn(name = "PRODUCT_OPTION_VALUE_ID", nullable = false)
   @AdminPresentation(friendlyName="Option", group = "SubscriptionOption_Group", order=1)
   @AdminPresentationToOneLookup(lookupDisplayProperty="attributeValue") //HERE
   private ProductOptionValue productOptionValue;

PS : if i edit the row, then the field is correctly filled

Thanks in advance for your help

Re: fetch custom datas

Posted: Mon Jan 28, 2013 4:55 am
by denis
Below what i have done in the fetch method to resolve the second problem (but im not sure that is the best way) :

Code: Select all

PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
               Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(ProductOptionValue.class.getName(), persistencePerspective);
              
               BaseCtoConverter ctoConverter = helper.getCtoConverter(persistencePerspective, cto,
                                                                       ceilingEntityFullyQualifiedClassname, originalProps);
               PersistentEntityCriteria queryCriteria = ctoConverter.convert(cto, ceilingEntityFullyQualifiedClassname);
//Retrieve current subscription here
Subscription subscription = ...

Criteria criteria = ((HibernateEntityManager) dynamicEntityDao.getStandardEntityManager()).getSession().createCriteria(ProductOptionValue.class, "pv");           
               criteria.createAlias("pv.productOption", "po");
               criteria.createAlias("po.products", "pr");
               criteria.add(Restrictions.eq("pr.id", subscription.getCurrentSku().getProduct().getId()));
                    //apply filters              
                    queryCriteria.apply(criteria);
               List<Serializable> finalOptionValuesList = criteria.list();   

Entity[] entities = helper.getRecords(originalProps, finalOptionValuesList);
               return new DynamicResultSet(entities, entities.length);     

Re: fetch custom datas

Posted: Thu Jan 31, 2013 11:59 am
by phillipuniverse
I think the way that you solved your second problem is probably just fine how you've done it in the fetch method.

For the first problem, I don't think that you can currently override the populateToOne behavior via XML. I think that currently the only way to do this is with subclassing. I've opened a Jira ticket: http://jira.broadleafcommerce.org/browse/BLC-833

For the 3rd problem, what do you mean that if you edit the row then the field is correctly filled? Does this mean that if you save this and then re-open to edit then everything shows up as you would expect?

Re: fetch custom datas

Posted: Thu Jan 31, 2013 2:30 pm
by denis
Hi Phillip!

First of all thanks for your reply. Then for the 3rd problem the response is yes, if I save the object then re-open to edit everything shows up as i'm expect. The problem only occur when i select a value in the entitySearchDialog, the field "option" still empty instead of displaying the "attributeValue" of the row selected.