Search in sources :

Example 1 with Property

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

the class AdminPermissionCustomPersistenceHandler method checkPermissionName.

protected Entity checkPermissionName(PersistencePackage persistencePackage) throws ServiceException {
    Entity entity = persistencePackage.getEntity();
    Property prop = entity.findProperty("name");
    String name = prop.getValue();
    name = name.toUpperCase();
    if (!name.startsWith("PERMISSION_")) {
        throw new ServiceException("All Permission names must start with PERMISSION_");
    }
    String[] parts = name.split("_");
    if (parts.length < 3) {
        throw new ServiceException("All Permission names must adhere to the naming standard: PERMISSION_[Permission Type]_[User Defined Section]. E.g. PERMISSION_READ_CATEGORY");
    }
    if (PermissionType.getInstance(parts[1]) == null) {
        throw new ServiceException("All Permission names must specify a valid permission type as part of the name. The permission name you specified (" + name + ") denotes the permission type of (" + parts[1] + "), which is not valid. See org.broadleafcommerce.openadmin.server.security.service.type.PermissionType for valid permission types.");
    }
    prop.setValue(name);
    return entity;
}
Also used : Entity(org.broadleafcommerce.openadmin.dto.Entity) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 2 with Property

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

the class BasicPersistenceModule method getPrimaryKey.

@Override
public Object getPrimaryKey(Entity entity, Map<String, FieldMetadata> mergedUnfilteredProperties) {
    Map<String, FieldMetadata> mergedProperties = filterOutCollectionMetadata(mergedUnfilteredProperties);
    Object primaryKey = null;
    String idPropertyName = null;
    BasicFieldMetadata metaData = null;
    for (String property : mergedProperties.keySet()) {
        BasicFieldMetadata temp = (BasicFieldMetadata) mergedProperties.get(property);
        if (temp.getFieldType() == SupportedFieldType.ID && !property.contains(".")) {
            idPropertyName = property;
            metaData = temp;
            break;
        }
    }
    if (idPropertyName == null) {
        throw new RuntimeException("Could not find a primary key property in the passed entity with type: " + entity.getType()[0]);
    }
    for (Property property : entity.getProperties()) {
        if (property.getName().equals(idPropertyName)) {
            switch(metaData.getSecondaryType()) {
                case INTEGER:
                    primaryKey = (property.getValue() == null) ? null : Long.valueOf(property.getValue());
                    break;
                case STRING:
                    primaryKey = property.getValue();
                    break;
            }
            break;
        }
    }
    if (primaryKey == null) {
        throw new RuntimeException("Could not find the primary key property (" + idPropertyName + ") in the passed entity with type: " + entity.getType()[0]);
    }
    return primaryKey;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 3 with Property

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

the class BasicPersistenceModule method extractPropertiesFromPersistentEntity.

protected void extractPropertiesFromPersistentEntity(Map<String, FieldMetadata> mergedProperties, Serializable entity, List<Property> props, String[] customCriteria) {
    FieldManager fieldManager = getFieldManager();
    try {
        if (entity instanceof AdminMainEntity) {
            // its display name.
            try {
                Property propertyItem = new Property();
                propertyItem.setName(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
                propertyItem.setValue(((AdminMainEntity) entity).getMainEntityName());
                props.add(propertyItem);
            } catch (Exception e) {
            // do nothing here except for not add the property. Exceptions could occur when there is a validation
            // issue and some properties/relationships that are used for gleaning the main entity name end up
            // not being set
            }
        }
        for (Entry<String, FieldMetadata> entry : mergedProperties.entrySet()) {
            String property = entry.getKey();
            BasicFieldMetadata metadata = (BasicFieldMetadata) entry.getValue();
            if (Class.forName(metadata.getInheritedFromType()).isAssignableFrom(entity.getClass()) || entity.getClass().isAssignableFrom(Class.forName(metadata.getInheritedFromType()))) {
                boolean proceed = true;
                if (property.contains(".")) {
                    StringTokenizer tokens = new StringTokenizer(property, ".");
                    Object testObject = entity;
                    while (tokens.hasMoreTokens()) {
                        String token = tokens.nextToken();
                        if (tokens.hasMoreTokens()) {
                            try {
                                testObject = fieldManager.getFieldValue(testObject, token);
                            } catch (FieldNotAvailableException e) {
                                proceed = false;
                                break;
                            }
                            if (testObject == null) {
                                Property propertyItem = new Property();
                                propertyItem.setName(property);
                                if (props.contains(propertyItem)) {
                                    proceed = false;
                                    break;
                                }
                                propertyItem.setValue(null);
                                props.add(propertyItem);
                                proceed = false;
                                break;
                            }
                        }
                    }
                }
                if (!proceed) {
                    continue;
                }
                boolean isFieldAccessible = true;
                Object value = null;
                try {
                    value = fieldManager.getFieldValue(entity, property);
                } catch (FieldNotAvailableException e) {
                    isFieldAccessible = false;
                }
                checkField: {
                    if (isFieldAccessible) {
                        Property propertyItem = new Property();
                        propertyItem.setName(property);
                        if (props.contains(propertyItem)) {
                            continue;
                        }
                        props.add(propertyItem);
                        String displayVal = propertyItem.getDisplayValue();
                        boolean handled = false;
                        for (FieldPersistenceProvider fieldPersistenceProvider : fieldPersistenceProviders) {
                            MetadataProviderResponse response = fieldPersistenceProvider.extractValue(new ExtractValueRequest(props, fieldManager, metadata, value, displayVal, persistenceManager, this, entity, customCriteria), propertyItem);
                            if (MetadataProviderResponse.NOT_HANDLED != response) {
                                handled = true;
                            }
                            if (MetadataProviderResponse.HANDLED_BREAK == response) {
                                break;
                            }
                        }
                        if (!handled) {
                            defaultFieldPersistenceProvider.extractValue(new ExtractValueRequest(props, fieldManager, metadata, value, displayVal, persistenceManager, this, entity, customCriteria), propertyItem);
                        }
                        break checkField;
                    }
                    // try a direct property acquisition via reflection
                    try {
                        String strVal = null;
                        Method method;
                        try {
                            // try a 'get' prefixed mutator first
                            String temp = "get" + property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
                            method = entity.getClass().getMethod(temp, new Class[] {});
                        } catch (NoSuchMethodException e) {
                            method = entity.getClass().getMethod(property, new Class[] {});
                        }
                        value = method.invoke(entity, new String[] {});
                        Property propertyItem = new Property();
                        propertyItem.setName(property);
                        if (props.contains(propertyItem)) {
                            continue;
                        }
                        props.add(propertyItem);
                        if (value == null) {
                            strVal = null;
                        } else {
                            if (Date.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format((Date) value);
                            } else if (Timestamp.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format(new Date(((Timestamp) value).getTime()));
                            } else if (Calendar.class.isAssignableFrom(value.getClass())) {
                                strVal = getSimpleDateFormatter().format(((Calendar) value).getTime());
                            } else if (Double.class.isAssignableFrom(value.getClass())) {
                                strVal = getDecimalFormatter().format(value);
                            } else if (BigDecimal.class.isAssignableFrom(value.getClass())) {
                                strVal = getDecimalFormatter().format(value);
                            } else {
                                strVal = value.toString();
                            }
                        }
                        propertyItem.setValue(strVal);
                    } catch (NoSuchMethodException e) {
                        LOG.debug("Unable to find a specified property in the entity: " + StringUtil.sanitize(property));
                    // do nothing - this property is simply not in the bean
                    }
                }
            }
        }
    } catch (ClassNotFoundException e) {
        throw new PersistenceException(e);
    } catch (IllegalAccessException e) {
        throw new PersistenceException(e);
    } catch (InvocationTargetException e) {
        throw new PersistenceException(e);
    }
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) FieldPersistenceProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.FieldPersistenceProvider) Timestamp(java.sql.Timestamp) Property(org.broadleafcommerce.openadmin.dto.Property) AdminMainEntity(org.broadleafcommerce.common.admin.domain.AdminMainEntity) MetadataProviderResponse(org.broadleafcommerce.openadmin.server.service.type.MetadataProviderResponse) ExtractValueRequest(org.broadleafcommerce.openadmin.server.service.persistence.module.provider.request.ExtractValueRequest) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) CriteriaConversionException(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.CriteriaConversionException) BeansException(org.springframework.beans.BeansException) ValidationException(org.broadleafcommerce.openadmin.server.service.ValidationException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) SecurityServiceException(org.broadleafcommerce.common.exception.SecurityServiceException) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringTokenizer(java.util.StringTokenizer) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) PersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceException) ParentEntityPersistenceException(org.broadleafcommerce.openadmin.server.service.persistence.ParentEntityPersistenceException) CriteriaTransferObject(org.broadleafcommerce.openadmin.dto.CriteriaTransferObject)

Example 4 with Property

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

the class MediaFieldPersistenceProvider method filterProperties.

@Override
public MetadataProviderResponse filterProperties(AddFilterPropertiesRequest addFilterPropertiesRequest, Map<String, FieldMetadata> properties) {
    // BP:  Basically copied this from RuleFieldPersistenceProvider
    List<Property> propertyList = new ArrayList<Property>();
    propertyList.addAll(Arrays.asList(addFilterPropertiesRequest.getEntity().getProperties()));
    Iterator<Property> itr = propertyList.iterator();
    List<Property> additionalProperties = new ArrayList<Property>();
    while (itr.hasNext()) {
        Property prop = itr.next();
        if (prop.getName().endsWith("Json")) {
            for (Map.Entry<String, FieldMetadata> entry : properties.entrySet()) {
                if (prop.getName().startsWith(entry.getKey())) {
                    BasicFieldMetadata originalFM = (BasicFieldMetadata) entry.getValue();
                    if (originalFM.getFieldType() == SupportedFieldType.MEDIA) {
                        Property originalProp = addFilterPropertiesRequest.getEntity().findProperty(originalFM.getName());
                        if (originalProp == null) {
                            originalProp = new Property();
                            originalProp.setName(originalFM.getName());
                            additionalProperties.add(originalProp);
                        }
                        originalProp.setValue(prop.getValue());
                        originalProp.setRawValue(prop.getRawValue());
                        originalProp.setUnHtmlEncodedValue(prop.getUnHtmlEncodedValue());
                        itr.remove();
                        break;
                    }
                }
            }
        }
    }
    propertyList.addAll(additionalProperties);
    addFilterPropertiesRequest.getEntity().setProperties(propertyList.toArray(new Property[propertyList.size()]));
    return MetadataProviderResponse.HANDLED;
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) ArrayList(java.util.ArrayList) Property(org.broadleafcommerce.openadmin.dto.Property) Map(java.util.Map)

Example 5 with Property

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

the class RuleFieldPersistenceProvider method extractSimpleRule.

protected void extractSimpleRule(ExtractValueRequest extractValueRequest, Property property, ObjectMapper mapper, MVELToDataWrapperTranslator translator) {
    Property jsonProperty;
    if (extractValueRequest.getRequestedValue() != null) {
        if (extractValueRequest.getRequestedValue() instanceof String) {
            String val = (String) extractValueRequest.getRequestedValue();
            property.setValue(val);
            property.setDisplayValue(extractValueRequest.getDisplayVal());
            jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, val, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
        } else {
            Object simpleRule = extractValueRequest.getRequestedValue();
            if (simpleRule != null) {
                if (simpleRule instanceof SimpleRule) {
                    String val = ((SimpleRule) simpleRule).getMatchRule();
                    property.setValue(val);
                    property.setDisplayValue(extractValueRequest.getDisplayVal());
                    jsonProperty = convertSimpleRuleToJson(translator, mapper, (SimpleRule) simpleRule, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
                } else {
                    throw new UnsupportedOperationException("RULE_SIMPLE type is currently only supported on " + "fields of type SimpleRule");
                }
            } else {
                jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, null, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
            }
        }
    } else {
        jsonProperty = ruleFieldExtractionUtility.convertSimpleRuleToJson(translator, mapper, null, property.getName() + "Json", extractValueRequest.getMetadata().getRuleIdentifier());
    }
    extractValueRequest.getProps().add(jsonProperty);
}
Also used : SimpleRule(org.broadleafcommerce.common.rule.SimpleRule) Property(org.broadleafcommerce.openadmin.dto.Property)

Aggregations

Property (org.broadleafcommerce.openadmin.dto.Property)120 Entity (org.broadleafcommerce.openadmin.dto.Entity)62 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)45 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)38 ArrayList (java.util.ArrayList)28 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)26 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)25 DataWrapper (org.broadleafcommerce.openadmin.web.rulebuilder.dto.DataWrapper)21 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)19 HashMap (java.util.HashMap)18 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)18 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)16 Map (java.util.Map)15 ServiceException (org.broadleafcommerce.common.exception.ServiceException)15 BasicCollectionMetadata (org.broadleafcommerce.openadmin.dto.BasicCollectionMetadata)15 Field (org.broadleafcommerce.openadmin.web.form.entity.Field)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 AdornedTargetCollectionMetadata (org.broadleafcommerce.openadmin.dto.AdornedTargetCollectionMetadata)13 RuleBuilderField (org.broadleafcommerce.openadmin.web.form.component.RuleBuilderField)12 ComboField (org.broadleafcommerce.openadmin.web.form.entity.ComboField)12