/// <summary> /// Method which return a list rules about question, depending of configuration in survey /// </summary> /// <param name="s">Survey Question</param> /// <param name="question">Question database</param> /// <returns>List of rules</returns> private List <FrmQuestionsRules> GetQuestionRules(Survey s, FrmQuestions question) { List <FrmQuestionsRules> questionRules = new List <FrmQuestionsRules>(); if (s.Required.Equals("yes")) { questionRules.Add(new FrmQuestionsRules() { App = "all", Message = s.RequiredMessage, Question = question.Id, Type = "required", Rule = "yes" }); } if (!string.IsNullOrEmpty(s.Constraint)) { questionRules.Add(new FrmQuestionsRules() { App = "odk", Message = s.ConstraintMessage, Question = question.Id, Type = "constraint", Rule = s.Constraint }); } if (!string.IsNullOrEmpty(s.Relevant)) { questionRules.Add(new FrmQuestionsRules() { App = "odk", Message = string.Empty, Question = question.Id, Type = "relevant", Rule = s.Relevant }); } if (!string.IsNullOrEmpty(s.Calculation)) { questionRules.Add(new FrmQuestionsRules() { App = "odk", Message = string.Empty, Question = question.Id, Type = "calculation", Rule = s.Calculation }); } if (!string.IsNullOrEmpty(s.ChoiceFilter)) { questionRules.Add(new FrmQuestionsRules() { App = "odk", Message = string.Empty, Question = question.Id, Type = "choice_filter", Rule = s.ChoiceFilter }); } if (!string.IsNullOrEmpty(s.Appearance)) { questionRules.Add(new FrmQuestionsRules() { App = "odk", Message = string.Empty, Question = question.Id, Type = "appearance", Rule = s.Appearance }); } return(questionRules); }
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)); } } }