private bool UpdateXrecord(Transaction t, DBDictionary nod, string xKey, ResultBuffer resbuf) { string errMessage = string.Empty; using (new WriteEnabler(_document, nod)) { if (nod.IsWriteEnabled) { if (nod.Contains(xKey)) { ObjectId oid = nod.GetAt(xKey); using (Xrecord xrec = t.GetObject <Xrecord>(oid, OpenMode.ForRead)) { if (xrec != null) { using (WriteEnabler we_xrec = new WriteEnabler(_document, xrec)) { if (xrec.IsWriteEnabled) { xrec.Data = resbuf; return(true); } errMessage = string.Format("Could not open Xrecord '{0}' for write.", xKey); } } errMessage = string.Format("Something went wrong with opening Xrecord '{0}'.", xKey); } } else { using (Xrecord xrec = new Xrecord()) { if (xrec != null) { xrec.Data = resbuf; nod.SetAt(xKey, xrec); t.AddNewlyCreatedDBObject(xrec, true); return(true); } errMessage = string.Format("Could not open Xrecord '{0}' for write.", xKey); } } } else { errMessage = string.Format("Could not open Named Objects Dictionary for write to update Xrecord '{1}'.", xKey); throw new XRecordHandlerException(errMessage, ErrorCode.NodNotFound); } } if (!string.IsNullOrEmpty(errMessage)) { throw new XRecordHandlerException(errMessage); } return(false); }
/// <summary> /// /// </summary> /// <param name="entityId"></param> /// <param name="xKey"></param> /// <param name="resbuf"></param> /// <returns></returns> public bool UpdateEntityXrecord(ObjectId entityId, string xKey, ResultBuffer resbuf) { string errMessage = string.Empty; bool result = false; try { Common.UsingTransaction(_document, tr => { Transaction t = tr.Transaction; using (Entity ent = t.GetObject <Entity>(entityId, OpenMode.ForRead)) { if (ent != null && !ent.IsErased) { using (WriteEnabler we_ent = new WriteEnabler(_document, ent)) { if (ent.IsWriteEnabled) { if (ent.ExtensionDictionary == null) { ent.CreateExtensionDictionary(); } DBDictionary nod = t.GetObject <DBDictionary>(ent.ExtensionDictionary, OpenMode.ForRead); result = UpdateXrecord(t, nod, xKey, resbuf); } else { throw new XRecordHandlerException("Couldn't open entity for write."); } } } else { errMessage = string.Format("Object is not an entity"); throw new XRecordHandlerException(errMessage, ErrorCode.NotAnEntity); } } }); return(result); } catch (XRecordHandlerException) { throw; } catch (System.Exception ex) { errMessage = string.Format("An unexpected error occured while updating Xrecord '{1}'.", xKey); throw new XRecordHandlerException(errMessage, ex); } }
/// <summary> /// Gets a Named Objects Dictionary with the given key /// If the collection does not contain the key, it will create a new Named Objects Dictionary instance. /// </summary> /// <param name="key">Name of the Named objects Dictionary</param> /// <returns>DBDictionary</returns> public DBDictionary GetNamedObjectsDictionary(string key) { DBDictionary nod = null; // Named Objects Dictionary DBDictionary nod_collection = null; // Named Objects Dictionary Collection try { _document.StartTransaction(tr => { Transaction t = tr.Transaction; nod_collection = t.GetObject <DBDictionary>(_nodId, OpenMode.ForRead); if (nod_collection.Contains(key)) { ObjectId oid = nod_collection.GetAt(key); nod = t.GetObject <DBDictionary>(oid, OpenMode.ForRead); } else { using (WriteEnabler we = new WriteEnabler(nod_collection)) { if (nod_collection.IsWriteEnabled) { nod = new DBDictionary(); nod_collection.SetAt(key, nod); t.AddNewlyCreatedDBObject(nod, true); } } } }); } catch (Exception ex) { string err_message = string.Format("DBDictionary named: '{0}' could not be found.", key); throw new XRecordHandlerException(err_message, ex); } return(nod); }