Search in sources :

Example 1 with BeanException

use of org.jowidgets.cap.common.api.exception.BeanException in project jo-client-platform by jo-source.

the class LinkCreatorServiceImpl method getBeans.

private <BEAN_TYPE extends IBean> List<IBeanDto> getBeans(final IBeanDto linkedBean, final IEntityLinkProperties linkProperties, final Collection<IBeanKey> beanKeys, final IBeanAccess<BEAN_TYPE> beanAccess, final IBeanDtoFactory<BEAN_TYPE> dtoFactory, final IExecutionCallback executionCallback) {
    final List<IBeanDto> result = new LinkedList<IBeanDto>();
    for (final IBeanKey beanKey : beanKeys) {
        CapServiceToolkit.checkCanceled(executionCallback);
        final List<IBeanKey> singletonList = Collections.singletonList(beanKey);
        final List<BEAN_TYPE> beans = beanAccess.getBeans(singletonList, executionCallback);
        if (!EmptyCheck.isEmpty(beans) && beans.size() == 1) {
            final BEAN_TYPE bean = beans.iterator().next();
            if (linkedBean != null && linkProperties != null) {
                final Object sourceKey = linkedBean.getValue(linkProperties.getKeyPropertyName());
                BeanUtils.setProperty(bean, linkProperties.getForeignKeyPropertyName(), sourceKey);
            }
            result.add(dtoFactory.createDto(bean));
        } else if (!EmptyCheck.isEmpty(beans) && beans.size() > 1) {
            throw new BeanException(beanKey.getId(), "More than one bean found for the key '" + beanKey + "'");
        } else {
            throw new DeletedBeanException(beanKey.getId());
        }
    }
    return result;
}
Also used : DeletedBeanException(org.jowidgets.cap.common.api.exception.DeletedBeanException) IBeanDto(org.jowidgets.cap.common.api.bean.IBeanDto) IBeanKey(org.jowidgets.cap.common.api.bean.IBeanKey) BeanException(org.jowidgets.cap.common.api.exception.BeanException) DeletedBeanException(org.jowidgets.cap.common.api.exception.DeletedBeanException) LinkedList(java.util.LinkedList)

Example 2 with BeanException

use of org.jowidgets.cap.common.api.exception.BeanException in project jo-client-platform by jo-source.

the class BeanModifierImpl method modify.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void modify(final BEAN_TYPE bean, final IBeanModification modification) {
    final Method writeMethod = writeMethods.get(modification.getPropertyName());
    if (writeMethod != null || bean instanceof IPropertyMap) {
        // plugin before invocation
        final List<IBeanModifierPlugin<?>> plugins;
        plugins = PluginProvider.getPlugins(IBeanModifierPlugin.ID, pluginProperties);
        for (final IBeanModifierPlugin plugin : plugins) {
            plugin.beforeModification(bean, modification);
        }
        // do modification
        if (writeMethod != null) {
            try {
                writeMethod.invoke(bean, modification.getNewValue());
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        } else if (bean instanceof IPropertyMap) {
            ((IPropertyMap) bean).setValue(modification.getPropertyName(), modification.getNewValue());
        }
        // plugin after invocation
        for (final IBeanModifierPlugin plugin : plugins) {
            plugin.afterModification(bean, modification);
        }
    } else {
        throw new BeanException(modification.getId(), "Tryed to set the property '" + modification.getPropertyName() + "', but the property is not defined / allowed for this bean.");
    }
}
Also used : IPropertyMap(org.jowidgets.cap.common.api.bean.IPropertyMap) BeanException(org.jowidgets.cap.common.api.exception.BeanException) Method(java.lang.reflect.Method) IBeanModifierPlugin(org.jowidgets.cap.service.api.plugin.IBeanModifierPlugin) BeanException(org.jowidgets.cap.common.api.exception.BeanException)

Example 3 with BeanException

use of org.jowidgets.cap.common.api.exception.BeanException in project jo-client-platform by jo-source.

the class LinkDeleterServiceImpl method resetDirectLink.

private <BEAN_TYPE extends IBean> void resetDirectLink(final IEntityLinkProperties linkProperties, final IBeanKey beanKey, final IBeanAccess<BEAN_TYPE> beanAccess, final IExecutionCallback executionCallback) {
    CapServiceToolkit.checkCanceled(executionCallback);
    final List<IBeanKey> singletonList = Collections.singletonList(beanKey);
    final List<BEAN_TYPE> beans = beanAccess.getBeans(singletonList, executionCallback);
    if (!EmptyCheck.isEmpty(beans) && beans.size() == 1) {
        final BEAN_TYPE bean = beans.iterator().next();
        BeanUtils.setProperty(bean, linkProperties.getForeignKeyPropertyName(), null);
    } else if (!EmptyCheck.isEmpty(beans) && beans.size() > 1) {
        throw new BeanException(beanKey.getId(), "More than one bean found for the key '" + beanKey + "'");
    } else {
        throw new DeletedBeanException(beanKey.getId());
    }
}
Also used : DeletedBeanException(org.jowidgets.cap.common.api.exception.DeletedBeanException) IBeanKey(org.jowidgets.cap.common.api.bean.IBeanKey) BeanException(org.jowidgets.cap.common.api.exception.BeanException) DeletedBeanException(org.jowidgets.cap.common.api.exception.DeletedBeanException)

Aggregations

BeanException (org.jowidgets.cap.common.api.exception.BeanException)3 IBeanKey (org.jowidgets.cap.common.api.bean.IBeanKey)2 DeletedBeanException (org.jowidgets.cap.common.api.exception.DeletedBeanException)2 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 IBeanDto (org.jowidgets.cap.common.api.bean.IBeanDto)1 IPropertyMap (org.jowidgets.cap.common.api.bean.IPropertyMap)1 IBeanModifierPlugin (org.jowidgets.cap.service.api.plugin.IBeanModifierPlugin)1