/// <summary>
 /// InsertDirect
 /// </summary>
 /// <param name="viewId"></param>
 /// <param name="companyId"></param>
 /// <param name="workType"></param>
 /// <param name="conditionId"></param>
 /// <param name="refId"></param>
 /// <param name="operator_"></param>
 /// <param name="conditionNumber"></param>
 /// <param name="value"></param>
 /// <param name="deleted"></param>
 public void InsertDirect(int viewId, int companyId, string workType, int conditionId, int refId, string operator_, int conditionNumber, string value, bool deleted)
 {
     WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway(null);
     workViewConditionGateway.Insert(viewId, companyId, workType, conditionId, refId, operator_, conditionNumber, value, deleted);
 }
        /// <summary>
        /// SaveForEdit
        /// </summary>
        /// <param name="viewId"></param>
        /// <param name="companyId"></param>
        /// <param name="workType"></param>
        public void SaveForEdit(int viewId, int companyId, string workType)
        {
            foreach (WorkViewTDS.WorkViewConditionNewRow rowNew in (WorkViewTDS.WorkViewConditionNewDataTable)Data.Tables["WorkViewConditionNew"])
            {
                WorkViewCondition workViewCondition = new WorkViewCondition(null);

                if (!rowNew.Deleted && !rowNew.InDatabase)
                {
                    WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway(null);

                    int refId = workViewConditionGateway.GetLastRefIdByViewIdWorkTypeConditionId(viewId, companyId, workType, rowNew.ConditionID);
                    refId = refId + 1;

                    workViewCondition.InsertDirect(viewId, companyId, workType, rowNew.ConditionID, refId, rowNew.Operator, rowNew.ConditionNumber, rowNew.Value_, rowNew.Deleted);
                }

                if (!rowNew.Deleted && rowNew.InDatabase)
                {
                    WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway();
                    workViewConditionGateway.LoadAllByViewIdWorkTypeConditionIdRefId(viewId, companyId, workType, rowNew.ConditionID, rowNew.RefID);

                    int originalViewId = viewId;
                    int originalCompanyId = companyId;
                    string originalWorkType = workType;
                    int originalConditionId = rowNew.ConditionID;
                    int originalRefId = rowNew.RefID; //workViewConditionGateway.GetRefId(viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID);
                    string originalOperator_ = workViewConditionGateway.GetOperator(viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID);
                    int originalConditionNumber = workViewConditionGateway.GetConditionNumber(viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID);
                    string originalValue_ = workViewConditionGateway.GetValue_(viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID);
                    bool originalDeleted = workViewConditionGateway.GetDeleted(viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID);

                    workViewCondition.UpdateDirect(originalViewId, originalWorkType, originalCompanyId, originalConditionId, originalRefId, originalOperator_, originalConditionNumber, originalValue_, originalDeleted, viewId, workType, companyId, rowNew.ConditionID, rowNew.RefID, rowNew.Operator, rowNew.ConditionNumber, rowNew.Value_, rowNew.Deleted);

                }

                if (rowNew.Deleted && rowNew.InDatabase)
                {
                    workViewCondition.DeleteDirectForEditView(viewId, companyId, workType, rowNew.ConditionID, rowNew.RefID);
                }
            }
        }
 /// <summary>
 /// DeleteDirectForEditView
 /// </summary>
 /// <param name="viewId"></param>
 /// <param name="companyId"></param>
 /// <param name="workType"></param>
 /// <param name="conditionId"></param>
 /// <param name="refId"></param>
 public void DeleteDirectForEditView(int viewId, int companyId, string workType, int conditionId, int refId)
 {
     WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway(null);
     workViewConditionGateway.DeleteForEditView(viewId, companyId, workType, conditionId, refId);
 }
 /// <summary>
 /// DeleteDirect
 /// </summary>
 /// <param name="viewId">viewId</param>
 /// <param name="companyId">companyId</param>
 public void DeleteDirect(int viewId, int companyId)
 {
     WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway(null);
     workViewConditionGateway.Delete(viewId, companyId);
 }
 // ////////////////////////////////////////////////////////////////////////
 // PUBLIC METHODS
 //
 /// <summary>
 /// UpdateDirect
 /// </summary>
 /// <param name="originalViewId"></param>
 /// <param name="originalWorkType"></param>
 /// <param name="originalCompanyId"></param>
 /// <param name="originalConditionId"></param>
 /// <param name="originalRefId"></param>
 /// <param name="originalOperator"></param>
 /// <param name="originalConditionNumber"></param>
 /// <param name="originalValue"></param>
 /// <param name="originalDeleted"></param>
 /// <param name="newViewId"></param>
 /// <param name="newWorkType"></param>
 /// <param name="newCompanyId"></param>
 /// <param name="newConditionId"></param>
 /// <param name="newRefId"></param>
 /// <param name="newOperator"></param>
 /// <param name="newConditionNumber"></param>
 /// <param name="newValue_"></param>
 /// <param name="newDeleted"></param>
 public void UpdateDirect(int originalViewId, string originalWorkType, int originalCompanyId, int originalConditionId, int originalRefId, string originalOperator, int originalConditionNumber, string originalValue, bool originalDeleted, int newViewId, string newWorkType, int newCompanyId, int newConditionId, int newRefId, string newOperator, int newConditionNumber, string newValue_, bool newDeleted)
 {
     WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway(null);
     workViewConditionGateway.Update(originalViewId, originalCompanyId, originalWorkType, originalConditionId, originalRefId, originalOperator, originalConditionNumber, originalValue, originalDeleted, newViewId, newCompanyId, newWorkType, newConditionId, newRefId, newOperator, newConditionNumber, newValue_, newDeleted);
 }
        protected void grdConditions_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("ConditionsUpdate");
            if (Page.IsValid)
            {
                int id = (int)e.Keys["ID"];

                int conditionId = int.Parse(((DropDownList)grdConditions.Rows[e.RowIndex].Cells[3].FindControl("ddlName")).SelectedValue);
                string name = ((DropDownList)grdConditions.Rows[e.RowIndex].Cells[3].FindControl("ddlName")).SelectedItem.Text;
                DropDownList ddlOperator = ((DropDownList)grdConditions.Rows[grdConditions.EditIndex].FindControl("ddlOperator"));
                string operator_ = ddlOperator.SelectedItem.Text;
                string sign = ddlOperator.SelectedValue;
                int conditionNumber = int.Parse(((DropDownList)grdConditions.Rows[e.RowIndex].Cells[2].FindControl("ddlConditionNumber")).SelectedValue);

                WorkTypeViewConditionGateway workTypeViewConditionGateway = new WorkTypeViewConditionGateway();
                workTypeViewConditionGateway.LoadByWorkTypeConditionId(hdfWorkType.Value, conditionId, int.Parse(hdfCompanyId.Value));
                string type = workTypeViewConditionGateway.GetType(hdfWorkType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                string value = "";

                if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal") || (type == "Distance"))
                {
                    value = ((TextBox)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("tbxValue")).Text;
                }

                if ((type == "FixedItems") || (type == "DynamicItems"))
                {
                    value = ((DropDownList)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("ddlValue")).SelectedItem.Text;
                }

                if (type == "Boolean")
                {
                    if (((RadioButton)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("rbtnYes")).Checked)
                        value = "Yes";
                    if (((RadioButton)grdConditions.Rows[e.RowIndex].Cells[5].FindControl("rbtnNo")).Checked)
                        value = "No";
                }

                WorkViewTDS dataSet = new WorkViewTDS();
                dataSet.WorkViewConditionNew.Merge(workViewConditionNew, true);
                WorkViewConditionNew model = new WorkViewConditionNew(dataSet);

                WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway();
                int refId = workViewConditionGateway.GetLastRefIdByViewIdWorkTypeConditionId(int.Parse(hdfViewId.Value), int.Parse(hdfCompanyId.Value), hdfWorkType.Value, conditionId);
                refId = refId + 1;

                model.UpdateForEdit(refId, id, conditionId, name, operator_, sign, conditionNumber, value, false, false);
                Session["workViewConditionNew"] = dataSet.WorkViewConditionNew;
                workViewConditionNew = dataSet.WorkViewConditionNew;
            }
            else
            {
                e.Cancel = true;
            }
        }
        private void GrdConditionsAdd()
        {
            if (ValidateFooterAdd())
            {
                Page.Validate("Conditions");
                if (Page.IsValid)
                {
                    int conditionId = int.Parse(((DropDownList)grdConditions.FooterRow.FindControl("ddlName")).SelectedValue);
                    string name = ((DropDownList)grdConditions.FooterRow.FindControl("ddlName")).SelectedItem.Text;
                    string operator_ = ((DropDownList)grdConditions.FooterRow.FindControl("ddlOperator")).SelectedItem.Text;
                    string sign = ((DropDownList)grdConditions.FooterRow.FindControl("ddlOperator")).SelectedValue;
                    int conditionNumber = int.Parse(((DropDownList)grdConditions.FooterRow.FindControl("ddlConditionNumber")).SelectedValue);

                    WorkTypeViewConditionGateway workTypeViewConditionGateway = new WorkTypeViewConditionGateway();
                    workTypeViewConditionGateway.LoadByWorkTypeConditionId(hdfWorkType.Value, conditionId, int.Parse(hdfCompanyId.Value));
                    string type = workTypeViewConditionGateway.GetType(hdfWorkType.Value, int.Parse(hdfCompanyId.Value), conditionId);
                    string value = "";

                    if ((type == "String") || (type == "Date") || (type == "Int") || (type == "Decimal") || (type == "Distance"))
                    {
                        value = ((TextBox)grdConditions.FooterRow.FindControl("tbxValue")).Text;
                    }

                    if ((type == "FixedItems") || (type == "DynamicItems"))
                    {
                        value = ((DropDownList)grdConditions.FooterRow.FindControl("ddlValue")).SelectedItem.Text;
                    }

                    if (type == "Boolean")
                    {
                        if (((RadioButton)grdConditions.FooterRow.FindControl("rbtnYes")).Checked)
                            value = "Yes";
                        if (((RadioButton)grdConditions.FooterRow.FindControl("rbtnNo")).Checked)
                            value = "No";
                    }

                    WorkViewTDS dataSet = new WorkViewTDS();
                    dataSet.WorkViewConditionNew.Merge(workViewConditionNew, true);
                    WorkViewConditionNew model = new WorkViewConditionNew(dataSet);

                    WorkViewConditionGateway workViewConditionGateway = new WorkViewConditionGateway();
                    int refId = workViewConditionGateway.GetLastRefIdByViewIdWorkTypeConditionId(int.Parse(hdfViewId.Value), int.Parse(hdfCompanyId.Value), hdfWorkType.Value, conditionId);
                    refId = refId + 1;

                    model.InsertForEdit(refId, conditionId, name, operator_, sign, conditionNumber, value, false, false);

                    Session.Remove("workViewConditionNewDummy");
                    workViewConditionNew = dataSet.WorkViewConditionNew;
                    Session["workViewConditionNew"] = dataSet.WorkViewConditionNew;

                    grdConditions.DataBind();
                    grdConditions.PageIndex = grdConditions.PageCount - 1;
                }
            }
        }