/// <summary>
    /// Sets data to database.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Get code name
        string codeName = txtCodeName.Text.Trim();

        // Perform validation
        string errorMessage = new Validator().NotEmpty(codeName, rfvCodeName.ErrorMessage)
                              .NotEmpty(txtDisplayName.Text, rfvDisplayName.ErrorMessage).Result;

        // Check CodeName for identifier format
        if (!ValidationHelper.IsCodeName(codeName))
        {
            errorMessage = GetString("General.ErrorCodeNameInIdentifierFormat");
        }

        // Check length of code name
        if (codeName.Length > codeNameLength)
        {
            errorMessage = GetString("srch.codenameexceeded");
        }

        if (errorMessage == "")
        {
            // Create new
            SearchIndexInfo sii = SearchIndexInfoProvider.GetSearchIndexInfo(codeName);

            if (sii == null)
            {
                sii = new SearchIndexInfo();

                // Set the fields
                sii.IndexName         = codeName;
                sii.IndexDisplayName  = txtDisplayName.Text.Trim();
                sii.IndexAnalyzerType = SearchIndexInfoProvider.AnalyzerCodenameToEnum(drpAnalyzer.SelectedValue);
                // Community indexing is not yet supported
                //sii.IndexIsCommunityGroup = chkCommunity.Checked;
                sii.IndexIsCommunityGroup      = false;
                sii.IndexType                  = drpType.SelectedValue;
                sii.CustomAnalyzerAssemblyName = stopCustomControl.CustomAnalyzerAssemblyName;
                sii.CustomAnalyzerClassName    = stopCustomControl.CustomAnalyzerClassName;
                sii.StopWordsFile              = stopCustomControl.StopWordsFile;

                // Save the object
                SearchIndexInfoProvider.SetSearchIndexInfo(sii);
                ItemID = sii.IndexID;

                // Assign to current website
                if (chkAddIndexToCurrentSite.Checked)
                {
                    SearchIndexSiteInfoProvider.AddSearchIndexToSite(sii.IndexID, CMSContext.CurrentSiteID);
                }

                // Redirect to edit mode
                RaiseOnSaved();
            }
            else
            {
                // Error message - code name already exists
                ShowError(GetString("srch.index.codenameexists"));
            }
        }
        else
        {
            // Error message - validation
            ShowError(errorMessage);
        }
    }
Example #2
0
    /// <summary>
    /// Sets data to database.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Trim blank space and too long string
        string codeName = txtCodeName.Text.Trim();

        if (codeName.Length > 200)
        {
            codeName = codeName.Substring(0, 200);
        }

        // Get code name
        codeName         = ValidationHelper.GetCodeName(codeName, null, null);
        txtCodeName.Text = codeName;

        // Perform validation
        string errorMessage = new Validator().NotEmpty(codeName, rfvCodeName.ErrorMessage)
                              .NotEmpty(txtDisplayName.Text, rfvDisplayName.ErrorMessage).Result;

        // Check CodeName for identificator format
        if (!ValidationHelper.IsCodeName(codeName))
        {
            errorMessage = GetString("General.ErrorCodeNameInIdentificatorFormat");
        }

        // Check length of code name
        if (codeName.Length > codeNameLength)
        {
            errorMessage = GetString("srch.codenameexceeded");
        }

        if (errorMessage == "")
        {
            // Check code name
            SearchIndexInfo sii = SearchIndexInfoProvider.GetSearchIndexInfo(codeName);

            // Get current item
            SearchIndexInfo current = SearchIndexInfoProvider.GetSearchIndexInfo(this.ItemID);


            // Check if code name is unique
            if ((sii == null) || (sii == current))
            {
                // Get original index path
                string originalPath = current.CurrentIndexPath;

                // Set the fields
                current.IndexName = codeName;

                // Trim blank space and too long string
                txtDisplayName.Text = txtDisplayName.Text.Trim();
                if (txtDisplayName.Text.Length > 200)
                {
                    txtDisplayName.Text = txtDisplayName.Text.Substring(0, 200);
                }

                if (current.IndexDisplayName != txtDisplayName.Text)
                {
                    // Refresh a breadcrumb if used in the tabs layout
                    ScriptHelper.RefreshTabHeader(Page, string.Empty);
                }

                current.IndexDisplayName = txtDisplayName.Text;

                // Check if analyzer type is changed
                bool analyzerTypeChanged = false;
                if ((current != null) &&
                    ((current.IndexAnalyzerType != SearchIndexInfoProvider.AnalyzerCodenameToEnum(drpAnalyzer.SelectedValue)) ||
                     (String.Compare(current.StopWordsFile, stopCustomControl.StopWordsFile, true) != 0) ||
                     (String.Compare(current.CustomAnalyzerAssemblyName, stopCustomControl.CustomAnalyzerAssemblyName) != 0) ||
                     (String.Compare(current.CustomAnalyzerClassName, stopCustomControl.CustomAnalyzerClassName) != 0)))
                {
                    analyzerTypeChanged = true;
                }
                current.IndexAnalyzerType = SearchIndexInfoProvider.AnalyzerCodenameToEnum(drpAnalyzer.SelectedValue);

                // Community indexing is not yet supported
                //current.IndexIsCommunityGroup = chkCommunity.Checked;
                current.IndexIsCommunityGroup      = false;
                current.CustomAnalyzerAssemblyName = stopCustomControl.CustomAnalyzerAssemblyName;
                current.CustomAnalyzerClassName    = stopCustomControl.CustomAnalyzerClassName;
                current.StopWordsFile  = stopCustomControl.StopWordsFile;
                current.IndexBatchSize = ValidationHelper.GetInteger(txtBatchSize.Text, 10);

                // Save the object
                SearchIndexInfoProvider.SetSearchIndexInfo(current);

                // Codename changed
                bool codenameChanged = false;
                if (sii == null)
                {
                    try
                    {
                        DirectoryHelper.MoveDirectory(originalPath, current.CurrentIndexPath);
                    }
                    catch (Exception ex)
                    {
                        EventLogProvider ep = new EventLogProvider();
                        ep.LogEvent("SmartSearch", "Rename", ex);
                    }

                    codenameChanged = true;
                }

                if (codenameChanged || analyzerTypeChanged)
                {
                    lblInfo.Text = String.Format(GetString("general.changessaved") + " " + GetString("srch.indexrequiresrebuild"), "<a href=\"javascript:" + Page.ClientScript.GetPostBackEventReference(this, "saved") + "\">" + GetString("General.clickhere") + "</a>");
                }
                else
                {
                    lblInfo.Text = GetString("General.ChangesSaved");
                }

                // Redirect to edit mode
                RaiseOnSaved();
                lblInfo.Visible = true;
            }
            else
            {
                // Error message - code name already exists
                lblError.Visible = true;
                lblError.Text    = GetString("srch.index.codenameexists");
            }
        }
        else
        {
            // Error message - validation
            lblError.Visible = true;
            lblError.Text    = errorMessage;
        }
    }
    protected void Initialize()
    {
        string stopWordsScript = ScriptHelper.GetScript(@" function CheckAnalyzerOptions(){
        stopRow = document.getElementById('" + stopWordsRow.ClientID + @"');
        analyzerElem = document.getElementById('" + mDropDown.ClientID + @"');
        if ((analyzerElem.value == 'stop')||(analyzerElem.value == 'standard'))
        {
            stopRow.style.display = '';
        }
        else
        {
            stopRow.style.display = 'none';
        }

        customRow = document.getElementById('" + customAnalyzerAssemblyName.ClientID + @"');
        customRow2 = document.getElementById('" + customAnalyzerClassName.ClientID + @"');
        if (analyzerElem.value == 'custom')
        {
            customRow.style.display = '';
            customRow2.style.display = '';
        }
        else
        {
            customRow.style.display = 'none';
            customRow2.style.display = 'none';
        }
        }");

        mDropDown.Attributes.Add("onchange", "CheckAnalyzerOptions();");

        ScriptHelper.RegisterClientScriptBlock(this.Page, typeof(string), "StopWordsDropDown", stopWordsScript);

        if (!RequestHelper.IsPostBack())
        {
            string stopWordsDir = SearchIndexInfo.IndexPathPrefix + "_StopWords\\";
            if (Directory.Exists(stopWordsDir))
            {
                string[] files = Directory.GetFiles(stopWordsDir, "*.txt");

                if (files != null)
                {
                    foreach (string file in files)
                    {
                        string name  = file;
                        int    index = name.LastIndexOf('\\');
                        if (index > -1)
                        {
                            name = name.Substring(index + 1);
                        }

                        name = name.Substring(0, name.LastIndexOf('.'));

                        drpStopWords.Items.Add(new ListItem(name, name));
                    }
                }
            }

            drpStopWords.Items.Insert(0, new ListItem("(" + GetString("general.default") + ")", String.Empty));
        }

        bool hideCustom = true;
        bool hideStop   = true;

        AnalyzerTypeEnum type = SearchIndexInfoProvider.AnalyzerCodenameToEnum(mDropDown.SelectedValue);

        if ((!RequestHelper.IsPostBack()) && (mIndexInfo != null))
        {
            type = mIndexInfo.IndexAnalyzerType;
            txtCustomAnalyzerAssemblyName.Text = mIndexInfo.CustomAnalyzerAssemblyName;
            txtCustomAnalyzerClassName.Text    = mIndexInfo.CustomAnalyzerClassName;
            try
            {
                drpStopWords.SelectedValue = mIndexInfo.StopWordsFile;
            }
            catch
            {
            }
        }

        switch (type)
        {
        case AnalyzerTypeEnum.CustomAnalyzer:
            hideCustom = false;
            break;

        case AnalyzerTypeEnum.StandardAnalyzer:
        case AnalyzerTypeEnum.StopAnalyzer:
            hideStop = false;
            break;
        }

        stopWordsRow.Style.Clear();
        if (hideStop)
        {
            stopWordsRow.Style.Add("display", "none");
        }

        customAnalyzerAssemblyName.Style.Clear();
        customAnalyzerClassName.Style.Clear();

        if (hideCustom)
        {
            customAnalyzerAssemblyName.Style.Add("display", "none");
            customAnalyzerClassName.Style.Add("display", "none");
        }
    }