// ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// LoadAllByProjectId
 /// </summary>
 /// <param name="projectId">projectId</param>              
 public void LoadAllByProjectId(int projectId)
 {
     ProjectNavigatorProjectServiceGateway projectNavigatorProjectServiceGateway = new ProjectNavigatorProjectServiceGateway(Data);
     projectNavigatorProjectServiceGateway.LoadAllByProjectId(projectId);
 }
        /// <summary>
        /// Save all Service to database (direct)
        /// </summary>
        /// <param name="companyId">companyId</param>
        public void Save(int companyId)
        {
            ProjectNavigatorTDS serviceChanges = (ProjectNavigatorTDS)Data.GetChanges();

            if (serviceChanges != null)
            {
                if (serviceChanges.ProjectService.Rows.Count > 0)
                {
                    ProjectNavigatorProjectServiceGateway projectNavigatorProjectServiceGateway = new ProjectNavigatorProjectServiceGateway(serviceChanges);

                    foreach (ProjectNavigatorTDS.ProjectServiceRow row in (ProjectNavigatorTDS.ProjectServiceDataTable)serviceChanges.ProjectService)
                    {
                        // Insert new Service
                        if ((!row.Deleted) && (!row.InDatabase))
                        {
                            int? serviceId = null; if (!row.IsServiceIDNull()) serviceId = row.ServiceID;
                            string description = ""; if (!row.IsDescriptionNull()) description = row.Description;
                            string averageSize = ""; if (!row.IsAverageSizeNull()) averageSize = row.AverageSize;
                            decimal? averagePrice = null; if (!row.IsAveragePriceNull()) averagePrice = row.AveragePrice;

                            ProjectService projectService = new ProjectService(null);
                            projectService.InsertDirect(row.ProjectID, row.RefID, serviceId, description, averageSize, averagePrice, row.Quantity, row.Deleted, row.COMPANY_ID);
                        }

                        // Update Service
                        if ((!row.Deleted) && (row.InDatabase))
                        {
                            int projectId = row.ProjectID;
                            int refId = row.RefID;
                            bool originalDeleted = false;
                            int originalCompanyId = companyId;

                            // original values
                            int? originalServiceId = projectNavigatorProjectServiceGateway.GetServiceIDOriginal(projectId, refId);
                            string originalDescription = projectNavigatorProjectServiceGateway.GetDescriptionOriginal(projectId, refId);
                            string originalAverageSize = projectNavigatorProjectServiceGateway.GetAverageSizeOriginal(projectId, refId);
                            decimal? originalAveragePrice = projectNavigatorProjectServiceGateway.GetAveragePriceOriginal(projectId, refId);
                            int originalQuantity = projectNavigatorProjectServiceGateway.GetQuantityOriginal(projectId, refId);

                            // new values
                            int? newServiceId = projectNavigatorProjectServiceGateway.GetServiceID(projectId, refId);
                            string newDescription = projectNavigatorProjectServiceGateway.GetDescription(projectId, refId);
                            string newAverageSize = projectNavigatorProjectServiceGateway.GetAverageSize(projectId, refId);
                            decimal? newAveragePrice = projectNavigatorProjectServiceGateway.GetAveragePrice(projectId, refId);
                            int newQuantity = projectNavigatorProjectServiceGateway.GetQuantity(projectId, refId);

                            ProjectService projectService = new ProjectService(null);
                            projectService.UpdateDirect(projectId, refId,  originalServiceId, originalDescription, originalAverageSize, originalAveragePrice, originalQuantity, originalDeleted, originalCompanyId, projectId, refId, newServiceId, newDescription, newAverageSize, newAveragePrice, newQuantity, originalDeleted, originalCompanyId);
                        }

                        // Deleted Service
                        if ((row.Deleted) && (row.InDatabase))
                        {
                            ProjectService projectService = new ProjectService(null);
                            projectService.DeleteDirect(row.ProjectID, row.RefID, row.COMPANY_ID);
                        }
                    }
                }
            }
        }
        protected void grdServices_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Edit items
            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Edit) || (e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate))))
            {
                // Initialize values
                int projectId = Int32.Parse(((Label)e.Row.FindControl("lblProjectId")).Text);
                int refId = Int32.Parse(((Label)e.Row.FindControl("lblRefId")).Text);

                // For Services
                ProjectNavigatorProjectServiceGateway projectNavigatorProjectServiceGateway = new ProjectNavigatorProjectServiceGateway(projectNavigatorTDS);

                if (projectNavigatorProjectServiceGateway.GetServiceID(projectId, refId).HasValue)
                {
                    int? serviceId = projectNavigatorProjectServiceGateway.GetServiceID(projectId, refId);
                    if (serviceId.HasValue)
                    {
                        ((DropDownList)e.Row.FindControl("ddlServiceEdit")).SelectedValue = ((int)serviceId).ToString();
                    }
                }
                else
                {
                    ((DropDownList)e.Row.FindControl("ddlServiceEdit")).SelectedValue = "(Other)";
                }
            }

            if ((e.Row.RowType == DataControlRowType.DataRow) && ((e.Row.RowState == DataControlRowState.Normal) || (e.Row.RowState == (DataControlRowState.Normal | DataControlRowState.Alternate))))
            {
                // Initialize values
                int projectId = Int32.Parse(((Label)e.Row.FindControl("lblProjectId")).Text);
                if (projectId >= 0)
                {
                    int refId = Int32.Parse(((Label)e.Row.FindControl("lblRefId")).Text);

                    // For Services
                    ProjectNavigatorProjectServiceGateway projectNavigatorProjectServiceGateway = new ProjectNavigatorProjectServiceGateway(projectNavigatorTDS);
                    int? serviceId = projectNavigatorProjectServiceGateway.GetServiceID(projectId, refId);

                    string serviceName = "(Other)";
                    if ((serviceId.HasValue) && ((int)serviceId != -1))
                    {
                        // ... Get name
                        ServiceGateway serviceGateway = new ServiceGateway();
                        serviceGateway.LoadByServiceId((int)serviceId, Int32.Parse(hdfCompanyId.Value));
                        serviceName = serviceGateway.GetName((int)serviceId);
                    }

                    // ... Set name
                    ((Label)e.Row.FindControl("lblService")).Text = serviceName;
                }
            }
        }