public async Task <ActionResult> Edit(
            [Bind(Include = "Id,Name,Nature,Description,Group,SequenceType,MultisequenceId,MultisequenceNumber,CollectionCountry,CollectionDate")] Matter matter)
        {
            using (var db = new LibiadaWebEntities())
            {
                if (ModelState.IsValid)
                {
                    db.Entry(matter).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }

                var data = new Dictionary <string, object>
                {
                    { "natures", EnumHelper.GetSelectList(typeof(Nature), matter.Nature) },
                    { "groups", EnumExtensions.ToArray <Group>().ToSelectListWithNature() },
                    { "sequenceTypes", EnumExtensions.ToArray <SequenceType>().ToSelectListWithNature() },
                    { "sequencesCount", db.CommonSequence.Count(c => c.MatterId == matter.Id) },
                    { "matter", matter },
                    { "multisequences", db.Multisequence.ToList() },
                    { "nature", ((byte)matter.Nature).ToString() },
                    { "group", ((byte)matter.Group).ToString() },
                    { "sequenceType", ((byte)matter.SequenceType).ToString() }
                };

                ViewBag.data = JsonConvert.SerializeObject(data);
                return(View(matter));
            }
        }
        public async Task <ActionResult> Edit([Bind(Include = "Id,Notation,MatterId,RemoteDb,RemoteId,Description")] CommonSequence commonSequence)
        {
            using (var db = new LibiadaWebEntities())
            {
                if (ModelState.IsValid)
                {
                    db.Entry(commonSequence).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }

                ViewBag.MatterId = new SelectList(Cache.GetInstance().Matters.ToArray(), "Id", "Name", commonSequence.MatterId);
                ViewBag.Notation = EnumHelper.GetSelectList(typeof(Notation), commonSequence.Notation);
                ViewBag.RemoteDb = EnumHelper.GetSelectList(typeof(RemoteDb), commonSequence.RemoteDb);
                return(View(commonSequence));
            }
        }
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,Nature,SequenceGroupType")] SequenceGroup sequenceGroup, long[] matterIds)
        {
            if (ModelState.IsValid)
            {
                var originalSequenceGroup = db.SequenceGroup.Include(sg => sg.Matters).Single(sg => sg.Id == sequenceGroup.Id);
                originalSequenceGroup.Name              = sequenceGroup.Name;
                originalSequenceGroup.Nature            = sequenceGroup.Nature;
                originalSequenceGroup.SequenceGroupType = sequenceGroup.SequenceGroupType;
                originalSequenceGroup.ModifierId        = AccountHelper.GetUserId();
                var matters = db.Matter.Where(m => matterIds.Contains(m.Id)).ToArray();
                originalSequenceGroup.Matters.Clear();
                foreach (var matter in matters)
                {
                    originalSequenceGroup.Matters.Add(matter);
                }

                db.Entry(originalSequenceGroup).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(sequenceGroup));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Starts a new thread with the given task.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        private void ExecuteTaskAction(Task task)
        {
            try
            {
                Func <Dictionary <string, string> > actionToCall;
                lock (task)
                {
                    actionToCall          = task.Action;
                    task.TaskData.Started = DateTime.Now;
                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => t.Id == task.TaskData.Id);

                        databaseTask.Started         = DateTime.Now;
                        databaseTask.Status          = TaskState.InProgress;
                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }

                // executing action
                Dictionary <string, string> result = actionToCall();

                lock (task)
                {
                    task.TaskData.Completed     = DateTime.Now;
                    task.TaskData.ExecutionTime = task.TaskData.Completed - task.TaskData.Started;
                    TaskResult[] results = result.Select(r => new TaskResult {
                        Key = r.Key, Value = r.Value, TaskId = task.TaskData.Id
                    }).ToArray();

                    task.TaskData.TaskState = TaskState.Completed;
                    using (var db = new LibiadaWebEntities())
                    {
                        db.TaskResult.AddRange(results);

                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == task.TaskData.Id));
                        databaseTask.Completed = DateTime.Now;
                        databaseTask.Status    = TaskState.Completed;

                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }
            }
            catch (ThreadAbortException)
            {
                // TODO: implement an exception handling/logging
            }
            catch (Exception e)
            {
                string errorMessage = e.Message;
                string stackTrace   = e.StackTrace;

                while (e.InnerException != null)
                {
                    e             = e.InnerException;
                    errorMessage += $"{Environment.NewLine} {e.Message}";
                }

                lock (task)
                {
                    var taskData = task.TaskData;
                    taskData.Completed     = DateTime.Now;
                    taskData.ExecutionTime = taskData.Completed - taskData.Started;
                    taskData.TaskState     = TaskState.Error;

                    var error = new { Message = errorMessage, StackTrace = stackTrace };

                    TaskResult taskResult = new TaskResult
                    {
                        Key = "Error", Value = JsonConvert.SerializeObject(error), TaskId = taskData.Id
                    };

                    using (var db = new LibiadaWebEntities())
                    {
                        db.TaskResult.Add(taskResult);

                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == taskData.Id));
                        databaseTask.Completed = DateTime.Now;
                        databaseTask.Status    = TaskState.Error;

                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, taskData);
                }
            }
            finally
            {
                ManageTasks();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Starts a new thread with the given task.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        private void ExecuteTaskAction(Task task)
        {
            try
            {
                Func <Dictionary <string, object> > actionToCall;
                lock (task)
                {
                    actionToCall          = task.Action;
                    task.TaskData.Started = DateTime.Now;
                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => t.Id == task.TaskData.Id);

                        databaseTask.Started         = DateTime.Now;
                        databaseTask.Status          = TaskState.InProgress;
                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }

                // executing action
                Dictionary <string, object> result = actionToCall();

                lock (task)
                {
                    task.TaskData.Completed     = DateTime.Now;
                    task.TaskData.ExecutionTime = task.TaskData.Completed - task.TaskData.Started;
                    task.Result             = result;
                    task.TaskData.TaskState = TaskState.Completed;
                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == task.TaskData.Id));

                        databaseTask.Completed = DateTime.Now;
                        databaseTask.Status    = TaskState.Completed;
                        if (result.ContainsKey("data"))
                        {
                            databaseTask.Result = result["data"].ToString();
                        }

                        if (result.ContainsKey("additionalData"))
                        {
                            databaseTask.AdditionalResultData = JsonConvert.SerializeObject(result["additionalData"]);
                        }

                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }
            }
            catch (Exception e)
            {
                string errorMessage = e.Message;
                string stackTrace   = e.StackTrace;

                while (e.InnerException != null)
                {
                    e             = e.InnerException;
                    errorMessage += $"{Environment.NewLine} {e.Message}";
                }

                lock (task)
                {
                    task.TaskData.Completed     = DateTime.Now;
                    task.TaskData.ExecutionTime = task.TaskData.Completed - task.TaskData.Started;
                    task.TaskData.TaskState     = TaskState.Error;
                    task.Result = new Dictionary <string, object>
                    {
                        { "Error", true },
                        { "ErrorMessage", errorMessage },
                        { "StackTrace", stackTrace }
                    };

                    using (var db = new LibiadaWebEntities())
                    {
                        CalculationTask databaseTask = db.CalculationTask.Single(t => (t.Id == task.TaskData.Id));

                        databaseTask.Completed       = DateTime.Now;
                        databaseTask.Status          = TaskState.Error;
                        databaseTask.Result          = JsonConvert.SerializeObject(task.Result);
                        db.Entry(databaseTask).State = EntityState.Modified;
                        db.SaveChanges();
                    }

                    signalrHub.Send(TaskEvent.ChangeStatus, task.TaskData);
                }
            }

            ManageTasks();
        }