Ejemplo n.º 1
0
        public static Object GetSerializableObject(Type type, Object o, bool continueSerializing)
        {
            Object return_object = null;
            if (type == typeof(DegreeProgram))
            {
                DegreeProgram dp = o as DegreeProgram;
                if (dp == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.DegreeProgram newItem = new SerializableModels.DegreeProgram
                {
                    ID = dp.ID,
                    degreeProgramName = dp.degreeProgramName,
                    degreeProgramDescription = dp.degreeProgramDescription,
                };

                if (dp.requiredCourses != null && dp.electiveCourses != null && continueSerializing)
                {
                    List<SerializableModels.RequiredCourse> reqCourses = new List<SerializableModels.RequiredCourse>();
                    List<SerializableModels.ElectiveCourse> elecCourses = new List<SerializableModels.ElectiveCourse>();

                    foreach (RequiredCourse rc in dp.requiredCourses)
                    {
                        reqCourses.Add((SerializableModels.RequiredCourse)GetSerializableObject(typeof(RequiredCourse), rc, true));
                    }
                    foreach (ElectiveCourse ec in dp.electiveCourses)
                    {
                        elecCourses.Add((SerializableModels.ElectiveCourse)GetSerializableObject(typeof(ElectiveCourse), ec, true));
                    }

                    newItem.requiredCourses = reqCourses.ToArray();
                    newItem.electiveCourses = elecCourses.ToArray();
                }

                return_object = newItem;
            }
            else if (type == typeof(RequiredCourse))
            {
                RequiredCourse rq = o as RequiredCourse;
                if (rq == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.RequiredCourse newItem = new SerializableModels.RequiredCourse
                {
                    ID = rq.ID,
                    courseID = rq.courseID,
                    degreeProgramID = rq.degreeProgramID,
                    semester = rq.semester,
                    degreeProgram = (SerializableModels.DegreeProgram)GetSerializableObject(typeof(DegreeProgram), rq.degreeProgram, false),
                };

                newItem.course = (SerializableModels.Course)GetSerializableObject(typeof(Course), rq.course, true);

                return_object = newItem;
            }
            else if (type == typeof(ElectiveCourse))
            {
                ElectiveCourse eq = o as ElectiveCourse;
                if (eq == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.ElectiveCourse newItem = new SerializableModels.ElectiveCourse
                {
                    ID = eq.ID,
                    electiveListID = eq.electiveListID,
                    degreeProgramID = eq.degreeProgramID,
                    semester = eq.semester,
                    credits = eq.credits,
                };

                // If we are serializing from a degree program, continue serialization
                if (true)
                {
                    newItem.electiveList = (SerializableModels.ElectiveList)GetSerializableObject(typeof(ElectiveList), eq.electiveList, true);
                }

                return_object = newItem;
            }
            else if (type == typeof(Course))
            {
                Course c = o as Course;
                if (c == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.Course newItem = new SerializableModels.Course
                {
                    ID = c.ID,
                    coursePrefix = c.coursePrefix,
                    courseNumber = c.courseNumber,
                    courseTitle = c.courseTitle,
                    courseDescription = c.courseDescription,
                    minHours = c.minHours,
                    maxHours = c.maxHours,
                    undergrad = c.undergrad,
                    graduate = c.graduate,
                    variable = c.variable,
                };

                if (continueSerializing)
                {
                    List<SerializableModels.PrerequisiteCourse> prereqsList = new List<SerializableModels.PrerequisiteCourse>();
                    List<SerializableModels.PrerequisiteCourse> prereqsForList = new List<SerializableModels.PrerequisiteCourse>();
                    foreach (PrerequisiteCourse pc in c.prerequisites)
                    {
                        prereqsList.Add((SerializableModels.PrerequisiteCourse)GetSerializableObject(typeof(PrerequisiteCourse), pc, true));
                    }
                    foreach (PrerequisiteCourse pc in c.prerequisiteFor)
                    {
                        prereqsForList.Add((SerializableModels.PrerequisiteCourse)GetSerializableObject(typeof(PrerequisiteCourse), pc, true));
                    }
                    newItem.prerequisites = prereqsList.ToArray();
                    newItem.prerequisiteFor = prereqsForList.ToArray();
                }

                return_object = newItem;
            }
            else if (type == typeof(ElectiveList))
            {
                ElectiveList eList = o as ElectiveList;
                if (eList == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.ElectiveList newItem = new SerializableModels.ElectiveList
                {
                    ID = eList.ID,
                    electiveListName = eList.electiveListName,
                    shortName = eList.shortName,
                };

                if (continueSerializing)
                {
                    List<SerializableModels.ElectiveListCourse> eListCourses = new List<SerializableModels.ElectiveListCourse>();
                    foreach (ElectiveListCourse elc in eList.courses)
                    {
                        eListCourses.Add((SerializableModels.ElectiveListCourse)GetSerializableObject(typeof(ElectiveListCourse), elc, true));
                    }
                    // Currently causes to exceed message size limit
                    //newItem.courses = eListCourses.ToArray();
                }

                return_object = newItem;
            }
            else if (type == typeof(ElectiveListCourse))
            {
                ElectiveListCourse elc = o as ElectiveListCourse;
                if (elc == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.ElectiveListCourse newItem = new SerializableModels.ElectiveListCourse
                {
                    ID = elc.ID,
                    courseID = elc.courseID,
                    electiveListID = elc.electiveListID,
                    course = (SerializableModels.Course)GetSerializableObject(typeof(Course), elc.course, true),
                };

                return_object = newItem;
            }
            else if (type == typeof(PrerequisiteCourse))
            {
                PrerequisiteCourse pc = o as PrerequisiteCourse;
                if (pc == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.PrerequisiteCourse newItem = new SerializableModels.PrerequisiteCourse
                {
                    ID = pc.ID,
                    prerequisiteCourseID = pc.prerequisiteCourseID,
                    prerequisiteForCourseID = pc.prerequisiteForCourseID,
                    prerequisiteCourse = (SerializableModels.Course)GetSerializableObject(typeof(Course), pc.prerequisiteCourse, false),
                    prerequisiteForCourse = (SerializableModels.Course)GetSerializableObject(typeof(Course), pc.prerequisiteForCourse, false),
                };

                return_object = newItem;
            }
            else if (type == typeof(Plan))
            {
                Plan plan = o as Plan;
                if (plan == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.Plan newItem = new SerializableModels.Plan
                {
                    ID = plan.ID,
                    planName = plan.planName,
                    degreeProgramID = plan.degreeProgramID,
                    userID = plan.userID,
                    semesterID = plan.semesterID,
                    semester = plan.semester,
                };

                if (continueSerializing)
                {
                    newItem.degreeProgram = (SerializableModels.DegreeProgram)GetSerializableObject(typeof(DegreeProgram), plan.degreeProgram, false);
                    newItem.user = (SerializableModels.User)GetSerializableObject(typeof(User), plan.user, true);
                    List<SerializableModels.PlanCourse> planCourses = new List<SerializableModels.PlanCourse>();
                    foreach (PlanCourse planCourse in plan.planCourses)
                    {
                        planCourses.Add((SerializableModels.PlanCourse)GetSerializableObject(typeof(PlanCourse), planCourse, true));
                    }
                    newItem.planCourses = planCourses.ToArray();
                }

                return_object = newItem;
            }
            else if (type == typeof(User))
            {
                User user = o as User;
                if (user == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.User newItem = new SerializableModels.User
                {
                    ID = user.ID,
                    username = user.username,
                    realName = user.realName,
                };

                if (continueSerializing)
                {
                    List<SerializableModels.Plan> plans = new List<SerializableModels.Plan>();
                    foreach (Plan plan in user.plans)
                    {
                        plans.Add((SerializableModels.Plan)GetSerializableObject(typeof(Plan), plan, false));
                    }
                }

                return_object = newItem;
            }
            else if (type == typeof(PlanCourse))
            {
                PlanCourse pc = o as PlanCourse;
                if (pc == null)
                {
                    throw new ArgumentException();
                }

                SerializableModels.PlanCourse newItem = new SerializableModels.PlanCourse
                {
                    ID = pc.ID,
                    notes = pc.notes,
                    planID = pc.planID,
                    courseID = pc.courseID,
                    electiveListID = pc.electiveListID,
                    semesterID = pc.semesterID,
                    order = pc.order,
                    semester = pc.semester,
                    credits = pc.credits,
                };

                if (continueSerializing)
                {
                    if (pc.plan != null)
                    {
                        newItem.plan = (SerializableModels.Plan)GetSerializableObject(typeof(Plan), pc.plan, false);
                    }
                    if (pc.course != null)
                    {
                        newItem.course = (SerializableModels.Course)GetSerializableObject(typeof(Course), pc.course, true);
                    }
                    if (pc.electiveList != null)
                    {
                        newItem.electiveList = (SerializableModels.ElectiveList)GetSerializableObject(typeof(ElectiveList), pc.electiveList, false);
                    }
                }

                return_object = newItem;
            }
            else
            {
                throw new NotImplementedException();
            }
            return return_object;
        }
Ejemplo n.º 2
0
        public static void GetMessage()
        {
            if (!MessageQueue.Exists(REQUEST_QUEUE))
                _request_queue = MessageQueue.Create(REQUEST_QUEUE);
            else
                _request_queue = new MessageQueue(REQUEST_QUEUE);

            if (!MessageQueue.Exists(RESPONSE_QUEUE))
                _response_queue = MessageQueue.Create(RESPONSE_QUEUE);
            else
                _response_queue = new MessageQueue(RESPONSE_QUEUE);

            ThreadStart ts = delegate
            {
                while (continueRunning)
                {
                    Message msg = _request_queue.Receive(new TimeSpan(1, 0, 0));

                    Object msgBody = null;

                    String[] logic = msg.Label.Split('-');

                    switch (logic[0].Trim())
                    {
                        case "Index":
                            switch (logic[1].Trim())
                            {
                                case "DegreePrograms":
                                    List<SerializableModels.DegreeProgram> degreeprograms = new List<SerializableModels.DegreeProgram>();
                                    var degrees = degreePrograms.GetAll();
                                    foreach (DegreeProgram degree in degrees)
                                    {
                                        degreeprograms.Add((SerializableModels.DegreeProgram)GetSerializableObject(typeof(DegreeProgram), degree, true));
                                    }
                                    msgBody = degreeprograms.ToArray();
                                    break;

                                case "Plans":
                                    List<SerializableModels.Plan> listplans = new List<SerializableModels.Plan>();
                                    var returnplans = plans.GetAll();
                                    foreach (Plan plan in returnplans)
                                    {
                                        listplans.Add((SerializableModels.Plan)GetSerializableObject(typeof(Plan), plan, true));
                                    }
                                    msgBody = listplans.ToArray();
                                    break;

                                case "Semesters":
                                    msgBody = semesters.GetAll().ToArray();
                                    break;

                            }
                            break;

                        case "Create":
                            switch (logic[1].Trim())
                            {
                                case "DegreePrograms":
                                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(SerializableModels.DegreeProgram) });
                                    SerializableModels.DegreeProgram sdp = (SerializableModels.DegreeProgram) msg.Body;
                                    DegreeProgram dp = (DegreeProgram) GetRegularObject(typeof(DegreeProgram), sdp);
                                    degreePrograms.Add(dp);
                                    degreePrograms.SaveChanges();
                                    break;

                                case "Plans":
                                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(SerializableModels.Plan) });
                                    SerializableModels.Plan newSerPlan = (SerializableModels.Plan) msg.Body;
                                    Plan newPlan = (Plan) GetRegularObject(typeof(Plan), newSerPlan);
                                    plans.Add(newPlan);
                                    plans.SaveChanges();
                                    break;

                                case "ElectiveCourses":
                                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(ElectiveCourse) });
                                    ElectiveCourse ec = msg.Body as ElectiveCourse;
                                    electiveCourses.Add(ec);
                                    electiveCourses.SaveChanges();
                                    break;

                                case "RequiredCourses":
                                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(RequiredCourse) });
                                    RequiredCourse rc = msg.Body as RequiredCourse;
                                    requiredCourses.Add(rc);
                                    requiredCourses.SaveChanges();
                                    break;

                                case "Courses":
                                    msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(Course) });
                                    Course c = msg.Body as Course;
                                    courses.Add(c);
                                    courses.SaveChanges();
                                    break;
                            }
                            break;

                        case "Edit":
                            switch (logic[1].Trim())
                            {
                                case "DegreePrograms":
                                    break;

                                case "Plans":
                                    break;
                            }
                            break;

                        case "Delete":
                            msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(int) });
                            int id = (int)msg.Body;
                            switch (logic[1].Trim())
                            {
                                case "DegreePrograms":
                                    var degree = degreePrograms.Find(id);
                                    degreePrograms.Remove(degree);
                                    degreePrograms.SaveChanges();
                                    break;

                                case "Plans":
                                    var plan = plans.Find(id);
                                    plans.Remove(plan);
                                    plans.SaveChanges();
                                    break;

                                case "ElectiveCourses":
                                    var elective = electiveCourses.Find(id);
                                    electiveCourses.Remove(elective);
                                    electiveCourses.SaveChanges();
                                    break;

                                case "RequiredCourses":
                                    var required = requiredCourses.Find(id);
                                    requiredCourses.Remove(required);
                                    requiredCourses.SaveChanges();
                                    break;

                                case "Courses":
                                    var course = courses.Find(id);
                                    courses.Remove(course);
                                    courses.SaveChanges();
                                    break;
                            }
                            break;

                        case "Details":
                            msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(int) });
                            id = (int)msg.Body;
                            switch (logic[1].Trim())
                            {
                                case "DegreePrograms":
                                    var degree = degreePrograms.Find(id);
                                    SerializableModels.DegreeProgram ds = (SerializableModels.DegreeProgram)GetSerializableObject(typeof(DegreeProgram), degree, true);
                                    msgBody = new SerializableModels.DegreeProgram[] { ds };
                                    break;

                                case "Plans":
                                    var plan = plans.Find(id);
                                    SerializableModels.Plan ps = (SerializableModels.Plan)GetSerializableObject(typeof(Plan), plan, true);
                                    msgBody = new SerializableModels.Plan[] { ps };
                                    break;

                                case "ElectiveCourses":
                                    var electivelist = electiveLists.Find(id);
                                    SerializableModels.ElectiveList el = (SerializableModels.ElectiveList)GetSerializableObject(typeof(ElectiveList), electivelist, true);
                                    msgBody = new SerializableModels.ElectiveList[] { el };
                                    break;

                                case "RequiredCourses":
                                    var requiredcourse = requiredCourses.Find(id);
                                    SerializableModels.RequiredCourse rc = (SerializableModels.RequiredCourse)GetSerializableObject(typeof(RequiredCourse), requiredcourse, true);
                                    msgBody = new SerializableModels.RequiredCourse[] { rc };
                                    break;

                                case "Courses":
                                    var course = courses.Find(id);
                                    SerializableModels.Course c = (SerializableModels.Course)GetSerializableObject(typeof(Course), course, true);
                                    msgBody = new SerializableModels.Course[] { c };
                                    break;
                            }

                            break;

                        //case "Courses":
                        //    break;

                        //case "ElectiveLists":
                        //    break;

                        //case "RequiredCourses":
                        //    break;

                        //case "ElectiveCourses":
                        //    break;

                        //case "ElectiveListCourses":
                        //    break;

                        //case "PrerequisiteCourses":
                        //    break;

                        //case "Users":
                        //    break;

                        //case "Plans":
                        //    break;

                        //case "PlanCourses":
                        //    break;

                        //case "Semesters":
                        //    break;
                    }

                    _response_queue.Send(msgBody, msg.Label);
                }
            };

            Thread thread = new Thread(ts);
            thread.Start();
        }