public static int Write( CidList list, string name, string description, string folderId, UserObjectAccess accessLevel) { UserObject UserObject = new UserObject(UserObjectType.CnList); UserObject.Name = name; UserObject.Description = description; UserObject.ParentFolder = folderId; UserObject.ParentFolderType = (folderId.ToUpper().StartsWith("FOLDER_")) ? FolderTypeEnum.User : FolderTypeEnum.System; UserObject.AccessLevel = accessLevel; return(Write(list, UserObject)); }
/// <summary> /// Edit current list /// </summary> /// <returns></returns> public static string EditTemp( string tempListName) { tempListName = GetInternalTempListName(tempListName); CidList cnList = Read(tempListName); if (cnList == null) // create empty current if needed { cnList = new CidList(); UserObject uo = UserObject.ParseInternalUserObjectName(tempListName, UserObjectType.CnList, SS.I.UserName); Write(cnList, uo); } CidListEditor.Edit(tempListName); return(""); }
/// <summary> /// Save current to specified destination /// </summary> /// <param name="saveToTempList"></param> /// <param name="tempListName"></param> /// <returns></returns> static UserObject SaveCurrentList( bool saveToTempList, string tempListName) { CidList curList = Read(CidList.CurrentListInternalName); if (curList == null || curList.Count == 0) { MessageBoxMx.ShowError("The current list is empty"); return(null); } UserObject uo = SaveList(curList, "Save Current List", saveToTempList, tempListName); return(uo); }
private void GroupSalts_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { DisplayStatusMsg("Grouping Salts, wait please..."); GetNormalizedListFromControl(CidList); List <string> al = MoleculeUtil.GroupSalts(CidList.ToStringList()); CidList newList = new CidList(al); newList.UserObject = CidList.UserObject; CidList = newList; string formattedList = FormatList(); CidListCtl.Text = formattedList; // put text in editor CidListCtl.Select(0, 0); // position cursor at beginning of list DisplayStatusMsg(""); return; }
/// <summary> /// Read the list of compounds for a library /// </summary> /// <param name="libId"></param> /// <returns></returns> public static CidList ReadLibrary( int libId) { CidList l = new CidList(); DbCommandMx dao = new DbCommandMx(); string sql = @" SELECT l.library_name, l.library_desc_text, s.corp_nbr FROM corp_owner.corp_substance s, corp_owner.corp_library_substance ls, corp_owner.corp_library l WHERE l.lib_id = <libId> and s.cpd_id = ls.cpd_id and l.lib_id = ls.lib_id" ; sql = Lex.Replace(sql, "<libId>", libId.ToString()); dao.Prepare(sql); dao.ExecuteReader(); MetaTable rootMt = MetaTableCollection.Get(MetaTable.PrimaryRootTable); while (dao.Read()) { if (Lex.IsNullOrEmpty(l.UserObject.Name)) { string name = dao.GetString(0); if (Lex.IsNullOrEmpty(name) || Lex.IsInteger(name)) { name = dao.GetString(1); // use desc if no name or just a number } if (Lex.IsNullOrEmpty(name)) { name = "Library " + libId; } l.UserObject.Name = name; } int intCorpId = dao.GetInt(2); string corpId = CompoundId.Normalize(intCorpId, rootMt); l.Add(corpId); } dao.CloseReader(); dao.Dispose(); return(l); }
/// <summary> /// Read the current list from the current client query QueryManager data (server not accessed) /// </summary> /// <returns></returns> public static CidList ReadCurrentListLocal() { //CidList cidList = new CidList(); //QueryManager qm = QbUtil.QueryManager; // (error: may be original qm rather than derived with correct keys) //if (qm == null || qm.DataTableManager == null) return cidList; //List<string> keys = qm.DataTableManager.ResultsKeys; //cidList = new CidList(keys); CidList cidList = new CidList(SessionManager.CurrentResultKeys); UserObject uo = cidList.UserObject; uo.Owner = SS.I.UserName; uo.ParentFolder = UserObject.TempFolderName; uo.Name = "Current"; uo.Id = SessionManager.CurrentListId; return(cidList); }
/// <summary> /// Adding a new SasMap view to an existing query /// </summary> /// <param name="args"></param> /// <returns></returns> public DialogResult ShowInitialViewSetupDialog( QueryColumn sasQc, Query baseQuery) { MetaTable mt; QueryColumn qc = null; DialogResult dr; AssertMx.IsNotNull(sasQc, "SasQc"); AssertMx.IsNotNull(SasQuery, "SasMapQuery"); SasQc = sasQc; BaseQuery = baseQuery; Parms = GetParmsFromQueryColumnCriteria(SasQc); qc = BaseQuery?.GetFirstKeyColumn(); if (qc == null) { qc = QueryTable.GetDefaultRootQueryTable()?.KeyQueryColumn; } qc = qc.Clone(); Parms.KeyCriteriaQc = qc; // assign QC with undefined criteria QueryManager refQm = (baseQuery.QueryManager as QueryManager); List <string> resultsKeys = refQm?.DataTableManager?.ResultsKeys; if (resultsKeys != null && resultsKeys.Count > 0) { string listText = Csv.JoinCsvString(resultsKeys); qc.CriteriaDisplay = CidList.FormatAbbreviatedCidListForDisplay(qc, listText); qc.Criteria = qc.MetaColumn.Name + " IN (" + listText + ")"; } Parms.EndpointMc = BaseQuery?.GetFirstResultColumnByPriority(true)?.MetaColumn; // pick the highest priority result column as the default endpoint dr = Edit(); if (dr == DialogResult.OK) { StoreParmsInQueryColumnCriteria(Parms, SasQc); } return(dr); }
/// <summary> /// Open the specified query or copy the list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void GridView_RowCellClick(object sender, RowCellClickEventArgs e) { StreamReader sr; int r = e.RowHandle; int c = e.Column.AbsoluteIndex; HistoryItem hi = SS.I.History[r]; // get item allowing for header row if (c == 0) // open query { sr = new StreamReader(hi.QueryFileName); string queryText = sr.ReadToEnd(); Query q = Query.Deserialize(queryText); q.UserObject.Name = // append time to name q.UserObject.Name + " - " + hi.DateTime.ToLongTimeString(); q.Mode = QueryMode.Build; // want to be in build mode q.UserObject.Content = q.Serialize(); // set content so not prompted for save when closed QbUtil.AddQueryAndRender(q, false); } // Copy list to current list sr = new StreamReader(hi.ListFileName); string listText = sr.ReadToEnd(); CidList cidList = new CidList(listText); CidListCommand.WriteCurrentList(cidList); SessionManager.CurrentResultKeys = cidList.ToStringList(); SessionManager.DisplayCurrentCount(); if (c == 1) // return message if just copying list to current { string msg = "The list has been copied to the current list (" + SessionManager.CurrentResultKeysCount + ")"; MessageBoxMx.Show(msg); } HistoryList_Deactivate(null, null); return; }
/// <summary> /// Read a compound id list given an internal list name (e.g. FOLDER_123.name or LIST_1234) /// </summary> /// <param name="name"></param> /// <param name="mt"></param> /// <returns></returns> public static CidList Read( string internalName, MetaTable mt, bool allowLocalRead) { UserObject uo, uo2; string fileName, cn; int i1; uo = UserObjectUtil.ParseInternalUserObjectName(internalName, UserObjectType.CnList); if (uo == null) { return(null); } if (allowLocalRead && UserObject.IsCurrentObjectInternalName(internalName)) // get from the current query { return(ReadCurrentListLocal()); } uo2 = UserObjectDao.Read(uo); // get from the server side if (uo2 == null) { MessageBoxMx.ShowError( "Unable to find list: " + internalName + "\r\n\r\n" + CommandExec.GetUserObjectReadAccessErrorMessage(uo.Id, "list")); return(null); } CidList cnList = CidList.Deserialize(uo2, mt); if (UserObject.IsCurrentObjectInternalName(internalName)) // if current list store keys with query { SessionManager.CurrentResultKeys = cnList.ToStringList(); } return(cnList); }
/// <summary> /// Read a compound id list given a list name /// </summary> /// <param name="name"></param> /// <returns></returns> public static CidList Read( string name, MetaTable mt) { UserObject uo; CidList cnList = new CidList(); string fileName, cn; int i1; uo = UserObject.ParseInternalUserObjectName(name, UserObjectType.CnList, Security.UserName); if (uo == null) { return(null); } uo = UserObjectDao.Read(uo); if (uo == null) { return(new CidList()); } return(CidList.Deserialize(uo, mt)); }
/// <summary> /// Execute list logic on a pair of persisted list objects & store results in current list for user /// </summary> /// <param name="list1InternalName"></param> /// <param name="list2InternalName"></param> /// <param name="op"></param> /// <returns>Number of results from logic operation</returns> public static int ExecuteListLogic( string list1InternalName, string list2InternalName, ListLogicType op) { CidList list1 = Read(list1InternalName); if (list1 == null) { throw new Exception("List not found: " + list1InternalName); } CidList list2 = Read(list2InternalName); if (list2 == null) { throw new Exception("List not found: " + list2InternalName); } list1.ApplyListLogic(list2, op); CidListDao.Write(list1, "Current"); // write new current list return(list1.Count); }
string FormatCidListForDisplay(List <string> cidList) { string txt; QueryColumn qc = null; int count = 0; if (cidList != null && cidList.Count > 0) { count = cidList.Count; } if (count > 0 && SourceQuery.Tables.Count > 0 && SourceQuery.Tables[0].KeyQueryColumn.MetaColumn.DataType == MetaColumnType.CompoundId) { qc = SourceQuery.Tables[0].KeyQueryColumn; string cidListString = MqlUtil.FormatValueListString(cidList, false); txt = CidList.FormatAbbreviatedCidListForDisplay(qc, cidListString); return(txt); } else { return(count.ToString()); } }
/// <summary> /// Write a compound number list /// </summary> /// <returns></returns> public static int Write( CidList list, UserObject uo) { string fileName; string content = list.ToMultilineString(); uo.Type = UserObjectType.CnList; if (Lex.IsNullOrEmpty(uo.Owner)) // set current user as owner if owner not defined { uo.Owner = SS.I.UserName; } if (Lex.IsNullOrEmpty(uo.ParentFolder)) { throw new Exception("No parent folder for list"); } uo.Content = content; uo.Count = list.Count; UserObjectDao.Write(uo, uo.Id); if (uo.IsCurrentObject) { SessionManager.CurrentResultKeys = list.ToStringList(); SessionManager.DisplayCurrentCount(); } if (uo.HasDefinedParentFolder) { MainMenuControl.UpdateMruList(uo.InternalName); } return(list.Count); }
/// <summary> /// Get list from control removing blank lines & normalizing the ids /// </summary> /// <param name="CidList"></param> void GetNormalizedListFromControl(CidList CidList) { CidList.Clear(); // clear contents, keep name string[] cna = CidListCtl.Lines; bool allowDuplicates = !RemoveDuplicates.Checked; for (int i1 = 0; i1 < cna.Length; i1++) { string extCn = cna[i1]; if (extCn.Trim() == "" || extCn.StartsWith("(")) { continue; } string intCid = CompoundId.Normalize(extCn, RootTable); if (String.IsNullOrEmpty(intCid)) { intCid = extCn; } CidList.Add(intCid, allowDuplicates); } return; }
/// <summary> /// Search button clicked, process input /// </summary> /// <returns></returns> DialogResult ProcessInput() { CidList cidList = null; StreamReader structureFileReader = null; string qid; // query identifier, compoundId, file name or sdFile key value QueryManager qm; DataTableMx dt; DataColumn dc; DataRowMx dr; //object[] dr; // if using Qe DialogResult dlgRslt = DialogResult.OK; Query q = null; QueryTable qt = null; QueryColumn simScoreQc, structQc; // query column containing latest query settings MetaTable mt = null; MetaColumn keyMc = null, structMc = null, dbSetMc = null, simScoreMc = null, mc; MetaColumnType storageType; string txt, tok; if (DatabasesToSearch.Text == "") { MessageBoxMx.ShowError("Databases to search must be defined."); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } // Get list of databases string[] dba = DatabasesToSearch.Text.Split(','); List <MetaTable> tables = new List <MetaTable>(); foreach (string dbLabel0 in dba) { string dbLabel = dbLabel0.Trim(); RootTable dbInfo = RootTable.GetFromTableLabel(dbLabel); if (dbInfo == null) { MessageBoxMx.ShowError("Can't find database " + DatabasesToSearch.Text); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } mt = MetaTableCollection.Get(dbInfo.MetaTableName); if (mt == null) { MessageBoxMx.ShowError("Unable to locate parent structure table for database: " + DatabasesToSearch.Text); DatabasesToSearch.Focus(); return(DialogResult.Cancel); } if (dbSetMc == null) { dbSetMc = mt.DatabaseListMetaColumn; } tables.Add(mt); } if (dbSetMc == null) { throw new Exception("\"Databases\" metacolumn not found for any of the databases to search"); } // Validate other form values RetrieveStructures = RetrieveMatchingStructures.Checked; bool fromList = FromList.Checked; int listCidsRead = 0; int inputQueryCount = -1; if (fromList) // using list, validate list name { if (SavedListUo == null) { MessageBoxMx.ShowError("Compound list must be defined."); ListName.Focus(); return(DialogResult.Cancel); } cidList = CidListCommand.Read(SavedListUo); if (cidList == null) { MessageBoxMx.ShowError("Error reading list."); ListName.Focus(); return(DialogResult.Cancel); } inputQueryCount = cidList.Count; } else // Using SdFile, validate SdFile name { StructureFile = FileName.Text; if (StructureFile == "") { MessageBoxMx.ShowError("File must be defined."); FileName.Focus(); return(DialogResult.Cancel); } try { structureFileReader = new StreamReader(StructureFile); structureFileReader.Close(); } catch (Exception ex) { MessageBoxMx.ShowError("Can't read file: " + Lex.Dq(StructureFile)); FileName.Focus(); return(DialogResult.Cancel); } keyField = KeyField.Text; // get key, blank to use name in 1st line inputQueryCount = -1; // don't know how many queries unless we read the file (todo?) } tok = ResultsName.Text.Trim(); // name to store results under if (tok == "") { MessageBoxMx.ShowError("A name for the results must be provided."); ResultsName.Focus(); return(DialogResult.Cancel); } if (SubStruct.Checked) { Psc.SearchType = StructureSearchType.Substructure; } else if (Full.Checked) { Psc.SearchType = StructureSearchType.FullStructure; } else if (Similarity.Checked) { Psc.SearchType = StructureSearchType.MolSim; } else { throw new Exception("Unrecognized search type"); } // Write initial log entries SearchCount++; string logFileName = ClientDirs.DefaultMobiusUserDocumentsFolder + @"\Multistructure Search " + SearchCount + ".txt"; if (!UIMisc.CanWriteFileToDefaultDir(logFileName)) { return(DialogResult.Cancel); } LogStream = new StreamWriter(logFileName); if (ResultsUo == null) { ResultsUo = new UserObject(UserObjectType.Annotation); } ResultsUo.Name = tok; UserObjectTree.GetValidUserObjectTypeFolder(ResultsUo); DateTime startTime = DateTime.Now; WriteToLog("Multiple " + Psc.SearchType + " Search"); WriteToLog("Databases: " + DatabasesToSearch.Text); WriteToLog("Date: " + startTime); if (fromList) { WriteToLog("Input List: " + SavedListUo.Name); } else { WriteToLog("Input Structure File: " + StructureFile); } WriteToLog("Output List: " + ResultsUo.Name); WriteToLog("Log File: " + logFileName); WriteToLog(""); WriteToLog("Query, Match, Score"); int queryCount = 0; int matchAtLeastOneCount = 0; MoleculeMx queryStructure = null; // current structure being searched CidList matchList = new CidList(); List <MatchData> matchData = new List <MatchData>(); if (FromFile.Checked) // open SdFile as required { structureFileReader = new StreamReader(StructureFile); } // Search of structures one at a time while (true) { if (fromList) // get next structure from list { if (listCidsRead >= cidList.Count) { break; } qid = cidList[listCidsRead].Cid; listCidsRead++; if (qid.Trim() == "") { continue; } if (qid.ToLower().IndexOf(".mol") > 0 || qid.ToLower().IndexOf(".skc") > 0) { // file reference if (!File.Exists(qid)) { continue; } if (qid.ToLower().IndexOf(".mol") > 0) { queryStructure = MoleculeMx.ReadMolfile(qid); } else { queryStructure = MoleculeMx.ReadSketchFile(qid); } } else { queryStructure = MoleculeUtil.SelectMoleculeForCid(qid); } if (queryStructure == null || queryStructure.AtomCount == 0) { continue; // oops } } else // get next structure from input file { qid = null; if (StructureFile.ToLower().EndsWith(".sdf")) { List <SdFileField> fList = SdFileDao.Read(structureFileReader); if (fList == null) // end of sdFile { structureFileReader.Close(); break; } if (fList.Count == 0) { continue; } queryStructure = new MoleculeMx(MoleculeFormat.Molfile, fList[0].Data); if (queryStructure == null || queryStructure.AtomCount == 0) { continue; } if (keyField != "") // key field specified? { qid = SdFileDao.GetDataField(fList, keyField); } else // get name from 1st line of molfile { string molFile = fList[0].Data; int i1 = molFile.IndexOf("\n"); if (i1 == 0) { qid = ""; } else { qid = molFile.Substring(0, i1).Trim(); } } if (string.IsNullOrEmpty(qid)) { qid = SdFileDao.GetDataField(fList, "compound_id"); } } else // assume smiles file { string smiles = structureFileReader.ReadLine(); if (smiles == null) // end of sdFile { structureFileReader.Close(); break; } smiles = smiles.Trim(); if (smiles.Length == 0) { continue; } int i1 = smiles.IndexOf(","); // get any preceeding queryId if (i1 < 0) { i1 = smiles.IndexOf("\t"); } if (i1 >= 0) { qid = smiles.Substring(0, i1).Trim(); smiles = smiles.Substring(i1 + 1).Trim(); } queryStructure = new MoleculeMx(MoleculeFormat.Smiles, smiles); if (queryStructure == null || queryStructure.AtomCount == 0) { continue; } } if (qid == null || qid.Trim() == "") { qid = (queryCount + 1).ToString(); // be sure we have a query id } } queryCount++; // count the query if (queryStructure == null || queryStructure.AtomCount == 0) { WriteToLog("Error converting specific structure " + queryCount.ToString() + ", " + qid); continue; } queryStructure.RemoveStructureCaption(); // remove any Mobius-added caption Psc.Molecule = queryStructure; string msg = "Searching Structure: " + queryCount.ToString(); if (inputQueryCount > 0) { msg += " of " + inputQueryCount.ToString(); } msg += "\n" + "Structures with one or more matches: " + matchAtLeastOneCount.ToString() + "\n" + "Total Matches: " + matchList.Count.ToString(); Progress.Show(msg); // Do the search over the list of databases for (int ti = 0; ti < tables.Count; ti++) { mt = tables[ti]; q = new Query(); // build basic query //q.SingleStepExecution = true; // do in single step (doesn't currently return sim score) q.ShowStereoComments = false; qt = new QueryTable(mt); q.AddQueryTable(qt); qt.SelectKeyOnly(); // start selecting desired cols keyMc = mt.KeyMetaColumn; structMc = mt.FirstStructureMetaColumn; structQc = qt.GetQueryColumnByName(structMc.Name); structQc.Selected = RetrieveStructures; dbSetMc = mt.DatabaseListMetaColumn; if (dbSetMc == null) { throw new Exception("\"Databases\" metacolumn not found for table: " + mt.Label); } QueryColumn dbSetQc = qt.GetQueryColumnByName(dbSetMc.Name); dbSetQc.Selected = true; // select the database name RootTable root = RootTable.GetFromTableName(mt.Name); txt = " in (" + root.Label + ")"; dbSetQc.Criteria = dbSetMc.Name + txt; dbSetQc.CriteriaDisplay = txt; simScoreMc = mt.SimilarityScoreMetaColumn; simScoreQc = null; if (simScoreMc != null) // get sim score if it exists { simScoreQc = qt.GetQueryColumnByName(simScoreMc.Name); simScoreQc.Selected = true; // return sim score } Psc.QueryColumn = structQc; ParsedStructureCriteria psc2 = AdjustSearchForSmallWorldAsNeeded(Psc); psc2.ConvertToQueryColumnCriteria(structQc); // format the QC for the structure search DateTime t0 = DateTime.Now; //QueryEngine qe = new QueryEngine(); //qe.NextRowsMin = 1000; // minimum number of rows to prefetch //qe.NextRowsMax = -1; // maximum number of rows to prefetch //qe.NextRowsMaxTime = 10000; // max time in milliseconds for next fetch //qe.ExecuteQuery(q); qm = new QueryManager(); try { dlgRslt = qm.ExecuteQuery(ref q); } catch (Exception ex) { WriteToLog("Error searching structure: " + ex.Message + ", " + queryCount.ToString() + ", " + qid); continue; } if (dlgRslt != DialogResult.OK) { return(dlgRslt); } double executeTime = TimeOfDay.Delta(ref t0); int offset = qm.DataTableManager.KeyValueVoPos + 1; //int offset = 0; // for QE int keyPos = offset++; int strPos = RetrieveStructures ? offset++ : -1; int dbPos = offset++; int simPos = offset++; int fetchCnt = 0; while (true) { dr = qm.NextRow(); //dr = qe.NextRow(); // for Qe if (dr == null) { break; } fetchCnt++; if (fetchCnt == 1) { matchAtLeastOneCount++; // number of queries that have at least one match } MatchData md = new MatchData(); md.Qno = queryCount; md.Qid = qid; if (RetrieveStructures) { md.Qstr = "Chime=" + queryStructure.GetChimeString(); } CompoundId cid = CompoundId.ConvertTo(dr[keyPos]); md.Mid = cid.Value; if (RetrieveStructures) { MoleculeMx ms = MoleculeMx.ConvertTo(dr[strPos]); if (!NullValue.IsNull(ms)) { md.Mstr = "Chime=" + ms.GetChimeString(); } } StringMx db = StringMx.ConvertTo(dr[dbPos]); if (!NullValue.IsNull(db)) { md.Db = db.Value; } if (Psc.SearchType == StructureSearchType.MolSim) { NumberMx nex = NumberMx.ConvertTo(dr[simPos]); if (!NullValue.IsNull(nex)) { md.Score = nex.Value; } } if (matchList.Contains(cid.Value)) // already have compound id as match for other query? { if (Psc.SearchType != StructureSearchType.MolSim) { continue; // if similarity search see if more similar } CidListElement le = matchList.Get(cid.Value); // reference current score if (le.Tag > md.Score) { continue; // only replace if more similar } matchList.Remove(le.Cid); // remove from list for (int mi = 0; mi < matchData.Count; mi++) // remove from data { if (matchData[mi].Mid == md.Mid) { matchData.RemoveAt(mi); break; } } } matchList.Add(md.Mid); matchList.Get(md.Mid).Tag = md.Score; // keep score in list matchData.Add(md); // add to results txt = md.Qid + ", " + md.Mid + ", " + md.Score.ToString(); WriteToLog(txt); } // Fetch result loop double fetchTime = TimeOfDay.Delta(ref t0); } // DB loop if (Progress.CancelRequested) { Progress.Hide(); MessageBoxMx.ShowError("Search cancelled."); try { LogStream.Close(); } catch { } return(DialogResult.Cancel); } } // key loop CidListCommand.WriteCurrentList(matchList); // write the list of numbers UsageDao.LogEvent("MultipleStructSearch"); txt = "=== Multiple structure search complete ===\r\n\r\n" + "Structures Searched: " + queryCount.ToString() + "\r\n"; txt += "Structures with one or more matches: " + matchAtLeastOneCount.ToString() + "\r\n" + "Total Matches: " + matchList.Count.ToString() + "\r\n"; TimeSpan ts = DateTime.Now.Subtract(startTime); ts = new TimeSpan(ts.Hours, ts.Minutes, ts.Seconds); txt += "Total Time: " + ts + "\r\n\r\n"; WriteToLog("\r\n" + txt); try { LogStream.Close(); } catch { } if (matchList.Count == 0) { Progress.Hide(); MessageBoxMx.ShowError("No matches have been found."); return(DialogResult.Cancel); } tok = "Matching compound ids"; if (Psc.SearchType == StructureSearchType.MolSim) { tok = "Similar compound ids"; } txt += tok + " have been written to the current list: " + ResultsUo.Name + "\n" + "Log file written to: " + logFileName + "\n\n" + "Do you want to view the match results?"; DialogResult dRslt = MessageBoxMx.Show(txt, "Multiple Structure Search", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dRslt == DialogResult.Cancel) { return(DialogResult.Cancel); } else if (dRslt == DialogResult.No) // show log { SystemUtil.StartProcess(logFileName); return(DialogResult.Cancel); } // Display results Progress.Show("Formatting results..."); mt = new MetaTable(); mt.Name = "MULTSTRUCTSEARCH_" + SearchCount; mt.Label = "Multiple Structure Search " + SearchCount; mt.TableMap = "Mobius.Tools.MultStructSearch"; // plugin id mt.MetaBrokerType = MetaBrokerType.NoSql; ColumnSelectionEnum structureColumnSelection = RetrieveStructures ? ColumnSelectionEnum.Selected : ColumnSelectionEnum.Unselected; keyMc = keyMc.Clone(); keyMc.Name = "MatchingCid"; keyMc.Label = "Matching Compound Id"; mt.AddMetaColumn(keyMc); structMc = structMc.Clone(); structMc.Name = "MatchingStructure"; structMc.Label = "Matching Structure"; structMc.InitialSelection = structureColumnSelection; mt.AddMetaColumn(structMc); dbSetMc = dbSetMc.Clone(); dbSetMc.Name = "Database"; mt.AddMetaColumn(dbSetMc); //if (DatabasesToSearch.Text.Contains(",")) dbSetMc.InitialSelection = ColumnSelectionEnum.Selected; mc = mt.AddMetaColumn("Molsimilarity", "Similarity Search Score", MetaColumnType.Number, ColumnSelectionEnum.Unselected, 10); if (Psc.SearchType == StructureSearchType.MolSim) { mc.InitialSelection = ColumnSelectionEnum.Selected; } mc = mt.AddMetaColumn("QueryNo", "Query Number", MetaColumnType.Integer); //mc = mt.AddMetaColumn("QueryMatchNo", "Query Match Number", MetaColumnType.Integer); mc = mt.AddMetaColumn("QueryId", "Query Id", MetaColumnType.String); mc = mt.AddMetaColumn("QueryStructure", "Query Structure", MetaColumnType.Structure); mc.InitialSelection = structureColumnSelection; q = ToolHelper.InitEmbeddedDataToolQuery(mt); dt = q.ResultsDataTable as DataTableMx; for (int mi = 0; mi < matchData.Count; mi++) { MatchData md = matchData[mi]; dr = dt.NewRow(); dr[qt.Alias + ".MatchingCid"] = new CompoundId(md.Mid); if (RetrieveStructures) { dr[qt.Alias + ".MatchingStructure"] = new MoleculeMx(MoleculeFormat.Chime, md.Mstr); } dr[qt.Alias + ".Database"] = new StringMx(md.Db); if (Psc.SearchType == StructureSearchType.MolSim) { dr[qt.Alias + ".Molsimilarity"] = new NumberMx(md.Score); } dr[qt.Alias + ".QueryNo"] = new NumberMx(md.Qno); dr[qt.Alias + ".QueryId"] = new StringMx(md.Qid); if (RetrieveStructures) { dr[qt.Alias + ".QueryStructure"] = new MoleculeMx(MoleculeFormat.Chime, md.Qstr); } dt.Rows.Add(dr); } ToolHelper.DisplayData(q, dt, true); Progress.Hide(); return(DialogResult.OK); }
/// <summary> /// Attempt to read existing results file into the query DataTable /// </summary> /// <param name="qm"></param> public void ReadBinaryResultsFile(string fileName) { QueryTable qt; QueryColumn qc; BinaryReader br = null; Stopwatch sw = Stopwatch.StartNew(); try { bool saveHandlersEnabled = Qm.DataTable.EnableDataChangedEventHandlers(false); // disable for faster load bool saveUpdateMaxRowsPerKey = UpdateMaxRowsPerKeyEnabled; UpdateMaxRowsPerKeyEnabled = false; // disable for faster load int id = Query.UserObject.Id; if (id <= 0) { throw new Exception("Query not saved"); } if (DataTableMx == null || DataTableMx.Columns.Count == 0) { throw new Exception("DataTable not defined"); } br = BinaryFile.OpenReader(fileName); string sq = br.ReadString(); Query q0 = Query.Deserialize(sq); // deserialize the saved query QueryManager qm0 = new QueryManager(); qm0.LinkMember(q0); ResultsFormat rf0 = new ResultsFormat(qm0, OutputDest.WinForms); ResultsFormatFactory rff0 = new ResultsFormatFactory(qm0, OutputDest.WinForms); rff0.Build(); // build format with vo positions // The cached query cols should match those of the current query: however, // we'll create a mapping just in case they don't int voArrayLen0 = br.ReadInt32(); // cached vo array len int voArrayLen = DataTableMx.Columns.Count - KeyValueVoPos; // current query vo array len List <int> q0VoMap = new List <int>(); // vo position in cached query data List <int> qVoMap = new List <int>(); // vo position in current version of query q0VoMap.Add(0); // first position is the common key value qVoMap.Add(0); foreach (QueryTable qt0 in q0.Tables) // scan each table in cached data { foreach (QueryColumn qc0 in qt0.QueryColumns) // and each column { if (qc0.VoPosition < 0) { continue; // skip if not mapped to the vo in cached data } int q0VoPos = qc0.VoPosition - KeyValueVoPos; // where it is in cache int qvoPos = -1; // where it will go qt = Query.GetTableByName(qt0.MetaTable.Name); if (qt != null) { qc = qt.GetQueryColumnByName(qc0.MetaColumn.Name); if (qc != null) { qvoPos = qc.VoPosition - KeyValueVoPos; } } q0VoMap.Add(q0VoPos); // where it is in saved data qVoMap.Add(qvoPos); // where it will go (not including attributes & check cols) } } if (q0VoMap.Count != voArrayLen0) { throw new Exception("Cached Vo length doesn't match list of selected columns"); } DataTableMx.Clear(); // clear the rows CidList cidList = new CidList(); object[] voa = new object[voArrayLen]; // array to fill while (!BinaryFile.ReaderEof(br)) // process each row { for (int mi = 0; mi < q0VoMap.Count; mi++) // each col { object o = VoArray.ReadBinaryItem(br); if (mi == 0 && o != null) // add to key list if key { cidList.Add(o.ToString(), false); } if (qVoMap[mi] >= 0) // save in new buf if mapped { voa[qVoMap[mi]] = o; } } DataRowMx dr = AddDataRow(voa); } br.Close(); Qm.DataTable.EnableDataChangedEventHandlers(saveHandlersEnabled); UpdateMaxRowsPerKeyEnabled = saveUpdateMaxRowsPerKey; InitializeRowAttributes(false); ResultsKeys = cidList.ToStringList(); // include keys in DTM as well double ms = sw.Elapsed.TotalMilliseconds; return; } catch (Exception ex) { if (br != null) { br.Close(); } throw new Exception(ex.Message, ex); } }
/// <summary> /// Set the display form of the list /// </summary> void SetCidListDisplay() { string txt = CidList.FormatAbbreviatedCidListForDisplay(Qc, CidListString); CidListDisplay.Text = txt; }
/// <summary> /// Setup the form for display /// </summary> /// <param name="cid"></param> /// <param name="qm"></param> /// <returns></returns> bool SetupForm( string cid, QueryManager qm) { AssertMx.IsDefined(cid, "Compound Id (CorpId)"); SelectedCid = cid; PreviousCid = cid; RootTable = CompoundId.GetRootMetaTableFromCid(cid); Qm = null; // no query context SourceQuery = null; CurrentBaseQueryCidHitList = null; if (qm != null) // if querymanager defined then base the related data query on the "current" query { Qm = qm; // setup query context SourceQuery = qm.Query; RootTable = qm.Query.RootMetaTable; if (qm.Query != null && qm.Query.Mode == QueryMode.Browse) // if browsing current base query results then get that cid list { CurrentBaseQueryCidHitList = Qm.DataTableManager.GetMostCompleteResultsKeyList(); } } //else throw new Exception("Parameters not defined"); //if (Lex.IsUndefined(SelectedCid) && // (Qm == null || Qm.MoleculeGrid == null || Qm.MoleculeGrid.Helpers == null)) // return false; SetupCheckmarks(); // Current Cid //if (Qm != null) // SelectedCid = Qm.MoleculeGrid.Helpers.GetCidForSelectedCell(); SelectedCid = CompoundId.Format(SelectedCid); CidCtl.Text = SelectedCid; //CidCtl.Focus(); // Marked cid count MarkedCidsList = null; if (Qm?.MoleculeGrid != null) { CidList cl = Qm.MoleculeGrid.GetMarkedList(); if (cl != null) { MarkedCidsList = cl.ToStringList(); } } int selCnt = (MarkedCidsList != null ? MarkedCidsList.Count : 0); MarkedCidsCheckEdit.Text = "Selected compound Ids (" + FormatCidListForDisplay(MarkedCidsList) + ")"; MarkedCidsCheckEdit.Enabled = (selCnt > 0); if (selCnt == 0 && MarkedCidsCheckEdit.Checked) { CurrentCidCheckEdit.Checked = true; } // All Cid count int allCnt = (CurrentBaseQueryCidHitList != null ? CurrentBaseQueryCidHitList.Count : 0); AllCidsCheckEdit.Text = "All Ids in the current result set (" + FormatCidListForDisplay(CurrentBaseQueryCidHitList) + ")"; AllCidsCheckEdit.Enabled = (allCnt > 0); if (selCnt == 0 && AllCidsCheckEdit.Checked) { CurrentCidCheckEdit.Checked = true; } // Structure MoleculeMx cs = new MoleculeMx(); if (Lex.IsDefined(SelectedCid)) { cs = MoleculeUtil.SelectMoleculeForCid(SelectedCid); } QueryMolCtl.SetupAndRenderMolecule(cs); // MRU list RenderMruList(); return(true); }
/// <summary> /// Setup for edit /// </summary> /// <param name="inList"></param> /// <param name="rootTable"></param> /// <returns></returns> bool Setup( CidList inList, MetaTable rootTable) { string formattedCid, label, txt; RootTable = rootTable; InListName = inList.UserObject.Name; ListName = InListName; CidList = inList.Clone(); // make copy of list to edit CidCount = InitialCount = CidList.Count; if (CidCount > 32000) // too big to edit? { txt = "The list contains " + CidList.Count.ToString() + " compound numbers which\n" + "exceeds the maximum of 32,000 that can be directly edited.\n" + " Do you want to clear the list and start a new list?"; DialogResult dr = MessageBoxMx.Show(txt, "List Too Large to Edit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation); if (dr != DialogResult.Yes) { return(false); } CidList = new CidList(); CidList.UserObject = // set name properly UserObjectUtil.ParseInternalUserObjectName(InListName, UserObjectType.CnList); } Text = "Edit List - [" + CidList.UserObject.Name + "]"; Before = FormatList(); // convert & save initial list if (rootTable != null) { label = rootTable.KeyMetaColumn.Label; } else { label = "Compound Id"; } ListLabel.Text = " " + label + " - (Enter one number per line) "; RNLabel.Text = label + ":"; MolCtl.ClearMolecule(); // clear any structure CidCtl.Text = HeavyAtoms.Text = Weight.Text = Formula.Text = ""; CidListCtl.Text = Before; // put text in editor DisplayStatusMsg(""); if (SS.I.AllowGroupingBySalts) { SaltsMenu.Enabled = true; } else { SaltsMenu.Enabled = false; } return(true); }
/// <summary> /// Write a compound number list /// </summary> /// <returns></returns> public static int Write( CidList list) { return(Write(list, list.UserObject)); }
/// <summary> /// Run Query & return any error message /// </summary> /// <param name="query"></param> /// <param name="rf"></param> /// <param name="browseExistingResults">If true browse existing results</param> /// <returns>Command command or an error message</command></returns> public static string RunQuery2( Query query, ResultsFormat rf, bool browseExistingResults = false) { QueryManager qm = null; DataTableMx dt; QueryTable qt = null; QueryColumn qc; MetaTable mt; MetaColumn mc; QueryExec qex; ResultsFormatter rfmtr; Query modifiedQuery = null; DialogResult dr; bool success; int ti, ci, count; if (query == null) { throw new Exception("Null Query"); } qm = query.QueryManager as QueryManager; if (qm == null || !qm.ContainsRenderedResults()) { browseExistingResults = false; } if (ToolHelper.IsOldToolQuery(query) && !browseExistingResults) { return(ToolHelper.RunOldToolQuery(query)); } //if (query.FirstView == null) //{ ResultsViewType tableViewType = ResultsViewType.Table; if (rf.OutputDestination == OutputDest.Html) { tableViewType = ResultsViewType.HtmlTable; } //} //CheckForConnectionLeaks(); // do routine check for connection leaks (disabled for now) // Build/update the QueryManager as needed (including the QueryExec) if running the query rather than just browsing existing results if (!browseExistingResults) // initialize QueryManager { query.SetupQueryPagesAndViews(tableViewType); // adjust views as necessary to match query qm = BuildQueryManager(query, rf); } // Attempt to display already retrieved data in memory or in cached results file else { if (qm != null) { if (qm.QueryExec == null) // create QueryExec if not defined { qex = new QueryExec(qm); } if (qm.ResultsFormatter == null) // create ResultsFormatter if not defined { rfmtr = new ResultsFormatter(qm); } } else { qm = BuildQueryManager(query, rf); } if (Lex.IsDefined(query.ResultsDataTableFileName)) // get data from file { DataTableManager.LoadDataTableFromFile(query); } else // should have data in DataTable already { DataTableManager.ValidateExistingDataTable(query); } if (qm.DataTableManager != null && qm.QueryExec != null) { qm.QueryExec.ResultsKeys = qm.DataTableManager.ResultsKeys; // have data in memory already } } if (rf.OutputDestination == OutputDest.WinForms && rf.SessionOutputFormContext) { // save qm and query if this is a runquery from the main window QueriesControl.Instance.CurrentBrowseQuery = query; // save ref to the query being browsed } if (rf.SessionOutputFormContext) // if not a popup then update main status bar { qm.StatusBarManager = SessionManager.Instance.StatusBarManager; } LastQEx = qm.QueryExec; // keep track of last QueryExec SetupRunQuery: bool saveHitList = (rf.SessionOutputFormContext && !browseExistingResults); // if popup or browsing existing results don't change hitlist qex = qm.QueryExec; string response = qex.RunQuery3(rf, saveHitList, browseExistingResults); // run the query if (qex.ResultsKeys != null && qex.ResultsKeys.Count > 0 && // save in history list if normal query that resulted in hits !query.Preview && !MqlUtil.SingleStepExecution(query) && !browseExistingResults && rf.OutputDestination == OutputDest.WinForms) { CidList keyList = new CidList(qex.ResultsKeys); AddToHistoryList(query, keyList); // use original query } if (response != "" && !response.ToLower().StartsWith("command")) { return(response); } else if (Lex.EndsWith(response, "refetch")) // data to be retrieved has changed, redo fetch part of query { if (!query.Preview) // use current keys if not preview { query.ResultKeys = qex.QueryEngine.GetKeys(); } goto SetupRunQuery; } //else if (response == "") response = "Command EditQuery"; //if (rf.OutputDestination == OutputDest.Grid) //{ // QbUtil.SetMode(QueryMode.Build); // back to build mode // QbUtil.RenderQuery(QbUtil.Qt); //} return(response); }
/// <summary> /// Run the query /// </summary> /// <param name="browseExistingResults">If true browse existing results</param> /// <returns></returns> public string RunQuery3( ResultsFormat rf, bool saveHitlist, bool browseExistingResults) { Query modifiedQuery; QueryTable qt; QueryColumn qc; ResultsTable rt; ResultsField rfld; MetaTable mt; MetaColumn mc; string txt, msg; DialogResult dr; bool success; CellGraphic cg; Lex lex = new Lex(); string tempfile, tok, command, unrecognizedCommand, response; int ti, gi, rc, i1, i2; // Begin execution if (rf == null) { throw new Exception("QueryExec.Run - Null ResultsFormat"); } if (ResultsFormatter == null) { throw new Exception("QueryExec.Run - Null ResultsFormatter"); } if (rf.Segments == null) { throw new Exception("QueryExec.Run - Null ResultsFormat.Segments"); } if (Query == null) { throw new Exception("QueryExec.Run - Null Rf.Query"); } if (Query.Tables == null || Query.Tables.Count <= 0) { throw new QueryException("QueryExec.Run - No Query Tables"); } QueryManager qm = QueryManager; ReturnMsg = ""; //bool useExistingQueryEngine = Rf.ParentQe != null; //bool useExistingDataTable = Query.BrowseExistingResultsWhenOpened && Query.SerializeResults && // qm.DataTable != null && qm.DataTable.Rows.Count > 0; try { //if (Math.Sqrt(4) == 2) throw new Exception("test"); // debug if (!browseExistingResults) // normal open of search { Progress.Show("Analyzing query..."); // put up a status message to the user as soon as possible to let them know something is happening... dr = ValidateQuery(Query); if (dr == DialogResult.Cancel) { return(""); } WriteCurrentListToServerIfNeeded(Query); if (rf.OutputDestination == OutputDest.WinForms) // update access stats if grid { UpdateTableUsageStatistics(Query); } Query.ResultsDataTable = null; // be sure to get new results qm = BuildQueryManager(Query, rf); Query.ResetViewStates(); // reset state of views for proper operation if (Rf.ParentQe == null) // open search unless using existing query engine { if (!ExecuteSearch(saveHitlist)) // returns false if cancelled by user { Progress.Hide(); return(""); } } if ((ResultsKeys == null || ResultsKeys.Count == 0) && // nothing for search !Query.Preview && !MqlUtil.SingleStepExecution(Query) && qm.DataTable.Rows.Count == 0 && Query.RetrievesDataFromQueryEngine) { // if (!Rf.PopupDisplay) Progress.Hide(); if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayStatusMessage(""); } // if (QueryEngine.Cancelled) return ""; // cancelled by user msg = "No data have been found that matches your query."; if (ResultsFormat.PopupOutputFormContext && !ResultsFormat.SuppressNoDataMessage) { MessageBoxMx.Show(msg, "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return("Command EditQuery"); // return to edit query menu } else { return(msg); } } //if (ResultsFormat.PopupDisplay) // SessionManager.DisplayStatusMessage("Retrieving data..."); //else Progress.Show("Retrieving data...", UmlautMobius.Value, true, "Cancelling Retrieval..."); //Progress.Show("Retrieving data...", UmlautMobius.String, true, "Cancelling Retrieval..."); Progress.Hide(); // hide progress - "Retrieving data..." message now appears as bottom line of grid if (ResultsFormat.Grid) { if (ResultsFormat.SessionOutputFormContext) // if normal main session form grid display, set browse mode & view state { Query.ResetViewStates(); // reset view state for all views QbUtil.SetMode(QueryMode.Browse, Query); if (ResultsFormat.Query.LogicType == QueryLogicType.And) // log grid query by logic type { UsageDao.LogEvent("QueryGridAnd", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Or) { UsageDao.LogEvent("QueryGridOr", ""); } else if (ResultsFormat.Query.LogicType == QueryLogicType.Complex) { UsageDao.LogEvent("QueryGridComplex", ""); } } else if (ResultsFormat.PopupOutputFormContext) // create popup window & configure { PopupResults.Show(qm); //MoleculeGridPanel.ConfigureAndShow(qm, null); } else if (ResultsFormat.ToolOutputFormContext) { ContainerControl cc; QueryResultsControl qrc = ResultsFormat.OutputContainerControl as QueryResultsControl; AssertMx.IsTrue(qrc != null, "ResultsFormat.OutputContainerControl must be a QueryResultsControl"); if (!WindowsHelper.FindContainerControl(qrc, typeof(ToolResultsContainer), out cc)) { throw new Exception("ToolResultsContainer not found"); } ToolResultsContainer trc = cc as ToolResultsContainer; trc.SetupQueryResultsControlForResultsDisplay(qm); } else { throw new Exception("Invalid OutputformContext: " + ResultsFormat.OutputFormContext); } } } else // reentering display switch to browse tab { QbUtil.SetMode(QueryMode.Browse, Query); } response = ResultsFormatter.BeginFormatting(browseExistingResults); // format the data if (ResultsFormat.SessionOutputFormContext) // normal display { if (MqlUtil.SingleStepExecution(Query)) { // be sure hit count display is up to date if (ResultsKeys != null) { if (qm.StatusBarManager != null) { qm.StatusBarManager.DisplayCurrentCount(); } } } if (saveHitlist) { CidList hitList = new CidList(ResultsKeys); rc = CidListCommand.WriteCurrentList(hitList); SessionManager.DisplayCurrentCount(); } } return(response); } // end of surrounding try catch (Exception ex) { Progress.Hide(); if (ex is UserQueryException) // exception that can occur from user error { throw new UserQueryException(ex.Message, ex); } else { msg = DebugLog.FormatExceptionMessage(ex); if (!Lex.Contains(msg, "QueryLogged:")) // exception & query { QueryEngine.LogExceptionAndSerializedQuery(msg, Query); } else { ServicesLog.Message(msg); // just log exception } throw new Exception(ex.Message, ex); // pass it up } } }
/// <summary> /// Execute search & get hit list /// </summary> /// <param name="saveHitlist"></param> /// <returns></returns> public bool ExecuteSearch(bool saveHitlist) { int voi, ti, fi; ResultsTable rt; ResultsField rfld; MetaTable mt; MetaColumn mc, mc2; if (saveHitlist) // clear current list { SessionManager.CurrentResultKeys = new List <string>(); if (QueryManager.StatusBarManager != null) { QueryManager.StatusBarManager.DisplayCurrentCount(); } } // Start thread running the query QueryResultsAvailable = false; QueryEngineException = null; ThreadStart ts = new ThreadStart(ExecuteQueryThreadMethod); Thread executeQueryThread = new Thread(ts); executeQueryThread.Name = "ExecuteSearch"; executeQueryThread.IsBackground = true; executeQueryThread.SetApartmentState(ApartmentState.STA); executeQueryThread.Start(); // Put up message for user if (((Query.ResultKeys != null && Query.UseResultKeys) || // already have list ResultsFormat.SessionOutputFormContext) && !QueryEngineStatsForm.ShowStats) { Progress.Show("Retrieving data...", UmlautMobius.String, true, "Cancelling retrieval..."); } else // normal type search { Progress.Show("Searching database - 0:00", UmlautMobius.String, true, "Cancelling search..."); if (QueryEngineStatsForm.ShowStats) { QueryEngineStatsForm.StartNewQueryExecution(Query); QueryEngineStatsForm.StartingSearch(); } } // Wait until results available or the query is cancelled by the user while (true) { Thread.Sleep(100); Application.DoEvents(); if (QueryResultsAvailable) // completed normally { ResultsKeys = QueryEngine.GetKeys(); if (saveHitlist) // store for session manager also (may differ but probably shouldn't) { SessionManager.CurrentResultKeys = ResultsKeys; } //Progress.Hide(); break; } else if (QueryEngineException != null) { Progress.Hide(); if (QueryEngineException is QueryException || QueryEngineException is UserQueryException) { throw QueryEngineException; } else { throw new Exception(QueryEngineException.Message, QueryEngineException); } } else if (Progress.CancelRequested) { if (QueryEngine != null) { QueryEngine.Cancel(false); // start the cancel } Thread.Sleep(250); Application.DoEvents(); if (executeQueryThread != null) { executeQueryThread.Abort(); // kill the local thread executing the query } Progress.Hide(); return(false); } } // If the query contains tables marked for remapping then build new expanded query & use going forward // modifiedQuery = QueryEngine.DoPresearchChecksAndTransforms(Query); // do any presearch transforms OriginalQuery = Query; if (TransformedQuery != null) { //ResultsPages qrp = query.ResultsPages; //ResultsPages mqrp = modifiedQuery.ResultsPages; // use same set of results pages so view changes propagate back to original query Query.PresearchDerivedQuery = TransformedQuery; // link original query to the transformed query Query = TransformedQuery; // replace original query with this query } else { Query.PresearchDerivedQuery = null; } InitializeQueryManager(QueryManager, Query, QueryManager.ResultsFormat, QueryEngine, ResultsKeys); // Save the hit list as needed if (saveHitlist) { CidList currentList = new CidList(ResultsKeys); CidListCommand.WriteCurrentList(currentList); SessionManager.DisplayCurrentCount(); } return(true); }
private void OK_Click(object sender, EventArgs e) { int CnCount; string listName, listText, errMsg = "", tok, cidLo, cidHi, cid = null, cid2 = null; QueryColumn qc = Qc; MetaColumn mc = qc.MetaColumn; MetaTable mt = mc.MetaTable; // Perform validity check on form // Equality if (EQ.Checked) { tok = Cid.Text.ToUpper(); if (tok == "") { XtraMessageBox.Show("Single compound value is missing", UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.None); Cid.Focus(); return; } if (SS.I.ValidateCompoundIds) { if (tok == "") { errMsg = "You must supply a " + qc.ActiveLabel; } else if (RootTable.IsStructureDatabaseRootTable(mt.Root)) { if (!CompoundId.IsValidForDatabase(tok, mt)) { errMsg = tok + " is not a valid " + qc.ActiveLabel; } else { cid = CompoundId.Normalize(tok, mt); tok = CompoundId.Format(cid, mt); errMsg = ""; } } else { cid = tok; // use as is errMsg = ""; // no error } if (errMsg != "") { MessageBoxMx.Show(errMsg, UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } qc.CriteriaDisplay = "= " + tok; qc.Criteria = mc.Name + " = " + Lex.AddSingleQuotes(cid); CnCount = 1; } // List else if (InList.Checked) { listText = CidListString.Trim(); if (listText == "") { XtraMessageBox.Show("The list must contain one or more items", UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.None); if (CidListDisplayIsEditable()) { CidListDisplay.Focus(); } return; } qc.CriteriaDisplay = CidList.FormatAbbreviatedCidListForDisplay(qc, listText); qc.Criteria = mc.Name + " IN (" + listText + ")"; } // Current list else if (TempList.Checked) { qc.CriteriaDisplay = "In temporary list: " + TempListName.Text; qc.Criteria = mc.Name + " IN LIST " + UserObject.TempFolderNameQualified + TempListName.Text; CnCount = SessionManager.CurrentResultKeysCount; } // Saved list else if (SavedList.Checked) { if (ListName.Text.Trim() == "") { XtraMessageBox.Show("A list name must be supplied", UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.None); ListName.Focus(); return; } string internalName = ResolveSavedListUo(); if (internalName == null) { return; } qc.CriteriaDisplay = "In list: " + SavedListUo.Name; qc.Criteria = mc.Name + " IN LIST " + Lex.AddSingleQuotes("CNLIST_" + SavedListUo.Id.ToString()); // quote list name CnCount = 1; // may be larger } // Between else if (Between.Checked) { cidLo = CidLo.Text.Trim(); cidHi = CidHi.Text.Trim(); if (cidLo == "" || cidHi == "") { XtraMessageBox.Show("Between value is missing", UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.None); if (CidLo.Text.Trim() == "") { CidLo.Focus(); } else if (CidHi.Text.Trim() == "") { CidHi.Focus(); } return; } if (SS.I.ValidateCompoundIds) { errMsg = ""; if (cidLo == "") { errMsg = "You must supply a " + qc.ActiveLabel; } else { if (!CompoundId.IsValidForDatabase(cidLo, mt)) { errMsg = cidLo + " is not a valid " + qc.ActiveLabel; } else { cid = CompoundId.Normalize(cidLo, mt); cidLo = CompoundId.Format(cid); } } if (errMsg != "") { MessageBoxMx.Show(errMsg, UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } else { cid = cidLo; // use as is } if (SS.I.ValidateCompoundIds) { errMsg = ""; if (cidHi == "") { errMsg = "You must supply a " + qc.ActiveLabel; } else { if (!CompoundId.IsValidForDatabase(cidHi, mt)) { errMsg = cidHi + " is not a valid " + qc.ActiveLabel; } else { cid2 = CompoundId.Normalize(cidHi, mt); cidHi = CompoundId.Format(cid2); } } if (errMsg != "") { MessageBoxMx.Show(errMsg, UmlautMobius.String, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } else { cid2 = cidHi; // use as is } qc.CriteriaDisplay = "Between " + cidLo + " and " + cidHi; qc.Criteria = mc.Name + " BETWEEN " + Lex.AddSingleQuotes(cid) + " AND " + Lex.AddSingleQuotes(cid2); } else if (None.Checked) { qc.CriteriaDisplay = ""; qc.Criteria = ""; CnCount = 0; } DialogResult = DialogResult.OK; // this.Hide(); }
/// <summary> /// Analyze key criteria in token list. /// A saved list reference is returned as a set of key values & /// blanked out in the token list. /// Other key references are returned as a set of indexes in a list. /// </summary> /// <param name="q"></param> /// <param name="Qtd"></param> /// <param name="tokens"></param> /// <param name="keyCriteriaPositions">Token indexes for key criteria column names</param> /// <param name="keyCriteriaSavedListKeys">Keys in any referenced saved list</param> /// <param name="keyCriteriaInListKeys">Keys in any literal list </param> /// <param name="keyCriteriaConstantPositions">Positions of key values for literal lists</param> public static void AnalyzeKeyCriteria( Query q, QueryTableData[] Qtd, List <MqlToken> tokens, out CompareOp keyCriteriaOp, out List <int> keyCriteriaPositions, out List <string> keyCriteriaSavedListKeys, out List <string> keyCriteriaInListKeys, out List <int> keyCriteriaConstantPositions) { string tok1, tok2, tok3, tok4, tok5; keyCriteriaOp = CompareOp.Unknown; keyCriteriaPositions = new List <int>(); keyCriteriaSavedListKeys = null; keyCriteriaInListKeys = null; keyCriteriaConstantPositions = new List <int>(); for (int tki = 0; tki < tokens.Count; tki++) { QueryColumn qc = tokens[tki].Qc; if (qc == null) { continue; } if (!qc.IsKey) { continue; } keyCriteriaPositions.Add(tki); // remember position of key col reference if (tokens.Count < tki + 2) { throw new QueryException("Incomplete compound id criteria at end of statement"); } bool notLogic = false; tok1 = GetTokenListString(tokens, tki + 1); if (Lex.Eq(tok1, "Not")) { notLogic = true; tki++; if (tokens.Count < tki + 2) { throw new QueryException("Incomplete compound id criteria at end of statement"); } tok1 = GetTokenListString(tokens, tki + 1); } tok2 = GetTokenListString(tokens, tki + 2); tok3 = GetTokenListString(tokens, tki + 3); tok4 = GetTokenListString(tokens, tki + 4); tok5 = GetTokenListString(tokens, tki + 5); // Saved List if (tokens.Count > tki + 3 && Lex.Eq(tok1, "In") && Lex.Eq(tok2, "List")) { // have a saved list keyCriteriaOp = CompareOp.InList; if (keyCriteriaSavedListKeys != null) // already have it? { throw new UserQueryException("Only one condition is allowed for the " + qc.ActiveLabel + " field"); } if (notLogic) // not currently allowed for saved lists { throw new UserQueryException("\"Not\" logic is not allowed for " + qc.ActiveLabel + " saved lists"); } string listName = tokens[tki + 3].Tok.Text; UserObject uo = ResolveCidListReference(listName); if (uo == null) { throw new UserQueryException("Key list " + listName + " not found"); } listName = uo.InternalName; CidList keyCriteriaSavedList = CidListDao.Read(listName, QueryEngine.GetRootTable(q)); if (keyCriteriaSavedList == null) { throw new UserQueryException("Key list " + listName + " not found"); } keyCriteriaSavedListKeys = keyCriteriaSavedList.ToStringList(); tokens[tki].Qc = null; tokens[tki].Tok.Text = ""; for (int tki2 = tki + 1; tki2 <= tki + 3; tki2++) { tokens[tki2].Tok.Text = ""; } if (!MqlUtil.DisableAdjacentAndLogic(tokens, tki, tki + 3)) { throw new UserQueryException("Only \"And\" logic is allowed with " + qc.ActiveLabel + " saved lists"); } int ti = q.GetQueryTableIndexByAlias(qc.QueryTable.Alias); Qtd[ti].CriteriaCount--; keyCriteriaPositions.RemoveAt(keyCriteriaPositions.Count - 1); } // Explicit list of allowed keys else if (tokens.Count > tki + 2 && Lex.Eq(tok1, "In") && Lex.Eq(tok2, "(")) { keyCriteriaOp = CompareOp.In; int listKeyCount = 0; // count keys in this list for (int tki2 = tki + 3; tki2 < tokens.Count; tki2++) { tok1 = tokens[tki2].Tok.Text; if (tok1 == ",") { continue; } else if (tok1 == ")") { break; } keyCriteriaConstantPositions.Add(tki2); if (keyCriteriaInListKeys == null) { keyCriteriaInListKeys = new List <string>(); } MetaTable rootTable = QueryEngine.GetRootTable(q); string normKey = CompoundId.Normalize(tok1, rootTable); if (normKey != null && normKey != "") { keyCriteriaInListKeys.Add(normKey); listKeyCount++; } } if (listKeyCount == 0) { throw new UserQueryException("The query contains an invalid empty list of " + qc.ActiveLabel + "s"); } } // Between a range of key values else if (tokens.Count > tki + 4 && Lex.Eq(tok1, "Between") && Lex.Eq(tok3, "And")) { keyCriteriaOp = CompareOp.Between; keyCriteriaConstantPositions.Add(tki + 2); keyCriteriaConstantPositions.Add(tki + 4); } // Single key value, treat like a list with a single value else if (Lex.Eq(tok1, "=")) { keyCriteriaOp = CompareOp.Eq; keyCriteriaConstantPositions.Add(tki + 2); if (keyCriteriaInListKeys == null) { keyCriteriaInListKeys = new List <string>(); } MetaTable rootTable = QueryEngine.GetRootTable(q); string normKey = CompoundId.Normalize(tok2, rootTable); if (Lex.IsDefined(normKey)) { keyCriteriaInListKeys.Add(normKey); } } // Other binary operator else if (MqlUtil.IsBasicComparisonOperator(tok1)) { keyCriteriaOp = CompareOpString.ToCompareOp(tok1); keyCriteriaConstantPositions.Add(tki + 2); } // Unary operator "is null" else if (Lex.Eq(tok1, "Is") && Lex.Eq(tok2, "Null")) { keyCriteriaOp = CompareOp.IsNull; } // Unary operator "is not null" else if (Lex.Eq(tok1, "Is") && Lex.Eq(tok2, "Not") && Lex.Eq(tok3, "Null")) { keyCriteriaOp = CompareOp.IsNotNull; } else { throw new QueryException("Unrecognized compound id condition " + tok1); } } return; }