use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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;
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method fetch.
@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
Entity[] payload;
int totalRecords;
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
try {
if (foreignKey != null && foreignKey.getSortField() != null) {
FilterAndSortCriteria sortCriteria = cto.get(foreignKey.getSortField());
sortCriteria.setSortAscending(foreignKey.getSortAscending());
}
Map<String, FieldMetadata> mergedProperties = getMergedProperties(persistencePackage, cto);
List<FilterMapping> filterMappings = getFilterMappings(persistencePerspective, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), mergedProperties);
List<FilterMapping> standardFilterMappings = new ArrayList<FilterMapping>(filterMappings);
if (CollectionUtils.isNotEmpty(cto.getAdditionalFilterMappings())) {
standardFilterMappings.addAll(cto.getAdditionalFilterMappings());
}
if (CollectionUtils.isNotEmpty(cto.getNonCountAdditionalFilterMappings())) {
standardFilterMappings.addAll(cto.getNonCountAdditionalFilterMappings());
}
FetchRequest fetchRequest = new FetchRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), standardFilterMappings);
List<Serializable> records = getPersistentRecords(fetchRequest);
List<FilterMapping> countFilterMappings = new ArrayList<FilterMapping>(filterMappings);
if (CollectionUtils.isNotEmpty(cto.getAdditionalFilterMappings())) {
countFilterMappings.addAll(cto.getAdditionalFilterMappings());
}
FetchRequest countFetchRequest = new FetchRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), countFilterMappings);
totalRecords = getTotalRecords(countFetchRequest);
FetchExtractionRequest fetchExtractionRequest = new FetchExtractionRequest(persistencePackage, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), mergedProperties, records);
payload = getRecords(fetchExtractionRequest);
} catch (Exception e) {
throw new ServiceException("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname, e);
}
return new DynamicResultSet(null, payload, totalRecords);
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata 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.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method extractPropertiesFromMetadata.
protected void extractPropertiesFromMetadata(Class<?>[] inheritanceLine, Map<String, FieldMetadata> mergedProperties, List<Property> properties, Boolean isHiddenOverride, MergedPropertyType type) {
for (Map.Entry<String, FieldMetadata> entry : mergedProperties.entrySet()) {
String property = entry.getKey();
Property prop = new Property();
FieldMetadata metadata = mergedProperties.get(property);
prop.setName(property);
Comparator<Property> comparator = new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
return o1.getName().compareTo(o2.getName());
}
};
Collections.sort(properties, comparator);
int pos = Collections.binarySearch(properties, prop, comparator);
if (pos >= 0 && MergedPropertyType.MAPSTRUCTUREKEY != type && MergedPropertyType.MAPSTRUCTUREVALUE != type) {
logWarn: {
if ((metadata instanceof BasicFieldMetadata) && SupportedFieldType.ID.equals(((BasicFieldMetadata) metadata).getFieldType())) {
// don't warn for id field collisions, but still ignore the colliding fields
break logWarn;
}
// LOG.warn("Detected a field name collision (" + metadata.getTargetClass() + "." + property + ") during inspection for the inheritance line starting with (" + inheritanceLine[0].getName() + "). Ignoring the additional field. This can occur most commonly when using the @AdminPresentationAdornedTargetCollection and the collection type and target class have field names in common. This situation should be avoided, as the system will strip the repeated fields, which can cause unpredictable behavior.");
}
continue;
}
properties.add(prop);
prop.setMetadata(metadata);
if (isHiddenOverride && prop.getMetadata() instanceof BasicFieldMetadata) {
// this only makes sense for non collection types
((BasicFieldMetadata) prop.getMetadata()).setVisibility(VisibilityEnum.HIDDEN_ALL);
}
}
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class BasicPersistenceModule method getRecords.
@Override
public Entity[] getRecords(Map<String, FieldMetadata> primaryUnfilteredMergedProperties, List<? extends Serializable> records, Map<String, FieldMetadata> alternateUnfilteredMergedProperties, String pathToTargetObject, String[] customCriteria) {
Map<String, FieldMetadata> primaryMergedProperties = filterOutCollectionMetadata(primaryUnfilteredMergedProperties);
Map<String, FieldMetadata> alternateMergedProperties = filterOutCollectionMetadata(alternateUnfilteredMergedProperties);
Entity[] entities = new Entity[records.size()];
int j = 0;
for (Serializable recordEntity : records) {
Serializable entity;
if (pathToTargetObject != null) {
try {
entity = (Serializable) getFieldManager().getFieldValue(recordEntity, pathToTargetObject);
} catch (Exception e) {
throw new PersistenceException(e);
}
} else {
entity = recordEntity;
}
Entity entityItem = new Entity();
entityItem.setType(new String[] { entity.getClass().getName() });
entities[j] = entityItem;
List<Property> props = new ArrayList<Property>(primaryMergedProperties.size());
extractPropertiesFromPersistentEntity(primaryMergedProperties, entity, props, customCriteria);
if (alternateMergedProperties != null) {
extractPropertiesFromPersistentEntity(alternateMergedProperties, recordEntity, props, customCriteria);
}
// Try to add the "main name" property. Log a debug message if we can't
try {
Property p = new Property();
p.setName(MAIN_ENTITY_NAME_PROPERTY);
String mainEntityName = (String) MethodUtils.invokeMethod(entity, "getMainEntityName");
p.setValue(mainEntityName);
props.add(p);
} catch (Exception e) {
LOG.debug(String.format("Could not execute the getMainEntityName() method for [%s]", entity.getClass().getName()), e);
}
// Try to add the alternate id property if available
if (alternateMergedProperties != null) {
for (Entry<String, FieldMetadata> entry : alternateMergedProperties.entrySet()) {
if (entry.getValue() instanceof BasicFieldMetadata) {
if (((BasicFieldMetadata) entry.getValue()).getFieldType() == SupportedFieldType.ID) {
Map<String, FieldMetadata> alternateOnEntity = new HashMap<String, FieldMetadata>();
alternateOnEntity.put(entry.getKey(), entry.getValue());
List<Property> props2 = new ArrayList<Property>();
extractPropertiesFromPersistentEntity(alternateOnEntity, recordEntity, props2, customCriteria);
List<Property> filtered = new ArrayList<Property>();
for (Property prop : props2) {
if (!prop.getName().startsWith("__")) {
filtered.add(prop);
}
}
if (filtered.size() == 1 && !filtered.get(0).getName().contains(".")) {
Property alternateIdProp = filtered.get(0);
alternateIdProp.setName(ALTERNATE_ID_PROPERTY);
props.add(alternateIdProp);
}
}
}
}
}
Property[] properties = new Property[props.size()];
properties = props.toArray(properties);
entityItem.setProperties(properties);
j++;
}
return entities;
}
Aggregations