bool Save(bool prompt) { UserObject uo2; if (!IsValidCalcField()) { return(false); } if (!GetCalcFieldForm()) { return(false); } if (prompt) { uo2 = UserObjectSaveDialog.Show("Save Calculated Field Definition", UoIn); if (uo2 == null) { return(false); } } else { uo2 = UoIn.Clone(); } if (!UserObjectUtil.UserHasWriteAccess(uo2)) { // is the user authorized to save this? MessageBoxMx.ShowError("You are not authorized to save this calculated field"); return(false); } SessionManager.DisplayStatusMessage("Saving calculated field..."); string content = CalcField.Serialize(); uo2.Content = content; //need the name of the folder to which the object will be saved MetaTreeNode targetFolder = UserObjectTree.GetValidUserObjectTypeFolder(uo2); if (targetFolder == null) { MessageBoxMx.ShowError("Failed to save your calculated field."); return(false); } UserObjectDao.Write(uo2); string tName = "CALCFIELD_" + uo2.Id.ToString(); QbUtil.UpdateMetaTableCollection(tName); MainMenuControl.UpdateMruList(tName); string title = "Edit Calculated Field - " + uo2.Name; Text = title; UoIn = uo2.Clone(); // now becomes input object SessionManager.DisplayStatusMessage(""); return(true); }
/// <summary> /// Set flag to try to browse results upon open in both the saved query and any currently open query /// </summary> /// <param name="qid"></param> void SetBrowseSavedResultsUponOpenForQuery( int qid, bool value) { if (qid <= 0) { return; } UserObject uo = UserObjectDao.Read(qid); // update any saved version of query if (uo == null || uo.Type != UserObjectType.Query) { return; } Query q = Query.Deserialize(uo.Content); q.BrowseSavedResultsUponOpen = value; uo.Content = q.Serialize(); UserObjectDao.Write(uo, uo.Id); int di = QueriesControl.Instance.GetQueryIndexByUserObjectId(qid); // update any in-memory version of query if (di < 0) { return; } q = QueriesControl.Instance.GetQuery(di); q.BrowseSavedResultsUponOpen = value; return; // todo }
/// <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; uo.Owner = Security.UserName; // assume current user if (!uo.HasDefinedParentFolder && (Lex.Eq(uo.Name, "Current") || Lex.Eq(uo.Name, "Previous") || Lex.Eq(uo.Name, "Criteria List"))) { uo.ParentFolder = UserObject.TempFolderName; uo.ParentFolderType = FolderTypeEnum.None; uo.Owner = Security.UserName; } else if (!uo.HasDefinedParentFolder) { throw new Exception("No parent folder for list"); } uo.Content = content; uo.Count = list.Count; UserObjectDao.Write(uo); return(list.Count); }
/// <summary> /// Serialize & write the alert to a UserObject. /// Basic information is serialized into the Description /// column and the results of the most recent check are /// serialized into Content. /// </summary> /// <returns></returns> public int Write() { UserObject alertUserObject; if (ExistingAlert) { alertUserObject = UserObjectDao.ReadHeader(Id); //alertUserObject.UpdateDateTime = DateTime.Now; alertUserObject.Description = SerializeHeader(); alertUserObject.Content = SerializeResults(); UserObjectDao.Write(alertUserObject, alertUserObject.Id); } else { alertUserObject = new UserObject(UserObjectType.Alert) { Id = Id, Owner = Owner, Name = "Alert_" + QueryObjId }; alertUserObject.Description = SerializeHeader(); alertUserObject.Content = SerializeResults(); UserObjectDao.Write(alertUserObject); Id = alertUserObject.Id; } return(Id); }
/// <summary> /// Save the SpotfireLink UserObject /// </summary> /// <param name="prompt"></param> /// <returns></returns> bool Save(bool prompt) { UserObject uo2; if (!IsValidSpotfireLink()) { return(false); } if (!GetSpotfireLinkForm()) { return(false); } if (prompt) { uo2 = UserObjectSaveDialog.Show("Save Spotfire link Definition", UoIn); if (uo2 == null) { return(false); } } else { uo2 = UoIn.Clone(); } uo2.Content = SpotfireViewProps.Serialize(); if (!UserObjectUtil.UserHasWriteAccess(uo2)) { // is the user authorized to save this? MessageBoxMx.ShowError("You are not authorized to save this Spotfire link"); return(false); } SessionManager.DisplayStatusMessage("Saving Spotfire link..."); //need the name of the folder to which the object will be saved MetaTreeNode targetFolder = UserObjectTree.GetValidUserObjectTypeFolder(uo2); if (targetFolder == null) { MessageBoxMx.ShowError("Failed to save your Spotfire link."); return(false); } UserObjectDao.Write(uo2); MainMenuControl.UpdateMruList(uo2.InternalName); string title = "Edit Spotfire Link - " + uo2.Name; Text = title; UoIn = uo2.Clone(); // now becomes input object SessionManager.DisplayStatusMessage(""); return(true); }
/// <summary> /// Create an annotation table given its metatable and user object /// </summary> /// <param name="mt"></param> /// <param name="uo"></param> public static void CreateAnnotationTable( MetaTable mt, UserObject uo) { uo.Type = UserObjectType.Annotation; // be sure type is set uo.Content = mt.Serialize(); // serialize the metatable to the user object UserObjectTree.GetValidUserObjectTypeFolder(uo); UserObjectDao.Write(uo); // write the user object MetaTableCollection.UpdateGlobally(mt); // update map with modified metatable return; }
/// <summary> /// Edit a new or existing calculated field /// </summary> /// <param name="uo">Existing UserObject with content or null to create a new CF</param> /// <returns></returns> public static CondFormat EditUserObject( UserObject uo = null) { if (uo == null) // prompt if not supplied { UserObject defaultUo = new UserObject(UserObjectType.CondFormat); defaultUo.ParentFolder = CondFormat.PredefinedCondFormatFolderName; uo = UserObjectOpenDialog.ShowDialog(UserObjectType.CondFormat, "Edit Conditional Formatting", defaultUo); } if (uo == null) { return(null); } uo = UserObjectDao.Read(uo); if (uo == null) { return(null); } CondFormat cf = CondFormat.Deserialize(uo.Content); if (cf == null) { return(null); } string title = "Conditional Formatting - " + uo.Name + " (" + uo.InternalName + ")"; CondFormat cf2 = Edit(cf, true, title); if (cf2 == null) { return(null); } uo.Content = cf2.Serialize(); UserObjectDao.Write(uo, uo.Id); if (MainMenuControl != null) { MainMenuControl.UpdateMruList(uo.InternalName); } CondFormat.UpdatePredefined(uo.InternalName, cf2); // update the dictionary of predefined CFs UpdateExistingCfReferences(uo, cf2); // update any references to this cf in the list of open queries return(cf); }
/// <summary> /// Update the list /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OkButton_Click(object sender, EventArgs e) { if (!Editable) { DialogResult = DialogResult.OK; return; } AccessControlList acl = PermissionsList.GetAcl(); acl.AddReadWriteUserItem(SS.I.UserName); // be sure current user is included Uo.ACL = acl.Serialize(); // just an ACL, no content Uo.AccessLevel = UserObjectAccess.ACL; UserObjectDao.Write(Uo, Uo.Id); UserGroups.UpdateInMemoryCollection(Uo.Name, acl); DialogResult = DialogResult.OK; }
/// <summary> /// Save list of hits and optional reference to a results file from a background query /// </summary> /// <param name="qm"></param> /// <param name="listOwner"></param> /// <returns></returns> public static UserObject SaveBackgroundQueryResultsReferenceObject( QueryManager qm, string listOwner, string resultsFileName) { Query q = qm.Query; DataTableManager dtm = qm.DataTableManager; UserObject cidListUo = new UserObject(UserObjectType.CnList); cidListUo.Owner = listOwner; cidListUo.Id = UserObjectDao.GetNextId(); cidListUo.Name = "List " + cidListUo.Id; // assign unique name cidListUo.Description = q.UserObject.Id + "\t" + // store query id DateTimeUS.ToString(DateTime.Now); // and a time stamp if (!Lex.IsNullOrEmpty(resultsFileName)) { cidListUo.Description += "\t" + resultsFileName; } SortOrder sortDirection = (q.KeySortOrder > 0) ? SortOrder.Ascending : SortOrder.Descending; ResultsSorter.SortKeySet(dtm.ResultsKeys, sortDirection); // sort properly StringBuilder sb = new StringBuilder(); foreach (string s in dtm.ResultsKeys) { // build comma separated list of numbers if (sb.Length > 0) { sb.Append("\r\n"); } sb.Append(s); } cidListUo.Content = sb.ToString(); cidListUo.Count = dtm.ResultsKeys.Count; UserObjectDao.Write(cidListUo, cidListUo.Id); // write list with supplied id return(cidListUo); }
/// <summary> /// Create a new conditional format user object /// </summary> /// <returns></returns> public static CondFormat CreateNewUserObject() { MetaColumnType mcType = MetaColumnType.Number; CondFormat cf = CreateAndInitializeCf(mcType); string title = "New Conditional Format"; CondFormat cf2 = Edit(cf, true, title); if (cf2 == null) { return(null); } UserObject uo = new UserObject(UserObjectType.CondFormat); // Predefined cond formats by default are owned by a single owner and stored in a single folder uo.Owner = SS.I.UserName; uo.ParentFolder = CondFormat.PredefinedCondFormatFolderName; uo.AccessLevel = UserObjectAccess.ACL; uo.ACL = AccessControlList.GetAdministratorGroupRwPublicReadAcl(SS.I.UserName); uo = UserObjectSaveDialog.Show("Save As", uo); if (uo == null) { return(null); } uo.Content = cf2.Serialize(); UserObjectDao.Write(uo, uo.Id); MessageBoxMx.Show("Conditional format " + uo.Name + " saved (" + uo.InternalName + ")"); // show internal name so it may be referenced in a MetaColumn definition if (MainMenuControl != null) { MainMenuControl.UpdateMruList(uo.InternalName); } return(cf); }
/// <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> /// Right-click commands on objects in a tree /// </summary> /// <param name="command">The command to process</param> /// <param name="mtn">MetaTreeNode</param> /// <param name="uo">Any associated user object</param> /// <param name="ctc">ContentsTreeControl</param> /// <returns></returns> public static bool ProcessCommonRightClickObjectMenuCommands( string command, MetaTreeNode mtn, UserObject[] uoArray, ContentsTreeControl ctc) { UserObject uo2; string txt; UserObject uo = null; if (uoArray != null && uoArray.Length == 1) { uo = uoArray[0]; } //if (mtn == null) //{ // MessageBoxMx.ShowError("Operation is not allowed for this Database Contents node."); // return true; //} if (Lex.Eq(command, "Cut")) { CopyCutDelete(command, uoArray, ctc, true, true); } else if (Lex.Eq(command, "Copy")) { CopyCutDelete(command, uoArray, ctc, true, false); } else if (Lex.Eq(command, "Paste")) { try { txt = Clipboard.GetText(); //uo2 = UserObject.Deserialize(txt); uoArray = Deserialize(txt); if (uoArray == null) { throw new Exception("Not a UserObject"); } //if (uo2 == null) throw new Exception("Not a UserObject"); } catch (Exception ex) { MessageBoxMx.ShowError("The clipboard does not contain a recognized user objects"); return(true); } foreach (var userObject in uoArray) { Progress.Show("Pasting " + userObject.Name + "...", UmlautMobius.String, false); Permissions.UpdateAclForNewOwner(userObject, userObject.Owner, SS.I.UserName); // fixup the ACL for the new owner userObject.Owner = SS.I.UserName; userObject.ParentFolder = mtn.Name; mtn = UserObjectTree.GetValidUserObjectTypeFolder(userObject); for (int ci = 0; ; ci++) // find a name that's not used { UserObject uo3 = UserObjectDao.ReadHeader(userObject); if (uo3 == null) { break; } if (ci == 0) { userObject.Name = "Copy of " + userObject.Name; } else if (ci == 1) { userObject.Name = Lex.Replace(userObject.Name, "Copy of ", "Copy (2) of "); } else { userObject.Name = Lex.Replace(userObject.Name, "Copy (" + ci + ") of ", "Copy (" + (ci + 1) + ") of "); } } UserObject userObjectFinal = null; if (UserObjectDao.ReadHeader(userObject.Id) != null) // create a deep clone if orignal object exists { userObjectFinal = DeepClone(userObject); } if (userObjectFinal == null) { userObjectFinal = userObject; } UserObjectDao.Write(userObjectFinal, userObjectFinal.Id); // write using the current id Progress.Hide(); } //if (ctc != null) // need to update form directly? // UserObjectTree.RefreshSubtreeInTreeControl(uo2, ctc); } else if (Lex.Eq(command, "Delete")) { CopyCutDelete(command, uoArray, ctc, false, true); } else if (Lex.Eq(command, "Rename")) { if (!UserHasWriteAccess(uo)) { MessageBoxMx.ShowError("You are not authorized to rename " + uo.Name); return(true); } string newName = InputBoxMx.Show("Enter the new name for " + uo.Name, "Rename", uo.Name); if (newName == null || newName == "" || newName == uo.Name) { return(true); } if (!IsValidUserObjectName(newName)) { MessageBoxMx.ShowError("The name " + newName + " is not valid."); return(true); } uo2 = uo.Clone(); uo2.Name = newName; uo2.Id = 0; // clear Id so not looked up by id if (!Lex.Eq(newName, uo.Name) && UserObjectDao.ReadHeader(uo2) != null) { MessageBoxMx.ShowError(newName + " already exists."); return(true); } uo2.Id = uo.Id; UserObjectDao.UpdateHeader(uo2); if (ctc != null) { UserObjectTree.UpdateObjectInTreeControl(uo, uo2, ctc); } } else if (Lex.Eq(command, "MakePublic") || Lex.Eq(command, "MakePrivate")) { UserObjectAccess newAccess; MetaTreeNode objFolder; if (!UserHasWriteAccess(uo)) { MessageBoxMx.ShowError("You are not authorized to make " + uo.Name + ((Lex.Eq(command, "MakePublic")) ? " public" : " private")); return(true); } if (Lex.Eq(command, "MakePublic")) { if (uo.ParentFolder == "DEFAULT_FOLDER") { MessageBoxMx.ShowError("Items in the Default Folder cannot be made public"); return(true); } else { //check the folder id parentage to ensure that the current folder isn't a subfolder of the default folder if (UserObjectTree.FolderNodes.ContainsKey(uo.ParentFolder)) { objFolder = UserObjectTree.FolderNodes[uo.ParentFolder]; while (objFolder != null) { if (objFolder.Target == "DEFAULT_FOLDER") { MessageBoxMx.ShowError("Items in the Default Folder cannot be made public"); return(true); } objFolder = objFolder.Parent; } } else { throw new Exception("Failed to recognize the folder that this object is in!"); } } newAccess = UserObjectAccess.Public; } else { //user folders cannot be made private if they contain public children if (uo.Type == UserObjectType.Folder) { objFolder = UserObjectTree.BuildNode(uo); if (UserObjectTree.FolderNodes.ContainsKey(objFolder.Target)) { objFolder = UserObjectTree.FolderNodes[objFolder.Target]; for (int i = 0; i < objFolder.Nodes.Count; i++) { MetaTreeNode currentChild = (MetaTreeNode)objFolder.Nodes[i]; } } } newAccess = UserObjectAccess.Private; } uo2 = UserObjectDao.Read(uo); if (uo2 == null) { return(true); } if (uo2.AccessLevel == newAccess) { return(true); // no change } uo2.AccessLevel = newAccess; UserObjectDao.UpdateHeader(uo2); if (ctc != null) // need to update form directly? { UserObjectTree.RefreshSubtreeInTreeControl(uo2, ctc); } return(true); } else if (Lex.Eq(command, "ChangeOwner")) { string newOwner = InputBoxMx.Show("Enter the user name of the person to transfer ownership of " + Lex.AddDoubleQuotes(uo.Name) + " to:", "Change Owner", ""); if (Lex.IsNullOrEmpty(newOwner)) { return(true); } string result = UserObjectUtil.ChangeOwner(uo.Id, newOwner); if (!Lex.IsNullOrEmpty(result)) { MessageBoxMx.Show(result); } return(true); } else if (Lex.Eq(command, "Permissions")) // set object permissions { PermissionsDialog.Show(uo); return(true); } else if (Lex.Eq(command, "ViewSource")) { uo = UserObjectDao.Read(uo); if (uo == null) { return(true); } string ext = ".txt"; // default extension if (uo.Type == UserObjectType.Query || uo.Type == UserObjectType.Annotation) { ext = ".xml"; } string cFile = ClientDirs.TempDir + @"\Source" + ext; StreamWriter sw = new StreamWriter(cFile); sw.Write(uo.Content); sw.Close(); SystemUtil.StartProcess(cFile); // show it } else { return(false); // command not recognized } return(true); // command recognized and processed }
/// <summary> /// Perform a deep clone of an annotation table UserObject including the underlying data /// </summary> /// <param name="uo"></param> /// <returns></returns> public static UserObject DeepClone(UserObject uo) { UserObject uo2 = uo.Clone(); long oldCode, newCode; // Create a copy of the user object including remapping of the codes AnnotationDao dao = new AnnotationDao(); MetaTable mt = MetaTable.Deserialize(uo.Content); // deserialize metatable xml int newMethodCode = UserObjectDao.GetNextId(); uo2.Id = newMethodCode; // store in UserObject id as well mt.Code = newMethodCode.ToString(); mt.Name = "ANNOTATION_" + mt.Code; mt.Label = uo.Name; Dictionary <long, long> codeMap = new Dictionary <long, long>(); foreach (MetaColumn mc in mt.MetaColumns) { if (!Lex.IsNullOrEmpty(mc.ResultCode)) { if (!long.TryParse(mc.ResultCode, out oldCode)) { continue; } newCode = dao.GetNextIdLong(); codeMap[oldCode] = newCode; mc.Name = "R_" + newCode; mc.ResultCode = newCode.ToString(); } } // Write import state if checking for updates if (mt.ImportParms != null && mt.ImportParms.CheckForFileUpdates) { UserObject udisUo = new UserObject(UserObjectType.ImportState, uo2.Owner, mt.Name); UserDataImportState udis = new UserDataImportState(); udis.UserDatabase = false; // indicate annotation table udis.UserDataObjectId = uo2.Id; // store id of annotation table user object udis.ClientFile = mt.ImportParms.FileName; udis.CheckForFileUpdates = mt.ImportParms.CheckForFileUpdates; udis.ClientFileModified = mt.ImportParms.ClientFileModified; udis.FileName = mt.ImportParms.FileName; udisUo.Description = udis.Serialize(); // serialize to description UserObjectDao.Write(udisUo); } // Copy the data dao.BeginTransaction(); dao.BufferInserts(true); dao.OpenReader(uo.Id); Dictionary <long, long> groupMap = new Dictionary <long, long>(); int readCount = 0; while (true) { AnnotationVo vo = dao.Read(); if (vo == null) { break; } if (!codeMap.ContainsKey(vo.rslt_typ_id)) { continue; } vo.rslt_typ_id = codeMap[vo.rslt_typ_id]; // map the result code vo.rslt_id = dao.GetNextIdLong(); // new result id vo.mthd_vrsn_id = newMethodCode; if (!groupMap.ContainsKey(vo.rslt_grp_id)) // map the group id { groupMap[vo.rslt_grp_id] = dao.GetNextIdLong(); } vo.rslt_grp_id = groupMap[vo.rslt_grp_id]; dao.Insert(vo); readCount++; if (readCount % 1000 == 0) { dao.ExecuteBufferedInserts(); dao.Commit(); } } dao.ExecuteBufferedInserts(); dao.Commit(); dao.Dispose(); uo2.Count = groupMap.Count; // update the count uo2.CreationDateTime = uo2.UpdateDateTime = DateTime.Now; uo2.Content = mt.Serialize(); return(uo2); }
/// <summary> /// Thread to check to see if any imports need to started /// </summary> public void CheckForImportFileUpdatesThreadMethod(Object CheckAll) { // Check each ImportState user object for the user to see if any imports need to be started. // If any are found then start a new hidden Mobius client & server to upload the file(s) // and start an import user data process for each one. UserDataImportState udis; List <UserObject> imps = new List <UserObject>(); UserCmpndDbDao udbs = new UserCmpndDbDao(); int t0 = TimeOfDay.Milliseconds(); bool checkAllImportFiles = (bool)CheckAll; if (checkAllImportFiles) { imps = UserObjectDao.ReadMultiple(UserObjectType.ImportState, false); } else { imps = UserObjectDao.ReadMultiple(UserObjectType.ImportState, SS.I.UserName, false, false); } int t1 = TimeOfDay.Milliseconds() - t0; if (imps.Count == 0) { return; } // return ""; // debug int i1 = 0; while (i1 < imps.Count) { // pare list down do those needing updating UserObject uo = imps[i1]; try { udis = UserDataImportState.Deserialize(uo); } catch (Exception ex) { imps.RemoveAt(i1); continue; } if (udis.CheckForFileUpdates && ((checkAllImportFiles == true && udis.ClientFile.Substring(0, 1) == "\\" && FileUtil.Exists(udis.ClientFile)) || checkAllImportFiles == false)) { DateTime clientFileModDt = FileUtil.GetFileLastWriteTime(udis.ClientFile); // get client file mod date if (clientFileModDt == DateTime.MinValue || // skip if client file not found or udis.ImportIsRunning || // import is already running ((clientFileModDt - udis.ClientFileModified).TotalSeconds < 1 && // no change in client file mod date and !udis.ImportHasFailed)) // prev load attempt hasn't failed { imps.RemoveAt(i1); continue; } udis.ClientFileModified = clientFileModDt; // write the updated file date uo.Description = udis.Serialize(); UserObjectDao.Write(uo); } else // running or failed manual background import { if (udis.ImportHasFailed) // delete if failed { bool deleted = UserObjectDao.Delete(udis.Id); udbs.LogMessage("Deleted ImportState object for failed manual background import on " + uo.Name); } imps.RemoveAt(i1); // don't consider further here continue; } i1++; } //write a debug message and return udbs.LogMessage(string.Format("Found {0} annotation files that could be updated by the {1} account", imps.Count, SS.I.UserName)); int t2 = TimeOfDay.Milliseconds() - t0; if (imps.Count == 0) { return; } // Upload the file to the server and start a background process to update the annotation table foreach (UserObject uo2 in imps) { try { udis = UserDataImportState.Deserialize(uo2); string internalUoName = "Annotation_" + uo2.Id; string exportDir = ServicesIniFile.Read("BackgroundExportDirectory"); string serverFileName = // location for file on server exportDir + @"\" + internalUoName + Path.GetExtension(udis.FileName); ServerFile.CopyToServer(udis.FileName, serverFileName); string command = "ImportUserData " + serverFileName + ", " + internalUoName; CommandLine.StartBackgroundSession("ImportUserData " + serverFileName + ", " + uo2.Name); udbs.LogMessage("Auto-upload for ImportState ObjId = " + ", " + uo2.Id + ", Name = " + uo2.Name + ", Desc = " + uo2.Description); } catch (Exception ex) { try { udbs.LogMessage("Auto-upload exception ImportState ObjId = " + uo2.Id + ", Name = " + uo2.Name + ", Desc = " + uo2.Description + "\n" + DebugLog.FormatExceptionMessage(ex)); } catch (Exception ex2) { ex2 = ex2; } continue; } } Progress.Hide(); int t3 = TimeOfDay.Milliseconds() - t0; return; }