public ResidentItem SaveResident(ResidentItem residentItem)
        {
            Resident resident = null;

            switch (residentItem.InitializationType)
            {
            case InitializationTypes.Insert:
                resident = new Resident
                {
                    FirstName           = residentItem.FirstName,
                    LastName            = residentItem.LastName,
                    MiddleName          = residentItem.MiddleName,
                    IdentificatorNumber = residentItem.IdentificatorNumber,
                    GenderID            = residentItem.GenderID,
                    RegionID            = residentItem.RegionID,
                    CommunityID         = residentItem.CommunityID,
                    Street    = residentItem.Street,
                    Building  = residentItem.Building,
                    Home      = residentItem.Home,
                    BirthYear = residentItem.BirthYear,
                    Phone     = residentItem.Phone,
                    Email     = residentItem.Email
                };
                if (residentItem.BirthYear != null)
                {
                    resident.BirthDate = new DateTime((Int16)residentItem.BirthYear, 1, 1);
                }
                db.Residents.Add(resident);
                break;

            case InitializationTypes.Update:
                resident = new Resident
                {
                    ResidentID          = residentItem.ResidentID,
                    FirstName           = residentItem.FirstName,
                    LastName            = residentItem.LastName,
                    MiddleName          = residentItem.MiddleName,
                    IdentificatorNumber = residentItem.IdentificatorNumber,
                    GenderID            = residentItem.GenderID,
                    RegionID            = residentItem.RegionID,
                    CommunityID         = residentItem.CommunityID,
                    Street    = residentItem.Street,
                    Building  = residentItem.Building,
                    Home      = residentItem.Home,
                    BirthYear = residentItem.BirthYear,
                    Phone     = residentItem.Phone,
                    Email     = residentItem.Email
                };
                if (residentItem.BirthYear != null)
                {
                    resident.BirthDate = new DateTime((Int16)residentItem.BirthYear, 1, 1);
                }
                db.Residents.Attach(resident);
                db.Entry(resident).State = EntityState.Modified;
                break;
            }
            db.SaveChanges();
            residentItem.ResidentID = resident.ResidentID;
            return(residentItem);
        }
        public OralConsultationItem GetOralConsultationItemByIssueID(int issueID)
        {
            var      issue    = db.Issues.Find(issueID);
            Resident resident = null;

            if (issue.ResidentID != null)
            {
                resident = db.Residents.Find(issue.ResidentID);
            }
            OralConsultationItem item = new OralConsultationItem
            {
                OralConsultationID      = 0,
                IssueID                 = issue.IssueID,
                IssueName               = issue.IssueName,
                IssueDate               = issue.IssueDate,
                IssueDescription        = issue.IssueDescription,
                OralConsultationDate    = null,
                InvocationTypeID        = null,
                TargetGroupID           = null,
                ProblemDescription      = String.Empty,
                ConsultationDescription = String.Empty,
                GUID = Guid.NewGuid(),
                InitializationType = InitializationTypes.Insert,
                UserID             = String.Empty,
                ChangeDate         = null
            };

            return(item);
        }
        /// <summary>
        /// Get existing temporary Resident and return it as a result
        /// </summary>
        /// <returns></returns>
        public ResidentItem GetResidentItem(int?residentID = null)
        {
            try
            {
                if (residentID != null)
                {
                    Resident     resident = db.Residents.Find(residentID);
                    ResidentItem item     = new ResidentItem
                    {
                        ResidentID          = resident.ResidentID,
                        FirstName           = resident.FirstName,
                        LastName            = resident.LastName,
                        MiddleName          = resident.MiddleName,
                        BirthDate           = resident.BirthDate,
                        IdentificatorNumber = resident.IdentificatorNumber,
                        GenderID            = resident.GenderID,
                        RegionID            = resident.RegionID,
                        CommunityID         = resident.CommunityID,
                        Street    = resident.Street,
                        Building  = resident.Building,
                        Home      = resident.Home,
                        BirthYear = resident.BirthYear,
                        Phone     = resident.Phone,
                        Email     = resident.Email,

                        InitializationType = InitializationTypes.Update,
                    };
                    return(item);
                }
                else
                {
                    var item = new ResidentItem
                    {
                        InitializationType = InitializationTypes.Insert,
                    };
                    return(item);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
                return(null);
            }
        }
        public IssueItem GetIssueItemByID(int issueID)
        {
            var issue = db.Issues.Find(issueID);

            Resident resident = null;

            if (issue.ResidentID != null)
            {
                resident = db.Residents.Find(issue.ResidentID);
            }

            Company company = null;

            if (issue.CompanyID != null)
            {
                company = db.Companys.Find(issue.CompanyID);
            }

            if (issue == null)
            {
                throw new NullReferenceException(String.Format("There is no record in the 'Issue' table with given {0} ID", issueID));
            }

            IssueItem item = new IssueItem();

            if (issue.IssueCategoryID == 1)
            {
                item.IssueID          = issue.IssueID;
                item.IssueName        = issue.IssueName;
                item.IssueDescription = issue.IssueDescription;
                item.IssueDate        = issue.IssueDate;
                item.IssueCategoryID  = issue.IssueCategoryID;
                item.IssueTypeID      = issue.IssueTypeID;

                item.ResidentID = issue.ResidentID;

                if (resident != null)
                {
                    item.FirstName           = resident.FirstName;
                    item.LastName            = resident.LastName;
                    item.MiddleName          = resident.MiddleName;
                    item.IdentificatorNumber = resident.IdentificatorNumber;
                    item.BirthDate           = resident.BirthDate;
                }
            }
            else if (issue.IssueCategoryID == 2)
            {
                item.IssueID          = issue.IssueID;
                item.IssueName        = issue.IssueName;
                item.IssueDescription = issue.IssueDescription;
                item.IssueDate        = issue.IssueDate;
                item.IssueCategoryID  = issue.IssueCategoryID;
                item.IssueTypeID      = issue.IssueTypeID;

                item.CompanyID = issue.CompanyID;

                if (company != null)
                {
                    item.CompanyName = company.CompanyName;
                }
            }
            else
            {
                throw new NullReferenceException(String.Format("The issue have not a category"));
            }
            return(item);
        }