use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class RuleFieldPersistenceProvider method convertQuantityBasedRuleToJson.
protected Property convertQuantityBasedRuleToJson(MVELToDataWrapperTranslator translator, ObjectMapper mapper, Collection<QuantityBasedRule> quantityBasedRules, String jsonProp, String fieldService) {
int k = 0;
Entity[] targetItemCriterias = new Entity[quantityBasedRules.size()];
for (QuantityBasedRule quantityBasedRule : quantityBasedRules) {
Property[] properties = new Property[4];
Property mvelProperty = new Property();
mvelProperty.setName("matchRule");
mvelProperty.setValue(quantityBasedRule.getMatchRule());
Property quantityProperty = new Property();
quantityProperty.setName("quantity");
quantityProperty.setValue(quantityBasedRule.getQuantity().toString());
Property idProperty = new Property();
idProperty.setName("id");
Long id = quantityBasedRule.getId();
id = transformId(id, quantityBasedRule);
idProperty.setValue(String.valueOf(id));
Object containedRule = findContainedRuleIfApplicable(quantityBasedRule);
Property containedIdProperty = new Property();
if (containedRule != null) {
containedIdProperty.setName("containedId");
EntityManager em = PersistenceManagerFactory.getDefaultPersistenceManager().getDynamicEntityDao().getStandardEntityManager();
Long containedId = (Long) em.unwrap(Session.class).getIdentifier(containedRule);
containedId = transformId(containedId, containedRule);
containedIdProperty.setValue(String.valueOf(containedId));
}
properties[0] = mvelProperty;
properties[1] = quantityProperty;
properties[2] = idProperty;
properties[3] = containedIdProperty;
Entity criteria = new Entity();
criteria.setProperties(properties);
targetItemCriterias[k] = criteria;
k++;
}
String json;
try {
DataWrapper oiWrapper = translator.createRuleData(targetItemCriterias, "matchRule", "quantity", "id", "containedId", ruleBuilderFieldServiceFactory.createInstance(fieldService));
json = mapper.writeValueAsString(oiWrapper);
} catch (Exception e) {
throw new RuntimeException(e);
}
Property p = new Property();
p.setName(jsonProp);
p.setValue(json);
return p;
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule method fetch.
@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto) throws ServiceException {
Entity[] payload;
int totalRecords;
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
if (StringUtils.isEmpty(persistencePackage.getFetchTypeFullyQualifiedClassname())) {
persistencePackage.setFetchTypeFullyQualifiedClassname(ceilingEntityFullyQualifiedClassname);
}
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Class<?>[] entities = persistenceManager.getPolymorphicEntities(ceilingEntityFullyQualifiedClassname);
Map<String, FieldMetadata> mergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(ceilingEntityFullyQualifiedClassname, entities, (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY), persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
MapStructure mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
PersistentClass persistentClass = persistenceManager.getDynamicEntityDao().getPersistentClass(mapStructure.getValueClassName());
Map<String, FieldMetadata> valueUnfilteredMergedProperties;
if (persistentClass == null) {
valueUnfilteredMergedProperties = persistenceManager.getDynamicEntityDao().getPropertiesForPrimitiveClass(((SimpleValueMapStructure) mapStructure).getValuePropertyName(), ((SimpleValueMapStructure) mapStructure).getValuePropertyFriendlyName(), Class.forName(mapStructure.getValueClassName()), Class.forName(ceilingEntityFullyQualifiedClassname), MergedPropertyType.MAPSTRUCTUREVALUE);
} else {
String valueClassName = mapStructure.getValueClassName();
Class<?>[] mapEntities = persistenceManager.getPolymorphicEntities(valueClassName);
valueUnfilteredMergedProperties = persistenceManager.getDynamicEntityDao().getMergedProperties(valueClassName, mapEntities, null, new String[] {}, new ForeignKey[] {}, MergedPropertyType.MAPSTRUCTUREVALUE, false, new String[] {}, new String[] {}, null, "");
}
Map<String, FieldMetadata> valueMergedProperties = filterOutCollectionMetadata(valueUnfilteredMergedProperties);
List<FilterMapping> filterMappings = getFilterMappings(persistencePerspective, cto, persistencePackage.getFetchTypeFullyQualifiedClassname(), mergedProperties);
if (CollectionUtils.isNotEmpty(cto.getAdditionalFilterMappings())) {
filterMappings.addAll(cto.getAdditionalFilterMappings());
}
totalRecords = getTotalRecords(persistencePackage.getFetchTypeFullyQualifiedClassname(), filterMappings);
if (totalRecords > 1) {
throw new ServiceException("Queries to retrieve an entity containing a MapStructure must return only 1 entity. Your query returned (" + totalRecords + ") values.");
}
List<Serializable> records = getPersistentRecords(persistencePackage.getFetchTypeFullyQualifiedClassname(), filterMappings, cto.getFirstResult(), cto.getMaxResults());
Map<String, FieldMetadata> ceilingMergedProperties = getSimpleMergedProperties(ceilingEntityFullyQualifiedClassname, persistencePerspective);
payload = getMapRecords(records.get(0), mapStructure, ceilingMergedProperties, valueMergedProperties, null, persistencePackage.getCustomCriteria());
} catch (Exception e) {
throw new ServiceException("Unable to fetch results for " + ceilingEntityFullyQualifiedClassname, e);
}
DynamicResultSet results = new DynamicResultSet(null, payload, payload.length);
return results;
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule method getMapRecord.
protected Entity getMapRecord(String ceilingClass, Serializable valueInstance, MapStructure mapStructure, Map<String, FieldMetadata> valueMergedProperties, Property symbolicIdProperty, Object key, String[] customCriteria) {
Entity entityItem = new Entity();
entityItem.setType(new String[] { ceilingClass });
List<Property> props = new ArrayList<Property>();
Property keyProperty = new Property();
keyProperty.setName(mapStructure.getKeyPropertyName());
String keyPropertyValue;
if (Date.class.isAssignableFrom(key.getClass())) {
keyPropertyValue = getSimpleDateFormatter().format((Date) key);
} else if (Timestamp.class.isAssignableFrom(key.getClass())) {
keyPropertyValue = getSimpleDateFormatter().format(new Date(((Timestamp) key).getTime()));
} else if (Calendar.class.isAssignableFrom(key.getClass())) {
keyPropertyValue = getSimpleDateFormatter().format(((Calendar) key).getTime());
} else if (Double.class.isAssignableFrom(key.getClass())) {
keyPropertyValue = getDecimalFormatter().format(key);
} else if (BigDecimal.class.isAssignableFrom(key.getClass())) {
keyPropertyValue = getDecimalFormatter().format(key);
} else {
keyPropertyValue = key.toString();
}
keyProperty.setValue(keyPropertyValue);
props.add(keyProperty);
if (SimpleValueMapStructure.class.isInstance(mapStructure)) {
SimpleValueMapStructure simpleValueMapStructure = (SimpleValueMapStructure) mapStructure;
Property valueProperty = new Property();
valueProperty.setName(simpleValueMapStructure.getValuePropertyName());
valueProperty.setDisplayValue((String) valueInstance);
valueProperty.setValue((String) valueInstance);
props.add(valueProperty);
}
extractPropertiesFromPersistentEntity(valueMergedProperties, valueInstance, props, customCriteria);
if (symbolicIdProperty != null) {
props.add(symbolicIdProperty);
}
Property[] properties = new Property[props.size()];
properties = props.toArray(properties);
entityItem.setProperties(properties);
return entityItem;
}
use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule 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();
MapStructure mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
if (!mapStructure.getMutable()) {
throw new SecurityServiceException("Field not mutable");
}
try {
Map<String, FieldMetadata> ceilingMergedProperties = getSimpleMergedProperties(entity.getType()[0], persistencePerspective);
String mapKey = entity.findProperty(mapStructure.getKeyPropertyName()).getValue();
if (ceilingMergedProperties.containsKey(mapStructure.getMapProperty() + FieldManager.MAPFIELDSEPARATOR + mapKey)) {
throw new ServiceException("\"" + mapKey + "\" is a reserved property name.");
}
Serializable instance = persistenceManager.getDynamicEntityDao().retrieve(Class.forName(entity.getType()[0]), Long.valueOf(entity.findProperty("symbolicId").getValue()));
Assert.isTrue(instance != null, "Entity not found");
FieldManager fieldManager = getFieldManager();
Map map = (Map) fieldManager.getFieldValue(instance, mapStructure.getMapProperty());
Object value = map.remove(entity.findProperty("priorKey").getValue());
if (mapStructure.getDeleteValueEntity()) {
persistenceManager.getDynamicEntityDao().remove((Serializable) value);
}
} 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 DefaultBasicPersistenceModuleExtensionHandler method rebalanceForUpdate.
@Override
public ExtensionResultStatusType rebalanceForUpdate(final BasicPersistenceModule basicPersistenceModule, PersistencePackage persistencePackage, Serializable instance, Map<String, FieldMetadata> mergedProperties, Object primaryKey, ExtensionResultHolder<Serializable> resultHolder) {
try {
Entity entity = persistencePackage.getEntity();
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
Integer requestedSequence = Integer.valueOf(entity.findProperty(foreignKey.getSortField()).getValue());
Integer previousSequence = new BigDecimal(String.valueOf(basicPersistenceModule.getFieldManager().getFieldValue(instance, foreignKey.getSortField()))).intValue();
final String idPropertyName = basicPersistenceModule.getIdPropertyName(mergedProperties);
final Object pKey = primaryKey;
instance = basicPersistenceModule.createPopulatedInstance(instance, entity, mergedProperties, false, persistencePackage.isValidateUnsubmittedProperties());
if (!previousSequence.equals(requestedSequence)) {
// Sequence has changed. Rebalance the list
Serializable manyToField = (Serializable) basicPersistenceModule.getFieldManager().getFieldValue(instance, foreignKey.getManyToField());
List<Serializable> records = (List<Serializable>) basicPersistenceModule.getFieldManager().getFieldValue(manyToField, foreignKey.getOriginatingField());
Serializable myRecord = (Serializable) CollectionUtils.find(records, new TypedPredicate<Serializable>() {
@Override
public boolean eval(Serializable record) {
try {
return (pKey.equals(basicPersistenceModule.getFieldManager().getFieldValue(record, idPropertyName)));
} catch (IllegalAccessException e) {
return false;
} catch (FieldNotAvailableException e) {
return false;
}
}
});
records.remove(myRecord);
if (CollectionUtils.isEmpty(records)) {
records.add(myRecord);
} else {
records.add(requestedSequence - 1, myRecord);
}
int index = 1;
Class<?> type = basicPersistenceModule.getFieldManager().getField(myRecord.getClass(), foreignKey.getSortField()).getType();
boolean isBigDecimal = BigDecimal.class.isAssignableFrom(type);
for (Serializable record : records) {
basicPersistenceModule.getFieldManager().setFieldValue(record, foreignKey.getSortField(), isBigDecimal ? new BigDecimal(index) : Long.valueOf(index));
index++;
}
}
resultHolder.setResult(instance);
} catch (IllegalAccessException e) {
throw ExceptionHelper.refineException(e);
} catch (FieldNotAvailableException e) {
throw ExceptionHelper.refineException(e);
} catch (ValidationException e) {
throw ExceptionHelper.refineException(e);
} catch (InstantiationException e) {
throw ExceptionHelper.refineException(e);
}
return ExtensionResultStatusType.HANDLED;
}
Aggregations