Exemple #1
0
        public static bool UpdateOrInsert(int safeCitationID, int violationID, Safe_CitationViolations data)
        {
            bool result;

            try
            {
                using (var context = new SafeEntities())
                {
                    var dbRecord = (from r in context.Safe_CitationViolations
                                    where r.SafeCitationID == safeCitationID &&
                                    r.ViolationID == violationID
                                    select r).Single() ?? new Safe_CitationViolations();

                    PropertyInfo[] props = data.GetType().GetProperties();

                    Array.ForEach(props, prop =>
                    {
                        var val = prop.GetType().GetProperty(prop.Name).GetValue(data, null);
                        prop.SetValue(dbRecord, val, null);
                    });

                    context.SaveChanges();
                }
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }

            return(result);
        }
Exemple #2
0
        public static bool Save(ICollection <Safe_CitationViolations> citationViolations, int safeCitationID)
        {
            bool result = false;

            try
            {
                using (var context = new SafeEntities())
                {
                    #region Get SafeCitationID

                    var tmp = from c in citationViolations
                              select c;
                    var citationId = safeCitationID;

                    #endregion

                    #region Get List of items to delete

                    var ids = from id in citationViolations
                              select new { id.ViolationID };
                    var idList = ids.Select(item => item.ViolationID).ToList();


                    var deleteList = from d in context.Safe_CitationViolations
                                     where d.SafeCitationID == citationId &&
                                     !idList.Contains(d.ViolationID)
                                     select d;
                    #endregion


                    #region Remove deleted items

                    foreach (var item in deleteList)
                    {
                        context.Safe_CitationViolations.Remove(item);
                    }

                    #endregion

                    #region Update changes

                    var updateList = from d in context.Safe_CitationViolations
                                     where d.SafeCitationID == citationId &&
                                     idList.Contains(d.ViolationID)
                                     select d;


                    foreach (var item in updateList)
                    {
                        var theItem = item;
                        // find matching record from param

                        var records = from record in citationViolations
                                      where record.ViolationID == theItem.ViolationID
                                      select record;

                        Safe_CitationViolations sourceRecord = records.FirstOrDefault();

                        PropertyInfo[] props = theItem.GetType().GetProperties();
                        Array.ForEach(props, prop =>
                        {
                            if (!prop.GetGetMethod().IsVirtual)
                            {
                                var value = prop.GetValue(sourceRecord, null);
                                prop.SetValue(theItem, value, null);
                            }
                        });

                        theItem.SafeCitationID = citationId;
                    }

                    #endregion

                    #region Add new items

                    var adds = from d in context.Safe_CitationViolations
                               where d.SafeCitationID == citationId
                               select d;

                    var addIds = adds.Select(item => item.ViolationID).ToList();

                    var addRecords = from addRecord in citationViolations
                                     where !addIds.Contains(addRecord.ViolationID)
                                     select addRecord;

                    foreach (var item in addRecords)
                    {
                        var theItem   = item;
                        var newRecord = new Safe_CitationViolations();

                        PropertyInfo[] props = theItem.GetType().GetProperties();
                        Array.ForEach(props, prop =>
                        {
                            if (!prop.GetGetMethod().IsVirtual)
                            {
                                var value = prop.GetValue(theItem, null);
                                prop.SetValue(newRecord, value, null);
                            }
                        });
                        newRecord.SafeCitationID = citationId;
                        newRecord.Conditions     = string.Empty;
                        context.Safe_CitationViolations.Add(newRecord);
                    }


                    #endregion

                    #region Save changes
                    try
                    {
                        context.SaveChanges();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                string message = string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                                Trace.TraceInformation(message);
                            }
                        }
                    }


                    #endregion
                }


                result = true;
            }
            catch (Exception ex)
            {
                Exception theException = ex;
                while (theException.InnerException != null)
                {
                    theException = theException.InnerException;
                }
                throw theException;
            }

            return(result);
        }
Exemple #3
0
        public string Save()
        {
            string result = "true";

            try
            {
                var safeCitationID = this.SafeCitationID;
                using (var context = new SafeEntities())
                {
                    Safe_Citations safeCitation;
                    if (this.SafeCitationID == 0)
                    {
                        safeCitation = new Safe_Citations();
                    }
                    else
                    {
                        safeCitation = (from c in context.Safe_Citations
                                        where c.SafeCitationID == safeCitationID
                                        select c).Single();
                    }

                    PropertyInfo[] props = safeCitation.GetType().GetProperties();

                    foreach (PropertyInfo prop in props)
                    {
                        if (prop.GetGetMethod().IsVirtual)
                        {
                            //if (prop.PropertyType.Name.Contains("ICollection"))
                            //{
                            //  // get static save method
                            //  MethodInfo saveMethod = prop.GetType().GetMethod("Save", BindingFlags.Static | BindingFlags.Public);
                            //  // call save method and pass it the collection
                            //  saveMethod.Invoke(null, new object[] {(object) prop.GetValue(this, null)});
                            //}
                            //else
                            //{
                            //  continue;
                            //}
                            continue;
                        }

                        var val = this.GetType().GetProperty(prop.Name).GetValue(this, null);
                        prop.SetValue(safeCitation, val, null);
                    }

                    if (SafeCitationID == 0)
                    {
                        context.Safe_Citations.Add(safeCitation);
                    }

                    try
                    {
                        var saveFlag = true;

                        #region Validate
                        string errorMessage = string.Empty;

                        var validationErrors = context.GetValidationErrors()
                                               .Where(vr => !vr.IsValid)
                                               .SelectMany(vr => vr.ValidationErrors).ToList();

                        // Check for duplicate citation number.
                        if (SafeCitationID == 0)
                        {
                            var citationNumberAlreadyExists = (from c in context.Safe_Citations
                                                               where c.CitationNumber == safeCitation.CitationNumber
                                                               select c).Any();

                            if (citationNumberAlreadyExists)
                            {
                                validationErrors.Add(new DbValidationError("CitationNumber", "You entered a duplicate citation number. Either modify the citation number or edit the existing citation."));
                            }
                        }

                        if (Safe_CitationViolations.Count() < 1)
                        {
                            validationErrors.Add(new DbValidationError("Safe_CitationViolations", "At least one violation is required."));
                        }

                        if (Safe_CitationPoliceOfficers.Count() < 1 && safeCitation.Superintendent == "")
                        {
                            validationErrors.Add(new DbValidationError("Safe_CitationPoliceOfficers", "At least one officer or a superintendent is required."));
                        }

                        foreach (var error in validationErrors)
                        {
                            saveFlag      = false;
                            errorMessage += error.ErrorMessage + "</br>";
                        }
                        #endregion

                        // Save Collections
                        if (saveFlag)
                        {
                            context.SaveChanges();

                            if (this.WhenDue != null)
                            {
                                foreach (Safe_CitationViolations violation in Safe_CitationViolations)
                                {
                                    if (violation.Safe_ViolationTypes.ViolationTypeDescription == "Equipment")
                                    {
                                        violation.ViolationPoints = 0;
                                        violation.Safe_Violations.ViolationPoints = 0;
                                    }
                                }
                            }

                            SAFE.Safe_CitationViolations.Save(Safe_CitationViolations, safeCitation.SafeCitationID);

                            SAFE.Safe_CitationPoliceOfficers.Save(Safe_CitationPoliceOfficers, safeCitation.SafeCitationID);
                            SAFE.Documents.Save(this.Documents, safeCitation.SafeCitationID);
                            SAFE.Safe_Hearings.Save(Safe_Hearings, safeCitation.SafeCitationID);
                            context.SaveChanges();

                            if (this.SafeCitationID == 0)
                            {
                                this.SafeCitationID = safeCitation.SafeCitationID;
                            }

                            return("true");
                        }
                        else
                        {
                            return(errorMessage);
                        }
                    }
                    catch (Exception ex)
                    {
                        result = ((ex.InnerException).InnerException).Message;
                    }
                }
            }
            catch (Exception ex)
            {
                result = "ERROR:\n\r\n\r" + ex.Message + " INNER EXCEPTION: " + ((ex.InnerException).InnerException).Message;
            }

            return(result);
        }