public async Task <T> GetFromStoreAsync <T>(T query, OptionsIn optionsIn) where T : IWitsmlQueryType, new() { try { var request = new WMLS_GetFromStoreRequest { WMLtypeIn = query.TypeName, OptionsIn = optionsIn.GetName(), QueryIn = XmlHelper.Serialize(query), CapabilitiesIn = "" }; var response = await client.WMLS_GetFromStoreAsync(request); LogQueriesSentAndReceived(request.QueryIn, response.IsSuccessful(), response.XMLout); if (response.IsSuccessful()) { return(XmlHelper.Deserialize <T>(response.XMLout)); } var errorResponse = await client.WMLS_GetBaseMsgAsync(response.Result); throw new Exception($"Error while querying store: {response.Result} - {errorResponse.Result}. {response.SuppMsgOut}"); } catch (XmlException e) { Log.Error(e, "Failed to deserialize response from Witsml server"); return(new T()); } }
/// <summary> /// Validates the WITSML Store API request. /// </summary> /// <param name="providers">The capServer providers.</param> /// <exception cref="WitsmlException"></exception> public static void ValidateRequest(IEnumerable <ICapServerProvider> providers) { var context = WitsmlOperationContext.Current; _log.DebugFormat("Validating WITSML request for {0}", context.Request.ObjectType); ValidateUserAgent(WebOperationContext.Current); context.OptionsIn = OptionsIn.Parse(context.Request.Options); // Document version is derived from the input string so it needs to be decompressed before the below checks. context.RequestCompressed = ValidateCompressedInput(context.Request.Function, context.Request.Xml, context.OptionsIn); var xml = context.RequestCompressed ? CompressionUtil.DecompressRequest(context.Request.Xml) : context.Request.Xml; context.Document = ValidateInputTemplate(xml); var dataSchemaVersion = ObjectTypes.GetVersion(context.Document.Root); ValidateDataSchemaVersion(dataSchemaVersion); var capServerProvider = providers.FirstOrDefault(x => x.DataSchemaVersion == dataSchemaVersion); if (capServerProvider == null) { throw new WitsmlException(ErrorCodes.DataObjectNotSupported, "WITSML object type not supported: " + context.Request.ObjectType + "; Version: " + dataSchemaVersion); } capServerProvider.ValidateRequest(); context.DataSchemaVersion = dataSchemaVersion; }
private void Trajectory141DataAdapter_GetFromStore_Results_Are_Limited_To_MaxReturnNodes_OptionsIn(int numberOfStations) { AddParents(); var maxDataNodes = 5; Trajectory.TrajectoryStation = DevKit.TrajectoryStations(maxDataNodes + numberOfStations, 0); DevKit.AddAndAssert(Trajectory); short errorCode; var result = DevKit.QueryWithErrorCode <TrajectoryList, Trajectory>( new Trajectory() { Uid = Trajectory.Uid, UidWell = Trajectory.UidWell, UidWellbore = Trajectory.UidWellbore }, out errorCode, ObjectTypes.Trajectory, null, OptionsIn.Join(OptionsIn.ReturnElements.All, OptionsIn.MaxReturnNodes.Eq(maxDataNodes))); Assert.AreEqual((short)ErrorCodes.ParialSuccess, errorCode, "Returning partial data."); Assert.IsNotNull(result); Assert.AreEqual(1, result.Count); var trajectory = result[0]; Assert.IsNotNull(trajectory); Assert.AreEqual(maxDataNodes, trajectory.TrajectoryStation.Count); }
public async Task <QueryResult> AddToStoreAsync <T>(T query, OptionsIn optionsIn = OptionsIn.Requested) where T : IWitsmlQueryType { try { var request = new WMLS_AddToStoreRequest { WMLtypeIn = query.TypeName, OptionsIn = optionsIn.GetName(), XMLin = XmlHelper.Serialize(query) }; var response = await client.WMLS_AddToStoreAsync(request); LogQueriesSentAndReceived(request.XMLin, response.IsSuccessful()); if (response.IsSuccessful()) { return(new QueryResult(true)); } var errorResponse = await client.WMLS_GetBaseMsgAsync(response.Result); var message = $"Error while adding to store: {response.Result} - {errorResponse.Result}. {response.SuppMsgOut}"; return(new QueryResult(false, message)); } catch (MessageSecurityException e) { const string message = "Request forbidden. Verify credentials"; Log.Error(e, message); return(new QueryResult(false, message)); } }
/// <summary> /// Gets the objects of the specified query. /// </summary> /// <param name="objectType"></param> /// <param name="queryIn"></param> /// <param name="optionsIn"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public IEnumerable <T> GetObjects <T>(string objectType, string queryIn, params OptionsIn[] optionsIn) where T : IDataObject { var result = ExecuteQuery(objectType, queryIn, OptionsIn.Join(optionsIn)); var dataObjects = (IEnumerable <T>)result?.Items ?? Enumerable.Empty <T>(); return(dataObjects.OrderBy(x => x.Name)); }
public void SidewallCore141WitsmlStore_GetFromStore_Can_Transform_SidewallCore() { AddParents(); DevKit.AddAndAssert <SidewallCoreList, SidewallCore>(SidewallCore); // Re-initialize all capServer providers DevKit.Store.CapServerProviders = null; DevKit.Container.BuildUp(DevKit.Store); string typeIn, queryIn; var query = DevKit.List(DevKit.CreateQuery(SidewallCore)); DevKit.SetupParameters <SidewallCoreList, SidewallCore>(query, ObjectTypes.SidewallCore, out typeIn, out queryIn); var options = OptionsIn.Join(OptionsIn.ReturnElements.All, OptionsIn.DataVersion.Version131); var request = new WMLS_GetFromStoreRequest(typeIn, queryIn, options, null); var response = DevKit.Store.WMLS_GetFromStore(request); Assert.IsFalse(string.IsNullOrWhiteSpace(response.XMLout)); Assert.AreEqual((short)ErrorCodes.Success, response.Result); var result = WitsmlParser.Parse(response.XMLout); var version = ObjectTypes.GetVersion(result.Root); Assert.AreEqual(OptionsIn.DataVersion.Version131.Value, version); }
/// <summary> /// Processes the GetFromStore query result. /// </summary> /// <param name="xmlOut">The XML out.</param> /// <param name="optionsIn">The options in.</param> /// <returns>An XML string.</returns> private string ProcessQueryResult(string xmlOut, string optionsIn) { if (string.IsNullOrWhiteSpace(xmlOut)) { return(xmlOut); } if (string.IsNullOrWhiteSpace(Model.OutputPath)) { return(xmlOut); } var options = OptionsIn.Parse(optionsIn); var returnElements = OptionsIn.GetValue(options, OptionsIn.ReturnElements.Requested); var outputPath = new DirectoryInfo(Path.Combine(Model.OutputPath, returnElements)).FullName; var isAutoSave = xmlOut.Length > Model.TruncateSize; var xmlOutOriginal = xmlOut; if (isAutoSave) { xmlOut = $"<!-- WARNING: Response larger than {Model.TruncateSize} characters -->"; } else if (QueryResults.IsPrettyPrintAllowed && QueryResults.IsPrettyPrintEnabled) { var document = WitsmlParser.Parse(xmlOut); xmlOut = document.ToString(); } if (Model.IsSaveQueryResponse || isAutoSave) { Task.Run(async() => { Runtime.ShowBusy(); try { var document = WitsmlParser.Parse(xmlOutOriginal); outputPath = await SaveQueryResult(outputPath, document, Model.IsSplitResults); if (isAutoSave) { var message = $"{Environment.NewLine}<!-- Results automatically saved to {outputPath} -->"; QueryResults.Append(message); } } catch (Exception ex) { _log.Error("Error saving query results to file", ex); } finally { Runtime.ShowBusy(false); } }); } return(xmlOut); }
/// <summary> /// Gets the objects of the specified query. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objectType">Type of the object.</param> /// <param name="queryIn">The query in.</param> /// <param name="logXmlRequest">if set to <c>true</c> log XML request.</param> /// <param name="logXmlResponse">if set to <c>true</c> log XML response.</param> /// <param name="returnNullIfError">if set to <c>true</c> and if there was an error querying return null, else empty.</param> /// <param name="optionsIn">The options in.</param> /// <returns>The data objects.</returns> public IEnumerable <T> GetObjects <T>(string objectType, string queryIn, bool logXmlRequest = true, bool logXmlResponse = true, bool returnNullIfError = false, params OptionsIn[] optionsIn) where T : IDataObject { var result = ExecuteGetFromStoreQuery(objectType, queryIn, OptionsIn.Join(optionsIn), logXmlRequest, logXmlResponse); var dataObjects = (IEnumerable <T>)result?.Items ?? (returnNullIfError ? null : Enumerable.Empty <T>()); return(dataObjects?.OrderBy(x => x.Name)); }
private async Task <string> ExecuteQuery(string queryFile, string returnElements, int?maxReturnNodes) { var queryPath = Path.Combine(Directory.GetCurrentDirectory(), queryFile); var query = await File.ReadAllTextAsync(queryPath); var returnElementsEnum = EnumParser <ReturnElements> .GetEnum(returnElements); var optionsIn = new OptionsIn(returnElementsEnum, maxReturnNodes); return(await witsmlClient.GetFromStoreAsync(query, optionsIn)); }
/// <summary> /// Requests the CascadedDelete OptionIn. /// </summary> /// <returns>The CascadedDelete value</returns> public bool CascadedDelete() { string value = OptionsIn.GetValue(Options, OptionsIn.CascadedDelete.False); bool result; if (!bool.TryParse(value, out result)) { result = false; } return(result); }
/// <summary> /// Performs validation for the specified function and supplied parameters. /// </summary> public override void ValidateRequest() { var context = WitsmlOperationContext.Current; var request = context.Request; var document = context.Document; Logger.DebugFormat("Validating WITSML request for {0}; Function: {1}", request.ObjectType, request.Function); base.ValidateRequest(); var optionsIn = OptionsIn.Parse(request.Options); if (request.Function == Functions.GetFromStore) { ValidateKeywords(optionsIn, OptionsIn.ReturnElements.Keyword, OptionsIn.MaxReturnNodes.Keyword, OptionsIn.RequestLatestValues.Keyword, OptionsIn.RequestPrivateGroupOnly.Keyword, OptionsIn.RequestObjectSelectionCapability.Keyword, OptionsIn.CompressionMethod.Keyword, OptionsIn.DataVersion.Keyword, OptionsIn.IntervalRangeInclusion.Keyword); ValidateRequestMaxReturnNodes(optionsIn); ValidateRequestRequestLatestValue(optionsIn); ValidateRequestObjectSelectionCapability(optionsIn, request.ObjectType, document); ValidateEmptyRootElement(request.ObjectType, document); ValidateReturnElements(optionsIn, request.ObjectType); ValidateIntervalRangeInclusion(optionsIn, request.ObjectType); ValidateSelectionCriteria(document); } else if (request.Function == Functions.AddToStore) { ValidateKeywords(optionsIn, OptionsIn.CompressionMethod.Keyword, OptionsIn.DataVersion.Keyword); ValidateCompressionMethod(optionsIn, GetCapServer().CapServer.CompressionMethod); ValidateEmptyRootElement(request.ObjectType, document); ValidateSingleChildElement(request.ObjectType, document); } else if (request.Function == Functions.UpdateInStore) { ValidateKeywords(optionsIn, OptionsIn.CompressionMethod.Keyword, OptionsIn.DataVersion.Keyword); ValidateCompressionMethod(optionsIn, GetCapServer().CapServer.CompressionMethod); ValidateEmptyRootElement(request.ObjectType, document); ValidateSingleChildElement(request.ObjectType, document); } else if (request.Function == Functions.DeleteFromStore) { ValidateKeywords(optionsIn, OptionsIn.CascadedDelete.Keyword, OptionsIn.DataVersion.Keyword); ValidateCascadedDelete(optionsIn, GetCapServer().CapServer.CascadedDelete.GetValueOrDefault()); ValidateEmptyRootElement(request.ObjectType, document); ValidateSingleChildElement(request.ObjectType, document); } }
/// <summary> /// Requests the private group only. /// </summary> /// <returns></returns> public bool RequestPrivateGroupOnly() { string value = OptionsIn.GetValue(Options, OptionsIn.RequestPrivateGroupOnly.False); bool result; if (!bool.TryParse(value, out result)) { result = false; } return(result); }
public static string GetName(this OptionsIn input) { return(input switch { OptionsIn.All => "returnElements=all", OptionsIn.IdOnly => "returnElements=id-only", OptionsIn.HeaderOnly => "returnElements=header-only", OptionsIn.DataOnly => "returnElements=data-only", OptionsIn.StationLocationOnly => "returnElements=station-location-only", OptionsIn.LatestChangeOnly => "returnElements=latest-change-only", OptionsIn.Requested => "returnElements=requested", _ => "returnElements=all" });
private List <ChangeLog> GetAndAssertChangeLog(string queryIn, OptionsIn optionsIn, int?expectedChangeLogCount = null) { var changeLogs = DevKit.Query <ChangeLogList, ChangeLog>(ObjectTypes.ChangeLog, queryIn, null, optionsIn); Assert.IsNotNull(changeLogs); if (expectedChangeLogCount.HasValue) { Assert.AreEqual(expectedChangeLogCount, changeLogs.Count); } return(changeLogs); }
/// <summary> /// Submits an asynchronous query to the WITSML server for a given function type. /// The results of a query are displayed in the Results and Messages tabs. /// </summary> /// <param name="functionType">Type of the function.</param> /// <param name="optionsIn">The options in.</param> /// <param name="isPartialQuery">if set to <c>true</c> [is partial query].</param> public void SubmitQuery(Functions functionType, string optionsIn = null, bool isPartialQuery = false) { // Trim query text before submitting request string xmlIn = XmlQuery.Text.Trim(); // Format the text of XmlQuery XmlQuery.SetText(xmlIn); _log.DebugFormat("Query submitted for function '{0}'", functionType); // Options In if (string.IsNullOrEmpty(optionsIn)) { optionsIn = GetOptionsIn(functionType, isPartialQuery); } else if (isPartialQuery) { var optionsInUpdated = new List <OptionsIn> { OptionsIn.ReturnElements.DataOnly }; var optionsInFromPreviousQuery = OptionsIn.Parse(optionsIn); foreach (var key in optionsInFromPreviousQuery.Keys) { if (key != OptionsIn.ReturnElements.All.Key && key != OptionsIn.ReturnElements.DataOnly.Key) { optionsInUpdated.Add(new OptionsIn(key, optionsInFromPreviousQuery[key])); } } optionsIn = OptionsIn.Join(optionsInUpdated.ToArray()); } // Output Request OutputRequestMessages(functionType, functionType == Functions.GetCap ? string.Empty : xmlIn, optionsIn); Runtime.ShowBusy(); Task.Run(async() => { // Call internal SubmitQuery method with references to all inputs and outputs. var result = await SubmitQuery(functionType, xmlIn, optionsIn); // Clear any previous query results if this is not a partial query if (!isPartialQuery) { ClearQueryResults(); } ShowSubmitResult(functionType, result, isPartialQuery); Runtime.ShowBusy(false); }); }
/// <summary> /// Initializes a new instance of the <see cref="WitsmlQueryParser" /> class. /// </summary> /// <param name="element">The element.</param> /// <param name="objectType">The object type.</param> /// <param name="options">The options.</param> public WitsmlQueryParser(XElement element, string objectType, string options) { Root = element; ObjectType = objectType; Options = OptionsIn.Parse(options); _options = options; _namespace = element.GetDefaultNamespace(); _element = element; _elements = element.Attributes("version").Any() ? element.Elements(_namespace + objectType) : new[] { element }; QueryCount = _elements.Count(); }
/// <summary> /// Gets the selected item's details using a GetFromStore request. /// </summary> public void GetObjectDetailsWithExtraOptionsIn() { if (CanGetObjectHeader && !string.IsNullOrWhiteSpace(ExtraOptionsIn)) { var optionsIn = new List <OptionsIn> { OptionsIn.ReturnElements.All }; var extraOptions = OptionsIn.Parse(ExtraOptionsIn); foreach (var extraOptionsKey in extraOptions.Keys) { try { optionsIn.Add(new OptionsIn(extraOptionsKey, extraOptions[extraOptionsKey])); } catch { //ignore if invalid optionsIn pair } } GetObjectDetails(optionsIn.ToArray()); } }
/// <summary> /// Converts a data object collection to XML and optionally converts to a requested version. /// </summary> /// <param name="request">The GetFromStore request.</param> /// <param name="collection">The data object collection.</param> /// <returns></returns> private string GetXmlOut(WMLS_GetFromStoreRequest request, IEnergisticsCollection collection) { if (collection == null) { return(string.Empty); } EnsureCapServerProviders(); var optionsIn = OptionsIn.Parse(request.OptionsIn); string requestedVersion; // Attempt transformation if client requested a different version if (optionsIn.TryGetValue(OptionsIn.DataVersion.Keyword, out requestedVersion) && _capServerMap.ContainsKey(requestedVersion) && collection.GetVersion() != requestedVersion) { _log.Debug($"Transforming XMLOut to data schema version {requestedVersion}"); collection = WitsmlParser.Transform(collection, requestedVersion); } return(WitsmlParser.ToXml(collection)); }
/// <summary> /// Returns the capServer object that describes the capabilities of the server for one Data Schema Version. /// </summary> /// <param name="request">The request object encapsulating the method input parameters.</param> /// <returns>A positive value indicates a success; a negative value indicates an error.</returns> public WMLS_GetCapResponse WMLS_GetCap(WMLS_GetCapRequest request) { try { WitsmlOperationContext.Current.Request = request.ToContext(); EnsureCapServerProviders(); _log.Debug(WebOperationContext.Current.ToLogMessage()); _log.Debug(request.ToLogMessage()); UserAuthorizationProvider.CheckSoapAccess(); var options = OptionsIn.Parse(request.OptionsIn); var version = OptionsIn.GetValue(options, new OptionsIn.DataVersion(_defaultDataSchemaVersion)); // return error if WITSML 1.3.1 not supported AND dataVersion not specified (required in WITSML 1.4.1) if (!_capServerMap.ContainsKey(OptionsIn.DataVersion.Version131.Value) && !options.ContainsKey(OptionsIn.DataVersion.Keyword)) { throw new WitsmlException(ErrorCodes.MissingDataVersion); } if (_capServerMap.ContainsKey(version)) { var response = new WMLS_GetCapResponse((short)ErrorCodes.Success, _capServerMap[version].ToXml(), string.Empty); _log.Debug(response.ToLogMessage()); return(response); } throw new WitsmlException(ErrorCodes.DataVersionNotSupported, "Data schema version not supported: " + version); } catch (WitsmlException ex) { var response = new WMLS_GetCapResponse((short)ex.ErrorCode, string.Empty, ex.Message); _log.Error(response.ToLogMessage(_log.IsWarnEnabled)); return(response); } }
public void GetKeyWords_WithReturnElementsDataOnly_And_MaxReturnNodes50_ReturnsCorrectValue() { var optionsIn = new OptionsIn(ReturnElements.DataOnly, 50); Assert.Equal("returnElements=data-only;maxReturnNodes=50", optionsIn.GetKeywords()); }
public async Task <LogObject> GetLog(string wellUid, string wellboreUid, string logUid, OptionsIn queryOptions) { var query = LogQueries.QueryById(wellUid, wellboreUid, logUid); var result = await WitsmlClient.GetFromStoreAsync(query, queryOptions); var witsmlLog = result.Logs.FirstOrDefault(); if (witsmlLog == null) { return(null); } var logObject = new LogObject { Uid = witsmlLog.Uid, Name = witsmlLog.Name, IndexType = witsmlLog.IndexType, WellUid = witsmlLog.UidWell, WellName = witsmlLog.NameWell, WellboreUid = witsmlLog.UidWellbore, WellboreName = witsmlLog.NameWellbore, IndexCurve = witsmlLog.IndexCurve.Value, ServiceCompany = witsmlLog.ServiceCompany, RunNumber = witsmlLog.RunNumber }; if (string.IsNullOrEmpty(witsmlLog.IndexType)) { return(logObject); } logObject.StartIndex = GetIndexAsString(witsmlLog.IndexType, witsmlLog.StartIndex, witsmlLog.StartDateTimeIndex); logObject.EndIndex = GetIndexAsString(witsmlLog.IndexType, witsmlLog.EndIndex, witsmlLog.EndDateTimeIndex); return(logObject); }
/// <summary> /// Sets the options that will be passed in to the GetFromStore query. /// </summary> /// <param name="optionsIn"></param> /// <returns></returns> public IWitsmlQuery <T> With(OptionsIn optionsIn) { Options[optionsIn.Key] = optionsIn.Value; return(this); }
/// <summary> /// Sets the options that will be passed in to the GetFromStore query. /// </summary> /// <param name="optionsIn"></param> /// <returns></returns> IWitsmlQuery IWitsmlQuery.With(OptionsIn optionsIn) { With(optionsIn); return(this); }
public void GetKeywords_WithReturnElementsAll_ReturnsCorrectValue() { var optionsIn = new OptionsIn(ReturnElements.All); Assert.Equal("returnElements=all", optionsIn.GetKeywords()); }
/// <summary> /// Get the ReturnElements. /// </summary> /// <returns>The ReturnElements.</returns> public string ReturnElements() { return(OptionsIn.GetValue(Options, OptionsIn.ReturnElements.Requested)); }
/// <summary> /// Requests the object selection capability. /// </summary> /// <returns>The capability value.</returns> public string RequestObjectSelectionCapability() { return(OptionsIn.GetValue(Options, OptionsIn.RequestObjectSelectionCapability.None)); }
/// <summary> /// Get the interval range inclusion. /// </summary> /// <returns>The IntervalRangeInclusion.</returns> public string IntervalRangeInclusion() { return(OptionsIn.GetValue(Options, OptionsIn.IntervalRangeInclusion.MinimumPoint)); }