Search in sources :

Example 1 with FieldData

use of org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldData in project BroadleafCommerce by BroadleafCommerce.

the class AbstractRuleBuilderFieldService method setFields.

@Override
@SuppressWarnings("unchecked")
public void setFields(final List<FieldData> fields) {
    List<FieldData> proxyFields = (List<FieldData>) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { List.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("add")) {
                FieldData fieldData = (FieldData) args[0];
                testFieldName(fieldData);
            }
            if (method.getName().equals("addAll")) {
                Collection<FieldData> addCollection = (Collection<FieldData>) args[0];
                Iterator<FieldData> itr = addCollection.iterator();
                while (itr.hasNext()) {
                    FieldData fieldData = itr.next();
                    testFieldName(fieldData);
                }
            }
            return method.invoke(fields, args);
        }

        private void testFieldName(FieldData fieldData) throws ClassNotFoundException {
            if (!fieldData.getSkipValidation()) {
                if (!StringUtils.isEmpty(fieldData.getFieldName()) && dynamicEntityDao != null) {
                    String dtoClassName = getDtoClassName();
                    if (fieldData.getOverrideDtoClassName() != null) {
                        dtoClassName = fieldData.getOverrideDtoClassName();
                    }
                    Class<?>[] dtos = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(Class.forName(dtoClassName));
                    if (ArrayUtils.isEmpty(dtos)) {
                        dtos = new Class<?>[] { Class.forName(dtoClassName) };
                    }
                    Field field = null;
                    for (Class<?> dto : dtos) {
                        field = dynamicEntityDao.getFieldManager().getField(dto, fieldData.getFieldName());
                        if (field != null) {
                            break;
                        }
                    }
                    if (field == null) {
                        throw new IllegalArgumentException("Unable to find the field declared in FieldData (" + fieldData.getFieldName() + ") on the target class (" + dtoClassName + "), or any registered entity class that derives from it.");
                    }
                }
            }
        }
    });
    this.fields = proxyFields;
}
Also used : Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) FieldData(org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldData) Field(java.lang.reflect.Field) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with FieldData

use of org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldData in project BroadleafCommerce by BroadleafCommerce.

the class AbstractRuleBuilderFieldService method onApplicationEvent.

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    // This bean only is valid when the following bean is active. (admin)
    if (applicationContext.containsBean(PersistenceManagerFactory.getPersistenceManagerRef()) && applicationContext.containsBean("blPersistenceManagerFactory")) {
        // initialize the factory bean
        applicationContext.getBean("blPersistenceManagerFactory");
        PersistenceManager persistenceManager = PersistenceManagerFactory.getDefaultPersistenceManager();
        dynamicEntityDao = persistenceManager.getDynamicEntityDao();
        setFields(new ArrayList<FieldData>());
        // This cannot be null during startup as we do not want to remove the null safety checks in a multi-tenant env.
        boolean contextWasNull = false;
        if (BroadleafRequestContext.getBroadleafRequestContext() == null) {
            BroadleafRequestContext brc = new BroadleafRequestContext();
            brc.setIgnoreSite(true);
            BroadleafRequestContext.setBroadleafRequestContext(brc);
            contextWasNull = true;
        }
        try {
            init();
            // Initialize additional static fields method for the component.
            if (extensionManager != null) {
                extensionManager.getProxy().addFields(fields, getName(), getDtoClassName());
            }
            validateRuleBuilderState(this);
        } finally {
            if (contextWasNull) {
                BroadleafRequestContext.setBroadleafRequestContext(null);
            }
        }
    }
}
Also used : FieldData(org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldData) PersistenceManager(org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Aggregations

FieldData (org.broadleafcommerce.openadmin.web.rulebuilder.dto.FieldData)2 Field (java.lang.reflect.Field)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 PersistenceManager (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceManager)1