use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class AdminPermissionCustomPersistenceHandler method checkPermissionName.
protected Entity checkPermissionName(PersistencePackage persistencePackage) throws ServiceException {
Entity entity = persistencePackage.getEntity();
Property prop = entity.findProperty("name");
String name = prop.getValue();
name = name.toUpperCase();
if (!name.startsWith("PERMISSION_")) {
throw new ServiceException("All Permission names must start with PERMISSION_");
}
String[] parts = name.split("_");
if (parts.length < 3) {
throw new ServiceException("All Permission names must adhere to the naming standard: PERMISSION_[Permission Type]_[User Defined Section]. E.g. PERMISSION_READ_CATEGORY");
}
if (PermissionType.getInstance(parts[1]) == null) {
throw new ServiceException("All Permission names must specify a valid permission type as part of the name. The permission name you specified (" + name + ") denotes the permission type of (" + parts[1] + "), which is not valid. See org.broadleafcommerce.openadmin.server.security.service.type.PermissionType for valid permission types.");
}
prop.setValue(name);
return entity;
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class AdminUserCustomPersistenceHandler method remove.
@Override
public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
String idValue = entity.findProperty("id").getValue();
String userLoginToRemove = entity.findProperty("login") == null ? null : entity.findProperty("login").getValue();
AdminUser persistentAdminUser = adminRemoteSecurityService.getPersistentAdminUser();
if (persistentAdminUser != null && persistentAdminUser.getLogin() != null && userLoginToRemove != null) {
if (persistentAdminUser.getLogin().equals(userLoginToRemove)) {
throw new ValidationException(entity, "admin.cantDeleteCurrentUserError");
}
}
if (idValue != null) {
Long id = Long.parseLong(idValue);
AdminUser adminInstance = adminSecurityService.readAdminUserById(id);
// Check if Status was Weaved in
if (Status.class.isAssignableFrom(adminInstance.getClass())) {
((Status) adminInstance).setArchived('Y');
adminSecurityService.saveAdminUser(adminInstance);
return;
}
}
OperationType removeType = persistencePackage.getPersistencePerspective().getOperationTypes().getRemoveType();
helper.getCompatibleModule(removeType).remove(persistencePackage);
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method add.
@Override
public Entity add(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 add types other than BASIC");
}
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
Entity entity = persistencePackage.getEntity();
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
if (!adornedTargetList.getMutable()) {
throw new SecurityServiceException("Field is not mutable");
}
Entity payload;
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(), "");
Class<?>[] entities2 = persistenceManager.getPolymorphicEntities(adornedTargetList.getAdornedTargetEntityClassname());
Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(adornedTargetList.getAdornedTargetEntityClassname(), entities2, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.ADORNEDTARGETLIST, false, new String[] {}, new String[] {}, null, "");
CriteriaTransferObject ctoInserted = new CriteriaTransferObject();
FilterAndSortCriteria filterCriteriaInsertedLinked = ctoInserted.get(adornedTargetList.getCollectionFieldName());
String linkedPath;
String targetPath;
if (adornedTargetList.getInverse()) {
linkedPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
targetPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
} else {
targetPath = adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty();
linkedPath = adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty();
}
filterCriteriaInsertedLinked.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? targetPath : linkedPath).getValue());
FilterAndSortCriteria filterCriteriaInsertedTarget = ctoInserted.get(adornedTargetList.getCollectionFieldName() + "Target");
filterCriteriaInsertedTarget.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? linkedPath : targetPath).getValue());
List<FilterMapping> filterMappingsInserted = getAdornedTargetFilterMappings(persistencePerspective, ctoInserted, mergedProperties, adornedTargetList);
List<Serializable> recordsInserted = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappingsInserted, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
if (recordsInserted.size() > 0) {
payload = getRecords(mergedPropertiesTarget, recordsInserted, mergedProperties, adornedTargetList.getTargetObjectPath(), null)[0];
} else {
Serializable instance = createPopulatedAdornedTargetInstance(adornedTargetList, entity);
instance = createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
instance = createPopulatedInstance(instance, entity, mergedPropertiesTarget, false, persistencePackage.isValidateUnsubmittedProperties());
FieldManager fieldManager = getFieldManager();
if (fieldManager.getField(instance.getClass(), "id") != null) {
fieldManager.setFieldValue(instance, "id", null);
}
if (adornedTargetList.getSortField() != null) {
// Construct a query that gets the last element in the join list ordered by the sort property. This will
// ensure that the new record is always the last element in the list
CriteriaTransferObject cto = new CriteriaTransferObject();
FilterAndSortCriteria filterCriteria = cto.get(adornedTargetList.getCollectionFieldName());
filterCriteria.setFilterValue(entity.findProperty(adornedTargetList.getInverse() ? targetPath : linkedPath).getValue());
FilterAndSortCriteria sortCriteria = cto.get(adornedTargetList.getSortField());
// criteria for which way to sort should be the opposite of how it is normally sorted so that it is
// always inserted at the end
sortCriteria.setSortAscending(!adornedTargetList.getSortAscending());
List<FilterMapping> filterMappings = getAdornedTargetFilterMappings(persistencePerspective, cto, mergedProperties, adornedTargetList);
List<Serializable> joinList = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappings, 0, 1);
Object adornedLastOrdering = null;
if (CollectionUtils.isNotEmpty(joinList)) {
adornedLastOrdering = fieldManager.getFieldValue(joinList.get(0), adornedTargetList.getSortField());
}
Field sortFieldDef = fieldManager.getField(instance.getClass(), adornedTargetList.getSortField());
int add = (adornedLastOrdering == null) ? 0 : 1;
if (sortFieldDef.getType().isAssignableFrom(Long.class)) {
adornedLastOrdering = (adornedLastOrdering == null) ? new Long(0) : adornedLastOrdering;
fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), new Long(((Long) adornedLastOrdering).longValue() + add));
} else if (sortFieldDef.getType().isAssignableFrom(Integer.class)) {
adornedLastOrdering = (adornedLastOrdering == null) ? new Integer(0) : adornedLastOrdering;
fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), new Integer(((Integer) adornedLastOrdering).intValue() + add));
} else if (sortFieldDef.getType().isAssignableFrom(BigDecimal.class)) {
adornedLastOrdering = (adornedLastOrdering == null) ? BigDecimal.ZERO : adornedLastOrdering;
fieldManager.setFieldValue(instance, adornedTargetList.getSortField(), ((BigDecimal) adornedLastOrdering).add(new BigDecimal(add)));
}
}
instance = persistenceManager.getDynamicEntityDao().merge(instance);
persistenceManager.getDynamicEntityDao().clear();
List<Serializable> recordsInserted2 = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappingsInserted, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
payload = getRecords(mergedPropertiesTarget, recordsInserted2, mergedProperties, adornedTargetList.getTargetObjectPath(), null)[0];
}
} catch (Exception e) {
throw new ServiceException("Problem adding new entity : " + e.getMessage(), e);
}
return payload;
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class AdornedTargetListPersistenceModule method remove.
@Override
public void remove(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 remove types other than BASIC");
}
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Entity entity = persistencePackage.getEntity();
try {
AdornedTargetList adornedTargetList = (AdornedTargetList) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
if (!adornedTargetList.getMutable()) {
throw new SecurityServiceException("Field is not mutable");
}
Class<?>[] entities = persistenceManager.getPolymorphicEntities(adornedTargetList.getAdornedTargetEntityClassname());
Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(adornedTargetList.getAdornedTargetEntityClassname(), entities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.ADORNEDTARGETLIST, false, new String[] {}, new String[] {}, null, "");
CriteriaTransferObject ctoInserted = new CriteriaTransferObject();
FilterAndSortCriteria filterCriteriaInsertedLinked = ctoInserted.get(adornedTargetList.getCollectionFieldName());
filterCriteriaInsertedLinked.setFilterValue(entity.findProperty(adornedTargetList.getLinkedObjectPath() + "." + adornedTargetList.getLinkedIdProperty()).getValue());
FilterAndSortCriteria filterCriteriaInsertedTarget = ctoInserted.get(adornedTargetList.getCollectionFieldName() + "Target");
filterCriteriaInsertedTarget.setFilterValue(entity.findProperty(adornedTargetList.getTargetObjectPath() + "." + adornedTargetList.getTargetIdProperty()).getValue());
List<FilterMapping> filterMappings = getAdornedTargetFilterMappings(persistencePerspective, ctoInserted, mergedProperties, adornedTargetList);
List<Serializable> recordsInserted = getPersistentRecords(adornedTargetList.getAdornedTargetEntityClassname(), filterMappings, ctoInserted.getFirstResult(), ctoInserted.getMaxResults());
Assert.isTrue(!CollectionUtils.isEmpty(recordsInserted), "Entity not found");
persistenceManager.getDynamicEntityDao().remove(recordsInserted.get(0));
} catch (Exception e) {
throw new ServiceException("Problem removing entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method remove.
@Override
public void remove(PersistencePackage persistencePackage) throws ServiceException {
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);
Object primaryKey = getPrimaryKey(entity, mergedProperties);
Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), primaryKey);
Assert.isTrue(instance != null, "Entity not found");
switch(persistencePerspective.getOperationTypes().getRemoveType()) {
case NONDESTRUCTIVEREMOVE:
FieldManager fieldManager = getFieldManager();
FieldMetadata manyToFieldMetadata = mergedUnfilteredProperties.get(foreignKey.getManyToField());
Object foreignKeyValue = entity.getPMap().get(foreignKey.getManyToField()).getValue();
try {
foreignKeyValue = Long.valueOf((String) foreignKeyValue);
} catch (NumberFormatException e) {
LOG.warn("Foreign primary key is not of type Long, assuming String for remove lookup");
}
Serializable foreignInstance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(foreignKey.getForeignKeyClass()), foreignKeyValue);
Collection collection = (Collection) fieldManager.getFieldValue(foreignInstance, foreignKey.getOriginatingField());
collection.remove(instance);
// set the manyTo field to null so that subsequent lookups will not find it
if (manyToFieldMetadata instanceof BasicFieldMetadata) {
if (BooleanUtils.isTrue(((BasicFieldMetadata) manyToFieldMetadata).getRequired())) {
throw new ServiceException("Could not remove from the collection as the ManyToOne side is a" + " non-optional relationship. Consider changing 'optional=true' in the @ManyToOne annotation" + " or nullable=true within the @JoinColumn annotation");
}
// Since this is occuring on a remove persistence package, merge up-front (before making a change) for proper operation in the presence of the enterprise module
instance = persistenceManager.getDynamicEntityDao().merge(instance);
Field manyToField = fieldManager.getField(instance.getClass(), foreignKey.getManyToField());
Object manyToObject = manyToField.get(instance);
if (manyToObject != null && !(manyToObject instanceof Collection) && !(manyToObject instanceof Map)) {
manyToField.set(instance, null);
instance = persistenceManager.getDynamicEntityDao().merge(instance);
}
}
break;
case BASIC:
persistenceManager.getDynamicEntityDao().remove(instance);
break;
}
} catch (Exception e) {
throw new ServiceException("Problem removing entity : " + e.getMessage(), e);
}
}
Aggregations