public void Post(SegmentModel model)
 {
     //Send update to repo
     try
     {
         RtpProjectRepository.AddSegment(model);
     }
     catch (Exception ex)
     {
         Logger.WarnException("Could not create RTP Project Segment", ex);
         throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.ExpectationFailed) { ReasonPhrase = ex.Message });
     }
 }
 public JsonResult GetSegmentLRSDetails(int LRSId)
 {
     SegmentModel model = new SegmentModel();
     try
     {
         XMLService xml = new XMLService(_rtpProjectRepository);
         model.LRSRecord = xml.LoadRecord((int)SchemeName.LRSProjects, LRSId);
     }
     catch (Exception ex)
     {
         return Json(new { message = "LRS Record not found." });
     }
     return Json(model);
 }
 public JsonResult GetSegmentLRS(int segmentId)
 {
     SegmentModel model = new SegmentModel();
     try
     {
         model.LRSSchemeBase = _rtpProjectRepository.GetLRSScheme((int)SchemeName.LRSProjects);
         XMLService xml = new XMLService(_rtpProjectRepository);
         model._LRS = xml.LoadRecords((int)SchemeName.LRSProjects, segmentId);
     }
     catch (Exception ex)
     {
         return Json(new { message = "Segment not found." });
     }
     return Json(model);
 }
        public JsonResult AddSegment(SegmentModel model)
        {
            int segmentId = 0;

            try
            {
                segmentId = _rtpProjectRepository.AddSegment(model);
                if (segmentId == 0)
                    throw new Exception("Returned 0 on projectVersionId" + model.ProjectVersionId);

            }
            catch (Exception ex)
            {
                return Json(new { error = "Changes could not be stored. An error has been logged." });
            }
            return Json(new { message = "Segment successfully added.", segmentId = segmentId });
        }
        public JsonResult AddLRSRecord(SegmentModel model)
        {
            try
            {
                XMLService xml = new XMLService(_rtpProjectRepository);
                if (!String.IsNullOrEmpty(model.LRSRecordRaw))
                {
                    string data = xml.GenerateXml(xml.GetScheme((int)SchemeName.LRSProjects), new LRSRecord() { Columns = model.LRSRecordRaw.ToDictionary(',') });
                    model.LRSxml = data;

                    _rtpProjectRepository.AddLRSRecord(model);
                }
                else return Json(new { message = "Changes were not found. Refresh your page and try again." });

            }
            catch (Exception ex)
            {
                return Json(new { message = "Changes could not be stored. An error has been logged." });
            }
            return Json(new { message = "Segment successfully updated." });
        }
 public JsonResult UpdateSegmentSummary(SegmentModel model)
 {
     try
     {
         _rtpProjectRepository.UpdateSegmentSummary(model);
     }
     catch (Exception ex)
     {
         return Json(new { error = "Changes could not be stored. An error has been logged." });
     }
     return Json(new { message = "Segment successfully updated." });
 }
        public Int32 AddSegment(SegmentModel model)
        {
            int retval = 0;
            using (SqlCommand command = new SqlCommand("[RTP].[AddSegment]") { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@ProjectVersionId", model.ProjectVersionId);
                command.Parameters.AddWithValue("@FacilityName", model.FacilityName != null ? (object)model.FacilityName.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@StartAt", model.StartAt != null ? (object)model.StartAt.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@EndAt", model.EndAt != null ? (object)model.EndAt.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@NetworkId", model.NetworkId > 0 ? model.NetworkId.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@ImprovementTypeId", model.ImprovementTypeId > 0 ? model.ImprovementTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@PlanFacilityTypeId", model.PlanFacilityTypeId > 0 ? model.PlanFacilityTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@ModelingFacilityTypeId", model.ModelingFacilityTypeId > 0 ? model.ModelingFacilityTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@OpenYear", model.OpenYear > 0 ? model.OpenYear : (object)DBNull.Value);
                command.Parameters.AddWithValue("@LanesBase", model.LanesBase > 0 ? model.LanesBase : (object)DBNull.Value);
                command.Parameters.AddWithValue("@LanesFuture", model.LanesFuture > 0 ? model.LanesFuture : (object)DBNull.Value);
                command.Parameters.AddWithValue("@SpacesFuture", model.SpacesFuture > 0 ? model.SpacesFuture : (object)DBNull.Value);
                command.Parameters.AddWithValue("@AssignmentStatusId", model.AssignmentStatusID > 0 ? model.AssignmentStatusID : (object)DBNull.Value);

                //command.Parameters.AddWithValue("@RouteName", !String.IsNullOrEmpty(model.LRS.RouteName) ? model.LRS.RouteName : (object)DBNull.Value);
                //command.Parameters.AddWithValue("@BeginMeasure", model.LRS.BeginMeasure > 0 ? model.LRS.BeginMeasure : 0);
                //command.Parameters.AddWithValue("@EndMeasure", model.LRS.EndMeasure > 0 ? model.LRS.EndMeasure : 0);
                //command.Parameters.AddWithValue("@Comments", !String.IsNullOrEmpty(model.LRS.Comments) ? model.LRS.Comments : (object)DBNull.Value);
                //command.Parameters.AddWithValue("@Offset", model.LRS.Offset > 0 ? model.LRS.Offset : (object)DBNull.Value);
                SqlParameter outParam = new SqlParameter("@SegmentId", SqlDbType.Int);
                outParam.Direction = ParameterDirection.Output;
                command.Parameters.Add(outParam);

                this.ExecuteNonQuery(command);
                retval = (int)command.Parameters["@SegmentId"].Value;
            }
            return retval;
        }
        public void AddLRSRecord(SegmentModel model)
        {
            using (SqlCommand command = new SqlCommand("[dbo].[AddLRSRecord]") { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@SegmentId", model.SegmentId);

                if (!String.IsNullOrEmpty(model.LRSxml))
                {
                    byte[] encodedString = System.Text.Encoding.UTF8.GetBytes(model.LRSxml);
                    MemoryStream ms = new MemoryStream(encodedString);
                    ms.Flush();
                    ms.Position = 0;

                    XmlDocument xml = new XmlDocument();
                    xml.Load(ms);

                    using (XmlNodeReader xnr = new XmlNodeReader(xml))
                    {
                        command.Parameters.Add("@xml", SqlDbType.Xml).Value = new SqlXml(xnr);
                        //command.Parameters.AddWithValue("@xml", !String.IsNullOrEmpty(model.LRSxml) ? model.LRSxml : (object)DBNull.Value);
                        command.Parameters.AddWithValue("@xmlSchemeId", !String.IsNullOrEmpty(model.LRSxml) ? (int)SchemeName.LRSProjects : (object)DBNull.Value);
                    }
                }

                this.ExecuteNonQuery(command);
            }
        }
 public void UpdateSegmentSummary(SegmentModel model)
 {
     using (SqlCommand command = new SqlCommand("[RTP].[UpdateSegmentSummary]") { CommandType = CommandType.StoredProcedure })
     {
         command.Parameters.AddWithValue("@SegmentId", model.SegmentId);
         command.Parameters.AddWithValue("@FacilityName", model.FacilityName != null ? (object)model.FacilityName.ToString() : (object)DBNull.Value);
         command.Parameters.AddWithValue("@StartAt", model.StartAt != null ? (object)model.StartAt.ToString() : (object)DBNull.Value);
         command.Parameters.AddWithValue("@EndAt", model.EndAt != null ? (object)model.EndAt.ToString() : (object)DBNull.Value);
         command.Parameters.AddWithValue("@NetworkId", model.NetworkId > 0 ? model.NetworkId : (object)DBNull.Value);
         command.Parameters.AddWithValue("@OpenYear", model.OpenYear > 0 ? model.OpenYear : (object)DBNull.Value);
         this.ExecuteNonQuery(command);
     }
 }
        public void UpdateSegment(SegmentModel model)
        {
            using (SqlCommand command = new SqlCommand("[RTP].[UpdateSegment]") { CommandType = CommandType.StoredProcedure })
            {
                command.Parameters.AddWithValue("@SegmentId", model.SegmentId);
                command.Parameters.AddWithValue("@FacilityName", model.FacilityName != null ? (object)model.FacilityName.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@StartAt", model.StartAt != null ? (object)model.StartAt.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@EndAt", model.EndAt != null ? (object)model.EndAt.ToString() : (object)DBNull.Value);
                command.Parameters.AddWithValue("@NetworkId", model.NetworkId > 0 ? model.NetworkId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@ImprovementTypeId", model.ImprovementTypeId > 0 ? model.ImprovementTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@PlanFacilityTypeId", model.PlanFacilityTypeId > 0 ? model.PlanFacilityTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@ModelingFacilityTypeId", model.ModelingFacilityTypeId > 0 ? model.ModelingFacilityTypeId : (object)DBNull.Value);
                command.Parameters.AddWithValue("@OpenYear", model.OpenYear > 0 ? model.OpenYear : (object)DBNull.Value);
                command.Parameters.AddWithValue("@LanesBase", model.LanesBase > 0 ? model.LanesBase : (object)DBNull.Value);
                command.Parameters.AddWithValue("@LanesFuture", model.LanesFuture > 0 ? model.LanesFuture : (object)DBNull.Value);
                command.Parameters.AddWithValue("@SpacesFuture", model.SpacesFuture > 0 ? model.SpacesFuture : (object)DBNull.Value);
                command.Parameters.AddWithValue("@AssignmentStatusId", model.AssignmentStatusID > 0 ? model.AssignmentStatusID : (object)DBNull.Value);

                if (!String.IsNullOrEmpty(model.LRSxml))
                {
                    byte[] encodedString = System.Text.Encoding.UTF8.GetBytes(model.LRSxml);
                    MemoryStream ms = new MemoryStream(encodedString);
                    ms.Flush();
                    ms.Position = 0;

                    XmlDocument xml = new XmlDocument();
                    xml.Load(ms);

                    using (XmlNodeReader xnr = new XmlNodeReader(xml))
                    {
                        command.Parameters.Add("@xml", SqlDbType.Xml).Value = new SqlXml(xnr);
                        //command.Parameters.AddWithValue("@xml", !String.IsNullOrEmpty(model.LRSxml) ? model.LRSxml : (object)DBNull.Value);
                        command.Parameters.AddWithValue("@xmlSchemeId", !String.IsNullOrEmpty(model.LRSxml) ? (int)SchemeName.LRSProjects : (object)DBNull.Value);
                    }
                }

                //command.Parameters.AddWithValue("@RouteName", !String.IsNullOrEmpty(model.LRS.RouteName) ? model.LRS.RouteName : (object)DBNull.Value);
                //command.Parameters.AddWithValue("@BeginMeasure", model.LRS.BeginMeasure > 0 ? model.LRS.BeginMeasure : 0);
                //command.Parameters.AddWithValue("@EndMeasure", model.LRS.EndMeasure > 0 ? model.LRS.EndMeasure : 0);
                //command.Parameters.AddWithValue("@Comments", !String.IsNullOrEmpty(model.LRS.Comments) ? model.LRS.Comments : (object)DBNull.Value);
                //command.Parameters.AddWithValue("@Offset", model.LRS.Offset > 0 ? model.LRS.Offset : (object)DBNull.Value);
                this.ExecuteNonQuery(command);
            }
        }
 public SegmentModel GetSegmentDetails(int segmentId)
 {
     DataTable data;
     using (SqlCommand command = new SqlCommand("[RTP].[GetSegment]") { CommandType = CommandType.StoredProcedure })
     {
         command.Parameters.AddWithValue("@SegmentId", segmentId);
         data = this.ExecuteDataTable(command);
     }
     SegmentModel model;
     DataRow row = data.Rows[0];
     model = new SegmentModel()
     {
         SegmentId = segmentId
         ,
         FacilityName = row["FacilityName"].ToString()
         ,
         StartAt = row["StartAt"].ToString()
         ,
         EndAt = row["EndAt"].ToString()
         ,
         NetworkId = row["NetworkId"].ToString().SmartParse<int>()
         ,
         ImprovementTypeId = row["ImprovementTypeId"].ToString().SmartParse<int>()
         ,
         PlanFacilityTypeId = row["PlanFacilityTypeId"].ToString().SmartParse<int>()
         ,
         ModelingFacilityTypeId = row["ModelingFacilityTypeID"].ToString().SmartParse<int>()
         ,
         OpenYear = row["OpenYear"].ToString().SmartParse<short>()
         ,
         LanesBase = row["LanesBase"].ToString().SmartParse<short>()
         ,
         LanesFuture = row["LanesFuture"].ToString().SmartParse<short>()
         ,
         SpacesFuture = row["SpacesFuture"].ToString().SmartParse<short>()
         ,
         AssignmentStatusID = row["AssignmentStatusID"].ToString().SmartParse<int?>()
         //,
         //LRS = new LRS()
         //{
         //    RouteName = row["Routename"].ToString()
         //    ,
         //    BeginMeasure = row["BeginMeasure"].ToString().SmartParse<double>()
         //    ,
         //    EndMeasure = row["EndMeasure"].ToString().SmartParse<double>()
         //    ,
         //    Comments = row["Comments"].ToString()
         //    ,
         //    Offset = row["Offset"].ToString().SmartParse<int>()
         //}
     };
     return model;
 }