示例#1
0
        private ResultObject ReadExam(string _path, ISheet sheet, List <Question> listQuestion)
        {
            var result = new ResultObject {
                Success = -1
            };

            var resultExam = new ResultObject {
                Success = -1
            };
            Question ques          = null;
            string   FILEERROR     = "";
            int      countQuestion = 0;
            int      questionRow   = 1;
            int      countExam     = 0;

            for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++)
            {
                var    currentRow = sheet.GetRow(rowIndex);
                string flag       = GetValueCell(currentRow.GetCell(0));
                if (string.Empty.Equals(flag) || "END".Equals(flag.ToUpper()))
                {
                    if (ValidateQuestion(ques, questionRow, ref FILEERROR))
                    {
                        listQuestion.Add(ques);
                    }
                    break;
                }

                if (ConfigurationManager.AppSettings["IsQuestion"].Equals(flag))
                {
                    if (ValidateQuestion(ques, questionRow, ref FILEERROR))
                    {
                        listQuestion.Add(ques);
                    }
                    ques = GetQuestionFromRow(currentRow, rowIndex, ref FILEERROR);
                    countQuestion++;
                }
                if (ConfigurationManager.AppSettings["IsAnswer"].Equals(flag))
                {
                    var ans = GetAnswerFromRow(currentRow, rowIndex, ref FILEERROR);
                    if (ans != null)
                    {
                        ques.Answers.Add(ans);
                    }
                }
                if (ConfigurationManager.AppSettings["IsExam"].Equals(flag))
                {
                    if (ValidateQuestion(ques, questionRow, ref FILEERROR))
                    {
                        listQuestion.Add(ques);
                    }
                    var         exam     = GetExamFromRow(currentRow, rowIndex, countQuestion, ref FILEERROR);
                    List <Exam> listExam = new List <Exam>();
                    listExam.Add(exam);
                    if (string.Empty.Equals(FILEERROR))
                    {
                        List <ExamQuestion> list = new List <ExamQuestion>();

                        foreach (var question in listQuestion)
                        {
                            if (listExam.Count > 0)
                            {
                                list.Add(new ExamQuestion
                                {
                                    Exam       = listExam.ElementAt(0),
                                    ExamId     = listExam.ElementAt(0).Id,
                                    Question   = question,
                                    QuestionId = question.Id
                                });
                            }
                        }
                        ;

                        listExam.ElementAt(0).ExamQuestions = list;
                        listQuestion.Clear();
                        result.Success     = 0;
                        resultExam.Success = serviceExam.Import(listExam);
                        ClearFile(_path);
                        countQuestion = 0;
                        countExam++;
                    }
                    else
                    {
                        FILEERROR += "error exam " + countExam;
                        ClearFile(_path);
                        result.Message = FILEERROR;
                        return(result);
                    }
                }
            }
            return(resultExam);
        }