use of org.broadleafcommerce.openadmin.dto.EntityResult in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method add.
@Override
public EntityResult add(PersistencePackage persistencePackage, boolean includeRealEntityObject) throws ServiceException {
EntityResult entityResult = new EntityResult();
Entity entity = persistencePackage.getEntity();
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
if (foreignKey != null && !foreignKey.getMutable()) {
throw new SecurityServiceException("Entity not mutable");
}
try {
Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
Map<String, FieldMetadata> mergedUnfilteredProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
Map<String, FieldMetadata> mergedProperties = filterOutCollectionMetadata(mergedUnfilteredProperties);
String idProperty = null;
for (String property : mergedProperties.keySet()) {
if (((BasicFieldMetadata) mergedProperties.get(property)).getFieldType() == SupportedFieldType.ID) {
idProperty = property;
break;
}
}
if (idProperty == null) {
throw new RuntimeException("Could not find a primary key property in the passed entity with type: " + entity.getType()[0]);
}
Object primaryKey = null;
try {
primaryKey = getPrimaryKey(entity, mergedProperties);
} catch (Exception e) {
// don't do anything - this is a valid case
}
if (primaryKey == null) {
Serializable instance = (Serializable) Class.forName(entity.getType()[0]).newInstance();
instance = createPopulatedInstance(instance, entity, mergedProperties, false);
if (foreignKey != null && foreignKey.getSortField() != null) {
ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
extensionManager.getProxy().rebalanceForAdd(this, persistencePackage, instance, mergedProperties, result);
instance = result.getResult();
}
instance = persistenceManager.getDynamicEntityDao().merge(instance);
if (includeRealEntityObject) {
entityResult.setEntityBackingObject(instance);
}
List<Serializable> entityList = new ArrayList<Serializable>(1);
entityList.add(instance);
entity = getRecords(mergedProperties, entityList, null, null, null)[0];
entityResult.setEntity(entity);
return entityResult;
} else {
return update(persistencePackage, primaryKey, includeRealEntityObject);
}
} catch (Exception e) {
throw new ServiceException("Problem adding new entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.EntityResult in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method update.
protected EntityResult update(PersistencePackage persistencePackage, Object primaryKey, boolean includeRealEntity) throws ServiceException {
EntityResult entityResult = new EntityResult();
Entity entity = persistencePackage.getEntity();
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
if (foreignKey != null && !foreignKey.getMutable()) {
throw new SecurityServiceException("Entity not mutable");
}
try {
Class<?>[] entities = persistenceManager.getPolymorphicEntities(persistencePackage.getCeilingEntityFullyQualifiedClassname());
Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(persistencePackage.getCeilingEntityFullyQualifiedClassname(), entities, foreignKey, persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
if (primaryKey == null) {
primaryKey = getPrimaryKey(entity, mergedProperties);
}
Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), primaryKey);
Assert.isTrue(instance != null, "Entity not found");
if (!entity.isValidationFailure()) {
// Re-Balance the list if it is a Foreign Key toMany collection with a sort field property
if (foreignKey != null && foreignKey.getSortField() != null && entity.findProperty(foreignKey.getSortField()) != null && entity.findProperty(foreignKey.getSortField()).getValue() != null) {
ExtensionResultHolder<Serializable> result = new ExtensionResultHolder<Serializable>();
extensionManager.getProxy().rebalanceForUpdate(this, persistencePackage, instance, mergedProperties, primaryKey, result);
instance = result.getResult();
} else {
instance = createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
}
instance = persistenceManager.getDynamicEntityDao().merge(instance);
if (includeRealEntity) {
entityResult.setEntityBackingObject(instance);
}
List<Serializable> entityList = new ArrayList<Serializable>(1);
entityList.add(instance);
entity = getRecords(mergedProperties, entityList, null, null, null)[0];
entityResult.setEntity(entity);
return entityResult;
} else {
entityResult.setEntity(entity);
return entityResult;
}
} catch (Exception e) {
throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
}
}
Aggregations