Search in sources :

Example 1 with MultiTenantCopyContext

use of org.broadleafcommerce.common.copy.MultiTenantCopyContext in project BroadleafCommerce by BroadleafCommerce.

the class EntityDuplicatorImpl method copy.

@Override
public <T> T copy(Class<T> entityClass, Long id, Map<String, String> copyHints, EntityDuplicateModifier... modifiers) {
    genericEntityService.flush();
    genericEntityService.clear();
    Object entity = genericEntityService.readGenericEntity(entityClass, id);
    if (!(entity instanceof MultiTenantCloneable)) {
        IllegalArgumentException e = new IllegalArgumentException("Copying is only supported for classes implementing MultiTenantCloneable");
        LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
        throw e;
    }
    boolean isValid = validate(entity);
    T dup;
    if (isValid) {
        try {
            Site currentSite = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite();
            MultiTenantCopyContext context = new MultiTenantCopyContext(null, null, currentSite, currentSite, genericEntityService, mtCopierExtensionManager);
            if (extensionManager != null) {
                ExtensionResultHolder<MultiTenantCopyContext> contextResponse = new ExtensionResultHolder<MultiTenantCopyContext>();
                extensionManager.setupDuplicate(entity, contextResponse);
                if (contextResponse.getResult() != null) {
                    context = contextResponse.getResult();
                }
            }
            dup = performCopy(context, (MultiTenantCloneable<T>) entity, copyHints, modifiers);
        } catch (Exception e) {
            LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
            throw ExceptionHelper.refineException(e);
        } finally {
            if (extensionManager != null) {
                extensionManager.tearDownDuplicate();
            }
        }
    } else {
        LOG.error(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
        throw new IllegalArgumentException(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
    }
    return dup;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) MultiTenantCloneable(org.broadleafcommerce.common.copy.MultiTenantCloneable) MultiTenantCopyContext(org.broadleafcommerce.common.copy.MultiTenantCopyContext) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Aggregations

MultiTenantCloneable (org.broadleafcommerce.common.copy.MultiTenantCloneable)1 MultiTenantCopyContext (org.broadleafcommerce.common.copy.MultiTenantCopyContext)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 Site (org.broadleafcommerce.common.site.domain.Site)1