public async Task <IActionResult> RemoveBlock([Bind("Form,Block")] FrmBlocksForms entity) { try { if (ModelState.IsValid) { if (await _context.GetRepository <FrmBlocksForms>().DeleteAsync(entity)) { LogInformation(LogginEvent.Delete, "User deleted the entity: " + entity.ToString()); } else { LogWarnning(LogginEvent.Delete, "Entity wasn't deleted " + entity.ToString()); } return(RedirectToAction("SetBlocks", new { id = entity.Form })); } LogWarnning(LogginEvent.UserError, "Entity is not valid " + entity.ToString()); return(RedirectToAction("SetBlocks", new { id = entity.Form })); } catch (Exception ex) { LogCritical(LogginEvent.Exception, "System failed", ex); return(View("Error")); } }
public async Task <IActionResult> AddBlock([Bind("Form,Block,Order")] FrmBlocksForms entity) { try { if (ModelState.IsValid) { entity = await _context.GetRepository <FrmBlocksForms>().InsertAsync(entity); LogInformation(LogginEvent.Create, "Registered a new entity: " + entity.ToString()); return(RedirectToAction("SetBlocks", new { id = entity.Form })); } LogWarnning(LogginEvent.UserError, "Entity is not valid " + entity.ToString()); return(RedirectToAction("SetBlocks", new { id = entity.Form })); } catch (Exception ex) { LogCritical(LogginEvent.Exception, "System failed", ex); return(View("Error")); } }
public async Task <IActionResult> ImportXLSForm(IFormFile file, string xlsformvar) { if (file == null || string.IsNullOrEmpty(xlsformvar)) { LogWarnning(LogginEvent.UserError, "Parameters don't come"); return(NotFound()); } //Declaring variables XLSFormSummaryViewModel e; ImportXLSForm import; Stream stream; XLSForm xlsform; RepositoryFrmBlocksForms rBlocksForms = (RepositoryFrmBlocksForms)_context.GetRepository <FrmBlocksForms>(); RepositoryFrmBlocks rBlocks = (RepositoryFrmBlocks)_context.GetRepository <FrmBlocks>(); RepositoryFrmForms rForms = (RepositoryFrmForms)_context.GetRepository <FrmForms>(); RepositoryFrmFormsSettings rFormsSettings = (RepositoryFrmFormsSettings)_context.GetRepository <FrmFormsSettings>(); RepositoryFrmQuestions rQuestions = (RepositoryFrmQuestions)_context.GetRepository <FrmQuestions>(); RepositoryFrmQuestionsRules rQuestionsRules = (RepositoryFrmQuestionsRules)_context.GetRepository <FrmQuestionsRules>(); RepositoryFrmOptions rOptions = (RepositoryFrmOptions)_context.GetRepository <FrmOptions>(); FrmForms form = null; FrmBlocks block = null; FrmBlocksForms blockForm = null; FrmQuestions question = null; List <FrmFormsSettings> formSettings = null; List <FrmQuestionsRules> questionRules = null; List <FrmOptions> options = null; string[] types; int order = 1; try { // Create a file var f = file; string fileName = string.Empty; do { fileName = ImportFolder + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + f.FileName; LogInformation(LogginEvent.Import, "Loading file: " + fileName); } while (System.IO.File.Exists(fileName)); using (stream = new FileStream(fileName, FileMode.Create)) { await f.CopyToAsync(stream); stream.Close(); } LogInformation(LogginEvent.Import, "Loading XLSForm with variable: " + xlsformvar); // Importing data import = new ImportXLSForm(); xlsform = await import.LoadAsync(fileName, xlsformvar); } catch (Exception ex) { LogCritical(LogginEvent.Exception, "System failed loading file", ex); e = new XLSFormSummaryViewModel() { Succesful = false, Message = ex.Message }; return(View("XLSFormSummary", e)); } using (IDbContextTransaction transaction = await rForms.BeginTransactionAsync()) { try { LogInformation(LogginEvent.Import, "Starting transaction for XLSForm import"); // Saving form form = new FrmForms() { Name = xlsform.Settings.FormId, Title = xlsform.Settings.FormTitle, Description = xlsform.Settings.FormTitle, ExtId = xlsform.Settings.FormId }; await rForms.InsertAsync(form); // Save form with id using (stream = new FileStream(ImportFolder + "form-" + form.Id.ToString(), FileMode.Create)) { await file.CopyToAsync(stream); stream.Close(); } // Settings form formSettings = GetSettings(xlsform.Settings, form); foreach (var fs in formSettings) { await rFormsSettings.InsertAsync(fs); } // Saving blocks and questions foreach (var s in xlsform.Surveys) { try { // Start a group, it could be a group or repeat if (s.Type.Equals("begin group") || s.Type.Equals("begin repeat")) { block = new FrmBlocks() { Name = s.Name, Title = s.Label, Description = s.Label, Repeat = (byte)(s.Type.Equals("begin repeat") ? 1 : 0), }; block = await rBlocks.InsertAsync(block); blockForm = new FrmBlocksForms() { Block = block.Id, Form = form.Id, Order = order }; await rBlocksForms.InsertAsync(blockForm); } // End the group else if (s.Type.StartsWith("end")) { block = null; } // Questions else { types = s.Type.Split(" "); string t = GetTypeODK(types[0]); if (!t.Equals("ignore")) { question = new FrmQuestions() { Block = block.Id, Name = s.Name, Label = s.Label, Description = s.Hint, Type = t, Order = order }; question = await rQuestions.InsertAsync(question); // Question Rules questionRules = GetQuestionRules(s, question); foreach (var qr in questionRules) { await rQuestionsRules.InsertAsync(qr); } // Selection questions if (question.Type.Equals("unique") || question.Type.Equals("multiple")) { options = new List <FrmOptions>(); foreach (var o in xlsform.Choices.Where(p => p.ListName.Equals(types[1]))) { await rOptions.InsertAsync(new FrmOptions() { Question = question.Id, Name = o.Name, Label = o.Label }); } } } } order += 1; } catch (Exception ex) { LogCritical(LogginEvent.Exception, "Processing the question " + s.ToString(), ex); throw ex; } } transaction.Commit(); LogInformation(LogginEvent.Import, "Finished transaction for XLSForm import. Form id:" + form.Id.ToString()); e = new XLSFormSummaryViewModel() { Succesful = true, Message = string.Empty }; return(View("XLSFormSummary", e)); } catch (Exception ex) { transaction.Rollback(); LogCritical(LogginEvent.Exception, "System saving into database", ex); e = new XLSFormSummaryViewModel() { Succesful = false, Message = ex.Message }; return(View("XLSFormSummary", e)); } } }