use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminUserCustomPersistenceHandler 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(AdminUser.class.getName(), persistencePerspective);
Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
AdminUser adminInstance = (AdminUser) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
Entity errorEntity = validateLegalUsernameAndEmail(entity, adminInstance, false);
if (errorEntity != null) {
return errorEntity;
}
String passwordBefore = adminInstance.getPassword();
adminInstance.setPassword(null);
adminInstance = (AdminUser) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
Property passwordProperty = entity.getPMap().get("password");
if (passwordProperty != null) {
if (StringUtils.isNotEmpty(passwordProperty.getValue())) {
adminInstance.setUnencodedPassword(passwordProperty.getValue());
adminInstance.setPassword(null);
} else {
adminInstance.setPassword(passwordBefore);
}
}
validateUserUpdateSecurity(persistencePackage, adminInstance);
adminInstance = adminSecurityService.saveAdminUser(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.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminUserCustomPersistenceHandler method add.
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
adminRemoteSecurityService.securityCheck(persistencePackage, EntityOperationType.ADD);
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
AdminUser adminInstance = (AdminUser) Class.forName(entity.getType()[0]).newInstance();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(AdminUser.class.getName(), persistencePerspective);
adminInstance = (AdminUser) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
Entity errorEntity = validateLegalUsernameAndEmail(entity, adminInstance, true);
if (errorEntity != null) {
return errorEntity;
}
adminInstance.setUnencodedPassword(adminInstance.getPassword());
adminInstance.setPassword(null);
adminInstance = adminSecurityService.saveAdminUser(adminInstance);
Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
return adminEntity;
} catch (Exception e) {
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method getPagedRecordsForCollection.
@Override
public PersistenceResponse getPagedRecordsForCollection(ClassMetadata containingClassMetadata, Entity containingEntity, Property collectionProperty, FilterAndSortCriteria[] fascs, FetchPageRequest fetchPageRequest, String idValueOverride, List<SectionCrumb> sectionCrumbs) throws ServiceException {
PersistencePackageRequest ppr = PersistencePackageRequest.fromMetadata(collectionProperty.getMetadata(), sectionCrumbs).withFilterAndSortCriteria(fascs).withStartIndex(fetchPageRequest.getStartIndex()).withMaxIndex(fetchPageRequest.getMaxIndex()).withFirstId(fetchPageRequest.getFirstId()).withLastId(fetchPageRequest.getLastId()).withLowerCount(fetchPageRequest.getLowerCount()).withUpperCount(fetchPageRequest.getUpperCount()).withPageSize(fetchPageRequest.getPageSize()).withPresentationFetch(true);
FilterAndSortCriteria fasc;
FieldMetadata md = collectionProperty.getMetadata();
String collectionCeilingClass = null;
if (md instanceof BasicCollectionMetadata) {
fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
} else if (md instanceof AdornedTargetCollectionMetadata) {
fasc = new FilterAndSortCriteria(ppr.getAdornedList().getCollectionFieldName());
collectionCeilingClass = ((CollectionMetadata) md).getCollectionCeilingEntity();
} else if (md instanceof MapMetadata) {
fasc = new FilterAndSortCriteria(ppr.getForeignKey().getManyToField());
} else {
throw new IllegalArgumentException(String.format("The specified field [%s] for class [%s] was not a " + "collection field.", collectionProperty.getName(), containingClassMetadata.getCeilingType()));
}
String id;
if (idValueOverride == null) {
id = getContextSpecificRelationshipId(containingClassMetadata, containingEntity, collectionProperty.getName());
} else {
id = idValueOverride;
}
fasc.setFilterValue(id);
ppr.addFilterAndSortCriteria(fasc);
if (collectionCeilingClass != null) {
ppr.setCeilingEntityClassname(collectionCeilingClass);
}
ppr.setSectionEntityField(collectionProperty.getName());
return fetch(ppr);
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method buildProperties.
protected void buildProperties(Class<?> targetClass, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, MergedPropertyType mergedPropertyType, Map<String, FieldMetadata> presentationAttributes, List<Property> componentProperties, Map<String, FieldMetadata> fields, List<String> propertyNames, List<Type> propertyTypes, String idProperty, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, Boolean isComponentPrefix, String parentPrefix) {
int j = 0;
Comparator<String> propertyComparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// check for property name equality and for map field properties
if (o1.equals(o2) || o1.startsWith(o2 + FieldManager.MAPFIELDSEPARATOR) || o2.startsWith(o1 + FieldManager.MAPFIELDSEPARATOR)) {
return 0;
}
return o1.compareTo(o2);
}
};
List<String> presentationKeyList = new ArrayList<>(presentationAttributes.keySet());
Collections.sort(presentationKeyList);
for (String propertyName : propertyNames) {
final Type type = propertyTypes.get(j);
boolean isPropertyForeignKey = testForeignProperty(foreignField, prefix, propertyName);
int additionalForeignKeyIndexPosition = findAdditionalForeignKeyIndex(additionalForeignFields, prefix, propertyName);
j++;
Field myField = getFieldManager().getField(targetClass, propertyName);
if (myField == null) {
// try to get the field with the prefix - needed for advanced collections that appear in @Embedded classes
myField = getFieldManager().getField(targetClass, prefix + propertyName);
}
if (!type.isAnyType() && !type.isCollectionType() || isPropertyForeignKey || additionalForeignKeyIndexPosition >= 0 || Collections.binarySearch(presentationKeyList, propertyName, propertyComparator) >= 0) {
if (myField != null) {
boolean handled = false;
for (FieldMetadataProvider provider : fieldMetadataProviders) {
FieldMetadata presentationAttribute = presentationAttributes.get(propertyName);
if (presentationAttribute != null) {
setExcludedBasedOnShowIfProperty(presentationAttribute);
}
MetadataProviderResponse response = provider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(myField, targetClass, foreignField, additionalForeignFields, mergedPropertyType, componentProperties, idProperty, prefix, propertyName, type, isPropertyForeignKey, additionalForeignKeyIndexPosition, presentationAttributes, presentationAttribute, null, type.getReturnedClass(), this), fields);
if (MetadataProviderResponse.NOT_HANDLED != response) {
handled = true;
}
if (MetadataProviderResponse.HANDLED_BREAK == response) {
break;
}
}
if (!handled) {
buildBasicProperty(myField, targetClass, foreignField, additionalForeignFields, additionalNonPersistentProperties, mergedPropertyType, presentationAttributes, componentProperties, fields, idProperty, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, parentClasses, prefix, isParentExcluded, propertyName, type, isPropertyForeignKey, additionalForeignKeyIndexPosition, isComponentPrefix, parentPrefix);
}
}
}
}
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method getSimpleMergedProperties.
@Override
public Map<String, FieldMetadata> getSimpleMergedProperties(String entityName, PersistencePerspective persistencePerspective) {
Class<?>[] entityClasses;
try {
entityClasses = getAllPolymorphicEntitiesFromCeiling(Class.forName(entityName));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (!ArrayUtils.isEmpty(entityClasses)) {
return getMergedProperties(entityName, entityClasses, (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY), persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
} else {
Map<String, FieldMetadata> mergedProperties = new HashMap<>();
Class<?> targetClass;
try {
targetClass = Class.forName(entityName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Map<String, FieldMetadata> attributesMap = metadata.getFieldMetadataForTargetClass(null, targetClass, this, "");
for (String property : attributesMap.keySet()) {
FieldMetadata presentationAttribute = attributesMap.get(property);
if (!presentationAttribute.getExcluded()) {
Field field = FieldManager.getSingleField(targetClass, property);
if (!Modifier.isStatic(field.getModifiers())) {
boolean handled = false;
for (FieldMetadataProvider provider : fieldMetadataProviders) {
MetadataProviderResponse response = provider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
if (MetadataProviderResponse.NOT_HANDLED != response) {
handled = true;
}
if (MetadataProviderResponse.HANDLED_BREAK == response) {
break;
}
}
if (!handled) {
// this provider is not included in the provider list on purpose - it is designed to handle basic
// AdminPresentation fields, and those fields not admin presentation annotated at all
defaultFieldMetadataProvider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
}
}
}
}
return mergedProperties;
}
}
Aggregations