Page 1 of 1
How to create domain entity object from entityForm?
Posted: Sat Mar 28, 2015 6:57 am
by prabhat.kataria
I am looking for a way to pull domain entity objects like
product,
admin user from entity form that we receice as model attribute in CRUD methods of EntityController classes like
AdminProductController.Basically I want to have this form object so that I can ask admin user to fill some of the form fields while other will be filled on backend.
Is it possible? If yes, how?
Below is a sample code from AdminProductController class.
Code: Select all
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addEntity(HttpServletRequest request, HttpServletResponse response, Model model,
@PathVariable Map<String, String> pathVars,
@ModelAttribute(value="entityForm") EntityForm entityForm, BindingResult result) throws Exception {
//some code
}
Re: How to create domain entity object from entityForm?
Posted: Mon Mar 30, 2015 11:57 pm
by phillipuniverse
Sounds like this is a good use of a CustomPersistenceHandler or a PersistenceEventHandler.
Re: How to create domain entity object from entityForm?
Posted: Sun Jul 19, 2015 12:28 am
by prabhat.kataria
Hi Phillip,
I am trying to solve my problem as per the inputs from your previous comment but I have following questions:
1. I was not able to find any class or interface by the name of PersistenceEventHandler. Were you trying to point to PersistenceManagerEventHandler?
2. I am trying to use the other option that you have mentioned i.e. CustomPersistenceHandler. I have extended ProductCustomPersistenceHandler to create my custom ProductPersistence handler. Here in add method I am trying to add custom values to the dynamicEntityDao as done in add dmethod of ProductCustomPersistenceHandler but I am still getting validation failed error. Below is the code that I am using. Am I doing something wrong here?
Code: Select all
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
CustomProduct adminInstance = (CustomProduct) Class.forName(entity.getType()[0]).newInstance();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
CustomAdminUser customAdminUser = null;
if(adminUser instanceof CustomAdminUser){
customAdminUser = (CustomAdminUser) adminUser;
}
Store userStore = customAdminUser.getStore();
adminInstance.setStore(userStore);
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
String manufacturer = userStore.getName();
adminInstance.setManufacturer(manufacturer);
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
//String url = String.format("/%s/%s", manufacturer, adminInstance.getDefaultSku().getName());
adminInstance.setUrl("/test");
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
Category category = categoryDao.readCategoryById(defaultCategoryId);
adminInstance.setDefaultCategory(category);
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
entity = helper.getRecord(adminProperties, adminInstance, null, null);
adminInstance = (CustomProduct) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
CategoryProductXref categoryXref = new CategoryProductXrefImpl();
categoryXref.setCategory(adminInstance.getDefaultCategory());
categoryXref.setProduct(adminInstance);
if (adminInstance.getDefaultCategory() != null && !adminInstance.getAllParentCategoryXrefs().contains(categoryXref)) {
categoryXref = (CategoryProductXref) dynamicEntityDao.merge(categoryXref);
adminInstance.getAllParentCategoryXrefs().add(categoryXref);
}
//Since none of the Sku fields are required, it's possible that the user did not fill out
//any Sku fields, and thus a Sku would not be created. Product still needs a default Sku so instantiate one
if (adminInstance.getDefaultSku() == null) {
Sku newSku = catalogService.createSku();
adminInstance.setDefaultSku(newSku);
adminInstance = (CustomProduct) dynamicEntityDao.merge(adminInstance);
}
//also set the default product for the Sku
adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
dynamicEntityDao.merge(adminInstance.getDefaultSku());
return helper.getRecord(adminProperties, adminInstance, null, null);
} catch (Exception e) {
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
Re: How to create domain entity object from entityForm?
Posted: Mon Jul 27, 2015 4:50 am
by phillipuniverse
1. I was not able to find any class or interface by the name of PersistenceEventHandler. Were you trying to point to PersistenceManagerEventHandler?
Yes, sorry I misspoke.
What validation error are you getting?
Re: How to create domain entity object from entityForm?
Posted: Mon Jul 27, 2015 6:04 am
by prabhat.kataria
I get validation error related to the fields that I am trying to assign values through code and not taking them directly from user. Such fields are Url, Manufacturer and Category.
Re: How to create domain entity object from entityForm?
Posted: Mon Jul 27, 2015 11:46 am
by phillipuniverse
We do not have any validation rules on Manufacturer out of the box. What validation rules fail and where?