Beispiel #1
0
        public IEnumerable <IAccudemiaEntity> GetEntities()
        {
            // This example shows how to create a sample group and add 2 students to it.
            // You can play with other entities located in the AccudemiaDataX.Core.Model namespace.

            var group = new PersonsGroup {
                Name = "At risk students"
            };
            var s1 = new Student
            {
                FirstName    = "John",
                MiddleName   = "",
                LastName     = "Williams",
                PersonNumber = "123123123",
                Active       = true
            };
            var s2 = new Student
            {
                FirstName    = "Albert",
                MiddleName   = "",
                LastName     = "Johansen",
                PersonNumber = "456456456",
                Active       = true
            };

            var member1 = new PersonsGroupMember(s1, group);
            var member2 = new PersonsGroupMember(s2, group);

            // Entities MUST be returned in a logical order to avoid foreign-key constrains errors.
            // The same entity can be returned multiple times because the engine will remove duplicates.
            yield return(s1);

            yield return(s2);

            yield return(group);

            yield return(member1);

            yield return(member2);
        }
        public IEnumerable <IAccudemiaEntity> GetEntities()
        {
            using (var sreader = new StreamReader(Source))
            {
                sreader.BaseStream.Seek(0, SeekOrigin.Begin);

                // Start reading the CSV
                using (var reader = new CsvReader(sreader, true,
                                                  Delimiter, TextQualifier, CsvReader.DefaultEscape,
                                                  CsvReader.DefaultComment, true))
                {
                    reader.DefaultParseErrorAction = ParseErrorAction.ThrowException;
                    reader.MissingFieldAction      = MissingFieldAction.ReplaceByEmpty;

                    // Flag to return the semester only once
                    bool     semesterWasReturned = false;
                    Semester semester            = null;

                    // Reset the default semester, because it might be changed since the last time the
                    // import ran
                    defaultSemester = null;

                    while (reader.ReadNextRecord())
                    {
                        Student                student                = null;
                        Tutor                  tutor                  = null;
                        Course                 course                 = null;
                        Clazz                  clazz                  = null;
                        Instructor             instructor             = null;
                        StudentEnrollment      studentEnrollment      = null;
                        InstructorEnrollment   instructorEnrollment   = null;
                        TutorEnrollment        tutorEnrollment        = null;
                        Center                 center                 = null;
                        CourseCenterAssignment courseCenterAssignment = null;
                        PersonsGroup           personsGroup           = null;
                        PersonsGroupMember     personsGroupMember     = null;

                        // Create entities mapped
                        if (IsEntityMapped(typeof(Student)))
                        {
                            student = CreateEntity <Student>(reader);
                            // Set the middle name, because as this is a not commonly used field
                            // it might be null but it's required to have something in order to enforce
                            // unique constraint
                            student.MiddleName = student.MiddleName ?? "";
                        }

                        if (IsEntityMapped(typeof(Instructor)))
                        {
                            instructor = CreateEntity <Instructor>(reader);

                            // Set the middle name, because as this is a not commonly used field
                            // it might be null but it's required to have something in order to enforce
                            // unique constraint
                            instructor.MiddleName = instructor.MiddleName ?? "";
                        }

                        if (IsEntityMapped(typeof(Tutor)))
                        {
                            tutor = CreateEntity <Tutor>(reader);

                            // Set the middle name, because as this is a not commonly used field
                            // it might be null but it's required to have something in order to enforce
                            // unique constraint
                            tutor.MiddleName = tutor.MiddleName ?? "";
                        }

                        if (IsEntityMapped(typeof(Semester)))
                        {
                            semester = CreateEntity <Semester>(reader);
                            if (semester.Name == null)
                            {
                                semester.Name = "Default Term";
                            }
                        }

                        if (IsEntityMapped(typeof(Course)))
                        {
                            course = CreateEntity <Course>(reader);
                        }

                        if (IsEntityMapped(typeof(Center)))
                        {
                            center = CreateEntity <Center>(reader);
                        }

                        if (IsEntityMapped(typeof(PersonsGroup)))
                        {
                            personsGroup = CreateEntity <PersonsGroup>(reader);
                        }

                        if (IsEntityMapped(typeof(Clazz)))
                        {
                            clazz = CreateEntity <Clazz>(reader);

                            if (semester == null)
                            {
                                semester = DefaultSemester;
                            }

                            clazz.Semester = semester;
                            if (course == null)
                            {
                                throw new InvalidDataException(
                                          "Unable to create the classes, you must specify the subject area to create classes.");
                            }

                            clazz.Course = course;
                            course.HasScheduledClasses = true;
                        }
                        else if (student != null && course != null)
                        {
                            if (semester == null)
                            {
                                semester = DefaultSemester;
                            }

                            clazz = new Clazz(course, semester);
                            FillEntityData(reader, clazz);

                            if (clazz.ShortName == null)
                            {
                                clazz.ShortName = course + " - " + semester;
                            }
                        }

                        // Make relations between entities created
                        if (student != null && clazz != null)
                        {
                            studentEnrollment = student.AddEnrollment(clazz);
                        }

                        if (instructor != null && clazz != null)
                        {
                            instructorEnrollment = instructor.AddEnrollment(clazz);
                        }

                        if (tutor != null && course != null)
                        {
                            if (semester == null)
                            {
                                semester = DefaultSemester;
                            }
                            tutorEnrollment = tutor.AddEnrollment(course, semester);
                        }

                        if (center != null && course != null)
                        {
                            courseCenterAssignment = center.AddCourse(course);
                        }

                        if (student != null && personsGroup != null)
                        {
                            personsGroupMember = personsGroup.AddUser(student);
                        }

                        // Start returning all loaded entities
                        if (!semesterWasReturned && semester != null)
                        {
                            // Return the default semester only once, all the other semester must be always returned
                            if (DefaultSemester.Equals(semester))
                            {
                                semesterWasReturned = true;
                            }

                            yield return(LogEntityFoundAndReturn(semester));
                        }

                        if (course != null)
                        {
                            yield return(LogEntityFoundAndReturn(course));
                        }
                        if (clazz != null)
                        {
                            yield return(LogEntityFoundAndReturn(clazz));
                        }
                        if (student != null)
                        {
                            yield return(LogEntityFoundAndReturn(student));
                        }
                        if (instructor != null)
                        {
                            yield return(LogEntityFoundAndReturn(instructor));
                        }
                        if (tutor != null)
                        {
                            yield return(LogEntityFoundAndReturn(tutor));
                        }
                        if (center != null)
                        {
                            yield return(LogEntityFoundAndReturn(center));
                        }
                        if (studentEnrollment != null)
                        {
                            yield return(LogEntityFoundAndReturn(studentEnrollment));
                        }
                        if (instructorEnrollment != null)
                        {
                            yield return(LogEntityFoundAndReturn(instructorEnrollment));
                        }
                        if (tutorEnrollment != null)
                        {
                            yield return(LogEntityFoundAndReturn(tutorEnrollment));
                        }
                        if (personsGroup != null)
                        {
                            yield return(LogEntityFoundAndReturn(personsGroup));
                        }
                        if (personsGroupMember != null)
                        {
                            yield return(LogEntityFoundAndReturn(personsGroupMember));
                        }
                        if (courseCenterAssignment != null)
                        {
                            yield return(LogEntityFoundAndReturn(courseCenterAssignment));
                        }
                    }
                }
            }
        }