Exemplo n.º 1
0
        public ActionResult CreateCapacityGroup(CapacityGroupCreateModel model)
        {
            var currGroup = Db.CurriculumGroups.SingleOrDefault(g => g.Id == model.CurriculumGroup.Id);

            if (currGroup != null)
            {
                var capGroup = new CapacityGroup
                {
                    CurriculumGroup = currGroup,
                    Name            = model.Name,
                    InSS            = model.InSS,
                    InWS            = model.InWS
                };
                Db.CapacityGroups.Add(capGroup);

                if (!string.IsNullOrEmpty(model.AliasList))
                {
                    var elems = model.AliasList.Split(',');
                    foreach (var elem in elems)
                    {
                        var groupAlias = new GroupAlias
                        {
                            CapacityGroup = capGroup,
                            Name          = elem
                        };

                        Db.GroupAliases.Add(groupAlias);
                    }
                }

                Db.SaveChanges();
            }

            return(RedirectToAction("GroupList", new { id = currGroup.Curriculum.Id }));
        }
Exemplo n.º 2
0
        private void createCurricula(string shortName, string name)
        {
            var orgs = Db.Organisers.Where(x => x.ShortName.StartsWith("FK")).ToList();

            foreach (var org in orgs)
            {
                var cieB = org.Curricula.SingleOrDefault(x => x.ShortName.Equals(shortName));

                if (cieB == null)
                {
                    cieB = new Curriculum
                    {
                        Name      = name,
                        ShortName = shortName,
                        Organiser = org
                    };

                    Db.Curricula.Add(cieB);
                }

                var firstGroup = cieB.CurriculumGroups.FirstOrDefault();
                if (firstGroup != null)
                {
                    firstGroup.Name = string.Empty;
                }
                else
                {
                    firstGroup = new CurriculumGroup
                    {
                        Curriculum     = cieB,
                        Name           = string.Empty,
                        IsSubscribable = true
                    };

                    Db.CurriculumGroups.Add(firstGroup);
                }

                var firstCapGroup = firstGroup.CapacityGroups.FirstOrDefault();

                if (firstCapGroup == null)
                {
                    firstCapGroup = new  CapacityGroup
                    {
                        CurriculumGroup = firstGroup,
                        Name            = string.Empty,
                        InSS            = true,
                        InWS            = true
                    };

                    Db.CapacityGroups.Add(firstCapGroup);
                }
            }



            Db.SaveChanges();
        }
Exemplo n.º 3
0
 private void MoveGroup(SemesterGroup semGroup, CapacityGroup capGroup)
 {
     // beide Gruppen müssen existieren
     if (semGroup != null && capGroup != null && semGroup.CapacityGroup != capGroup)
     {
         if (semGroup.CapacityGroup != null)
         {
             // aus der bisherigen CapGroup die Semestergruppe entfernen
             semGroup.CapacityGroup.SemesterGroups.Remove(semGroup);
         }
         semGroup.CapacityGroup = capGroup;
         capGroup.SemesterGroups.Add(semGroup);
         Db.SaveChanges();
     }
 }
Exemplo n.º 4
0
        public ActionResult CreateGroup(CurriculumGroupCreateModel model)
        {
            var curriculum = Db.Curricula.SingleOrDefault(c => c.Id == model.Curriculum.Id);

            var group = new CurriculumGroup
            {
                Curriculum     = curriculum,
                IsSubscribable = model.IsSubscribable,
                Name           = model.Name
            };

            Db.CurriculumGroups.Add(group);

            if (model.CapacityGroupCount > 1)
            {
                for (int i = 1; i <= model.CapacityGroupCount; i++)
                {
                    Char c = (Char)((65) + (i - 1));

                    var capGroup = new CapacityGroup
                    {
                        CurriculumGroup = group,
                        InSS            = true,
                        InWS            = true,
                        Name            = c.ToString()
                    };

                    group.CapacityGroups.Add(capGroup);
                }
            }
            else
            {
                var capGroup = new CapacityGroup
                {
                    CurriculumGroup = group,
                    InSS            = true,
                    InWS            = true
                };

                group.CapacityGroups.Add(capGroup);
            }


            Db.SaveChanges();

            return(RedirectToAction("Structure", "Curriculum", new { id = model.Curriculum.Id }));
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="currName">Name des Studiengangs: WI, MBA</param>
        /// <param name="semName">Name des Semesters: WS15</param>
        /// <param name="srcCurrGroupName">Name der Studiengruppe (Ausgangslage)</param>
        /// <param name="srcSemGroupName">Name in der Semestergruppe (veraltet)</param>
        /// <param name="trgCurrGroupName">Name der Studiengruppe (Ziel) - i.d.R. identisch zu Ausgangslage</param>
        /// <param name="trgCapGroupName">Name der Kapazitätsgruppe</param>
        public void MoveGroup(string currName, string semName, string srcCurrGroupName, string srcSemGroupName,
                              string trgCurrGroupName, string trgCapGroupName)
        {
            // Die Semestergruppe, um die es geht
            SemesterGroup semGroup = null;

            // Die Semestergruppe hat keinen Eigennamen, d.h. es muss nach "leer" gesucht werden
            if (string.IsNullOrEmpty(srcSemGroupName))
            {
                semGroup = Db.SemesterGroups.SingleOrDefault(s => s.Semester.Name.Equals(semName) &&
                                                             s.CurriculumGroup.Curriculum.ShortName.Equals(currName) && s.CurriculumGroup.Name.Equals(srcCurrGroupName) &&
                                                             string.IsNullOrEmpty(s.Name));
            }
            else
            {
                semGroup = Db.SemesterGroups.SingleOrDefault(s => s.Semester.Name.Equals(semName) &&
                                                             s.CurriculumGroup.Curriculum.ShortName.Equals(currName) && s.CurriculumGroup.Name.Equals(srcCurrGroupName) &&
                                                             !string.IsNullOrEmpty(s.Name) && s.Name.Equals(srcSemGroupName));
            }

            // Die Kapazitätsgruppe, die verbunden werden soll
            CapacityGroup capGroup = null;

            if (string.IsNullOrEmpty(trgCapGroupName))
            {
                capGroup = Db.CapacityGroups.SingleOrDefault(g =>
                                                             g.CurriculumGroup.Curriculum.ShortName.Equals(currName) &&
                                                             g.CurriculumGroup.Name.Equals(trgCurrGroupName) &&
                                                             string.IsNullOrEmpty(g.Name));
            }
            else
            {
                capGroup = Db.CapacityGroups.SingleOrDefault(g =>
                                                             g.CurriculumGroup.Curriculum.ShortName.Equals(currName) &&
                                                             g.CurriculumGroup.Name.Equals(trgCurrGroupName) &&
                                                             !string.IsNullOrEmpty(g.Name) && g.Name.Equals(trgCapGroupName));
            }

            MoveGroup(semGroup, capGroup);
        }
Exemplo n.º 6
0
        /// <summary>
        /// es werden alle Zuordnungen durchsucht
        /// ob der Studiengang existiert,
        /// ob Studiengruppe und Kapazitätsgruppe existieren
        /// Fehlendes würde später automatisch angelegt werden
        /// </summary>
        public void CheckGroups()
        {
            var db = new TimeTableDbContext();

            foreach (var zuordnung in _import.GruppenZuordnungen)
            {
                // muss es überhaupt berücksichtigt werden
                var klasse = _import.Gruppen.SingleOrDefault(x => x.GruppenID.ToUpper().Equals(zuordnung.Alias.ToUpper()));
                if (klasse == null)
                {
                    _import.AddErrorMessage("Import", string.Format(
                                                "Klasse [{0}]: Zuordnung vorhanden, aber keine zugehörige Klasse in GPU003",
                                                zuordnung.Alias), true);
                }
                else
                {
                    if (klasse.IsTouched)
                    {
                        // Nach dem Studiengang suchen
                        var curr = db.Curricula.SingleOrDefault(x =>
                                                                x.ShortName.ToUpper().Equals(zuordnung.Studiengang.ToUpper()) && x.Organiser.Id == _orgId);

                        if (curr == null)
                        {
                            _import.AddErrorMessage("Import", string.Format(
                                                        "Klasse [{0}]: Studiengang [{1}] existiert nicht. Wird angelegt",
                                                        zuordnung.Alias, zuordnung.Studiengang), true);
                        }
                        else
                        {
                            var group = curr.CurriculumGroups.SingleOrDefault(x =>
                                                                              x.Name.ToUpper().Equals(zuordnung.Studiengruppe.ToUpper()));

                            if (group == null)
                            {
                                _import.AddErrorMessage("Import", string.Format(
                                                            "Klasse [{0}]: Studiengruppe [{1}] in Studienfgang [{2}] existiert nicht. Wird angelegt",
                                                            zuordnung.Alias, zuordnung.Studiengruppe, zuordnung.Studiengang), true);
                            }
                            else
                            {
                                CapacityGroup capGroup = null;


                                if (string.IsNullOrEmpty(zuordnung.Kapazitätsgruppe))
                                {
                                    capGroup = group.CapacityGroups.SingleOrDefault(x =>
                                                                                    string.IsNullOrEmpty(x.Name));
                                }
                                else
                                {
                                    capGroup = group.CapacityGroups.SingleOrDefault(x =>
                                                                                    x.Name.ToUpper().Equals(zuordnung.Kapazitätsgruppe.ToUpper()));
                                }

                                if (capGroup == null)
                                {
                                    _import.AddErrorMessage("Import", string.Format(
                                                                "Klasse [{0}]: Kapzitätsgruppe [{1}] der Studiengruppe [{2}] in Studienfgang [{3}] existiert nicht. Wird angelegt",
                                                                zuordnung.Alias, zuordnung.Kapazitätsgruppe, zuordnung.Studiengruppe,
                                                                zuordnung.Studiengang), true);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="db"></param>
        /// <param name="gruppenId">Gruppenalias nach dem in der Datenbank gesucht wird</param>
        /// <returns></returns>
        private List <SemesterGroup> InitSemesterGroups(TimeTableDbContext db, string gruppenId)
        {
            var semester = db.Semesters.SingleOrDefault(s => s.Id == _semId);
            var org      = db.Organisers.SingleOrDefault(x => x.Id == _orgId);

            // Annahme, die Semestergruppen existieren!
            var semGroupList = new List <SemesterGroup>();

            // suche alle aktuellen Zuordnungen zu dieser gruppenID
            var zuordnungen = _import.GruppenZuordnungen.Where(x => x.Alias.Equals(gruppenId));

            foreach (var zuordnung in zuordnungen)
            {
                // Studiengang finden
                var curr = db.Curricula.SingleOrDefault(x =>
                                                        x.ShortName.Equals(zuordnung.Studiengang) &&
                                                        x.Organiser.Id == org.Id);
                if (curr == null)
                {
                    curr = new TimeTable.Data.Curriculum
                    {
                        Organiser = org,
                        ShortName = zuordnung.Studiengang,
                        Name      = zuordnung.Studiengang
                    };
                    db.Curricula.Add(curr);
                }

                var sg = curr.CurriculumGroups.SingleOrDefault(x => x.Name.Equals(zuordnung.Studiengruppe));
                if (sg == null)
                {
                    sg = new CurriculumGroup
                    {
                        Name           = zuordnung.Studiengruppe,
                        IsSubscribable = true,
                        Curriculum     = curr
                    };
                    db.CurriculumGroups.Add(sg);
                    curr.CurriculumGroups.Add(sg);
                }

                var cg = string.IsNullOrEmpty(zuordnung.Kapazitätsgruppe) ?
                         sg.CapacityGroups.SingleOrDefault(x => string.IsNullOrEmpty(x.Name)) :
                         sg.CapacityGroups.SingleOrDefault(x => x.Name.Equals(zuordnung.Kapazitätsgruppe));
                if (cg == null)
                {
                    cg = new CapacityGroup
                    {
                        InSS            = true,
                        InWS            = true,
                        Name            = zuordnung.Kapazitätsgruppe,
                        CurriculumGroup = sg
                    };
                    db.CapacityGroups.Add(cg);
                    sg.CapacityGroups.Add(cg);
                }

                // bis hierher habe ich ohne Semesterbezug gearbeitet
                // jetzt noch die Semestergruppe

                var semGroup = cg.SemesterGroups.SingleOrDefault(x => x.Semester.Id == semester.Id);
                if (semGroup == null)
                {
                    // semestergruppe gibt es nicht => auf jeden Fall anlegen
                    semGroup = new SemesterGroup
                    {
                        CapacityGroup = cg,
                        Semester      = semester,
                    };

                    _Logger.InfoFormat("Semestergruppe {0} angelegt {1}", semGroup.FullName, gruppenId);

                    cg.SemesterGroups.Add(semGroup);
                    db.SemesterGroups.Add(semGroup);
                }

                semGroupList.Add(semGroup);
            }

            db.SaveChanges();


            return(semGroupList);
        }
Exemplo n.º 8
0
        public ActionResult ImportData(HttpPostedFileBase importFile)
        {
            if (importFile != null)
            {
                var bytes = new byte[importFile.ContentLength];
                importFile.InputStream.Read(bytes, 0, importFile.ContentLength);

                var stream = new System.IO.MemoryStream(bytes);
                var reader = new System.IO.StreamReader(stream, Encoding.Default);
                var text   = reader.ReadToEnd();

                string[] lines = text.Split('\n');


                var i = 0;
                foreach (var line in lines)
                {
                    if (i > 0)
                    {
                        string newline = line.Trim();

                        if (!string.IsNullOrEmpty(newline))
                        {
                            string[] words = newline.Split(';');

                            var fk    = words[0]; // Kurzname Fakultät
                            var cu    = words[1]; // Kurzname Studiengang
                            var sg    = words[2]; // Name Studiengruppe
                            var cg    = words[3]; // Name Kapazitätsgruppe - kann leer sein
                            var ws    = words[4]; // Kapazitätsgruppe wird im WS angeboten
                            var ss    = words[5]; // Kapazitätsgruppe wird im SS angeboten
                            var untis = words[6]; // Kürzel gpUntis

                            // Vorbedingung
                            // Fakultät und Studiengang müssen existieren
                            var faculty = Db.Organisers.SingleOrDefault(f => f.ShortName.Equals(fk));
                            if (faculty != null)
                            {
                                var curriculum = faculty.Curricula.SingleOrDefault(c => c.ShortName.Equals(cu));

                                if (curriculum != null)
                                {
                                    var studyGroup = curriculum.CurriculumGroups.SingleOrDefault(g => g.Name.Equals(sg));

                                    // Neue Studiengruppen werden automatisch angelegt
                                    if (studyGroup == null)
                                    {
                                        studyGroup = new CurriculumGroup
                                        {
                                            Name           = sg,
                                            Curriculum     = curriculum,
                                            IsSubscribable = false
                                        };
                                        Db.CurriculumGroups.Add(studyGroup);
                                        Db.SaveChanges();
                                    }

                                    var capacityGroup = string.IsNullOrEmpty(cg) ?
                                                        studyGroup.CapacityGroups.SingleOrDefault(g => string.IsNullOrEmpty(g.Name)) :
                                                        studyGroup.CapacityGroups.SingleOrDefault(g => g.Name.Equals(cg));

                                    // Neue Kapazitätsgruppen werden automatisch angelegt
                                    if (capacityGroup == null)
                                    {
                                        capacityGroup = new CapacityGroup
                                        {
                                            CurriculumGroup = studyGroup,
                                            Name            = cg
                                        };
                                        Db.CapacityGroups.Add(capacityGroup);
                                        Db.SaveChanges();
                                    }

                                    // ggf. bereits vorhandene Eigenschaften überschreiben
                                    capacityGroup.InWS = string.Equals(ws, "WS");
                                    capacityGroup.InSS = string.Equals(ss, "SS");
                                    Db.SaveChanges();

                                    var alias = capacityGroup.Aliases.SingleOrDefault(a => a.Name.Equals(untis));

                                    if (alias == null)
                                    {
                                        alias = new GroupAlias
                                        {
                                            CapacityGroup = capacityGroup,
                                            Name          = untis
                                        };
                                        Db.GroupAliases.Add(alias);
                                        Db.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                    i++;
                }
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 9
0
        private TimeTable.Data.Curriculum GetCurriculum(TimeTableDbContext db, Semester semester, string orgName, string currName)
        {
            var org  = db.Organisers.SingleOrDefault(x => x.ShortName.Equals(orgName));
            var curr = org.Curricula.SingleOrDefault(x => x.ShortName.Equals(currName));

            if (curr == null)
            {
                curr = new TimeTable.Data.Curriculum
                {
                    ShortName = currName,
                    Name      = currName,
                    Organiser = org
                };
                db.Curricula.Add(curr);
                db.SaveChanges();
            }

            // Gleich alles notwendige anlegen
            var wpmCurrciculumGroup = curr.CurriculumGroups.SingleOrDefault(x => x.Name.Equals("WPM"));

            if (wpmCurrciculumGroup == null)
            {
                wpmCurrciculumGroup = new CurriculumGroup
                {
                    Curriculum     = curr,
                    Name           = "WPM",
                    IsSubscribable = true,
                };
                db.CurriculumGroups.Add(wpmCurrciculumGroup);
                db.SaveChanges();
            }

            var wpmCapacityGroup = wpmCurrciculumGroup.CapacityGroups.FirstOrDefault();

            if (wpmCapacityGroup == null)
            {
                wpmCapacityGroup = new CapacityGroup
                {
                    CurriculumGroup = wpmCurrciculumGroup,
                    Name            = string.Empty,
                    InSS            = true,
                    InWS            = true
                };
                db.CapacityGroups.Add(wpmCapacityGroup);
                db.SaveChanges();
            }

            var wpmSemesterGroup = db.SemesterGroups.FirstOrDefault(x =>
                                                                    x.CapacityGroup.Id == wpmCapacityGroup.Id && x.Semester.Id == semester.Id);

            if (wpmSemesterGroup == null)
            {
                wpmSemesterGroup = new SemesterGroup
                {
                    Semester      = semester,
                    CapacityGroup = wpmCapacityGroup,
                    IsAvailable   = false // zu Beginn nicht freigegeben
                };
                db.SemesterGroups.Add(wpmSemesterGroup);



                db.SaveChanges();
            }


            return(curr);
        }
Exemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="db"></param>
        /// <param name="gruppenId">Gruppenalias nach dem in der Datenbank gesucht wird</param>
        /// <returns></returns>
        private List <SemesterGroup> InitSemesterGroups(TimeTableDbContext db, string gruppenId)
        {
            var semester = db.Semesters.SingleOrDefault(s => s.Id == _semId);
            var org      = db.Organisers.SingleOrDefault(x => x.Id == _orgId);

            // Annahme, die Semestergruppen existieren!
            var semGroupList = new List <SemesterGroup>();

            // Annahme, die Semestergruppen existieren nicht alle und müssen ggf. angelegt werden

            // damit man nach den Alias namen in Abhängigkeit der Studiengänge / Fakultät suchen kann
            // so müssen die Namen derzeit auf globale Ebene eindeutig sein
            var aliasList = db.GroupAliases.Where(g => g.Name.ToUpper().Equals(gruppenId.ToUpper()) &&
                                                  g.CapacityGroup.CurriculumGroup.Curriculum.Organiser.Id == _orgId).ToList();

            // falls leer, jetzt in Zuordnungen nachsehen
            if (!aliasList.Any())
            {
                var zuordnungen = _import.GruppenZuordnungen.Where(x => x.Alias.Equals(gruppenId));

                foreach (var zuordnung in zuordnungen)
                {
                    // Studiengang finden
                    var curr = db.Curricula.SingleOrDefault(x =>
                                                            x.ShortName.Equals(zuordnung.Studiengang) &&
                                                            x.Organiser.Id == org.Id);
                    if (curr == null)
                    {
                        curr = new TimeTable.Data.Curriculum
                        {
                            Organiser = org,
                            ShortName = zuordnung.Studiengang,
                            Name      = zuordnung.Studiengang
                        };
                        db.Curricula.Add(curr);
                    }

                    var sg = curr.CurriculumGroups.SingleOrDefault(x => x.Name.Equals(zuordnung.Studiengruppe));
                    if (sg == null)
                    {
                        sg = new CurriculumGroup
                        {
                            Name           = zuordnung.Studiengruppe,
                            IsSubscribable = true,
                            Curriculum     = curr
                        };
                        db.CurriculumGroups.Add(sg);
                        curr.CurriculumGroups.Add(sg);
                    }

                    var cg = string.IsNullOrEmpty(zuordnung.Kapazitätsgruppe) ?
                             sg.CapacityGroups.SingleOrDefault(x => string.IsNullOrEmpty(x.Name)) :
                             sg.CapacityGroups.SingleOrDefault(x => x.Name.Equals(zuordnung.Kapazitätsgruppe));
                    if (cg == null)
                    {
                        cg = new CapacityGroup
                        {
                            InSS            = true,
                            InWS            = true,
                            Name            = zuordnung.Kapazitätsgruppe,
                            CurriculumGroup = sg
                        };
                        db.CapacityGroups.Add(cg);
                        sg.CapacityGroups.Add(cg);
                    }

                    var al = cg.Aliases.SingleOrDefault(x => x.Name.Equals(zuordnung.Alias));
                    if (al == null)
                    {
                        al = new GroupAlias
                        {
                            Name          = zuordnung.Alias,
                            CapacityGroup = cg
                        };
                        db.GroupAliases.Add(al);
                        cg.Aliases.Add(al);
                    }

                    aliasList.Add(al);
                }
                db.SaveChanges();
            }


            foreach (var groupAlias in aliasList)
            {
                // zugehörige Kapazitätsgruppe
                var capGroup = groupAlias.CapacityGroup;

                // im semester suchen
                var semGroup = semester.Groups.SingleOrDefault(g => g.CapacityGroup.Id == capGroup.Id);

                if (semGroup == null)
                {
                    // semestergruppe gibt es nicht => auf jeden Fall anlegen
                    semGroup = new SemesterGroup
                    {
                        CapacityGroup = capGroup,
                        Semester      = semester,
                    };

                    _Logger.InfoFormat("Semestergruppe {0} angelegt {1}", semGroup.FullName, gruppenId);

                    capGroup.SemesterGroups.Add(semGroup);
                    db.SemesterGroups.Add(semGroup);
                    db.SaveChanges();
                }

                semGroupList.Add(semGroup);
            }

            return(semGroupList);
        }
Exemplo n.º 11
0
        public string ImportCourse(ScheduleCourse scheduleCourse)
        {
            string msg;

            _Logger.DebugFormat("Importiere Fach: {0}", scheduleCourse.Name);
            _report.AppendFormat("<h1>Erzeuge LV \"{0} ({1})\" - [{2}]</h1>", scheduleCourse.Name, scheduleCourse.ShortName,
                                 scheduleCourse.CourseId);
            _report.AppendLine();

            var db = new TimeTableDbContext();

            var organiser = db.Organisers.SingleOrDefault(s => s.Id == _orgId);
            var sem       = db.Semesters.SingleOrDefault(s => s.Id == _semId);

            long msStart = sw.ElapsedMilliseconds;
            var  course  = new Course
            {
                ExternalSource = "JSON",
                ExternalId     = scheduleCourse.CourseId,
                Organiser      = organiser,
                ShortName      = scheduleCourse.ShortName,
                Name           = scheduleCourse.Name,
                Description    = scheduleCourse.Description,
                Occurrence     = CreateDefaultOccurrence(scheduleCourse.SeatRestriction ?? 0),
                IsInternal     = true,
            };

            // Kurs sofort speichern, damit die ID gesichert ist
            db.Activities.Add(course);
            db.SaveChanges();
            long msEnd = sw.ElapsedMilliseconds;

            _Logger.DebugFormat("Dauer: {0}ms", msEnd - msStart);
            msStart = msEnd;

            _report.AppendLine("<h2>Bezeichnungen</h2>");
            _report.AppendLine("<table>");
            _report.AppendFormat("<tr><td>Name</td><td>{0}</td></tr>", course.Name);
            _report.AppendFormat("<tr><td>Kurzname</td><td>{0}</td></tr>", course.ShortName);
            _report.AppendFormat("<tr><td>Beschreibung</td><td>{0}</td></tr>", course.Description);
            _report.AppendLine("</table>");


            // jetzt die Gruppen
            foreach (var scheduleGroup in scheduleCourse.Groups)
            {
                // Fakultät ermitteln
                var org = db.Organisers.SingleOrDefault(x => x.ShortName.Equals(scheduleGroup.FacultyName));

                // Studiengang innerhalb der Fakultät ermitteln
                var curr = org.Curricula.SingleOrDefault(x => x.ShortName.Equals(scheduleGroup.CurriculumShortName));
                if (curr == null)
                {
                    curr = new TimeTable.Data.Curriculum
                    {
                        ShortName = scheduleGroup.CurriculumShortName,
                        Name      = scheduleGroup.CurriculumName,
                        Organiser = org
                    };
                    db.Curricula.Add(curr);
                    db.SaveChanges();
                }

                // Studiengruppe innerhalb des Studiengangs ermitteln
                var groupName = scheduleGroup.GroupName;

                // Sonderlocke FK 11
                // aus der LV-Nummer das Semester raussuchen

                /* OHI 20180720: wieder ausgebaut, weil Schnittstelle jetzt sauber befüllt ist
                 * if (org.ShortName.Equals("FK 11"))
                 * {
                 *  if (!string.IsNullOrEmpty(course.ShortName))
                 *  {
                 *      groupName = course.ShortName[1].ToString();
                 *
                 *  }
                 *  else
                 *  {
                 *      groupName = "#N.V.";
                 *  }
                 * }
                 */

                var currGroup = curr.CurriculumGroups.SingleOrDefault(x => x.Name.Equals(groupName));
                if (currGroup == null)
                {
                    currGroup = new CurriculumGroup
                    {
                        Curriculum     = curr,
                        Name           = groupName,
                        IsSubscribable = true,
                    };
                    db.CurriculumGroups.Add(currGroup);
                    db.SaveChanges();
                }

                // Kapazitätsgruppe innerhalb der Studiengruppe ermitteln
                CapacityGroup capGroup = null;
                if (string.IsNullOrEmpty(scheduleGroup.SubGroupName))
                {
                    capGroup = currGroup.CapacityGroups.SingleOrDefault(x => string.IsNullOrEmpty(x.Name));
                    if (capGroup == null)
                    {
                        capGroup = new CapacityGroup
                        {
                            CurriculumGroup = currGroup,
                            Name            = string.Empty,
                            InSS            = scheduleGroup.SemesterName.StartsWith("SS"),
                            InWS            = scheduleGroup.SemesterName.StartsWith("WS")
                        };
                        db.CapacityGroups.Add(capGroup);
                        db.SaveChanges();
                    }
                }
                else
                {
                    capGroup = currGroup.CapacityGroups.SingleOrDefault(x => !string.IsNullOrEmpty(x.Name) && x.Name.Equals(scheduleGroup.SubGroupName));
                    if (capGroup == null)
                    {
                        capGroup = new CapacityGroup
                        {
                            CurriculumGroup = currGroup,
                            Name            = scheduleGroup.SubGroupName,
                            InSS            = scheduleGroup.SemesterName.StartsWith("SS"),
                            InWS            = scheduleGroup.SemesterName.StartsWith("WS")
                        };
                        db.CapacityGroups.Add(capGroup);
                        db.SaveChanges();
                    }
                }

                // Semester ermitteln
                var semester = db.Semesters.SingleOrDefault(x => x.Name.Equals(scheduleGroup.SemesterName));

                if (semester.Id == sem.Id)
                {
                    // jetzt können wir die Semestergruppe ermitteln
                    var semGroup =
                        db.SemesterGroups.SingleOrDefault(x => x.Semester.Id == semester.Id &&
                                                          x.CapacityGroup.Id == capGroup.Id);
                    if (semGroup == null)
                    {
                        semGroup = new SemesterGroup
                        {
                            Semester      = semester,
                            CapacityGroup = capGroup,
                            IsAvailable   = false // zu Beginn nicht freigegeben
                        };
                        db.SemesterGroups.Add(semGroup);
                        db.SaveChanges();
                    }

                    course.SemesterGroups.Add(semGroup);
                    semGroup.Activities.Add(course);

                    // zu jeder Semestergruppe gibt es dann noch eine
                    // Gruppe für Platzverlosung
                    var occGroup =
                        course.Occurrence.Groups.SingleOrDefault(
                            gg => gg.SemesterGroups.Any(s => s.Id == semGroup.Id));

                    if (occGroup == null)
                    {
                        occGroup = new OccurrenceGroup
                        {
                            Capacity            = 0,
                            FitToCurriculumOnly = true,
                            Occurrence          = course.Occurrence
                        };
                        occGroup.SemesterGroups.Add(semGroup);
                        semGroup.OccurrenceGroups.Add(occGroup);
                        course.Occurrence.Groups.Add(occGroup);
                        db.OccurrenceGroups.Add(occGroup);
                    }

                    // NEU: Chapter und Topics
                    if (!string.IsNullOrEmpty(scheduleGroup.ChapterName) &&
                        !string.IsNullOrEmpty(scheduleGroup.TopicName))
                    {
                        var chapter = curr.Chapters.FirstOrDefault(x => x.Name.Equals(scheduleGroup.ChapterName));
                        if (chapter == null)
                        {
                            chapter = new CurriculumChapter
                            {
                                Curriculum = curr,
                                Name       = scheduleGroup.ChapterName
                            };
                            db.CurriculumChapters.Add(chapter);
                        }

                        var topic = chapter.Topics.FirstOrDefault(x => x.Name.Equals(scheduleGroup.TopicName));
                        if (topic == null)
                        {
                            topic = new CurriculumTopic
                            {
                                Chapter = chapter,
                                Name    = scheduleGroup.TopicName
                            };
                            db.CurriculumTopics.Add(topic);
                        }

                        var semTopic = db.SemesterTopics.FirstOrDefault(x =>
                                                                        x.Semester.Id == sem.Id && x.Topic.Id == topic.Id);

                        if (semTopic == null)
                        {
                            semTopic = new SemesterTopic
                            {
                                Semester = sem,
                                Topic    = topic,
                            };
                            db.SemesterTopics.Add(semTopic);
                        }

                        semTopic.Activities.Add(course);
                    }
                }
                // else => Semestergruppe wird nicht angelegt
            }

            db.SaveChanges();

            if (!course.SemesterGroups.Any())
            {
                _Logger.ErrorFormat("Kurs {0} ohne Gruppe", scheduleCourse.CourseId);
            }

            // zum Schluss die Termine
            _report.AppendLine("<h2>Neue Termine</h2>");
            _report.AppendLine("<table>");

            foreach (var scheduleDate in scheduleCourse.Dates)
            {
                // Der Tag
                var occDate = scheduleDate.Begin.Date;

                bool isVorlesung = true;
                foreach (var sd in _semester.Dates)
                {
                    // Wenn der Termin in eine vorlesungsfreie Zeit fällt, dann nicht importieren
                    if (sd.From.Date <= occDate.Date &&
                        occDate.Date <= sd.To.Date &&
                        sd.HasCourses == false)
                    {
                        isVorlesung = false;
                    }
                }

                if (isVorlesung)
                {
                    var occ = new ActivityDate
                    {
                        Begin      = scheduleDate.Begin,
                        End        = scheduleDate.End,
                        Activity   = course,
                        Occurrence = CreateDefaultOccurrence(),
                    };
                    _report.AppendLine("<tr>");
                    _report.AppendFormat("<td>{0}</td><td>{1}</td><td>{2}</td>", occ.Begin.ToShortDateString(), occ.Begin.ToShortTimeString(), occ.End.ToShortTimeString());

                    _report.AppendFormat("<td>");
                    foreach (var scheduleDateRoom in scheduleDate.Rooms)
                    {
                        _report.AppendFormat("<p>{0}", scheduleDateRoom.RoomNumber);
                        if (!string.IsNullOrEmpty(scheduleDateRoom.RoomNumber))
                        {
                            var room = db.Rooms.SingleOrDefault(r => r.Number.Equals(scheduleDateRoom.RoomNumber));
                            if (room == null)
                            {
                                room = new Room
                                {
                                    Number      = scheduleDateRoom.RoomNumber,
                                    Capacity    = 0,
                                    Description = string.Empty,
                                    Owner       = string.Empty,
                                };
                                db.Rooms.Add(room);
                                db.SaveChanges();

                                _numRooms++;
                                _report.AppendFormat(" !!!NEUER RAUM!!!");
                            }


                            var assignment = db.RoomAssignments.SingleOrDefault(x =>
                                                                                x.Room.Id == room.Id &&
                                                                                x.Organiser.Id == organiser.Id);
                            if (assignment == null)
                            {
                                assignment = new RoomAssignment
                                {
                                    Organiser = organiser,
                                    InternalNeedConfirmation = false, // offen für interne
                                    ExternalNeedConfirmation = true   // geschlossen für externe
                                };

                                room.Assignments.Add(assignment);
                                db.RoomAssignments.Add(assignment);
                                db.SaveChanges();
                            }

                            occ.Rooms.Add(room);
                            _report.AppendFormat("</p>");
                        }
                    }
                    _report.AppendFormat("</td>");

                    _report.AppendFormat("<td>");
                    foreach (var scheduleDateLecturer in scheduleDate.Lecturers)
                    {
                        _report.AppendFormat("<p>{0} ({1})", scheduleDateLecturer.Name, scheduleDateLecturer.ShortName);

                        var lecturer = organiser.Members.SingleOrDefault(l => l.ShortName.Equals(scheduleDateLecturer.ShortName));
                        if (lecturer == null)
                        {
                            lecturer = new OrganiserMember
                            {
                                ShortName   = scheduleDateLecturer.ShortName,
                                Name        = scheduleDateLecturer.Name,
                                Role        = String.Empty,
                                Description = String.Empty
                            };
                            organiser.Members.Add(lecturer);
                            db.Members.Add(lecturer);
                            db.SaveChanges();
                            _numLecturers++;
                            _report.AppendFormat(" !!!NEUER DOZENT!!!");
                        }

                        occ.Hosts.Add(lecturer);
                        _report.AppendFormat("</p>");
                    }
                    _report.AppendFormat("</td>");

                    db.ActivityDates.Add(occ);

                    _report.AppendLine();
                    _report.AppendLine("</tr>");
                }
            }

            _report.AppendLine("</table>");
            db.SaveChanges();

            msEnd = sw.ElapsedMilliseconds;
            _Logger.DebugFormat("Dauer {0}ms", msEnd - msStart);

            msg = $"Kurs {course.ShortName} mit Terminen importiert";

            return(msg);
        }