use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule 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();
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 (StringUtils.isEmpty(mapKey)) {
entity.addValidationError(mapStructure.getKeyPropertyName(), RequiredPropertyValidator.ERROR_MESSAGE);
LOG.debug("No key property passed in for map, failing validation");
}
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());
if (map.containsKey(mapKey)) {
entity.addValidationError(mapStructure.getKeyPropertyName(), "keyExistsValidationError");
}
if (StringUtils.isNotBlank(mapStructure.getMapKeyValueProperty())) {
Property p = entity.findProperty("key");
Property newP = new Property();
newP.setName(mapStructure.getMapKeyValueProperty());
newP.setValue(p.getValue());
newP.setIsDirty(p.getIsDirty());
entity.addProperty(newP);
}
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(entity.getType()[0]), 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, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
}
Map<String, FieldMetadata> valueMergedProperties = filterOutCollectionMetadata(valueUnfilteredMergedProperties);
if (persistentClass != null) {
Serializable valueInstance = (Serializable) Class.forName(mapStructure.getValueClassName()).newInstance();
valueInstance = createPopulatedInstance(valueInstance, entity, valueMergedProperties, false);
if (valueInstance instanceof ValueAssignable) {
// This is likely a OneToMany map (see productAttributes) whose map key is actually the name field from
// the mapped entity.
((ValueAssignable) valueInstance).setName(entity.findProperty(mapStructure.getKeyPropertyName()).getValue());
}
if (mapStructure.getManyToField() != null) {
// Need to fulfill a bi-directional association back to the parent entity
fieldManager.setFieldValue(valueInstance, mapStructure.getManyToField(), instance);
}
valueInstance = persistenceManager.getDynamicEntityDao().persist(valueInstance);
/*
* TODO this map manipulation code currently assumes the key value is a String. This should be widened to accept
* additional types of primitive objects.
*/
map.put(mapKey, valueInstance);
} else {
String propertyName = ((SimpleValueMapStructure) mapStructure).getValuePropertyName();
String value = entity.findProperty(propertyName).getValue();
Object convertedPrimitive = convertPrimitiveBasedOnType(propertyName, value, valueMergedProperties);
map.put(mapKey, convertedPrimitive);
}
Entity[] responses = getMapRecords(instance, mapStructure, ceilingMergedProperties, valueMergedProperties, entity.findProperty("symbolicId"), null);
for (Entity response : responses) {
if (response.findProperty(mapStructure.getKeyPropertyName()).getValue().equals(persistencePackage.getEntity().findProperty(mapStructure.getKeyPropertyName()).getValue())) {
return response;
}
}
return responses[0];
} catch (Exception e) {
throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class MapStructurePersistenceModule method update.
@Override
public Entity update(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 update 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());
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(entity.getType()[0]), 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, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
}
Map<String, FieldMetadata> valueMergedProperties = filterOutCollectionMetadata(valueUnfilteredMergedProperties);
if (StringUtils.isEmpty(mapKey)) {
entity.addValidationError(mapStructure.getKeyPropertyName(), RequiredPropertyValidator.ERROR_MESSAGE);
LOG.debug("No key property passed in for map, failing validation");
}
populate: {
if (persistentClass != null) {
Serializable valueInstance = (Serializable) map.get(entity.findProperty("priorKey").getValue());
if (valueInstance == null) {
valueInstance = procureSandBoxMapValue(mapStructure, entity);
if (valueInstance == null) {
break populate;
}
}
if (map.get(mapKey) != null && !map.get(mapKey).equals(valueInstance)) {
entity.addValidationError(mapStructure.getKeyPropertyName(), "keyExistsValidationError");
}
if (StringUtils.isNotBlank(mapStructure.getMapKeyValueProperty())) {
Property p = entity.findProperty("key");
Property newP = new Property();
newP.setName(mapStructure.getMapKeyValueProperty());
newP.setValue(p.getValue());
newP.setIsDirty(p.getIsDirty());
entity.addProperty(newP);
}
// allow validation on other properties in order to show key validation errors along with all the other properties
// validation errors
valueInstance = createPopulatedInstance(valueInstance, entity, valueMergedProperties, false);
if (StringUtils.isNotEmpty(mapKey) && !entity.isValidationFailure()) {
if (!entity.findProperty("priorKey").getValue().equals(mapKey)) {
map.remove(entity.findProperty("priorKey").getValue());
}
/*
* TODO this map manipulation code currently assumes the key value is a String. This should be widened to accept
* additional types of primitive objects.
*/
map.put(entity.findProperty(mapStructure.getKeyPropertyName()).getValue(), valueInstance);
}
} else {
if (StringUtils.isNotEmpty(mapKey) && !entity.isValidationFailure()) {
map.put(entity.findProperty(mapStructure.getKeyPropertyName()).getValue(), entity.findProperty(((SimpleValueMapStructure) mapStructure).getValuePropertyName()).getValue());
}
}
}
instance = persistenceManager.getDynamicEntityDao().merge(instance);
Entity[] responses = getMapRecords(instance, mapStructure, ceilingMergedProperties, valueMergedProperties, entity.findProperty("symbolicId"), null);
for (Entity response : responses) {
if (response.findProperty(mapStructure.getKeyPropertyName()).getValue().equals(persistencePackage.getEntity().findProperty(mapStructure.getKeyPropertyName()).getValue())) {
return response;
}
}
// could be empty if reverting a sandbox item that has experienced a deletion. make sure to at least return an empty instance of Entity.
return ArrayUtils.isEmpty(responses) ? new Entity() : responses[0];
} catch (Exception e) {
throw new ServiceException("Problem updating entity : " + e.getMessage(), e);
}
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceManagerImpl method executePostFetchHandlers.
protected PersistenceResponse executePostFetchHandlers(PersistencePackage persistencePackage, CriteriaTransferObject cto, PersistenceResponse persistenceResponse) throws ServiceException {
for (PersistenceManagerEventHandler handler : persistenceManagerEventHandlers) {
PersistenceManagerEventHandlerResponse response = handler.postFetch(this, persistenceResponse.getDynamicResultSet(), persistencePackage, cto);
if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED_BREAK == response.getStatus()) {
persistenceResponse.setDynamicResultSet(response.getDynamicResultSet());
if (!MapUtils.isEmpty(response.getAdditionalData())) {
persistenceResponse.getAdditionalData().putAll(response.getAdditionalData());
}
break;
} else if (PersistenceManagerEventHandlerResponse.PersistenceManagerEventHandlerResponseStatus.HANDLED == response.getStatus()) {
persistenceResponse.setDynamicResultSet(response.getDynamicResultSet());
if (!MapUtils.isEmpty(response.getAdditionalData())) {
persistenceResponse.getAdditionalData().putAll(response.getAdditionalData());
}
}
}
// support legacy api
persistenceResponse.setDynamicResultSet(postFetch(persistenceResponse.getDynamicResultSet(), persistencePackage, cto));
persistenceResponse.getDynamicResultSet().setStartIndex(cto.getFirstResult());
persistenceResponse.getDynamicResultSet().setPageSize(cto.getMaxResults());
Integer upperCount;
Integer lowerCount;
if (cto.getFirstId() != null) {
upperCount = cto.getLowerCount() - 1;
lowerCount = cto.getLowerCount() - persistenceResponse.getDynamicResultSet().getTotalRecords();
} else {
if (cto.getLowerCount() == null) {
lowerCount = 1;
} else {
lowerCount = cto.getUpperCount() + 1;
}
upperCount = lowerCount + persistenceResponse.getDynamicResultSet().getTotalRecords() - 1;
if (lowerCount == 1 && persistenceResponse.getDynamicResultSet().getTotalRecords() == 0) {
lowerCount = 0;
}
}
if (cto.getFirstId() == null && cto.getLastId() == null) {
persistenceResponse.getDynamicResultSet().setTotalCountLessThanPageSize(persistenceResponse.getDynamicResultSet().getTotalRecords() < cto.getMaxResults());
}
persistenceResponse.getDynamicResultSet().setUpperCount(upperCount);
persistenceResponse.getDynamicResultSet().setLowerCount(lowerCount);
Entity[] payload = persistenceResponse.getDynamicResultSet().getRecords();
if (!ArrayUtils.isEmpty(payload)) {
Entity first = payload[0];
Entity last = payload[payload.length - 1];
String idProperty = getIdPropertyName(persistencePackage.getCeilingEntityFullyQualifiedClassname());
if (!StringUtils.isEmpty(idProperty)) {
{
Property property = first.findProperty(idProperty);
if (property != null) {
try {
persistenceResponse.getDynamicResultSet().setFirstId(Long.parseLong(property.getValue()));
} catch (NumberFormatException e) {
// do nothing
}
}
}
{
Property property = last.findProperty(idProperty);
if (property != null) {
try {
persistenceResponse.getDynamicResultSet().setLastId(Long.parseLong(property.getValue()));
} catch (NumberFormatException e) {
// do nothing
}
}
}
}
}
if (fetchDetection != null) {
persistenceResponse.getDynamicResultSet().setFetchType(fetchDetection.getFetchType(persistencePackage, cto));
persistenceResponse.getDynamicResultSet().setPromptSearch(fetchDetection.shouldPromptForSearch(persistencePackage, cto));
}
return persistenceResponse;
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class PersistenceManagerImpl method processMergedProperties.
public Property[] processMergedProperties(Class<?>[] entities, Map<MergedPropertyType, Map<String, FieldMetadata>> mergedProperties) {
List<Property> propertiesList = new ArrayList<>();
for (PersistenceModule module : modules) {
module.extractProperties(entities, mergedProperties, propertiesList);
}
Property[] properties = new Property[propertiesList.size()];
properties = propertiesList.toArray(properties);
Arrays.sort(properties, new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
Integer tabOrder1 = o1.getMetadata().getTabOrder() == null ? 99999 : o1.getMetadata().getTabOrder();
Integer tabOrder2 = o2.getMetadata().getTabOrder() == null ? 99999 : o2.getMetadata().getTabOrder();
Integer groupOrder1 = null;
Integer groupOrder2 = null;
if (o1.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata b1 = (BasicFieldMetadata) o1.getMetadata();
groupOrder1 = b1.getGroupOrder();
}
groupOrder1 = groupOrder1 == null ? 99999 : groupOrder1;
if (o2.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata b2 = (BasicFieldMetadata) o2.getMetadata();
groupOrder2 = b2.getGroupOrder();
}
groupOrder2 = groupOrder2 == null ? 99999 : groupOrder2;
Integer fieldOrder1 = o1.getMetadata().getOrder() == null ? 99999 : o1.getMetadata().getOrder();
Integer fieldOrder2 = o2.getMetadata().getOrder() == null ? 99999 : o2.getMetadata().getOrder();
String friendlyName1 = o1.getMetadata().getFriendlyName() == null ? "zzzz" : o1.getMetadata().getFriendlyName();
String friendlyName2 = o2.getMetadata().getFriendlyName() == null ? "zzzz" : o2.getMetadata().getFriendlyName();
String name1 = o1.getName() == null ? "zzzzz" : o1.getName();
String name2 = o2.getName() == null ? "zzzzz" : o2.getName();
return new CompareToBuilder().append(tabOrder1, tabOrder2).append(groupOrder1, groupOrder2).append(fieldOrder1, fieldOrder2).append(friendlyName1, friendlyName2).append(name1, name2).toComparison();
}
});
return properties;
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class OfferCustomPersistenceHandler method update.
@Override
public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
// This can't be on a validator since the field is dynamically added with JavaScript
Property isMultiTierOffer = entity.findProperty(IS_TIERED_OFFER);
if (isMultiTierOffer != null) {
String multiTierValue = isMultiTierOffer.getValue();
if ("false".equalsIgnoreCase(multiTierValue)) {
Property offerValue = entity.findProperty(OFFER_VALUE);
if (offerValue != null) {
String value = offerValue.getValue();
if (value == null || "null".equalsIgnoreCase(value)) {
entity.addValidationError(OFFER_VALUE, "requiredFieldMessage");
}
}
}
}
Property qualifiersCanBeQualifiers = entity.findProperty(QUALIFIERS_CAN_BE_QUALIFIERS);
if (qualifiersCanBeQualifiers != null) {
qualifiersCanBeQualifiers.setIsDirty(true);
}
Property qualifiersCanBeTargets = entity.findProperty(QUALIFIERS_CAN_BE_TARGETS);
if (qualifiersCanBeTargets != null) {
qualifiersCanBeTargets.setIsDirty(true);
}
Property offerItemQualifierRuleType = buildOfferItemQualifierRuleTypeProperty(qualifiersCanBeQualifiers, qualifiersCanBeTargets);
entity.addProperty(offerItemQualifierRuleType);
Property stackable = entity.findProperty(STACKABLE);
if (stackable != null) {
stackable.setIsDirty(true);
}
Property offerItemTargetRuleType = buildOfferItemTargetRuleTypeProperty(stackable);
entity.addProperty(offerItemTargetRuleType);
OperationType updateType = persistencePackage.getPersistencePerspective().getOperationTypes().getUpdateType();
return helper.getCompatibleModule(updateType).update(persistencePackage);
}
Aggregations