Help me Broadleaf Commerce forum... you're my only hope!

This forum is in readonly mode and serves as an archive of old information. All posting, registration and commenting abilities have been turned off. To get help, the Broadleaf team reguarly monitors the broadleaf-commerce tag on Stack Overflow so please ask your questions there.
Moderator: jocanas
Code: Select all
@Entity
@AdminPresentationClass(populateToOneFields=PopulateToOneFieldsEnum.TRUE)
public class StoreImpl implements Serializable {
//other properties omitted
@ManyToOne(targetEntity = ProductImpl.class, optional=false)
@JoinColumn(name="PRODUCT_ID")
//make this field excluded, which will prevent any ToOne form inclusion. This is only needed if you have StoreImpl annotated with @AdminPresentationClass(populateToOneFields=PopulateToOneFieldsEnum.TRUE)
@AdminPresentation(friendlyName="Default Product", excluded=true, fieldType=SupportedFieldType.FOREIGN_KEY)
protected Product defaultProduct;
}
Code: Select all
public class StorePresenter extends DynamicEntityPresenter implements Instantiable {
//Other methods omitted
@Override
public void setup()
//I assume this is how you're setting up the Store datasource
getPresenterSequenceSetupManager().addOrReplaceItem(new PresenterSetupItem("storeDS", new StoreDataSourceFactory(), new AsyncCallbackAdapter() {
public void onSetupSuccess(DataSource top) {
setupDisplayItems(top);
((ListGridDataSource) top).setupGridFields(new String[]{"storeField1", "storeField2", "storeField3"}, new Boolean[]{false, true, true});
}
}));
//'productSearch' is an arbitrary name
//Notice that the 3rd OperationTypes parameter is OperationType.JOINSTRUCTURE which tells the add process that it needs to be a join
getPresenterSequenceSetupManager().addOrReplaceItem(new PresenterSetupItem("productSearchDS", new ProductDataSourceFactory(), new OperationTypes(OperationType.ENTITY, OperationType.ENTITY, OperationType.JOINSTRUCTURE, OperationType.ENTITY, OperationType.ENTITY), new Object[]{}, new AsyncCallbackAdapter() {
public void onSetupSuccess(DataSource result) {
ListGridDataSource productSearchDataSource = (ListGridDataSource) result;
//change these field names to whichever fields you want to be displayed when the search happens. If you leave this blank, it will build a form based on the @AdminPresentation annotations you have on Product
productSearchDataSource.resetPermanentFieldVisibility(
"name",
"description"
);
//usually you want the autofetch to true here; otherwise you'll have to hit the funnel button on the search
EntitySearchDialog productSearchView = new EntitySearchDialog(productSearchDataSource, true);
//Here's where the real magic happens. This will add a proper callback to the '...' button on the defaultProduct property from 'storeDS' (which was instantiated above).
getPresenterSequenceSetupManager().getDataSource("storeDS").getFormItemCallbackHandlerManager().addSearchFormItemCallback(
"defaultProduct",
productSearchView,
"Search for a Product",
getDisplay().getDynamicFormDisplay()
);
}
}));
Code: Select all
public class ProductDataSourceFactory implements DataSourceFactory {
public static ListGridDataSource dataSource = null;
public void createDataSource(String name, OperationTypes operationTypes, Object[] additionalItems, AsyncCallback<DataSource> cb) {
if (dataSource == null) {
operationTypes = new OperationTypes(OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY);
PersistencePerspective persistencePerspective = new PersistencePerspective(operationTypes, new String[]{}, new ForeignKey[]{});
DataSourceModule[] modules = new DataSourceModule[]{
new BasicClientEntityModule(CeilingEntities.PRODUCT, persistencePerspective, AppServices.DYNAMIC_ENTITY)
};
dataSource = new ListGridDataSource(name, persistencePerspective, AppServices.DYNAMIC_ENTITY, modules);
dataSource.buildFields(null, false, cb);
} else {
if (cb != null) {
cb.onSuccess(dataSource);
}
}
}
}
Code: Select all
public class StoreDataSourceFactory implements DataSourceFactory {
public static ListGridDataSource dataSource = null;
public void createDataSource(String name, OperationTypes operationTypes, Object[] additionalItems, AsyncCallback<DataSource> cb) {
if (dataSource == null) {
operationTypes = new OperationTypes(OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY);
PersistencePerspective persistencePerspective = new PersistencePerspective(operationTypes, new String[]{}, new ForeignKey[]{new ForeignKey("defaultProduct", EntityImplementations.PRODUCT, null)});
DataSourceModule[] modules = new DataSourceModule[]{
new BasicClientEntityModule(MyCompanyCeilingEntities.STORE, persistencePerspective, AppServices.DYNAMIC_ENTITY)
};
dataSource = new ListGridDataSource(name, persistencePerspective, AppServices.DYNAMIC_ENTITY, modules);
dataSource.buildFields(null, false, cb);
} else {
if (cb != null) {
cb.onSuccess(dataSource);
}
}
}
}
Code: Select all
public class ProductCustomPersistenceHandler extends CustomPersistenceHandlerAdapter {
private static final Log LOG = LogFactory.getLog(ProductCustomPersistenceHandler.class);
@Resource
private ProductService productService;
@Resource
private ProductCatalogService productCatalogService;
public Boolean canHandleFetch(PersistencePackage persistencePackage) {
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
String[] customCriteria = persistencePackage.getCustomCriteria();
return !ArrayUtils.isEmpty(customCriteria) && "productCatalogProductSearch".equals(customCriteria[0]) && Product.class.getName().equals(ceilingEntityFullyQualifiedClassname);
}
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> productProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
BaseCtoConverter ctoConverter = helper.getCtoConverter(persistencePerspective, cto, Product.class.getName(), productProperties);
//From within here, you can essentially do whatever you want to return the records that you need. Use JPA CriteriaBuilder, inject a custom service and call that, whatever
//If you go the CriteriaBuilder route, you can get an instance of it by doing: dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder();
//Get the criteria for the storeId
String storeId = persistencePackage.getCustomCriteria()[1];
ArrayList<Product> products = new ArrayList<Product>();
if (storeId != null) {
List<ProductCatalog> catalogs = productCatalogService.getProductCatalogsForStore(Long.parseLong(storeId));
for (ProductCatalog catalog : catalogs) {
products.addAll(productService.getProductsForCatalog(catalog.getId());
}
}
Entity[] entities = helper.getRecords(productProperties, products);
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);
}
}
Code: Select all
<bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
<property name="sourceList">
<list>
<bean class="com.mycompany.admin.server.service.handler.ProductCustomPersistenceHandler"/>
</list>
</property>
</bean>
Code: Select all
public class CatalogProductsDataSourceFactory implements DataSourceFactory {
public static CustomCriteriaListGridDataSource dataSource = null;
public void createDataSource(String name, OperationTypes operationTypes, Object[] additionalItems, AsyncCallback<DataSource> cb) {
if (dataSource == null) {
operationTypes = new OperationTypes(OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY, OperationType.ENTITY);
PersistencePerspective persistencePerspective = new PersistencePerspective(operationTypes, new String[]{}, new ForeignKey[]{});
DataSourceModule[] modules = new DataSourceModule[]{
new BasicClientEntityModule(CeilingEntities.PRODUCT, persistencePerspective, AppServices.DYNAMIC_ENTITY)
};
dataSource = new CustomCriteriaListGridDataSource(name, persistencePerspective, AppServices.DYNAMIC_ENTITY, modules);
dataSource.setCustomCriteria(new String[]{"productCatalogProductSearch", null});
dataSource.buildFields(null, false, cb);
} else {
if (cb != null) {
cb.onSuccess(dataSource);
}
}
}
}
Code: Select all
public class StorePresenter extends DynamicEntityPresenter implements Instantiable {
//other properties omitted
private CustomCriteriaListGridDataSource productCatalogProductDS;
protected void changeSelection(Record selectedRecord) {
String storeId = selectedRecord.getAttribute("id");
if (productCatalogProductDS != null) {
productCatalogProductDS.setCustomCriteria(new String[]{productCatalogProductDS.getCustomCriteria[0], storeId});
}
}
protected void setup() {
getPresenterSequenceSetupManager().addOrReplaceItem(new PresenterSetupItem("productSearchDS", new CatalogProductsDataSourceFactory(), new AsyncCallbackAdapter() {
public void onSetupSuccess(DataSource result) {
CustomCriteriaListGridDataSource productSearchDataSource = (CustomCriteriaListGridDataSource) result;
productCatalogProductDS = productSearchDataSource;
//change these field names to whichever fields you want to be displayed when the search happens. If you leave this blank, it will build a form based on the @AdminPresentation annotations you have on Product
productSearchDataSource.resetPermanentFieldVisibility(
"name",
"description"
);
//usually you want the autofetch to true here; otherwise you'll have to hit the funnel button on the search
EntitySearchDialog productSearchView = new EntitySearchDialog(productSearchDataSource, true);
//Here's where the real magic happens. This will add a proper callback to the '...' button on the defaultProduct property from 'storeDS' (which was instantiated above).
getPresenterSequenceSetupManager().getDataSource("storeDS").getFormItemCallbackHandlerManager().addSearchFormItemCallback(
"defaultProduct",
productSearchView,
"Search for a Product",
getDisplay().getDynamicFormDisplay()
);
}
}
}
Code: Select all
dataSource = new CustomCriteriaListGridDataSource(name, persistencePerspective,
AppServices.DYNAMIC_ENTITY, modules, true, false, false, false, false);
Return to “Broadleaf Commerce Admin”
Users browsing this forum: No registered users and 6 guests