use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.
the class CustomerCustomPersistenceHandler method add.
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Customer adminInstance = (Customer) Class.forName(entity.getType()[0]).newInstance();
adminInstance.setId(customerService.findNextCustomerId());
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
if (useEmailForLogin) {
adminInstance.setUsername(adminInstance.getEmailAddress());
}
if (!entity.isPreAdd()) {
Entity errorEntity = validateUniqueUsername(entity, adminInstance);
if (errorEntity != null) {
return errorEntity;
}
}
adminInstance = dynamicEntityDao.merge(adminInstance);
customerService.createRegisteredCustomerRoles(adminInstance);
Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
return adminEntity;
} catch (Exception e) {
LOG.error("Unable to execute persistence activity", e);
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.
the class CustomerCustomPersistenceHandler method update.
@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
Customer adminInstance = (Customer) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
String passwordBefore = adminInstance.getPassword();
adminInstance.setPassword(null);
adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
adminInstance.setPassword(passwordBefore);
if (useEmailForLogin) {
adminInstance.setUsername(adminInstance.getEmailAddress());
}
adminInstance = customerService.saveCustomer(adminInstance);
Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
return adminEntity;
} catch (Exception e) {
throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.
the class IndexFieldCustomPersistenceHandler method update.
@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective);
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
IndexField adminInstance = (IndexField) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
return getEntity(persistencePackage, dynamicEntityDao, helper, entity, adminProperties, adminInstance);
} catch (Exception e) {
throw new ServiceException("Unable to perform update for entity: " + IndexField.class.getName(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method update.
@Override
public Entity update(PersistencePackage persistencePackage) throws ServiceException {
String[] customCriteria = persistencePackage.getCustomCriteria();
if (customCriteria != null && customCriteria.length > 0) {
LOG.warn("custom persistence handlers and custom criteria not supported for update types other than BASIC");
}
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Entity entity = persistencePackage.getEntity();
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
if (!adornedTargetList.getMutable()) {
throw new SecurityServiceException("Field is not mutable");
}
try {
AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, entity, adornedTargetList).invokeForUpdate();
List<Serializable> records = adornedTargetRetrieval.getRecords();
Assert.isTrue(!CollectionUtils.isEmpty(records), "Entity not found");
int index = adornedTargetRetrieval.getIndex();
Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
FieldManager fieldManager = getFieldManager();
Serializable myRecord;
if (adornedTargetList.getSortField() != null && entity.findProperty(adornedTargetList.getSortField()).getValue() != null) {
myRecord = records.get(index);
BigDecimal requestedSequence = new BigDecimal(entity.findProperty(adornedTargetList.getSortField()).getValue());
BigDecimal previousSequence = new BigDecimal(String.valueOf(getFieldManager().getFieldValue(myRecord, adornedTargetList.getSortField())));
if (!previousSequence.equals(requestedSequence)) {
// Sequence has changed. Rebalance the list
myRecord = records.remove(index);
myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
if (CollectionUtils.isEmpty(records)) {
records.add(myRecord);
} else {
records.add(requestedSequence.intValue() - 1, myRecord);
}
index = 1;
Class<?> type = fieldManager.getField(myRecord.getClass(), adornedTargetList.getSortField()).getType();
boolean isBigDecimal = BigDecimal.class.isAssignableFrom(type);
for (Serializable record : records) {
fieldManager.setFieldValue(record, adornedTargetList.getSortField(), isBigDecimal ? new BigDecimal(index) : Long.valueOf(index));
index++;
}
}
} else {
myRecord = records.get(index);
}
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
myRecord = createPopulatedInstance(myRecord, entity, mergedProperties, false);
myRecord = persistenceManager.getDynamicEntityDao().merge(myRecord);
List<Serializable> myList = new ArrayList<Serializable>();
myList.add(myRecord);
Entity[] payload = getRecords(mergedPropertiesTarget, myList, mergedProperties, adornedTargetList.getTargetObjectPath(), null);
entity = payload[0];
return entity;
} catch (Exception e) {
throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.PersistencePerspective in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method fetch.
@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
Entity[] payload;
int totalRecords;
try {
Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
Map<String, FieldMetadata> mergedPropertiesTarget = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, null, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
AdornedTargetRetrieval adornedTargetRetrieval = new AdornedTargetRetrieval(persistencePackage, adornedTargetList, cto).invokeForFetch();
List<Serializable> records = adornedTargetRetrieval.getRecords();
Map<String, FieldMetadata> mergedProperties = adornedTargetRetrieval.getMergedProperties();
payload = getRecords(mergedPropertiesTarget, records, mergedProperties, adornedTargetList.getTargetObjectPath(), persistencePackage.getCustomCriteria());
totalRecords = getTotalRecords(adornedTargetList.getAdornedTargetEntityClassname(), adornedTargetRetrieval.getFilterMappings());
} catch (Exception e) {
throw new ServiceException("Unable to fetch results for " + adornedTargetList.getAdornedTargetEntityClassname(), e);
}
DynamicResultSet results = new DynamicResultSet(null, payload, totalRecords);
return results;
}
Aggregations