Beispiel #1
0
        public Boolean CreateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            message = "Learning Area Added Successfully";
            bool ret = true;

            LearningArea la = new LearningArea();
            try {
            ConvertLearningAreaBDOToLearningArea(laBDO, la);
            using (var DCEnt = new DCFIEntities())
            {
                DCEnt.LearningAreas.Add(la);
                DCEnt.Entry(la).State = System.Data.Entity.EntityState.Added;
                int num = DCEnt.SaveChanges();

                if (num == 0)
                {
                    ret = false;
                    message = "Adding of Learning Area failed";
                }
            }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }
            return ret;
        }
Beispiel #2
0
        public LearningAreaBDO GetLearningArea(string learningAreaCode)
        {
            LearningAreaBDO laBDO = null;
            LearningArea l = new LearningArea();
            try { 
            using (var DCEnt = new DCFIEntities())
            {
                var lA = (from learn in DCEnt.LearningAreas
                          where learn.LearningAreaCode == learningAreaCode
                          select learn).FirstOrDefault();

                l = lA;                
           

            if (l != null)
            {
                laBDO = new LearningAreaBDO();
                ConvertLearningAreaToLearningAreaBDO(l, laBDO);
            }
        }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }
            return laBDO;
        }
Beispiel #3
0
        public LearningAreaBDO GetLearningArea(string learningAreaCode)
        {
            LearningAreaBDO laBDO = null;
            LearningArea    l     = new LearningArea();

            try {
                using (var DCEnt = new DCFIEntities())
                {
                    var lA = (from learn in DCEnt.LearningAreas
                              where learn.LearningAreaCode == learningAreaCode
                              select learn).FirstOrDefault();

                    l = lA;
                }

                if (l != null)
                {
                    laBDO          = new LearningAreaBDO();
                    laBDO.Academic = l.Academic;
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                               validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            return(laBDO);
        }
Beispiel #4
0
 public void ConvertLearningAreaToLearningAreaBDO(LearningArea la, LearningAreaBDO labdo)
 {
     // SubjectDAO sdao = new SubjectDAO();
     labdo.Subjects = GetSubjectsforLearningArea(la.LearningAreaCode);
     labdo.Academic = la.Academic;
     labdo.Description = la.Description;
     labdo.LearningAreaCode = la.LearningAreaCode;
     labdo.Units = la.Units;
     labdo.RatePerUnit = la.RatePerUnit;
 }
Beispiel #5
0
 public void ConvertLearningAreaBDOToLearningArea(LearningAreaBDO labdo, LearningArea la)
 {
     SubjectDAO sdao = new SubjectDAO();
     la.Academic = labdo.Academic;
     la.Description = labdo.Description;
     la.LearningAreaCode = labdo.LearningAreaCode;
     la.Units = labdo.Units;
     la.RatePerUnit = labdo.RatePerUnit;
     la.Subjects = sdao.ToSubjectList(labdo.Subjects);
 }
Beispiel #6
0
 public void ConvertLearningAreaToLearningAreaBDO(LearningArea la, LearningAreaBDO labdo)
 {
     // SubjectDAO sdao = new SubjectDAO();
     labdo.Subjects         = GetSubjectsforLearningArea(la.LearningAreaCode);
     labdo.Academic         = la.Academic;
     labdo.Description      = la.Description;
     labdo.LearningAreaCode = la.LearningAreaCode;
     labdo.Units            = la.Units;
     labdo.RatePerUnit      = la.RatePerUnit;
 }
Beispiel #7
0
        public void ConvertLearningAreaBDOToLearningArea(LearningAreaBDO labdo, LearningArea la)
        {
            SubjectDAO sdao = new SubjectDAO();

            la.Academic         = labdo.Academic;
            la.Description      = labdo.Description;
            la.LearningAreaCode = labdo.LearningAreaCode;
            la.Units            = labdo.Units;
            la.RatePerUnit      = labdo.RatePerUnit;
            la.Subjects         = sdao.ToSubjectList(labdo.Subjects);
        }
Beispiel #8
0
 private Boolean CompareLearningArea(LearningArea inDB, LearningArea inNew)
 {
     if (inDB.LearningAreaCode == inNew.LearningAreaCode && inDB.Description == inNew.Description && inDB.Academic == inNew.Academic && inDB.RatePerUnit == inNew.RatePerUnit && inDB.Units == inNew.Units)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #9
0
 public void ConvertLearningAreaToLearningAreaBDO(LearningArea la, LearningAreaBDO labdo)
 {
     // labdo.Subjects = GetSubjectsforLearningArea(la.LearningAreaCode);
     labdo.Academic         = la.Academic;
     labdo.Description      = la.Description;
     labdo.LearningAreaCode = la.LearningAreaCode;
     labdo.Units            = la.Units;
     labdo.RatePerUnit      = la.RatePerUnit;
     labdo.RatePerSubject   = la.RatePerSubject;
     foreach (Subject s in la.Subjects)
     {
         SubjectBDO sb = new SubjectBDO();
         ConvertSubjectToSubjectBDO(s, sb);
         sb.Academic = (bool)la.Academic;
         labdo.Subjects.Add(sb);
     }
 }
Beispiel #10
0
 public void ConvertLearningAreaToLearningAreaBDO(LearningArea la, LearningAreaBDO labdo)
 {
     // labdo.Subjects = GetSubjectsforLearningArea(la.LearningAreaCode);
     labdo.Academic = la.Academic;
     labdo.Description = la.Description;
     labdo.LearningAreaCode = la.LearningAreaCode;
     labdo.Units = la.Units;
     labdo.RatePerUnit = la.RatePerUnit;
     labdo.RatePerSubject = la.RatePerSubject;
     foreach (Subject s in la.Subjects)
     {
         SubjectBDO sb = new SubjectBDO();
        ConvertSubjectToSubjectBDO(s, sb);
         sb.Academic =(bool) la.Academic;
         labdo.Subjects.Add(sb);
     }
 }
        public LearningAreaBDO GetLearningArea(string learningAreaCode)
        {
            LearningArea l = new LearningArea();
            using (var DCEnt = new DCFIEntities())
            {
                var lA = (from learn in DCEnt.LearningAreas
                          where learn.LearningAreaCode == learningAreaCode
                          select learn).FirstOrDefault();

                l = lA;
            }

            LearningAreaBDO laBDO = new LearningAreaBDO();
            ConvertLearningAreaToLearningAreaBDO(l, laBDO);

            return laBDO;
        }
Beispiel #12
0
        public LearningAreaBDO GetLearningArea(string learningAreaCode)
        {
            LearningArea l = new LearningArea();

            using (var DCEnt = new DCFIEntities())
            {
                var lA = (from learn in DCEnt.LearningAreas
                          where learn.LearningAreaCode == learningAreaCode
                          select learn).FirstOrDefault();

                l = lA;
            }

            LearningAreaBDO laBDO = new LearningAreaBDO();

            ConvertLearningAreaToLearningAreaBDO(l, laBDO);

            return(laBDO);
        }
Beispiel #13
0
        public Boolean CreateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            message = "Learning Area Added Successfully";
            bool ret = true;

            LearningArea la = new LearningArea();
            ConvertLearningAreaBDOToLearningArea(laBDO, la);
            using (var DCEnt = new DCFIEntities())
            {
                DCEnt.LearningAreas.Add(la);
                DCEnt.Entry(la).State = System.Data.Entity.EntityState.Added;
                int num = DCEnt.SaveChanges();

                if (num == 0)
                {
                    ret = false;
                    message = "Adding of Learning Area failed";
                }
            }
            return ret;
        }
Beispiel #14
0
        public Boolean CreateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            message = "Learning Area Added Successfully";
            bool ret = true;

            LearningArea la = new LearningArea();

            ConvertLearningAreaBDOToLearningArea(laBDO, la);
            using (var DCEnt = new DCFIEntities())
            {
                DCEnt.LearningAreas.Add(la);
                DCEnt.Entry(la).State = System.Data.Entity.EntityState.Added;
                int num = DCEnt.SaveChanges();


                if (num == 0)
                {
                    ret     = false;
                    message = "Adding of Learning Area failed";
                }
            }
            return(ret);
        }
Beispiel #15
0
        public Boolean CreateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            message = "Learning Area Added Successfully";
            bool ret = true;

            LearningArea la = new LearningArea();

            try {
                ConvertLearningAreaBDOToLearningArea(laBDO, la);
                using (var DCEnt = new DCFIEntities())
                {
                    DCEnt.LearningAreas.Add(la);
                    DCEnt.Entry(la).State = System.Data.Entity.EntityState.Added;
                    int num = DCEnt.SaveChanges();


                    if (num == 0)
                    {
                        ret     = false;
                        message = "Adding of Learning Area failed";
                    }
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                               validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            return(ret);
        }
        public Boolean UpdateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            Boolean ret = true;
            LearningArea l = new LearningArea();
            ConvertLearningAreaBDOToLearningArea(laBDO, l);
            LearningArea laInDB = new LearningArea();
            using (var DCEnt = new DCFIEntities())
            {
                var learningAreaCode = laBDO.LearningAreaCode;
                laInDB = (from la in DCEnt.LearningAreas
                               where la.LearningAreaCode.Equals(learningAreaCode)
                               select la).FirstOrDefault();

                if (laInDB == null)
                {
                    throw new Exception("No Learning Area with Code " + laBDO.LearningAreaCode);
                }

                if (laInDB.Subjects.Count == 0)
                {
                    foreach (Subject s in l.Subjects)
                    {
                        laInDB.Subjects.Add(s);
                    }
                }
                else if (laInDB.Subjects.Count < l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable<Subject> sToAdd = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    IEnumerable<Subject> sToRemove = laInDB.Subjects.Except(l.Subjects);
                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                }
                else if (laInDB.Subjects.Count > l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable<Subject> sToAdd = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    IEnumerable<Subject> sToRemove = laInDB.Subjects.Except(l.Subjects);
                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }
                }
                else if (laInDB.Subjects.Count == l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable<Subject> sToAdd = laInDB.Subjects.Except(l.Subjects);
                    IEnumerable<Subject> sToRemove = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Remove(child);
                        }
                    }

                }
                using (var DC = new DCFIEntities())
                {


                    //   DC.Teachers.Remove(teacherInDB);
                    laInDB = l;

                    foreach (Subject s in laInDB.Subjects)
                        DC.Entry(s).State = s.SubjectID == 0 ? System.Data.Entity.EntityState.Added : System.Data.Entity.EntityState.Modified;

                    DC.Entry(laInDB).State = System.Data.Entity.EntityState.Modified;                  

                     int num = DC.SaveChanges();

                    if (num > 0)
                    {
                        message = "No learning area is updated.";
                    }
                }
            }
            return ret;



        }
Beispiel #17
0
        public Boolean UpdateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            Boolean      ret = true;
            LearningArea l   = new LearningArea();

            ConvertLearningAreaBDOToLearningArea(laBDO, l);
            LearningArea laInDB = new LearningArea();

            using (var DCEnt = new DCFIEntities())
            {
                var learningAreaCode = laBDO.LearningAreaCode;
                laInDB = (from la in DCEnt.LearningAreas
                          where la.LearningAreaCode.Equals(learningAreaCode)
                          select la).FirstOrDefault();

                if (laInDB == null)
                {
                    throw new Exception("No Learning Area with Code " + laBDO.LearningAreaCode);
                }

                if (laInDB.Subjects.Count == 0)
                {
                    foreach (Subject s in l.Subjects)
                    {
                        laInDB.Subjects.Add(s);
                    }
                }
                else if (laInDB.Subjects.Count < l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable <Subject> sToAdd = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    IEnumerable <Subject> sToRemove = laInDB.Subjects.Except(l.Subjects);
                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }
                }
                else if (laInDB.Subjects.Count > l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable <Subject> sToAdd = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    IEnumerable <Subject> sToRemove = laInDB.Subjects.Except(l.Subjects);
                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }
                }
                else if (laInDB.Subjects.Count == l.Subjects.Count)
                {
                    //compare 2 lists check the non existing to the other
                    IEnumerable <Subject> sToAdd    = laInDB.Subjects.Except(l.Subjects);
                    IEnumerable <Subject> sToRemove = l.Subjects.Except(laInDB.Subjects);
                    if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                    if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Remove(child);
                        }
                    }
                }
                using (var DC = new DCFIEntities())
                {
                    //   DC.Teachers.Remove(teacherInDB);
                    laInDB = l;

                    foreach (Subject s in laInDB.Subjects)
                    {
                        DC.Entry(s).State = s.SubjectID == 0 ? System.Data.Entity.EntityState.Added : System.Data.Entity.EntityState.Modified;
                    }

                    DC.Entry(laInDB).State = System.Data.Entity.EntityState.Modified;

                    int num = DC.SaveChanges();

                    if (num > 0)
                    {
                        message = "No learning area is updated.";
                    }
                }
            }
            return(ret);
        }
Beispiel #18
0
        public Boolean UpdateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            List<Subject> toRemove;
            List<Subject> toAdd;
            List<Subject> toUpdate;
            Boolean ret = true;
            LearningArea l = new LearningArea();
            LearningArea laInDB = new LearningArea();
            LearningArea laInDB2 = new LearningArea();
            try {

                using (var DCEnt = new DCFIEntities())
            {
                var learningAreaCode = laBDO.LearningAreaCode;
                laInDB = (from la in DCEnt.LearningAreas
                          where la.LearningAreaCode.Equals(learningAreaCode)
                          select la).FirstOrDefault();

                    if (laInDB == null)
                    {
                        throw new Exception("No Learning Area with Code " + laBDO.LearningAreaCode);
                    }

                    l.Academic = laBDO.Academic;
                    l.Description = laBDO.Description;
                    l.LearningAreaCode = laBDO.LearningAreaCode;
                    l.RatePerSubject = laBDO.RatePerSubject;
                    l.RatePerUnit = laBDO.RatePerUnit;
                    l.Units = laBDO.Units;
                    l.Subjects = ToSubjectList(laBDO.Subjects);
                    if (laInDB.Subjects.Count == 0)
                     {
                         foreach (Subject s in l.Subjects)
                        {
                            laInDB.Subjects.Add(s);
                        }
                }
                else if (laInDB.Subjects.Count < l.Subjects.Count)
                {
                        //compare 2 lists check the non existing to the other
                        List<Subject> sToAdd = new List<Subject>();
                       sToAdd= CompareSubjectLists(l.Subjects.ToList<Subject>(),laInDB.Subjects.ToList<Subject>());
                        //List<Subject> newSubToAdd = new List<Subject>();
                        //newSubToAdd = sToAdd.ToList<Subject>();
                        if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                        List<Subject> sToRemove = new List<Subject>();
                        sToRemove = CompareSubjectLists(laInDB.Subjects.ToList<Subject>(), l.Subjects.ToList<Subject>());

                        if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Remove(child);
                        }
                    }

                }
                else if (laInDB.Subjects.Count > l.Subjects.Count)
                {
                        //compare 2 lists check the non existing to the other
                        List<Subject> sToAdd = new List<Subject>();
                        sToAdd = CompareSubjectLists(l.Subjects.ToList<Subject>(), laInDB.Subjects.ToList<Subject>());
                        if (sToAdd != null)
                    {
                        foreach (Subject child in sToAdd)
                        {
                            laInDB.Subjects.Add(child);
                        }
                    }

                        List<Subject> sToRemove = new List<Subject>();
                        sToRemove = CompareSubjectLists(laInDB.Subjects.ToList<Subject>(), l.Subjects.ToList<Subject>());
                        if (sToRemove != null)
                    {
                        foreach (Subject child in sToRemove)
                        {
                            laInDB.Subjects.Remove(child);
                        }
                    }
                }
                else if (laInDB.Subjects.Count == l.Subjects.Count)
                {
                    toRemove = new List<Subject>();
                    toAdd = new List<Subject>();
                    toUpdate = new List<Subject>();

                    foreach (Subject s in l.Subjects)
                    {
                        int c = laInDB.Subjects.Where(sub => sub.SubjectCode == s.SubjectCode).Count();
                        if (c == 0)
                            toAdd.Add(s);
                    }
                    foreach (Subject c in toAdd)
                    {
                        DCEnt.Subjects.Add(c);
                        DCEnt.Entry(c).State = System.Data.Entity.EntityState.Added;
                         //   DCEnt.SaveChanges();
                     }
                    foreach (Subject s in laInDB.Subjects)
                    {
                        int c = l.Subjects.Where(sub => sub.SubjectCode == s.SubjectCode).Count();
                        if (c == 0)
                            toRemove.Add(s);

                    }
                    foreach (Subject c in toRemove)
                    {
                        DCEnt.Subjects.Remove(c);
                        DCEnt.Entry(c).State = System.Data.Entity.EntityState.Deleted;
                      //  DCEnt.SaveChanges();
                    }

                        DCEnt.LearningAreas.Remove(laInDB);

                        DCEnt.LearningAreas.Attach(laInDB);
                        DCEnt.Entry(laInDB).State = System.Data.Entity.EntityState.Modified;
                       // DCEnt.SaveChanges();
                    }

                        //foreach (Subject s in laInDB.Subjects)
                        //    DCEnt.Entry(s).State = s.SubjectID == 0 ? System.Data.Entity.EntityState.Added : System.Data.Entity.EntityState.Modified;

                        int num = DCEnt.SaveChanges();

                        if (num > 0)
                        {
                            message = "No learning area is updated.";
                        }

            }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }
            return ret;
        }
Beispiel #19
0
 private Boolean CompareLearningArea(LearningArea inDB, LearningArea inNew)
 {
     if (inDB.LearningAreaCode == inNew.LearningAreaCode && inDB.Description == inNew.Description && inDB.Academic==inNew.Academic && inDB.RatePerUnit==inNew.RatePerUnit && inDB.Units==inNew.Units)
         return true;
     else return false;
 }
Beispiel #20
0
        public Boolean UpdateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            List<Subject> toRemove;
            List<Subject> toAdd;
            List<Subject> toUpdate;
            Boolean ret = true;
            LearningArea l = new LearningArea();
            LearningArea laInDB = new LearningArea();
            LearningArea laInDB2 = new LearningArea();
            try {

               
                using (var DCEnt = new DCFIEntities())
            {
                var learningAreaCode = laBDO.LearningAreaCode;
                laInDB = (from la in DCEnt.LearningAreas
                          where la.LearningAreaCode.Equals(learningAreaCode)
                          select la).FirstOrDefault();

                    if (laInDB == null)
                    {
                        throw new Exception("No Learning Area with Code " + laBDO.LearningAreaCode);
                    }
                 
                    l.Academic = laBDO.Academic;
                    l.Description = laBDO.Description;
                    l.LearningAreaCode = laBDO.LearningAreaCode;
                    l.RatePerSubject = laBDO.RatePerSubject;
                    l.RatePerUnit = laBDO.RatePerUnit;
                    l.Units = laBDO.Units;
                    l.Subjects = ToSubjectList(laBDO.Subjects);

                    if (laInDB.Subjects.Count == 0)
                     {
                         foreach (Subject s in l.Subjects)
                        {
                            laInDB.Subjects.Add(s);
                        }
                }
                
                else 
                {
                    toRemove = new List<Subject>();
                    toAdd = new List<Subject>();
                    toUpdate = new List<Subject>();

                        foreach (Subject s in l.Subjects)
                        {
                            Subject subj = new Subject();
                            subj = laInDB.Subjects.Where(sub => sub.SubjectCode == s.SubjectCode).FirstOrDefault();

                            if (subj == null)
                            {
                                DCEnt.Subjects.Add(s);
                                DCEnt.Entry(s).State = System.Data.Entity.EntityState.Added;
                            }
                            else
                            {
                                if (!CompareSubject(subj, s))
                                {
                                    toUpdate.Add(s);
                                }
                            }
                     }

                        //Add new Subject
                        if (toAdd.Count > 0)
                        {
                            foreach (Subject c in toAdd)
                            {
                                DCEnt.Subjects.Add(c);
                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Added;
                            }
                        }

                    foreach (Subject c in laInDB.Subjects)
                    {
                            Subject subj = new Subject();
                            subj = l.Subjects.Where(sub => sub.SubjectCode == c.SubjectCode).FirstOrDefault();
                            Subject upSubj = new Subject();
                            upSubj = toUpdate.Where(sub => sub.SubjectCode == c.SubjectCode).FirstOrDefault();

                            //toRemove subject
                            if (subj == null)
                            {
                                DCEnt.Subjects.Remove(c);
                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Deleted;
                            }

                            //toUpdate subject
                            if (upSubj != null)
                            {
                                c.Description = upSubj.Description;
                                c.MPW = upSubj.MPW;

                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Modified;
                            }


                    }
              

                        DCEnt.LearningAreas.Attach(laInDB);
                        DCEnt.Entry(laInDB).State = System.Data.Entity.EntityState.Modified;
                    }
                   
                       
                        int num = DCEnt.SaveChanges();
                              

                        if (num > 0)
                        {
                            message = "No learning area is updated.";
                        }
                    
                
            }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }
            return ret;
        }
Beispiel #21
0
        public Boolean UpdateLearningArea(ref LearningAreaBDO laBDO, ref string message)
        {
            List <Subject> toRemove;
            List <Subject> toAdd;
            List <Subject> toUpdate;
            Boolean        ret     = true;
            LearningArea   l       = new LearningArea();
            LearningArea   laInDB  = new LearningArea();
            LearningArea   laInDB2 = new LearningArea();

            try {
                using (var DCEnt = new DCFIEntities())
                {
                    var learningAreaCode = laBDO.LearningAreaCode;
                    laInDB = (from la in DCEnt.LearningAreas
                              where la.LearningAreaCode.Equals(learningAreaCode)
                              select la).FirstOrDefault();

                    if (laInDB == null)
                    {
                        throw new Exception("No Learning Area with Code " + laBDO.LearningAreaCode);
                    }

                    l.Academic         = laBDO.Academic;
                    l.Description      = laBDO.Description;
                    l.LearningAreaCode = laBDO.LearningAreaCode;
                    l.RatePerSubject   = laBDO.RatePerSubject;
                    l.RatePerUnit      = laBDO.RatePerUnit;
                    l.Units            = laBDO.Units;
                    l.Subjects         = ToSubjectList(laBDO.Subjects);

                    if (laInDB.Subjects.Count == 0)
                    {
                        foreach (Subject s in l.Subjects)
                        {
                            laInDB.Subjects.Add(s);
                        }
                    }

                    else
                    {
                        toRemove = new List <Subject>();
                        toAdd    = new List <Subject>();
                        toUpdate = new List <Subject>();

                        foreach (Subject s in l.Subjects)
                        {
                            Subject subj = new Subject();
                            subj = laInDB.Subjects.Where(sub => sub.SubjectCode == s.SubjectCode).FirstOrDefault();

                            if (subj == null)
                            {
                                DCEnt.Subjects.Add(s);
                                DCEnt.Entry(s).State = System.Data.Entity.EntityState.Added;
                            }
                            else
                            {
                                if (!CompareSubject(subj, s))
                                {
                                    toUpdate.Add(s);
                                }
                            }
                        }

                        //Add new Subject
                        if (toAdd.Count > 0)
                        {
                            foreach (Subject c in toAdd)
                            {
                                DCEnt.Subjects.Add(c);
                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Added;
                            }
                        }

                        foreach (Subject c in laInDB.Subjects)
                        {
                            Subject subj = new Subject();
                            subj = l.Subjects.Where(sub => sub.SubjectCode == c.SubjectCode).FirstOrDefault();
                            Subject upSubj = new Subject();
                            upSubj = toUpdate.Where(sub => sub.SubjectCode == c.SubjectCode).FirstOrDefault();

                            //toRemove subject
                            if (subj == null)
                            {
                                DCEnt.Subjects.Remove(c);
                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Deleted;
                            }

                            //toUpdate subject
                            if (upSubj != null)
                            {
                                c.Description = upSubj.Description;
                                c.MPW         = upSubj.MPW;

                                DCEnt.Entry(c).State = System.Data.Entity.EntityState.Modified;
                            }
                        }


                        DCEnt.LearningAreas.Attach(laInDB);
                        DCEnt.Entry(laInDB).State = System.Data.Entity.EntityState.Modified;
                    }


                    int num = DCEnt.SaveChanges();


                    if (num > 0)
                    {
                        message = "No learning area is updated.";
                    }
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                               validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            return(ret);
        }