/// <summary> /// Parses the specified XML document using the Standards DevKit. /// </summary> /// <typeparam name="T">The data object type.</typeparam> /// <param name="element">The XML element.</param> /// <param name="removeNaN">if set to <c>true</c> remove NaN elements.</param> /// <returns>The data object instance.</returns> /// <exception cref="WitsmlException"></exception> public static T Parse <T>(XElement element, bool removeNaN = true) { _log.DebugFormat("Deserializing XML element for type: {0}", typeof(T).FullName); try { // Create a copy of the element to prevent loss of NaN elements var xml = removeNaN ? RemoveNaNElements <T>(new XElement(element)) : element.ToString(); return(EnergisticsConverter.XmlToObject <T>(xml)); } catch (WitsmlException) { throw; } catch (Exception ex) { throw new WitsmlException(ErrorCodes.InputTemplateNonConforming, ex); } }
/// <summary> /// loadInstance is to handle the energistcs data instance. /// </summary> /// <param name="epcObject"></param> /// <returns></returns> public static Object loadInstance(EPCObject epcObject) { if (epcObject == null) { return(null); } ObjectHandle demoObject = createInance(epcObject); if (demoObject != null) { string xmlcontent = EPCReader.createInstance().getEPCFIleText(epcObject.PackageFile, epcObject.Uri.ToString()); // only load the xml , we can confirm the uuid from real document , otherwise it derieves from the filename. epcObject.Uuid = getUuid(epcObject.EPCObjectType, xmlcontent); // convert the stream to the energistics object. Type type = demoObject.Unwrap().GetType(); Object partEnergisticsObject = EnergisticsConverter.XmlToObject(type, xmlcontent, Encoding.UTF8); return(partEnergisticsObject); } else { return(null); } }
public void WitsmlParser_RemoveEmptyElements_Removes_Empty_Elements_From_Elements() { var xdoc = WitsmlParser.Parse(_wellXml); Assert.IsNotNull(xdoc.Root); var elemPurposeWell = new XElement(xdoc.Root.GetDefaultNamespace() + "purposeWell"); var elemStatusWell = new XElement(xdoc.Root.GetDefaultNamespace() + "statusWell", "plugged and abandoned"); var elemGroundElevation = new XElement(xdoc.Root.GetDefaultNamespace() + "groundElevation"); var nil = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance").GetName("nil"); elemGroundElevation.Add(new XAttribute(nil, true)); xdoc.Root.Elements().FirstOrDefault()?.Add(elemPurposeWell); xdoc.Root.Elements().FirstOrDefault()?.Add(elemStatusWell); xdoc.Root.Elements().FirstOrDefault()?.Add(elemGroundElevation); WitsmlParser.RemoveEmptyElements(xdoc.Root); var welllist = EnergisticsConverter.XmlToObject <WellList>(xdoc.Root.ToString()); Assert.IsNotNull(welllist.Well[0].StatusWell); Assert.IsNull(welllist.Well[0].PurposeWell); Assert.IsNull(welllist.Well[0].GroundElevation); }
private static void AssertAxisDefintion(Log expectedLog, LogCurveInfo curve, string xmlOut, int expectedCurveCount = 2) { var logList = EnergisticsConverter.XmlToObject <LogList>(xmlOut); Assert.IsNotNull(logList); Assert.IsTrue(logList.Log.Count > 0); var log = logList.Log.FirstOrDefault(x => x.Uid == expectedLog.Uid); Assert.IsNotNull(log); Assert.AreEqual(expectedCurveCount, log.LogCurveInfo.Count); var logCurveInfo = log.LogCurveInfo.FirstOrDefault(x => x.Mnemonic == curve.Mnemonic); Assert.IsNotNull(logCurveInfo); Assert.IsTrue(logCurveInfo.AxisDefinition.Count > 0); var axisDef = logCurveInfo.AxisDefinition.FirstOrDefault(); Assert.IsNotNull(axisDef); Assert.AreEqual(curve.AxisDefinition[0].Uid, axisDef.Uid); Assert.AreEqual(curve.AxisDefinition[0].Count, axisDef.Count); Assert.AreEqual(curve.AxisDefinition[0].DoubleValues, axisDef.DoubleValues); }
/// <summary> /// Serialize WITSML query results to XML and remove empty elements and xsi:nil attributes. /// </summary> /// <param name="obj">The object.</param> /// <param name="nilOnly">if set to <c>true</c> only elements with nil="true" are removed.</param> /// <param name="removeTypePrefix">if set to <c>true</c> any type prefix will be removed.</param> /// <returns>The serialized XML string.</returns> public static string ToXml(object obj, bool nilOnly = false, bool removeTypePrefix = false) { _log.Debug("Serializing object to XML."); if (obj == null) { return(string.Empty); } var xml = EnergisticsConverter.ObjectToXml(obj); var xmlDoc = Parse(xml); var root = xmlDoc.Root; if (root == null) { return(string.Empty); } var elementName = ObjectTypes.GetElementNameOverride(obj.GetType()); root = root.UpdateRootElementName(obj.GetType(), removeTypePrefix, elementNameOverride: elementName); if (ObjectTypes.GetVersion(root).StartsWith("1.")) { foreach (var element in root.Elements()) { RemoveEmptyElements(element, nilOnly); } } else { RemoveEmptyElements(root, nilOnly); } return(root.ToString(SaveOptions.OmitDuplicateNamespaces)); }
public void Log141Adapter_AddToStore_Can_Add_Data_Chunk_Exceeds_MongoDb_Document_Size() { var response = _devKit.Add <WellList, Well>(_well); // Adjust Points and Nodes for large file WitsmlSettings.LogMaxDataPointsAdd = 5000000; WitsmlSettings.LogMaxDataNodesAdd = 15000; WitsmlSettings.LogMaxDataPointsGet = 5000000; WitsmlSettings.LogMaxDataNodesGet = 15000; _wellbore.UidWell = response.SuppMsgOut; response = _devKit.Add <WellboreList, Wellbore>(_wellbore); var xmlfile = Path.Combine(_testDataDir, string.Format(_exceedFileFormat, "log")); var xmlin = File.ReadAllText(xmlfile); var logList = EnergisticsConverter.XmlToObject <LogList>(xmlin); Assert.IsNotNull(logList); var log = logList.Log.FirstOrDefault(); Assert.IsNotNull(log); log.Uid = null; log.UidWell = _wellbore.UidWell; log.UidWellbore = response.SuppMsgOut; log.NameWell = _well.Name; log.NameWellbore = _wellbore.Name; var logDataAdded = log.LogData.FirstOrDefault(); Assert.IsNotNull(logDataAdded); response = _devKit.Add <LogList, Log>(log); Assert.AreEqual((short)ErrorCodes.Success, response.Result); var uidLog = response.SuppMsgOut; log.Uid = uidLog; var uri = log.GetUri(); // Query Channel Data Chunk var filter = MongoDbUtility.BuildFilter <ChannelDataChunk>("Uri", uri.ToString()); var database = _provider.GetDatabase(); var collection = database.GetCollection <ChannelDataChunk>("channelDataChunk"); var chunks = collection.Find(filter).ToList(); Assert.IsTrue(chunks.Count > 0); // Query Mongo File var fileChunks = chunks.Where(c => string.IsNullOrEmpty(c.Data)).ToList(); Assert.IsTrue(fileChunks.Count > 0); var bucket = new GridFSBucket(database, new GridFSBucketOptions { BucketName = ChannelDataChunkAdapter.BucketName, ChunkSizeBytes = WitsmlSettings.ChunkSizeBytes }); foreach (var fc in fileChunks) { Assert.IsNull(fc.Data); var mongoFileFilter = Builders <GridFSFileInfo> .Filter.Eq(fi => fi.Metadata[ChannelDataChunkAdapter.FileName], fc.Uid); var mongoFile = bucket.Find(mongoFileFilter).FirstOrDefault(); Assert.IsNotNull(mongoFile); } // Query Log var query = new Log { Uid = uidLog, UidWell = log.UidWell, UidWellbore = log.UidWellbore }; var results = _devKit.Query <LogList, Log>(query, optionsIn: OptionsIn.ReturnElements.All); Assert.AreEqual(1, results.Count); var result = results.First(); Assert.IsNotNull(result); var logDataReturned = result.LogData.FirstOrDefault(); Assert.IsNotNull(logDataReturned); Assert.AreEqual(logDataAdded.Data.Count, logDataReturned.Data.Count); }
public WitsmlQueryParser Parser <T>(T entity, string options = null) { var document = WitsmlParser.Parse(EnergisticsConverter.ObjectToXml(entity)); return(new WitsmlQueryParser(document.Root, ObjectTypes.GetObjectType <T>(), options)); }
public void Log141Adapter_DeleteFromStore_Can_Delete_Log_With_Data_File() { // Add log AddParents(); // Adjust Points and Nodes for large file WitsmlSettings.LogMaxDataPointsAdd = 5000000; WitsmlSettings.LogMaxDataNodesAdd = 15000; WitsmlSettings.LogMaxDataPointsGet = 5000000; WitsmlSettings.LogMaxDataNodesGet = 15000; var xmlfile = Path.Combine(_testDataDir, string.Format(_exceedFileFormat, "log")); var xmlin = File.ReadAllText(xmlfile); var logList = EnergisticsConverter.XmlToObject <LogList>(xmlin); Assert.IsNotNull(logList); var log = logList.Log.FirstOrDefault(); Assert.IsNotNull(log); log.Uid = _devKit.Uid(); log.UidWell = _well.Uid; log.UidWellbore = _wellbore.Uid; log.NameWell = _well.Name; log.NameWellbore = _wellbore.Name; var logDataAdded = log.LogData.FirstOrDefault(); Assert.IsNotNull(logDataAdded); AddLog(log); // Query log var result = GetLog(log); var logDatas = result.LogData; Assert.IsNotNull(logDatas); var logData = logDatas.FirstOrDefault(); Assert.IsNotNull(logData); Assert.AreEqual(logDataAdded.Data.Count, logData.Data.Count); var uri = log.GetUri(); // Query Data Chunk var chunks = GetDataChunks(uri); Assert.IsTrue(chunks.Count > 0); // Query Mongo File var fileChunks = chunks.Where(c => string.IsNullOrEmpty(c.Data)).ToList(); Assert.IsTrue(fileChunks.Count > 0); var database = _provider.GetDatabase(); foreach (var fc in fileChunks) { Assert.IsNull(fc.Data); var mongoFile = GetMongoFile(database, fc.Uid); Assert.IsNotNull(mongoFile); } var fileUids = fileChunks.Select(fc => fc.Uid).ToList(); // Delete log DeleteLog(log, string.Empty); // Assert log is deleted var query = _devKit.CreateLog(log.Uid, null, log.UidWell, null, log.UidWellbore, null); var results = _devKit.Query <LogList, Log>(query, optionsIn: OptionsIn.ReturnElements.All); Assert.AreEqual(0, results.Count); // Assert Channel Data Chunk is deleted chunks = GetDataChunks(uri); Assert.IsTrue(chunks.Count == 0); // Assert Mongo file is deleted foreach (var uid in fileUids) { var mongoFile = GetMongoFile(database, uid); Assert.IsNull(mongoFile); } }
public async Task CascadedDelete141Tests_Can_Parallel_Delete_Well_With_Populated_Wellbores() { // Number of objects to generate var numOfObjects = 5; var wellList = new List <Well>(); for (var i = 0; i < numOfObjects; i++) { wellList.Add(new Well() { Uid = DevKit.Uid(), Name = DevKit.Name(), TimeZone = "-06:00" }); } // Add wells wellList.ForEach(x => DevKit.AddAndAssert(x)); foreach (var well in wellList) { // Create logs var logs = DevKit.GenerateLogs(well.Uid, well.Name, LogIndexType.measureddepth, numOfObjects); // Create trajectories var trajectories = DevKit.GenerateTrajectories(well.Uid, well.Name, numOfObjects); // Add 5 wellbores with data objects for (var i = 0; i < numOfObjects; i++) { var wellbore = new Wellbore() { Uid = DevKit.Uid(), UidWell = well.Uid, Name = DevKit.Name(), NameWell = well.Name }; DevKit.AddAndAssert(wellbore); DevKit.AddListOfLogsToWellbore(logs, wellbore); DevKit.AddListOfTrajectoriesToWellbore(trajectories, wellbore); } } // Delete each well in parallel var taskList = new List <Task>(); wellList.ForEach(x => { taskList.Add(new Task(() => { var deleteWell = new Well { Uid = x.Uid }; var result = DevKit.Delete <WellList, Well>(deleteWell, ObjectTypes.Well, string.Empty, OptionsIn.CascadedDelete.True); Assert.IsNotNull(result); Assert.AreEqual(result.Result, 1); // Ensure well does not exist anymore DevKit.GetAndAssert(deleteWell, false); })); }); taskList.ForEach(x => x.Start()); await Task.WhenAll(taskList); wellList.ForEach(x => { var wells = DevKit.Get <WellList, Well>(DevKit.List(new Well() { Uid = x.Uid })); Assert.IsNotNull(wells); var result = EnergisticsConverter.XmlToObject <WellList>(wells.XMLout); Assert.IsNotNull(wells); Assert.AreEqual(0, result.Well.Count); }); }
public async Task CascadedDelete141Tests_Can_Parallel_Delete_Populated_Wellbores() { DevKit.AddAndAssert(Well); // Number of objects to generate var numOfObjects = 5; // Create logs var logs = DevKit.GenerateLogs(Well.Uid, Well.Name, LogIndexType.measureddepth, numOfObjects); // Create trajectories var trajectories = DevKit.GenerateTrajectories(Well.Uid, Well.Name, numOfObjects); // Add 5 wellbores with data objects for (var i = 0; i < numOfObjects; i++) { var wellbore = new Wellbore() { Uid = DevKit.Uid(), UidWell = Well.Uid, Name = DevKit.Name(), NameWell = Well.Name }; DevKit.AddAndAssert(wellbore); DevKit.AddListOfLogsToWellbore(logs, wellbore); DevKit.AddListOfTrajectoriesToWellbore(trajectories, wellbore); } // Delete well with cascadedDelete options in var wellbores = DevKit.Get <WellboreList, Wellbore>(DevKit.List(new Wellbore() { UidWell = Well.Uid }), ObjectTypes.Wellbore, string.Empty, OptionsIn.ReturnElements.IdOnly); Assert.IsNotNull(wellbores); var wellboreList = EnergisticsConverter.XmlToObject <WellboreList>(wellbores.XMLout); Assert.IsNotNull(wellboreList); Assert.AreEqual(numOfObjects, wellboreList.Wellbore.Count); // Delete each wellbore in parallel var taskList = new List <Task>(); wellboreList.Wellbore.ForEach(x => { taskList.Add(new Task(() => { var deleteWellbore = new Wellbore { Uid = x.Uid, UidWell = x.UidWell }; var result = DevKit.Delete <WellboreList, Wellbore>(deleteWellbore, ObjectTypes.Wellbore, string.Empty, OptionsIn.CascadedDelete.True); Assert.IsNotNull(result); Assert.AreEqual(result.Result, 1); // Ensure wellbore does not exist anymore DevKit.GetAndAssert(deleteWellbore, false); })); }); taskList.ForEach(x => x.Start()); await Task.WhenAll(taskList); // Get all wellbores remaining under well wellbores = DevKit.Get <WellboreList, Wellbore>(DevKit.List(new Wellbore() { UidWell = Well.Uid }), ObjectTypes.Wellbore, string.Empty, OptionsIn.ReturnElements.IdOnly); Assert.IsNotNull(wellbores); wellboreList = EnergisticsConverter.XmlToObject <WellboreList>(wellbores.XMLout); Assert.IsNotNull(wellboreList); Assert.AreEqual(0, wellboreList.Wellbore.Count); }
public void Log131DataAdapter_AddToStore_Add_DepthLog_With_Unordered_LogCurveInfo() { AddParents(); DevKit.InitHeader(Log, LogIndexType.measureddepth); DevKit.InitDataMany(Log, DevKit.Mnemonics(Log), DevKit.Units(Log), 10); // Reverse logCurveInfo order Log.LogCurveInfo.Reverse(); DevKit.AddAndAssert(Log); var indexCurve = Log.LogCurveInfo.FirstOrDefault(x => x.Mnemonic == Log.IndexCurve.Value); // Create two new curves var newCurve1 = new LogCurveInfo() { Mnemonic = "Test", Unit = "gAPI", ColumnIndex = 3, NullValue = "|" }; var newCurve2 = new LogCurveInfo() { Mnemonic = "Test2", Unit = "gAPI", ColumnIndex = 2, NullValue = "|" }; // Update 2 new curves to the log header var update = new Log() { UidWell = Log.UidWell, UidWellbore = Log.UidWellbore, Uid = Log.Uid, LogCurveInfo = new List <LogCurveInfo>() { indexCurve, newCurve1, newCurve2 } }; DevKit.UpdateAndAssert(update); // Add data update.LogData = new List <string>(); for (int i = 1; i < 11; i++) { var val1 = i % 2 == 0 ? "|" : i.ToString(); var val2 = i % 2 == 1 ? "|" : i.ToString(); update.LogData.Add($"{i},{val1},{val2}"); } DevKit.UpdateAndAssert(update); // Query by example to get values from the index curve and 2 new curves var queryIn = "<logs version=\"1.3.1.1\" xmlns=\"http://www.witsml.org/schemas/131\" > " + Environment.NewLine + $" <log uidWell=\"{Log.UidWell}\" uidWellbore=\"{Log.UidWellbore}\" uid=\"{Log.Uid}\">" + Environment.NewLine + " <nameWell />" + Environment.NewLine + " <nameWellbore />" + Environment.NewLine + " <name />" + Environment.NewLine + " <objectGrowing />" + Environment.NewLine + " <serviceCompany />" + Environment.NewLine + " <runNumber />" + Environment.NewLine + " <creationDate />" + Environment.NewLine + " <indexType />" + Environment.NewLine + " <startIndex uom=\"\" />" + Environment.NewLine + " <endIndex uom=\"\" />" + Environment.NewLine + " <startDateTimeIndex />" + Environment.NewLine + " <endDateTimeIndex />" + Environment.NewLine + " <direction />" + Environment.NewLine + " <indexCurve columnIndex=\"\" />" + Environment.NewLine + " <logCurveInfo>" + Environment.NewLine + $" <mnemonic>{indexCurve.Mnemonic}</mnemonic>" + Environment.NewLine + " <unit />" + Environment.NewLine + " <columnIndex />" + Environment.NewLine + " </logCurveInfo>" + Environment.NewLine + " <logCurveInfo>" + Environment.NewLine + $" <mnemonic>{newCurve2.Mnemonic}</mnemonic>" + Environment.NewLine + " <unit />" + Environment.NewLine + " <columnIndex />" + Environment.NewLine + " </logCurveInfo>" + Environment.NewLine + " <logCurveInfo>" + Environment.NewLine + $" <mnemonic>{newCurve1.Mnemonic}</mnemonic>" + Environment.NewLine + " <unit />" + Environment.NewLine + " <columnIndex />" + Environment.NewLine + " </logCurveInfo>" + Environment.NewLine + " <logData />" + Environment.NewLine + " </log>" + Environment.NewLine + "</logs>"; var result = DevKit.GetFromStore(ObjectTypes.Log, queryIn, null, "returnElements=requested"); Assert.AreEqual((short)ErrorCodes.Success, result.Result); Assert.IsNotNull(result); var logs = EnergisticsConverter.XmlToObject <LogList>(result.XMLout); Assert.IsNotNull(logs); var log = logs.Log.FirstOrDefault(); Assert.IsNotNull(log); Assert.IsNotNull(log.LogData); Assert.AreEqual(3, log.LogCurveInfo.Count); Assert.AreEqual(10, log.LogData.Count); foreach (var lc in log.LogCurveInfo) { var curve = update.LogCurveInfo.FirstOrDefault(x => x.Mnemonic == lc.Mnemonic); Assert.IsNotNull(curve); // Ensure that the value from the update matches the response using columnIndex for (int i = 0; i < 10; i++) { Assert.AreEqual(log.LogData[i].Split(',')[lc.ColumnIndex.Value - 1], update.LogData[i].Split(',')[curve.ColumnIndex.Value - 1]); } } }