use of org.broadleafcommerce.common.copy.MultiTenantCloneable 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;
}
Aggregations