Search in sources :

Example 66 with Entity

use of org.broadleafcommerce.openadmin.dto.Entity 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;
}
Also used : AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) Entity(org.broadleafcommerce.openadmin.dto.Entity) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 67 with Entity

use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.

the class TranslationCustomPersistenceHandler method add.

@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    try {
        // Get an instance of SystemProperty with the updated values from the form
        PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
        Translation adminInstance = (Translation) Class.forName(entity.getType()[0]).newInstance();
        Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Translation.class.getName(), persistencePerspective);
        adminInstance = (Translation) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        Translation res = translationService.getTranslation(adminInstance.getEntityType(), adminInstance.getEntityId(), adminInstance.getFieldName(), adminInstance.getLocaleCode());
        if (res == null) {
            adminInstance = dynamicEntityDao.merge(adminInstance);
            Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
            return adminEntity;
        } else {
            Entity errorEntity = new Entity();
            errorEntity.addValidationError("localeCode", "translation.record.exists.for.locale");
            return errorEntity;
        }
    } catch (Exception e) {
        throw new ServiceException("Unable to perform add for entity: " + Translation.class.getName(), e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) Translation(org.broadleafcommerce.common.i18n.domain.Translation) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) ServiceException(org.broadleafcommerce.common.exception.ServiceException)

Example 68 with Entity

use of org.broadleafcommerce.openadmin.dto.Entity 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);
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) OperationType(org.broadleafcommerce.common.presentation.client.OperationType) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 69 with Entity

use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.

the class OfferCustomPersistenceHandler method fetch.

@Override
public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
    addIsActiveFiltering(cto);
    DynamicResultSet resultSet = helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto);
    String customCriteria = "";
    if (persistencePackage.getCustomCriteria().length > 0) {
        customCriteria = persistencePackage.getCustomCriteria()[0];
    }
    for (Entity entity : resultSet.getRecords()) {
        Property discountType = entity.findProperty("discountType");
        Property discountValue = entity.findProperty("value");
        String value = discountValue.getValue();
        if (discountType == null || StringUtils.isBlank(discountType.getValue())) {
            discountValue.setValue("");
        } else if (discountType.getValue().equals("PERCENT_OFF")) {
            value = !value.contains(".") ? value : value.replaceAll("0*$", "").replaceAll("\\.$", "");
            discountValue.setValue(value + "%");
        } else if (discountType.getValue().equals("AMOUNT_OFF")) {
            Locale locale = BroadleafRequestContext.getBroadleafRequestContext().getLocale();
            BroadleafCurrency currency = BroadleafRequestContext.getBroadleafRequestContext().getBroadleafCurrency();
            NumberFormat nf = BroadleafCurrencyUtils.getNumberFormatFromCache(locale.getJavaLocale(), currency.getJavaCurrency());
            discountValue.setValue(nf.format(new BigDecimal(value)));
        }
        Property timeRule = entity.findProperty("offerMatchRules---TIME");
        entity.addProperty(buildAdvancedVisibilityOptionsProperty(timeRule));
        Property offerItemQualifierRuleType = entity.findProperty(OFFER_ITEM_QUALIFIER_RULE_TYPE);
        entity.addProperty(buildQualifiersCanBeQualifiersProperty(offerItemQualifierRuleType));
        entity.addProperty(buildQualifiersCanBeTargetsProperty(offerItemQualifierRuleType));
        Property offerItemTargetRuleType = entity.findProperty(OFFER_ITEM_TARGET_RULE_TYPE);
        entity.addProperty(buildStackableProperty(offerItemTargetRuleType));
        if (!"listGridView".equals(customCriteria)) {
            String setValue = discountValue.getValue();
            setValue = setValue.replaceAll("\\%", "").replaceAll("\\$", "");
            discountValue.setValue(setValue);
        }
        addIsActiveStatus(helper, entity);
    }
    return resultSet;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) Entity(org.broadleafcommerce.openadmin.dto.Entity) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) Property(org.broadleafcommerce.openadmin.dto.Property) BigDecimal(java.math.BigDecimal) NumberFormat(java.text.NumberFormat)

Example 70 with Entity

use of org.broadleafcommerce.openadmin.dto.Entity in project BroadleafCommerce by BroadleafCommerce.

the class ProductCustomPersistenceHandler 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(Product.class.getName(), persistencePerspective);
        BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) adminProperties.get("defaultCategory"));
        defaultCategory.setFriendlyName("ProductImpl_Parent_Category");
        if (entity.findProperty("defaultCategory") != null && !StringUtils.isEmpty(entity.findProperty("defaultCategory").getValue())) {
            // Change the inherited type so that this property is disconnected from the entity and validation is temporarily skipped.
            // This is useful when the defaultCategory was previously completely empty for whatever reason. Without this, such
            // a case would fail the validation, even though the property was specified in the submission.
            defaultCategory.setInheritedFromType(String.class.getName());
        }
        Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
        Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
        if (adminInstance instanceof ProductBundle) {
            removeBundleFieldRestrictions((ProductBundle) adminInstance, adminProperties, entity);
        }
        CategoryProductXref oldDefault = getCurrentDefaultXref(adminInstance);
        // so override required flag for that field during deployment
        if (BroadleafRequestContext.getBroadleafRequestContext().isProductionSandBox()) {
            ((BasicFieldMetadata) adminProperties.get("defaultCategory")).setRequiredOverride(false);
        }
        adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
        adminInstance = dynamicEntityDao.merge(adminInstance);
        boolean handled = false;
        if (extensionManager != null) {
            ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForUpdate(persistencePackage, adminInstance);
            handled = ExtensionResultStatusType.NOT_HANDLED != result;
            extensionManager.getProxy().manageFields(persistencePackage, adminInstance);
        }
        if (!handled) {
            setupXref(adminInstance);
            removeOldDefault(adminInstance, oldDefault, entity);
        }
        return helper.getRecord(adminProperties, adminInstance, null, null);
    } catch (Exception e) {
        throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
    }
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CategoryProductXref(org.broadleafcommerce.core.catalog.domain.CategoryProductXref) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) PersistencePerspective(org.broadleafcommerce.openadmin.dto.PersistencePerspective) ServiceException(org.broadleafcommerce.common.exception.ServiceException) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Aggregations

Entity (org.broadleafcommerce.openadmin.dto.Entity)112 Property (org.broadleafcommerce.openadmin.dto.Property)62 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)51 ServiceException (org.broadleafcommerce.common.exception.ServiceException)48 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)36 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)34 ArrayList (java.util.ArrayList)25 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)24 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)24 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)20 Serializable (java.io.Serializable)19 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)19 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 ValidationException (org.broadleafcommerce.openadmin.server.service.ValidationException)17 Map (java.util.Map)16 CriteriaTransferObject (org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)16 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)16 HashMap (java.util.HashMap)15 SecurityServiceException (org.broadleafcommerce.common.exception.SecurityServiceException)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14