Ejemplo n.º 1
0
        /// <summary>
        /// Creates new element based on existing element.
        /// </summary>
        /// <param name="sourceTenantId">The source tenant.</param>
        /// <param name="sourceElementId">Identifies element instance to copy.</param>
        /// <param name="destTenantId">The destination tenant.</param>
        /// <param name="sourceElementTypeId">The type of the element that is copied (destination will be same type).</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Newly allocated element identifier.</returns>
        public long Copy(long sourceTenantId, long sourceElementId, long destTenantId, Guid sourceElementTypeId, IUnitOfWork unitOfWork = null)
        {
            try
            {
                IAdvancedElementService customElementService = (IAdvancedElementService)_elementFactory.GetElementService(sourceElementTypeId);
                IUnitOfWork             localUnitOfWork      = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null;

                try
                {
                    long destElementId = _elementRepository.Copy(sourceTenantId, sourceElementId, destTenantId, unitOfWork ?? localUnitOfWork);
                    customElementService.Copy(sourceTenantId, sourceElementId, destTenantId, destElementId, unitOfWork ?? localUnitOfWork);
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Commit();
                    }
                    return(destElementId);
                }
                catch
                {
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Rollback();
                    }
                    throw;
                }
                finally
                {
                    if (localUnitOfWork != null)
                    {
                        localUnitOfWork.Dispose();
                    }
                }
            }
            catch (ValidationErrorException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new ValidationErrorException(new ValidationError(null, ApplicationResource.UnexpectedErrorMessage), ex);
            }
        }