private List <PreservationSchedule> GetArchivePeriods(Guid archiveId)
        {
            var retval = new List <PreservationSchedule>();

            try
            {
                var service = new PreservationService();
                var periods = service.GetSchedule();

                //PreservationSchedule previouslyUsedSchedule = null;

                var selSchedule = service.GetPreservationScheduleWithinArchive(archiveId);
                if (selSchedule != null)
                {
                    foreach (var item in periods)
                    {
                        item.IsArchiveDefault = item.IdPreservationSchedule == selSchedule.IdPreservationSchedule;
                    }
                }
                retval = new List <PreservationSchedule>(periods);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw;
            }

            return(retval);
        }
        public ActionResult CreatePreservationTask(Guid idArchive, Guid?taskPeriodId, int taskYear, int taskMonth, int taskDay, bool onlyOneTask, bool isEnabled)
        {
            try
            {
                if (!taskPeriodId.HasValue)
                {
                    throw new Exception("La Periodicità del Task è necessaria");
                }
                var service          = new PreservationService();
                var selectedSchedule = service.GetSchedule(taskPeriodId).SingleOrDefault <PreservationSchedule>();
                if (selectedSchedule != null)
                {
                    if (onlyOneTask)
                    {
                        service.CreatePreservationTask(idArchive, selectedSchedule, new DateTime(taskYear, taskMonth, taskDay), isEnabled);
                    }
                    else
                    {
                        /*
                         * Tipologie frequenze:
                         * 0 - cadenzata
                         * 1 - giornaliera
                         * 2 - settimanale
                         * 3 - mensile
                         * 4 - annuale
                         */


                        var taskToCreate = new List <PreservationTask>();
                        if (selectedSchedule.FrequencyType == 3) //Mensile.
                        {
                            for (var i = 1; i < 13; i++)
                            {
                                if (i < taskMonth)
                                {
                                    continue;
                                }

                                taskToCreate.AddRange(service.getTasksFromSchedulerPeriod(selectedSchedule.Period, i, taskYear, taskDay, onlyOneTask));
                            }
                        }
                        else
                        {
                            taskToCreate.AddRange(service.getTasksFromSchedulerPeriod(selectedSchedule.Period, taskMonth, taskYear, taskDay, onlyOneTask));
                        }

                        foreach (var task in taskToCreate)
                        {
                            task.TaskType = new PreservationTaskType {
                                IdPreservationTaskType = Guid.Empty, Type = Library.Common.Objects.Enums.PreservationTaskTypes.Verify
                            };
                            task.Archive = new DocumentArchive(idArchive);
                            task.Enabled = isEnabled;

                            task.CorrelatedTasks = new BindingList <PreservationTask>();
                            task.CorrelatedTasks.Add(new PreservationTask
                            {
                                TaskType = new PreservationTaskType {
                                    IdPreservationTaskType = Guid.Empty, Type = Library.Common.Objects.Enums.PreservationTaskTypes.Preservation
                                },
                                Archive           = new DocumentArchive(idArchive),
                                StartDocumentDate = task.StartDocumentDate,
                                EndDocumentDate   = task.EndDocumentDate,
                                EstimatedDate     = task.EstimatedDate,
                                Enabled           = task.Enabled
                            });
                        }

                        //service.RemovePendigPreservation(idArchive);
                        var result = service.CreatePreservationTask(new BindingList <PreservationTask>(taskToCreate));
                    }
                }
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
            }
            return(RedirectToAction("ArchivePreservationTask", new { id = idArchive }));
        }