public static bool UpdatedDataStoreDefinition(
     DataStoreDefinition dataStoreDefinition, 
     Dictionary<string /*TemplateName*/, Guid /*TemplateID*/> templateIDLookup,
     Dictionary<Guid /*TemplateID*/, Dictionary<string /*TermName*/, Guid /*TermID*/>> termIDLookup,
     Dictionary<Guid /*TemplateID*/, Dictionary<Guid /*ComplexListID*/, Dictionary<string /*FieldName*/, Guid /*FieldID*/>>> fieldIDLookup)
 {
     return Data.DataStoreDefinitions.UpdateDataStoreDefinition(dataStoreDefinition.DataStoreDefinitionID, GetDataStoreDefinitionXML(dataStoreDefinition.DataStoreConfig, dataStoreDefinition.Name, dataStoreDefinition.Description, templateIDLookup, termIDLookup, fieldIDLookup), dataStoreDefinition.Description, dataStoreDefinition.Active);
 }
 public static bool UpdateDataStoreDefinitionOnRun(DataStoreDefinition dataStoreDefinition)
 {
     return Data.DataStoreDefinitions.UpdateDataStoreDefinitionOnRun(dataStoreDefinition.DataStoreDefinitionID, dataStoreDefinition.DefinitionFilePath, dataStoreDefinition.LastRunDate);
 }
 public static DataStoreDefinition GetDatatStoreDefinitionBySystemId(Guid SystemID)
 {
     ITATSystem system = ITATSystem.Get(SystemID);
     DataStoreDefinition rtn = null;
     DataTable dataStoreDef = Data.DataStoreDefinitions.GetDataStoreDefinitionBySystemID(SystemID);
     if (dataStoreDef.Rows.Count > 0)
     {
         try
         {
             rtn = new DataStoreDefinition((Guid)dataStoreDef.Rows[0][StoreNames._C_DEFINITION_ID], system);
             rtn.Name = dataStoreDef.Rows[0][StoreNames._C_DEFINITION_NAME].ToString();
             rtn.Description = dataStoreDef.Rows[0][StoreNames._C_DESCRIPTION].ToString();
             rtn.SystemID = (Guid)dataStoreDef.Rows[0][StoreNames._C_SYSTEM_ID];
             rtn.LastRunDate = dataStoreDef.Rows[0][StoreNames._C_LASTRUNDATE].ToString().Length > 0 ? DateTime.Parse(dataStoreDef.Rows[0][Data.DataNames._C_LastRunDate].ToString()) : Convert.ToDateTime(null);
             rtn.Definition = dataStoreDef.Rows[0][StoreNames._C_DEFINITION].ToString();
             rtn.Active = Convert.ToBoolean(dataStoreDef.Rows[0][StoreNames._C_ACTIVE].ToString());
         }
         catch (Exception ex)
         {
             throw ex;
         }
     }
     return rtn;
 }
        public static Guid AddDataStoreDefinition(DataStoreDefinition dataStoreDefinition, 
            Dictionary<string /*TemplateName*/, Guid /*TemplateID*/> templateIDLookup,
            Dictionary<Guid /*TemplateID*/, Dictionary<string /*TermName*/, Guid /*TermID*/>> termIDLookup,
            Dictionary<Guid /*TemplateID*/, Dictionary<Guid /*ComplexListID*/, Dictionary<string /*FieldName*/, Guid /*FieldID*/>>> fieldIDLookup)
        {
            try
            {
                return Data.DataStoreDefinitions.InsertDataStoreDefinition(dataStoreDefinition.Name, dataStoreDefinition.Description,
                                                                            dataStoreDefinition.SystemID,
                                                                            GetDataStoreDefinitionXML(dataStoreDefinition.DataStoreConfig, dataStoreDefinition.Name, dataStoreDefinition.Description, templateIDLookup, termIDLookup, fieldIDLookup), dataStoreDefinition.Active);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public static DataStoreDefinition GetDataStoreDefinitionByID(
            Guid dataStoreDefinitionID, 
            ITATSystem system, 
            Dictionary<Guid, Dictionary<Guid, string>> termNameLookup, 
            Dictionary<Guid /*TemplateID*/, Dictionary<Guid /*ComplexListID*/, Dictionary<Guid /*FieldID*/, string /*FieldName*/>>> fieldNameLookup,
            bool testTermName)
        {
            DataStoreDefinition rtn = null;
            DataTable dataStoreDef = Data.DataStoreDefinitions.GetDataStoreDefinitionsByDefinitionID(dataStoreDefinitionID);
            if (dataStoreDef.Rows.Count > 0)
            {
                try 
                {
                    rtn = new DataStoreDefinition((Guid)dataStoreDef.Rows[0][StoreNames._C_DEFINITION_ID], system);
                    rtn.Name = dataStoreDef.Rows[0][StoreNames._C_DEFINITION_NAME].ToString();
                    if (dataStoreDef.Rows[0][StoreNames._C_DESCRIPTION] == DBNull.Value)
                        rtn.Description = string.Empty;
                    else
                        rtn.Description = dataStoreDef.Rows[0][StoreNames._C_DESCRIPTION].ToString();
                    rtn.SystemID = (Guid)dataStoreDef.Rows[0][StoreNames._C_SYSTEM_ID];
                    if (dataStoreDef.Rows[0][StoreNames._C_LASTRUNDATE] == DBNull.Value)
                        rtn.LastRunDate = null;
                    else
                        rtn.LastRunDate = DateTime.Parse(dataStoreDef.Rows[0][StoreNames._C_LASTRUNDATE].ToString());
                    rtn.Definition = dataStoreDef.Rows[0][StoreNames._C_DEFINITION].ToString();
                    rtn.Active = Convert.ToBoolean(dataStoreDef.Rows[0][StoreNames._C_ACTIVE].ToString());
                    rtn.DataStoreConfig = new DataStoreConfig(dataStoreDef.Rows[0][StoreNames._C_DEFINITION].ToString(), rtn.SystemID, termNameLookup, fieldNameLookup,testTermName);
                    
                    if (dataStoreDef.Rows[0][StoreNames._C_DEFINITIONFILEPATH] == DBNull.Value)
                        rtn.DefinitionFilePath = string.Empty;
                    else
                        rtn.DefinitionFilePath = dataStoreDef.Rows[0][StoreNames._C_DEFINITIONFILEPATH].ToString();
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }


            return rtn;

        }
        protected void btnPromote_Click(object sender, EventArgs e)
        {
            UpdateTermSize();

            Page.Validate(VALIDATION_GROUP);

            string emailvalidationerror;

            bool aliasvalidation = CheckSelectedTermsHasAlias();

            bool emailvalidation = CheckErrorLogEmailList(out emailvalidationerror);

            string dupAliasError;
            bool duplicateAliasValidation = CheckAlias(out dupAliasError);

            Guid dataStoreDefinitionID = new Guid(Request.QueryString[Common.Names._QS_DEFINITION_ID].ToString());
            Dictionary<string /*TemplateName*/, Guid /*TemplateID*/> templateIDLookup = DataStoreConfig.GetActiveSearchOnlyTemplateListByName(_itatSystem.ID);

            Dictionary<Guid /*TemplateID*/, Dictionary<string /*TermName*/, Guid /*TermID*/>> termIDLookup = null;
            Dictionary<Guid /*TemplateID*/, Dictionary<Guid /*ComplexListID*/, Dictionary<string /*FieldName*/, Guid /*FieldID*/>>> fieldIDLookup = null;
            SystemStore.GetTermAndFieldIDLookup(_itatSystem.ID, ref termIDLookup, ref fieldIDLookup);

            bool pathValidation = CheckPath(txtPath.Text);
            if (Page.IsValid && aliasvalidation && emailvalidation && duplicateAliasValidation && pathValidation)
            {
                DataStoreDefinition dataStoreDef = new DataStoreDefinition(_itatSystem);
                dataStoreDef.SystemID = _itatSystem.ID;
                dataStoreDef.DataStoreDefinitionID = dataStoreDefinitionID;
                dataStoreDef.Active = radActiveYes.Checked ? true : false;
                dataStoreDef.Description = txtDescription.Text;
                dataStoreDef.DataStoreConfig.DefaultDateFormat = ddlDefaultDateFormat.SelectedValue.ToString();
                dataStoreDef.DataStoreConfig.LoadType = radTypeDelta.Checked ? LoadType.Delta : LoadType.Full;

                int deltadays;
                int.TryParse(txtDeltaDays.Text, out deltadays);

                dataStoreDef.DataStoreConfig.DeltaDays = deltadays;

                dataStoreDef.DataStoreConfig.SearchCriteria = GetSearchCriteria("MySearchControl");
                dataStoreDef.DataStoreConfig.PrimaryFacility = Extension.Concatenate<int>(dataStoreDef.DataStoreConfig.SearchCriteria.FacilityIds, PRIMARY_FACILITY_DELIMITER);
                dataStoreDef.DataStoreConfig.Path = txtPath.Text;

                foreach (ListItem li in chklstStatus.Items)
                {
                    if (li.Selected)
                    {
                        dataStoreDef.DataStoreConfig.SearchCriteria.Statuses.Add(li.Text);
                    }
                }

                foreach (ListItem li in lstSelectedTemplates.Items)
                {
                    dataStoreDef.DataStoreConfig.Templates.Add(new Guid(li.Value), li.Text);
                }

                foreach (DataStoreField dsf in SelectedTerms)
                {
                    dataStoreDef.DataStoreConfig.Terms.Add(dsf);
                }

                dataStoreDef.DataStoreConfig.ErrorLogRecepientEmail = txtEmail.Text;
                try
                {
                    DataStoreDefinition.UpdatedDataStoreDefinition(dataStoreDef, templateIDLookup, termIDLookup, fieldIDLookup);
                    Response.Redirect(string.Format("~/Admin/DataStoreDefinitionList.aspx?System={0}", dataStoreDef.SystemID));
                }
                catch (Exception ex)
                {
                    RegisterAlert(string.Format("UpdatedDataStoreDefinition call failed: {0}", ex.Message));
                }
            }
            else
            {
                string errorMsg = "";
                foreach (IValidator iv in Page.Validators)
                {
                    if (!iv.IsValid)
                    {
                        errorMsg = errorMsg + iv.ErrorMessage + "\\n";
                    }
                }

                if (!emailvalidation)
                {
                    errorMsg = errorMsg + emailvalidationerror + "\\n";
                }
                if (!pathValidation)
                {
                    errorMsg = errorMsg + "Path you have specified is not a valid Path." + "\\n";
                }
                if (!duplicateAliasValidation)
                {
                    errorMsg = errorMsg + dupAliasError;
                }
                if (errorMsg.Length > 0)
                    RegisterAlert(errorMsg);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString[Common.Names._QS_DEFINITION_ID].ToString() != string.Empty)
                {
                    try
                    {
                        Dictionary<Guid, Dictionary<Guid, string>> termNameLookup = null;
                        Dictionary<Guid /*TemplateID*/, Dictionary<Guid /*ComplexListID*/, Dictionary<Guid /*FieldID*/, string /*FieldName*/>>> fieldNameLookup = null;
                        SystemStore.GetTermAndFieldNameLookup(_itatSystem.ID, ref termNameLookup, ref fieldNameLookup);
                        _dataStoreDefinition = Business.DataStoreDefinition.GetDataStoreDefinitionByID(new Guid(Request.QueryString[Common.Names._QS_DEFINITION_ID].ToString()), _itatSystem, termNameLookup, fieldNameLookup, false);
                    }
                    catch (Exception ex)
                    {
                        RegisterAlert(string.Format("Error: {0}",ex.Message));
                    }
                }

                if (_dataStoreDefinition == null)
                {
                    RegisterAlert("Data Store Definition does not exists");
                }
                else
                {
                    DisplayTerm1 = true;
                    DisplayTerm2 = true;
                    DisplayTerm3 = false;
                    DisplayTerm4 = true;
                    DisplayTerm5 = true;
                    DisplayTerm6 = false;
                    DisplayTerm7 = false;
                    searchControl = LoadControl("~\\Controls\\SearchControl.ascx", base._itatSystem, DisplayTerm1, DisplayTerm2, DisplayTerm3, DisplayTerm4, DisplayTerm5, DisplayTerm6,DisplayTerm7, _dataStoreDefinition.DataStoreConfig.SearchCriteria);
                    searchControl.ID = "MySearchControl";

                    apnlCriteria.Controls.Add(searchControl);
                    _itatsystem = Business.ITATSystem.Get(_dataStoreDefinition.SystemID);

                    LoadStatusList();

                    lblSystemName.Text = Business.ITATSystem.Get(_dataStoreDefinition.SystemID).Name;
                    lblDefinitionName.Text = _dataStoreDefinition.Name;
                    txtDescription.Text = _dataStoreDefinition.Description;

                    ddlDefaultDateFormat.DataSource = DataStoreDefinition.GetDateFormatList();
                    ddlDefaultDateFormat.DataBind();

                    ddlDefaultDateFormat.SelectedIndex = ddlDefaultDateFormat.Items.IndexOf(new ListItem(_dataStoreDefinition.DataStoreConfig.DefaultDateFormat));

                    LoadTemplateList(_dataStoreDefinition.SystemID);
                    string errSelectedTemplates = "";
                    foreach (KeyValuePair<Guid, string> template in _dataStoreDefinition.DataStoreConfig.Templates)
                    {

                        if (!lstTemplates.Items.Contains(new ListItem(template.Value, template.Key.ToString())))
                            errSelectedTemplates = errSelectedTemplates + string.Format("- {0} \\n", template.Value.ToString());
                        else
                        {
                            lstTemplates.Items.Remove(new ListItem(template.Value, template.Key.ToString()));
                            lstSelectedTemplates.Items.Add(new ListItem(template.Value, template.Key.ToString()));
                        }
                    }

                    if (errSelectedTemplates.Length > 0)
                        RegisterAlert("Following selected Template(s) are no longer Active \\n" + errSelectedTemplates);

                    SelectedTerms = _dataStoreDefinition.DataStoreConfig.Terms;

                    LoadTermList();
                    SortlistItems(lstTemplates);
                    MoveTerm(SelectedTerms);


                    if (_dataStoreDefinition.Active)
                        radActiveYes.Checked = true;
                    else
                        radActiveNo.Checked = true;

                    foreach (ListItem li in chklstStatus.Items)
                    {
                        if (_dataStoreDefinition.DataStoreConfig.SearchCriteria.Statuses.IndexOf(li.Text) > -1)
                            li.Selected = true;
                        else
                            li.Selected = false;
                    }

                    foreach (string status in _dataStoreDefinition.DataStoreConfig.SearchCriteria.Statuses)
                    {
                        chklstStatus.Items.FindByText(status).Selected = true;
                    }
                    radTypeDelta.Checked = _dataStoreDefinition.DataStoreConfig.LoadType == LoadType.Delta;
                    txtDeltaDays.Text = _dataStoreDefinition.DataStoreConfig.DeltaDays.ToString();
                    txtPath.Text = _dataStoreDefinition.DataStoreConfig.Path;
                    txtEmail.Text = _dataStoreDefinition.DataStoreConfig.ErrorLogRecepientEmail;
                }

                btnRemoveTerm.OnClientClick = string.Format("return confirm('All the selected Terms will be removed from the list.');");

                btnRemoveTemplate.OnClientClick = string.Format("return confirm('All the selected Terms from the selected Templates also removed by removing Templates.');");
            }
        }