Search in sources :

Example 1 with PersistenceManager

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

the class FormBuilderServiceImpl method getValueClassNames.

protected List<String> getValueClassNames(String valueClassName) {
    PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager(valueClassName);
    List<String> classNames = new ArrayList<>();
    try {
        Class<?>[] mapEntities = pm.getPolymorphicEntities(valueClassName);
        for (Class clazz : mapEntities) {
            classNames.add(clazz.getName());
        }
    } catch (ClassNotFoundException e) {
        classNames.add(valueClassName);
    }
    return classNames;
}
Also used : PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) ArrayList(java.util.ArrayList)

Example 2 with PersistenceManager

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

the class DefaultFetchWrapper method getBasicPersistenceModule.

protected BasicPersistenceModule getBasicPersistenceModule() {
    PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
    BasicPersistenceModule basicPersistenceModule = (BasicPersistenceModule) ((InspectHelper) persistenceManager).getCompatibleModule(OperationType.BASIC);
    return basicPersistenceModule;
}
Also used : PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager)

Example 3 with PersistenceManager

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

the class FieldManager method setFieldValue.

public Object setFieldValue(Object bean, String fieldName, Object newValue) throws IllegalAccessException, InstantiationException {
    StringTokenizer tokens = new StringTokenizer(fieldName, ".");
    Class<?> componentClass = bean.getClass();
    Field field;
    bean = HibernateUtils.deproxy(bean);
    Object value = bean;
    int count = tokens.countTokens();
    int j = 0;
    StringBuilder sb = new StringBuilder();
    while (tokens.hasMoreTokens()) {
        String fieldNamePart = tokens.nextToken();
        sb.append(fieldNamePart);
        String mapKey = null;
        if (fieldNamePart.contains(FieldManager.MAPFIELDSEPARATOR)) {
            mapKey = fieldNamePart.substring(fieldNamePart.indexOf(FieldManager.MAPFIELDSEPARATOR) + FieldManager.MAPFIELDSEPARATOR.length(), fieldNamePart.length());
            fieldNamePart = fieldNamePart.substring(0, fieldNamePart.indexOf(FieldManager.MAPFIELDSEPARATOR));
        }
        field = getSingleField(componentClass, fieldNamePart);
        field.setAccessible(true);
        if (j == count - 1) {
            if (mapKey != null) {
                handleMapFieldPopulation(bean, fieldName, newValue, componentClass, field, value, fieldNamePart, mapKey);
            } else {
                FieldModifierManager modifierManager = FieldModifierManager.getFieldModifierManager();
                if (modifierManager != null) {
                    newValue = modifierManager.getModifiedWriteValue(field, value, newValue, entityManager);
                }
                field.set(value, newValue);
            }
        } else {
            Object myValue = field.get(value);
            if (myValue != null) {
                componentClass = myValue.getClass();
                value = myValue;
            } else {
                // configured entity for this class
                try {
                    Object newEntity = entityConfiguration.createEntityInstance(field.getType().getName());
                    SortableValue val = new SortableValue(bean, (Serializable) newEntity, j, sb.toString());
                    middleFields.add(val);
                    field.set(value, newEntity);
                    componentClass = newEntity.getClass();
                    value = newEntity;
                } catch (Exception e) {
                    // Use the most extended type based on the field type
                    PersistenceManager persistenceManager = getPersistenceManager(field.getType());
                    Class<?>[] entities = persistenceManager.getUpDownInheritance(field.getType());
                    if (!ArrayUtils.isEmpty(entities)) {
                        Object newEntity = entities[entities.length - 1].newInstance();
                        SortableValue val = new SortableValue(bean, (Serializable) newEntity, j, sb.toString());
                        middleFields.add(val);
                        field.set(value, newEntity);
                        componentClass = newEntity.getClass();
                        value = newEntity;
                        LOG.info("Unable to find a reference to (" + field.getType().getName() + ") in the EntityConfigurationManager. " + "Using the most extended form of this class identified as (" + entities[0].getName() + ")");
                    } else {
                        // Just use the field type
                        Object newEntity = field.getType().newInstance();
                        field.set(value, newEntity);
                        componentClass = newEntity.getClass();
                        value = newEntity;
                        LOG.info("Unable to find a reference to (" + field.getType().getName() + ") in the EntityConfigurationManager. " + "Using the type of this class.");
                    }
                }
            }
        }
        sb.append(".");
        j++;
    }
    return value;
}
Also used : Field(java.lang.reflect.Field) StringTokenizer(java.util.StringTokenizer) Serializable(java.io.Serializable) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with PersistenceManager

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

the class CustomPersistenceHandlerAdapter method getResultSet.

/**
 * This is a helper method that can be invoked as the last step in a custom inspect phase. It will assemble the
 * appropriate DynamicResultSet from the given parameters.
 */
protected DynamicResultSet getResultSet(PersistencePackage persistencePackage, InspectHelper helper, Map<String, FieldMetadata> metadata) throws ServiceException {
    String entityName = persistencePackage.getCeilingEntityFullyQualifiedClassname();
    try {
        if (helper instanceof PersistenceManager) {
            Class<?>[] entities = ((PersistenceManager) helper).getPolymorphicEntities(entityName);
            Map<MergedPropertyType, Map<String, FieldMetadata>> allMergedProperties = new HashMap<MergedPropertyType, Map<String, FieldMetadata>>();
            allMergedProperties.put(MergedPropertyType.PRIMARY, metadata);
            ClassMetadata mergedMetadata = helper.buildClassMetadata(entities, persistencePackage, allMergedProperties);
            DynamicResultSet results = new DynamicResultSet(mergedMetadata);
            return results;
        }
    } catch (ClassNotFoundException e) {
        throw new ServiceException(e);
    }
    return new DynamicResultSet();
}
Also used : ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) HashMap(java.util.HashMap) MergedPropertyType(org.broadleafcommerce.openadmin.dto.MergedPropertyType) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) Map(java.util.Map) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet)

Example 5 with PersistenceManager

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

the class SkuCustomPersistenceHandler method createToOneIndividualOptionField.

/**
 * Using a ToOne lookup performs much better for large product option value lists, speeds up initial page load,
 * and is generally more accurate in relation to option value updates and how they impact available selections and cache.
 *
 * @param option
 * @param order
 * @return
 */
protected FieldMetadata createToOneIndividualOptionField(ProductOption option, int order) {
    PersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
    FilterMapping filterMapping = new FilterMapping().withDirectFilterValues(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, option.getId())).withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {

        @Override
        public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) {
            return root.get("productOption").get("id").in(directValues);
        }
    }));
    List<FilterMapping> mappings = new ArrayList<>();
    mappings.add(filterMapping);
    TypedQuery<Serializable> countQuery = criteriaTranslator.translateCountQuery(persistenceManager.getDynamicEntityDao(), ProductOptionValueImpl.class.getName(), mappings);
    Long count = (Long) countQuery.getSingleResult();
    BasicFieldMetadata metadata = null;
    if (count > 0) {
        metadata = new BasicFieldMetadata();
        metadata.setFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
        metadata.setSecondaryType(SupportedFieldType.INTEGER);
        metadata.setForeignKeyProperty("id");
        metadata.setForeignKeyClass(ProductOptionValueImpl.class.getName());
        metadata.setForeignKeyDisplayValueProperty("attributeValue");
        metadata.setLookupDisplayProperty("attributeValue");
        metadata.setForeignKeyCollection(false);
        metadata.setCustomCriteria(new String[] { "option=" + String.valueOf(option.getId()) });
        metadata.setName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
        metadata.setFriendlyName(option.getLabel());
        metadata.setGroup("productOption_group");
        metadata.setGroupOrder(-1);
        metadata.setOrder(order);
        metadata.setExplicitFieldType(SupportedFieldType.ADDITIONAL_FOREIGN_KEY);
        metadata.setProminent(false);
        metadata.setVisibility(VisibilityEnum.FORM_EXPLICITLY_SHOWN);
        metadata.setReadOnly(false);
        // these may not be actually required, but the CPH has this as a requirement for parsing the data, so we'll stick with it here
        metadata.setRequiredOverride(true);
        metadata.setLookupType(LookupType.STANDARD);
        metadata.setMutable(true);
        metadata.setInheritedFromType(SkuImpl.class.getName());
        metadata.setAvailableToTypes(getPolymorphicClasses(SkuImpl.class, em, skuMetadataCacheService.useCache()));
        metadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
        metadata.setTargetClass(SkuImpl.class.getName());
        metadata.setFieldName(PRODUCT_OPTION_FIELD_PREFIX + option.getId());
    }
    return metadata;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Path(javax.persistence.criteria.Path) FieldPath(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPath) Serializable(java.io.Serializable) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) ArrayList(java.util.ArrayList) FilterMapping(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FilterMapping) From(javax.persistence.criteria.From) FieldPathBuilder(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.FieldPathBuilder) Restriction(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.Restriction) SkuImpl(org.broadleafcommerce.core.catalog.domain.SkuImpl) PredicateProvider(org.broadleafcommerce.openadmin.server.service.persistence.module.criteria.predicate.PredicateProvider) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) List(java.util.List) ArrayList(java.util.ArrayList) ProductOptionValueImpl(org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)

Aggregations

PersistenceManager (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager)6 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 From (javax.persistence.criteria.From)1 Path (javax.persistence.criteria.Path)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 ProductOptionValueImpl (org.broadleafcommerce.core.catalog.domain.ProductOptionValueImpl)1 SkuImpl (org.broadleafcommerce.core.catalog.domain.SkuImpl)1 BasicFieldMetadata (org.broadleafcommerce.openadmin.dto.BasicFieldMetadata)1 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)1 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)1 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)1