/// <summary>
 /// Retrieves the updated artifact.
 /// </summary>
 /// <param name="item">The project artifact.</param>
 /// <param name="release">The release.</param>
 /// <returns>
 /// Updated artifact
 /// </returns>
 internal static ProjectArtifact RetrieveUpdatedArtifact(this ProjectArtifact item, ReleaseCalendar release)
 {
     item.ProjectID = release.ProjectID;
     item.ApplicationEntityState = ApplicationEntityState.Modified;
     item.ModifiedByDeveloperID = release.ModifiedByDeveloperID;
     item.ReleaseCalendar = release;
     return item;
 }
 /// <summary>
 /// Function to map project fill item to release sub project.
 /// </summary>
 /// <param name="item">Project fill item</param>
 /// <param name="release">Release information</param>
 /// <returns>Release sub project information</returns>
 internal static ReleaseSubProject MapToReleaseSubproject(this ProjectFillItem item, ReleaseCalendar release)
 {
     return new ReleaseSubProject()
     {
         ReleaseSubProjectID = item.LinkId > 0 ? item.LinkId : item.ProjectID,
         ReleaseCalendarID = release.ReleaseCalendarID,
         SubProjectID = item.ProjectID,                
         ApplicationEntityState = item.LinkId > 0 ? ApplicationEntityState.Deleted : ApplicationEntityState.Added
     };
 }
 /// <summary>
 /// Function to map ArtifactListItem to release artifact.
 /// </summary>
 /// <param name="item">Item information</param>
 /// <param name="release">Release information</param>
 /// <returns>Release artifact information</returns>
 internal static ProjectArtifact MapToReleaseArtifact(this ArtifactFillListItem item, ReleaseCalendar release)
 {
     return new ProjectArtifact()
     {
         ProjectArtifactID = item.ProjectArtifactID.HasValue ? item.ProjectArtifactID.Value : item.ArtifactID,
         ProjectID = release.ProjectID,
         ReleaseCalendarID = release.ReleaseCalendarID,
         ArtifactID = item.ArtifactID,
         ModifiedByDeveloperID = release.ModifiedByDeveloperID,
         Status = (byte)ArtifactStatus.Pending,
         ApplicationEntityState = item.ProjectArtifactID.HasValue ? ApplicationEntityState.Deleted : ApplicationEntityState.Added,
         DueDate = release.StartDate.Date
     };
 }
        /// <summary>
        /// Function to save release details.
        /// </summary>
        /// <param name="releaseCalendar">Release information</param>
        public void InsertOrUpdate(ReleaseCalendar releaseCalendar)
        {
            if (releaseCalendar == null)
            {
                throw new ArgumentNullException(ReleaseCalendarConst);
            }

            if (releaseCalendar.ReleaseCalendarID == default(int))
            {
                this.unitOfWork.Context.ReleaseCalendars.Add(releaseCalendar);
            }
            else
            {
                releaseCalendar.ApplicationEntityState = ApplicationEntityState.Modified;
                this.unitOfWork.Context.ReleaseCalendars.Attach(releaseCalendar);
                this.unitOfWork.Context.ApplyStateChanges();
            }
        }
 /// <summary>
 /// Updates the due dates.
 /// </summary>
 /// <param name="releaseCalendar">The release calendar.</param>
 /// <param name="artifacts">The artifacts.</param>
 /// <param name="developerId">The developer identifier.</param>
 private void UpdateDueDates(ReleaseCalendar releaseCalendar, IList<ArtifactFillListItem> artifacts, int developerId)
 {
     var modifiedIds = artifacts.Except(artifacts.RetrieveAddedOrDeleted()).Where(a => a.IsSelected.Value).Select(a => a.ProjectArtifactID.Value).ToList();
     this.projectArtifactService.UpdateDueDate(modifiedIds, releaseCalendar.StartDate.Date, developerId);
 }
        /// <summary>
        /// Function to save release details.
        /// </summary>
        /// <param name="releaseCalendar">Release information</param>
        /// <param name="artifacts">List of artifacts</param>
        /// <param name="subprojects">List of sub project</param>
        /// <param name="developerId">Developer id</param>
        /// <param name="oldProjectId">The old project identifier.</param>
        public void InsertOrUpdate(ReleaseCalendar releaseCalendar, IList<ArtifactFillListItem> artifacts, IList<ProjectFillItem> subprojects, int developerId, int oldProjectId)
        {
            if (releaseCalendar == null)
            {
                throw new ArgumentNullException(ReleaseCalendarConst);
            }

            releaseCalendar.ModifiedByDeveloperID = developerId;

            if (artifacts != null)
            {
                releaseCalendar.ProjectArtifacts = artifacts.RetrieveAddedOrDeleted().Select(item => item.MapToReleaseArtifact(releaseCalendar)).ToList();
                if (releaseCalendar.ReleaseCalendarID != default(int) && releaseCalendar.ProjectID != oldProjectId)
                {
                    releaseCalendar.ProjectArtifacts = (from actualArtifact in this.projectArtifactService.RetrieveProjectArtifactByReleaseCalendarId(releaseCalendar.ReleaseCalendarID)
                                                        from artifact in artifacts.RetrieveModified()
                                                        where actualArtifact.ProjectArtifactID == artifact.ProjectArtifactID
                                                        select actualArtifact.RetrieveUpdatedArtifact(releaseCalendar)).Concat(releaseCalendar.ProjectArtifacts).ToList();
                }
            }

            if (subprojects != null)
            {
                subprojects = subprojects.Where(item => (item.LinkId <= 0 && item.IsSelected)
                    || (item.LinkId > 0 && !item.IsSelected)).ToList();
                releaseCalendar.ReleaseSubProjects = subprojects.Select(item => item.MapToReleaseSubproject(releaseCalendar)).ToList();
            }

            this.releaseRepository.InsertOrUpdate(releaseCalendar);
            this.unitOfWork.Save();
            if (releaseCalendar.ProjectID == oldProjectId)
            {
                this.UpdateDueDates(releaseCalendar, artifacts, developerId);
            }
        }
        /// <summary>
        /// Function to validate release.
        /// </summary>
        /// <param name="release">Release information</param>
        /// <returns>List of errors</returns>
        public IList<ErrorListItem> Validate(ReleaseCalendar release)
        {
            if (release == null)
            {
                throw new ArgumentNullException(ReleaseConst);
            }

            return this.releaseRepository.Validate(release);
        }
        /// <summary>
        /// Function to delete release information.
        /// </summary>
        /// <param name="id">Release id</param>
        public void Delete(int id)
        {
            var releaseCalendar = new ReleaseCalendar
            {
                ReleaseCalendarID = id
            };

            this.unitOfWork.Context.Entry(releaseCalendar).State = EntityState.Deleted;
        }
        /// <summary>
        /// Function to validate release.
        /// </summary>
        /// <param name="release">Release information</param>
        /// <returns>List of errors</returns>
        public IList<ErrorListItem> Validate(ReleaseCalendar release)
        {
            if (release == null)
            {
                throw new ArgumentNullException(ReleaseConst);
            }

            return this.unitOfWork.Context.ValidateReleaseInformation(
                release.ProjectID,
                !string.IsNullOrWhiteSpace(release.BuildNumber) ? release.BuildNumber.Trim() : string.Empty,
                release.Environment,
                release.ReleaseCalendarID > 0 ? release.ReleaseCalendarID : default(int?)).ToList();
        }