Search in sources :

Example 1 with FieldManager

use of org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager in project BroadleafCommerce by BroadleafCommerce.

the class AfterStartDateValidator method validate.

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration, BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    String otherField = validationConfiguration.get("otherField");
    FieldManager fm = getFieldManager(propertyMetadata);
    boolean valid = true;
    String message = "";
    Date startDate = null;
    Date endDate = null;
    if (StringUtils.isBlank(value) || StringUtils.isBlank(otherField)) {
        return new PropertyValidationResult(true);
    }
    try {
        startDate = (Date) fm.getFieldValue(instance, otherField);
        endDate = (Date) fm.getFieldValue(instance, propertyName);
    } catch (IllegalAccessException | FieldNotAvailableException e) {
        valid = false;
        message = e.getMessage();
    }
    if (valid && endDate != null && startDate != null && endDate.before(startDate)) {
        valid = false;
        message = END_DATE_BEFORE_START;
    }
    return new PropertyValidationResult(valid, message);
}
Also used : FieldManager(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) Date(java.util.Date)

Example 2 with FieldManager

use of org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager in project BroadleafCommerce by BroadleafCommerce.

the class BasicFieldMetadataProvider method buildDataDrivenEnumList.

protected void buildDataDrivenEnumList(BasicFieldMetadata metadata) {
    try {
        DynamicEntityDao dynamicEntityDao = PersistenceManagerFactory.getDefaultPersistenceManager().getDynamicEntityDao();
        FieldManager fieldManager = dynamicEntityDao.getFieldManager();
        Criteria criteria = dynamicEntityDao.createCriteria(Class.forName(metadata.getOptionListEntity()));
        if (metadata.getOptionListEntity().equals(DataDrivenEnumerationValueImpl.class.getName())) {
            criteria.add(Restrictions.eq("hidden", false));
        }
        if (metadata.getOptionFilterParams() != null) {
            for (String[] param : metadata.getOptionFilterParams()) {
                Criteria current = criteria;
                String key = param[0];
                if (!key.equals(".ignore")) {
                    if (key.contains(".")) {
                        String[] parts = key.split("\\.");
                        for (int j = 0; j < parts.length - 1; j++) {
                            current = current.createCriteria(parts[j], parts[j]);
                        }
                    }
                    current.add(Restrictions.eq(key, convertType(param[1], OptionFilterParamType.valueOf(param[2]))));
                }
            }
        }
        List results = criteria.list();
        String[][] enumerationValues = new String[results.size()][2];
        int j = 0;
        for (Object param : results) {
            enumerationValues[j][1] = String.valueOf(fieldManager.getFieldValue(param, metadata.getOptionDisplayFieldName()));
            enumerationValues[j][0] = String.valueOf(fieldManager.getFieldValue(param, metadata.getOptionValueFieldName()));
            j++;
        }
        if (!CollectionUtils.isEmpty(results) && metadata.getOptionListEntity().equals(DataDrivenEnumerationValueImpl.class.getName())) {
            metadata.setOptionCanEditValues((Boolean) fieldManager.getFieldValue(results.get(0), "type.modifiable"));
        }
        metadata.setEnumerationValues(enumerationValues);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : FieldManager(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager) List(java.util.List) ArrayList(java.util.ArrayList) DynamicEntityDao(org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao) Criteria(org.hibernate.Criteria) DataDrivenEnumerationValueImpl(org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValueImpl)

Example 3 with FieldManager

use of org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager in project BroadleafCommerce by BroadleafCommerce.

the class MaxGreaterThanMinValidator method validate.

@Override
public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration, BasicFieldMetadata propertyMetadata, String propertyName, String value) {
    String otherField = validationConfiguration.get("otherField");
    FieldManager fm = getFieldManager(propertyMetadata);
    boolean valid = true;
    String message = "";
    BigDecimal min = new BigDecimal(0);
    BigDecimal max = min;
    if (StringUtils.isBlank(value) || StringUtils.isBlank(otherField)) {
        return new PropertyValidationResult(true);
    }
    try {
        Object minObj = fm.getFieldValue(instance, otherField);
        if (minObj != null) {
            min = new BigDecimal(minObj.toString());
        }
        max = new BigDecimal(fm.getFieldValue(instance, propertyName).toString());
    } catch (IllegalAccessException | FieldNotAvailableException e) {
        valid = false;
        message = e.getMessage();
    }
    if (valid && max != null && min != null && max.compareTo(min) < 0) {
        valid = false;
        message = "minGreaterThanMax";
    }
    return new PropertyValidationResult(valid, message);
}
Also used : FieldManager(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) BigDecimal(java.math.BigDecimal)

Aggregations

FieldManager (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldManager)3 FieldNotAvailableException (org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 DataDrivenEnumerationValueImpl (org.broadleafcommerce.common.enumeration.domain.DataDrivenEnumerationValueImpl)1 DynamicEntityDao (org.broadleafcommerce.openadmin.server.dao.DynamicEntityDao)1 Criteria (org.hibernate.Criteria)1