/// <summary> /// Retrieve the results of a background export /// Example: Retrieve Background Export 231243 /// </summary> /// <param name="objectIdString">Id of BackgroundExport UserObject containing serialized ResultsFormat</param> /// <returns></returns> public static string RetrieveBackgroundExport( string objectIdString) { ResultsFormat rf; Query q; int objId; QbUtil.SetMode(QueryMode.Build); // be sure in build mode if (!int.TryParse(objectIdString, out objId)) { return("Invalid UserObjectId: " + objectIdString); } UserObject uo = UserObjectDao.Read(objId); if (uo == null) { return("RunInBackground UserObject not found: " + objectIdString); } if (uo.Type != UserObjectType.BackgroundExport) { return("Object is not the expected RunInBackground UserObject type"); } rf = ResultsFormat.Deserialize(uo.Content); if (rf == null) { throw new Exception("Failed to deserialize ResultsFormat: " + objectIdString); } string clientFile = rf.OutputFileName; string serverFile = GetServerFileName(rf, objId); string ext = Path.GetExtension(clientFile); string filter = "*" + ext + "|*" + ext; if (SharePointUtil.IsRegularFileSystemName(clientFile)) { clientFile = UIMisc.GetSaveAsFilename("Retrieve Background Export File", clientFile, filter, ext); } else { clientFile = SharePointFileDialog.GetSaveAsFilename("Retrieve Background Export File", clientFile, filter, ext); } if (String.IsNullOrEmpty(clientFile)) { return(""); } Progress.Show("Retrieving file..."); try { ServerFile.CopyToClient(serverFile, clientFile); } catch (Exception ex) { string msg = "Unable to retrieve cached export file: " + serverFile + "\n" + "to client file: " + clientFile + "\n" + DebugLog.FormatExceptionMessage(ex); ServicesLog.Message(msg); Progress.Hide(); return(msg); } Progress.Hide(); if (Lex.Eq(ext, ".xls") || Lex.Eq(ext, ".xlsx") || Lex.Eq(ext, ".doc") || Lex.Eq(ext, ".docx")) { DialogResult dr = MessageBoxMx.Show("Do you want to open " + clientFile + "?", UmlautMobius.String, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { SystemUtil.StartProcess(clientFile); } } return("Background export file retrieved: " + clientFile); }
/// <summary> /// This method runs in a background process and exports data to the specified destination /// </summary> /// <param name="objectIdString">Id of UserObject containing run parameters in a serialized ResultsFormat</param> /// <param name="templateFileName">Name of template file to use</param> /// <param name="emailResultsHtml">If true then send email otherwise just return html</param> /// <returns></returns> public static string RunBackgroundExport( string objectIdString, string templateFileName, bool emailResultsHtml, out bool copiedToDestinationFile, int alertId = 0) { ResultsFormat rf; Query q; string msg = ""; int objId; //if (ClientState.IsDeveloper) //{ // ServicesLog.Message(SS.I.UserName + ": BackgrounExport Debug"); // //DataTableManager.AllowCaching = false; // DataTableManager.DebugBasics = true; // DataTableManager.DebugCaching = true; //} if (String.IsNullOrEmpty(templateFileName)) { templateFileName = "MobiusBackgroundExportEmailTemplate.htm"; } ServicesLog.Message("RunBackgroundExport started: UserObject id = " + objectIdString); string emailSubject = UmlautMobius.String + " background export results"; copiedToDestinationFile = false; try { if (!int.TryParse(objectIdString, out objId)) { throw new Exception("Invalid UserObjectId"); } UserObject uo = UserObjectDao.Read(objId); if (uo == null) { throw new Exception("UserObject not found"); } QueryManager qm = new QueryManager(); rf = ResultsFormat.Deserialize(uo.Content); if (rf == null) { throw new Exception("Failed to deserialize ResultsFormat"); } string clientFile = rf.OutputFileName; // ultimate file we want to go to rf.OutputFileName = GetServerFileName(rf, objId); // get file name to export to on server & use here temporarily qm.ResultsFormat = rf; rf.QueryManager = qm; q = QbUtil.ReadQuery(rf.QueryId); if (q == null) { throw new Exception("Failed to read query: " + rf.QueryId); } q.IncludeRootTableAsNeeded(); qm.Query = q; q.QueryManager = qm; emailSubject += " for query " + Lex.Dq(q.UserObject.Name); // include query name in subject ResultsFormatFactory rff = new ResultsFormatFactory(rf); rff.Build(); ResultsFormatter fmtr = new ResultsFormatter(qm); DataTableManager dtm = new DataTableManager(qm); dtm.BeginCaching(); // allow caching of DataTable dtm.PurgeDataTableWithoutWritingToCacheFile = true; // skip actual writing of cache since it won't be read back in qm.DataTableManager = dtm; qm.DataTable = DataTableManager.BuildDataTable(rf.Query); // build data table to receive data QueryExec qex = new QueryExec(rf); msg = qex.RunQuery3(rf, false, false); // do the export int compoundCount = 0; int rowCount = 0; QueryEngine qe = qex.QueryEngine; if (qe != null) { compoundCount = qm.DataTableManager.KeyCount; rowCount = qm.DataTableManager.TotalRowsTransferredToDataTable; // use this for accurate row count } dtm.EndCaching(); // close cache file (note: resets key/row counts) if (compoundCount <= 0 || rowCount <= 0) // any results { msg = "Query " + Lex.Dq(q.UserObject.Name) + " returned no results."; Email.Send( null, SS.I.UserInfo.EmailAddress, emailSubject, msg); return(msg); } if (ServerFile.CanWriteFileFromServiceAccount(clientFile)) { // copy to dest file if possible try { FileUtil.CopyFile(rf.OutputFileName, clientFile); copiedToDestinationFile = true; rf.OutputFileName = clientFile; } catch (Exception ex) { ServicesLog.Message("Error copying file from service account: " + clientFile + "\n" + DebugLog.FormatExceptionMessage(ex)); } } string viewCmd = "Retrieve Background Export " + uo.Id; msg = "RunBackgroundExport ended: UserObjectId = " + objectIdString; if (emailResultsHtml) { MailBackgroundExportResults( q, clientFile, rowCount, compoundCount, copiedToDestinationFile, viewCmd, SS.I.UserInfo.EmailAddress, emailSubject, templateFileName); ServicesLog.Message(msg); return(msg); } else // just fill in values & return { string html = ReadTemplateFile(templateFileName); html = SubstituteBackgroundExportParameters( html, "", rf.OutputFileName, rowCount, compoundCount, copiedToDestinationFile, viewCmd); ServicesLog.Message(msg); return(html); } } catch (Exception ex) { if (alertId > 0) { msg += "Alert: " + alertId + " "; } msg += "RunBackgroundExport exception: BackgroundExportId = " + objectIdString + ",\r\n" + DebugLog.FormatExceptionMessage(ex); Email.Send( null, SS.I.UserInfo.EmailAddress, emailSubject, msg); ServicesLog.Message(msg); return(msg); } }
/// <summary> /// Get the ResultsFormat and Query associated with the supplied BackgroundExport UserObject /// </summary> /// <param name="uo">BackgroundExport UserObject</param> /// <param name="q"></param> /// <param name="rf"></param> public static void GetAssociatedResultsFormatandQuery( UserObject uo, out ResultsFormat rf, out Query q) { q = null; // Alert a = null; rf = null; if (uo.Type == UserObjectType.BackgroundExport) { // background export containing ResultsFormat in content rf = ResultsFormat.Deserialize(uo.Content); q = rf.Query = QbUtil.ReadQuery(rf.QueryId); if (q == null) { throw new Exception("Failed to read query: " + rf.QueryId); } // a = Alert.GetExistingAlertForQuery(rf.QueryId); } #if false else if (uo.Type == UserObjectType.Alert) { // alert possibly containing ExportOptions for export of full results to file a = Alert.Deserialize(uo); q = QbUtil.ReadQuery(a.QueryObjId); if (q == null) { throw new Exception("Failed to read query: " + a.QueryObjId); } rf = a.ExportParms; if (rf == null) { throw new Exception("ResultsFormat not defined"); } rf.Query = q; rf.RunInBackground = true; } else if (uo.Type == UserObjectType.CnList) { // Compound Id list from alert run, description contains queryId string[] sa = uo.Description.Split('\t'); int qId = Int32.Parse(sa[0]); q = QbUtil.ReadQuery(qId); if (q == null) { throw new Exception("Failed to read query: " + rf.QueryId); } a = Alert.GetExistingAlertForQuery(qId); if (a == null) { throw new Exception("Alert not found for query: " + qId); } rf = a.ExportParms; if (rf == null) { throw new Exception("ResultsFormat not defined"); } rf.Query = q; rf.RunInBackground = true; } #endif else { throw new Exception("Invalid UserObject type: " + uo.Type.ToString()); } return; }
/// <summary> /// Deserialize an alert header in a user object /// </summary> /// <param name="uo"></param> /// <returns></returns> private static Alert DeserializeHeader( UserObject uo) { Alert alert = new Alert(); alert.Id = uo.Id; string serializedForm = uo.Description; if (String.IsNullOrEmpty(serializedForm)) { return(alert); } if (!Lex.Contains(serializedForm, "<Alert")) { return(DeserializeHeaderOld(uo)); } XmlMemoryStreamTextReader mstr = new XmlMemoryStreamTextReader(serializedForm); XmlTextReader tr = mstr.Reader; tr.Read(); // get CondFormat element tr.MoveToContent(); if (!Lex.Eq(tr.Name, "Alert")) { throw new Exception("\"Alert\" element not found"); } XmlUtil.GetStringAttribute(tr, "Owner", ref alert.Owner); XmlUtil.GetIntAttribute(tr, "QueryObjId", ref alert.QueryObjId); XmlUtil.GetIntAttribute(tr, "Interval", ref alert.Interval); XmlUtil.GetStringAttribute(tr, "MailTo", ref alert.MailTo); XmlUtil.GetDateAttribute(tr, "LastCheck", ref alert.LastCheck); XmlUtil.GetIntAttribute(tr, "LastCheckElapsedTime", ref alert.LastCheckExecutionTime); XmlUtil.GetIntAttribute(tr, "NewCompounds", ref alert.NewCompounds); XmlUtil.GetIntAttribute(tr, "ChangedCompounds", ref alert.ChangedCompounds); XmlUtil.GetIntAttribute(tr, "NewRows", ref alert.NewRows); XmlUtil.GetIntAttribute(tr, "TotalRows", ref alert.TotalRows); XmlUtil.GetIntAttribute(tr, "TotalCompounds", ref alert.TotalCompounds); XmlUtil.GetBoolAttribute(tr, "CheckTablesWithCriteriaOnly", ref alert.CheckTablesWithCriteriaOnly); XmlUtil.GetDateAttribute(tr, "LastNewData", ref alert.LastNewData); XmlUtil.GetBoolAttribute(tr, "HighlightChangedCompounds", ref alert.HighlightChangedCompounds); XmlUtil.GetBoolAttribute(tr, "RunImmediately", ref alert.RunImmediately); XmlUtil.GetStringAttribute(tr, "Days", ref alert.Days); XmlUtil.GetIntAttribute(tr, "DayInterval", ref alert.DayInterval); XmlUtil.GetDateAttribute(tr, "StartTime", ref alert.StartTime); alert.StartTime = GetLocalFromEasternStandardTime(alert.StartTime); XmlUtil.GetStringAttribute(tr, "Pattern", ref alert.Pattern); if (string.IsNullOrEmpty(alert.Pattern)) { alert.Pattern = PatternEnum.Daily.ToString(); } if (!tr.IsEmptyElement) { tr.Read(); // move to Export options or end of Alert element tr.MoveToContent(); if (tr.NodeType == XmlNodeType.Element && Lex.Eq(tr.Name, "ResultsFormat")) { alert.ExportParms = ResultsFormat.Deserialize(tr); tr.Read(); // get Alert end element tr.MoveToContent(); } if (!Lex.Eq(tr.Name, "Alert") || tr.NodeType != XmlNodeType.EndElement) { throw new Exception("Alert.Deserialize - Expected Alert end element"); } } mstr.Close(); double daysSinceLastCheckwithDecimal = (alert.LastCheck == DateTime.MinValue) ? alert.Interval : (DateTime.Now - alert.LastCheck).TotalDays; double daysSinceNewDatawithDecimal = (alert.LastNewData == DateTime.MinValue) ? 0 : (DateTime.Now - alert.LastNewData).TotalDays; //Round these calculations so .5 days will become 1 day, effectively 12 hours or more will count as a whole day. alert.DaysSinceLastCheck = (int)Math.Round(daysSinceLastCheckwithDecimal, MidpointRounding.AwayFromZero); alert.DaysSinceNewData = (int)Math.Round(daysSinceNewDatawithDecimal, MidpointRounding.AwayFromZero); //DebugLog.Message("Alert: " + alert.Id + " daysSinceLastCheckwithDecimal: " + daysSinceLastCheckwithDecimal); //DebugLog.Message("Alert: " + alert.Id + " new DaysSinceLastCheck: " + alert.DaysSinceLastCheck); //DebugLog.Message("Alert: " + alert.Id + " daysSinceNewDatawithDecimal: " + daysSinceNewDatawithDecimal); //DebugLog.Message("Alert: " + alert.Id + " DaysSinceNewData: " + alert.DaysSinceNewData); return(alert); }