How can I create xquery using xml response - xquery

The below mentioned is actual xquery.
declare variable $rankCode_DL3UJ :=("CPT","FO","FM");
declare variable $rankCode_DL4UJ :=("CRFO","CL");
declare variable $type :=("DOM","ISH");
declare function local:getboardingPriorityCode($flightType as element(), $rankCode as element()) as xs:string
{
if(data($flightType) = $type) then
'DHD'
else
if(data($rankCode) = $rankCode_DL3UJ) then
'DL3UJ'
else if(data($rankCode) = $rankCode_DL4UJ) then
'DL4UJ'
else
'DL5UJ'
};
declare function local:getPreferredClass($flightType as element(flightCategory:Code)) as xs:string
{
if(data($flightType) = $type) then
'Y'
else
'J'
};
declare function xf:createDeadheadReservationRQ-to- PassengerDetailsRQ($createDeadheadReservationRQ1 as element(ns1:CreateDeadheadReservationRQ),$CreateDeadHeadReservationAdapterSelect RS as element(ns3:RefTickettypeCollection),
$airBookRS as element(flightCategory:GetFlightCategoryRS))
as element(ns2:PassengerDetailsRQ) {
<ns2:PassengerDetailsRQ Version = "2.0.0">
<ns2:SpecialReqDetails>
<ns2:AddRemarkRQ>
<ns2:RemarkInfo>
<ns2:Remark Type = "General">
<ns2:Text>{ data($createDeadheadReservationRQ1/ns1:SpecialRequestDetails/ns1:Remark) }</ns2:Text>
</ns2:Remark>
</ns2:RemarkInfo>
</ns2:AddRemarkRQ>
<ns2:SpecialServiceRQ>
<ns2:SpecialServiceInfo>
<ns2:SecureFlight SegmentNumber = "A" SSR_Code = "DOCS">
<ns2:PersonName DateOfBirth = "{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:DateOfBirth) }"
Gender = "{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:Gender) }"
NameNumber = "1.1">
<ns2:GivenName>{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:GivenName) }</ns2:GivenName>
<ns2:Surname>{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Surname) }</ns2:Surname>
</ns2:PersonName>
<ns2:VendorPrefs>
<ns2:Airline Hosted = "true"/>
</ns2:VendorPrefs>
</ns2:SecureFlight>
<ns2:Service SSR_Code = "{ data($createDeadheadReservationRQ1/ns1:SpecialRequestDetails/ns1:RankBasedSSR) }">
<ns2:VendorPrefs>
<ns2:Airline Hosted = "true"/>
</ns2:VendorPrefs>
</ns2:Service>
</ns2:SpecialServiceInfo>
</ns2:SpecialServiceRQ>
</ns2:SpecialReqDetails>
<ns2:TravelItineraryAddInfoRQ>
<ns2:AgencyInfo>
<ns2:Ticketing TicketType = "{fn:concat('7PS-', local:getboardingPriorityCode($airBookRS/flightCategory:ServiceType/flightCategory:Code, $createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:RankCode), local:getPreferredClass($airBookRS/flightCategory:ServiceType/flightCategory:Code))}"/>
</ns2:AgencyInfo>
<ns2:CustomerInfo>
<ns2:PersonName>
<ns2:GivenName>{
xs:string(fn:concat($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:GivenName,' ',
$createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Title))
}</ns2:GivenName>
<ns2:Surname>{ xs:string($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Surname) }</ns2:Surname>
</ns2:PersonName>
</ns2:CustomerInfo>
</ns2:TravelItineraryAddInfoRQ>
</ns2:PassengerDetailsRQ>
};
declare variable $createDeadheadReservationRQ1 as element(ns1:CreateDeadheadReservationRQ) external;
declare variable $airBookRS as element(flightCategory:GetFlightCategoryRS) external;
declare variable $CreateDeadHeadReservationAdapterSelectRS as element(ns3:RefTickettypeCollection) external;
xf:createDeadheadReservationRQ-to- PassengerDetailsRQ($createDeadheadReservationRQ1, $airBookRS, $CreateDeadHeadReservationAdapterSelectRS)
So We have to do the below changes.
for that we have created the below mentioned table in DB, using JCA adapter we have received the response and created the xml response creating business service of JCA adapter in OSB.
condition: In query we need to fetch OTHER always so query will be as follows and if we get 2 results then pick one that is not “other” like pick CPT if it returned in result of select query and if we get 1 result then it will be always “Other”.
Select *from <Table> where Code_name in (‘OTHER’, 'value received in request');
xml request and response of business service JCA adapter:
Request:
<cre:CreateDeadHeadReservationAdapterSelect_CODE_NAMEInputParameters>
<cre:CODE_NAME>CPT</cre:CODE_NAME>
</cre:CreateDeadHeadReservationAdapterSelect_CODE_NAMEInputParameters>
Response:
<cre:RefTickettypeCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cre="http://xmlns.oracle.com/pcbpel/adapter/db/top/CreateDeadHeadReservati onAdapter">
<cre:RefTickettype>
<cre:codeName>CPT</cre:codeName>
<cre:codeType>Rank</cre:codeType>
<cre:priorityCode>DL3UJ</cre:priorityCode>
<cre:preferredClass>J</cre:preferredClass>
</cre:RefTickettype>
<cre:RefTickettype>
<cre:codeName>OTHER</cre:codeName>
<cre:codeType>Other</cre:codeType>
<cre:priorityCode>DL7UJ</cre:priorityCode>
<cre:preferredClass>J</cre:preferredClass>
</cre:RefTickettype></cre:RefTickettypeCollection>
So using the above response could you pls help me to create the xquery logic.

I'm not sure I understand all of your question. But, it sounds like when two RefTickettype elements are returned, you want to select the one where codeName is not OTHER?
The following query checks to see how many RefTickettype elements are in the result. If there is only one, it is returned. If there is more than one, it will return all where codeName is not OTHER.
declare namespace cre="http://xmlns.oracle.com/pcbpel/adapter/db/top/CreateDeadHeadReservationAdapter";
let $response :=
<cre:RefTickettypeCollection>
<cre:RefTickettype>
<cre:codeName>CPT</cre:codeName>
<cre:codeType>Rank</cre:codeType>
<cre:priorityCode>DL3UJ</cre:priorityCode>
<cre:preferredClass>J</cre:preferredClass>
</cre:RefTickettype>
<cre:RefTickettype>
<cre:codeName>OTHER</cre:codeName>
<cre:codeType>Other</cre:codeType>
<cre:priorityCode>DL7UJ</cre:priorityCode>
<cre:preferredClass>J</cre:preferredClass>
</cre:RefTickettype>
</cre:RefTickettypeCollection>
return
if (count($response/cre:RefTickettype) eq 1) then
$response/cre:RefTickettype
else
$response/cre:RefTickettype[cre:codeName ne "OTHER"]

Related

LINQ to Entities does not recognize the method call within it

I completely understand that this is because LINQ query requires the whole expression to be translated to a server , and therefore I cant call an outside method in it. but as I have looked at other answers there is not relative solution to this. the only approach that I thought about is to loop through all the items in the model and than passing them to the query one by one but even though this approach would not help so I am seeking help in here for anyone to help me to figure out how to call a method or a way of calling a method appendstr that initializes a.PostedDate before checking its actual equivalent value in the giving LINQ Query.
[HttpGet]
public ActionResult SearchResult(int? page, string searchTitle = null, string searchLocation = null, string last24 = "")
{
ViewBag.searchTitle = searchTitle;
ViewBag.searchLocation = searchLocation;
ViewBag.page = page;
ViewBag.last24 = last24;
setUpApi(searchTitle, searchLocation);
var result = new List<AllJobModel>().AsQueryable();
if (!string.IsNullOrEmpty(ViewBag.searchTitle) || !string.IsNullOrEmpty(ViewBag.searchTitle) || !string.IsNullOrEmpty(ViewBag.last24))
{
setUpApi(searchTitle, searchLocation);
DateTime now = DateTime.Now;
result = db.AllJobModel.Where(a => a.JobTitle.Contains(searchTitle) && a.locationName.Contains(searchLocation) &&
appendstr(a.PostedDate).Equals(now.AddHours(-24).ToString("MM-dd-yyyy")));
}
else
{
result = from app in db.AllJobModel select app;
}
return View(result.ToList().ToPagedList(page ?? 1, 5));
}
The second method that gets called in the LINQ Query
public string appendstr(string str)
{
var x = str.Split(' ');
return 01 + "-" + x[1] + "-" + x[2];
}
I think you already understand that the .NET code you write in the Where clause is actually an expression that is parsed and converted to SQL. So if you have a funky string manipulation method, you can't use it directly.
The brute force option, as you seem to already understand, it to materialize the query first and then run the C# code over the results. You can do this with ToList() or AsEnumerable().
result = db.AllJobModel
.Where
(
a => a.JobTitle.Contains(searchTitle)
&& a.LocationName.Contains(searchLocation)
)
.AsEnumerable()
.Where
(
a => appendstr(a.PostedDate).Equals(now.AddHours(-24).ToString("MM-dd-yyyy")))
);
However in your specific case you can try a trick. You are attempting a date comparison, which SQL is perfectly capable of doing... you just need to convert that funky PostedDate to a SQL DateTime so that you can compare it directly. The gimmick for that is to use SqlFunctions.DateAdd to add null interval (e.g. 0 days). This implicitly converts the string to DateTime, where you can now query on the SQL side:
var targetDate = DateTime.Now.AddHours(-24);
result = db.AllJobModel
.Where
(
a => a.JobTitle.Contains(searchTitle)
&& a.LocationName.Contains(searchLocation)
&& SqlFunctions.DateAdd("DAY", 0, a.PostedDate) == targetDate
);
Credit goes to this post for the workaround.

How to create nested GraphQL fields with Sangria's `#GraphQLField` annotation

I have a simple case class MyContext(queries: Query) that I provide to the schema with : sangria.schema.Schema(deriveContextObjectType[MyContext, Query, Unit](_.queries)
MyQuery is a trait of Query
trait MyQuery {
#GraphQLField
def item(ctx: Context[MyContext, Unit])(id: String) ...
}
This works great. But what if I want to nest resolvers?
query {
item {
status # status is resolved from source B
price # price is resolved from source C
}
}
Is that possible to achieve? Would I return an ObjectType[Item] that has properties status and price annotated with #GraphQLField?
I think you can use deriveObjectType for an Item. It is also able to handle the #GraphQLField annotation (as an alternative you can also use IncludeMethods macro setting). Here is an example:
implicit val ItemType = deriveObjectType[MyContext, Item]()
Really appreciate #tenshi's answer! I tried using the deriveObjectType but was getting a type error:
type mismatch;
found : sangria.schema.Context[MyContext,Item]
required: sangria.schema.Context[MyContext,Unit]
But using deriveContextObjectType in conjunction with AddFields is working:
def schema = sangria.schema.Schema(
deriveContextObjectType[MyContext, Query, Unit](_.queries,
AddFields(
Field(
name = "item",
fieldType = deriveContextObjectType[MyContext, Item, Unit](_ => new Item),
resolve = _ => ()
)
))
)
And the Schema looks good, yielding:
type Query {
item: Item!
}

Import data using mlcp, a csv file and transform parse date

I have a csv file. I've managed import these data into MarkLogic using mlcp which then created a xml file in MarkLogic.
Now in csv I have this format "6/29/2013 5:00:00 PM" random in one of the column. How do I use xquery and probably node-replace as a transform function to convert this date into a different format such as "2013-06-29" as MarkLogic default date format?
Any help is appreciated...
I have created transform.xqy and install it on Modules in MLogic. I'm
thinking about using "xdmp:node-replace" to replace the date with expected
format. Or should I go thorugh the csv column by column (How to do?) and
use "castable as xs:dateTime" to determine date value or not. Yet, even
just printing out the content value/uri, always giving me error.
xquery version "1.0-ml";
module namespace example = "http://test.com/example";
(: If the input document is XML, insert #NEWATTR, with the value
: specified in the input parameter. If the input document is not
: XML, leave it as-is.
:)
declare function example:transform(
$content as map:map,
$context as map:map
) as map:map*
{
let $the-doc-uri := map:get($content, "uri")
let $the-doc := map:get($content, "value")
return
trace($the-doc, 'The value of doc is: ')
};
The MarkLogic documentation contains a full example of an MLCP transform:
https://docs.marklogic.com/guide/mlcp/import#id_65640
It shows this example, which adds an attribute to the XML content:
declare function example:transform(
$content as map:map,
$context as map:map
) as map:map*
{
let $attr-value :=
(map:get($context, "transform_param"), "UNDEFINED")[1]
let $the-doc := map:get($content, "value")
return
if (fn:empty($the-doc/element()))
then $content
else
let $root := $the-doc/*
return (
map:put($content, "value",
document {
$root/preceding-sibling::node(),
element {fn:name($root)} {
attribute { fn:QName("", "NEWATTR") } {$attr-value},
$root/#*,
$root/node()
},
$root/following-sibling::node()
}
), $content
)
};
Keep in mind you are supposed to update the "value" property of the $content map:map, and return $content to get your transformation result added to the database. I suggest using a (potentially recursive) typeswitch to identify element nodes, and then adjusting their value accordingly..
HTH!
finally did it.
The thing is I must use mem:node-replace because it is on the fly, on memory. While xdmp:node-replace is when the data is already on MarkLogic.
The rest is as expected I must use format-date and xdmp:parse-dateTime to get date format as required.
Here is some snippets
xquery version "1.0-ml";
module namespace ns_transform = "this_is_my_namespace";
import module namespace mem = "http://xqdev.com/in-mem-update" at "/MarkLogic/appservices/utils/in-mem-update.xqy";
declare variable $ns := "this_is_my_namespace";
declare function ns_transform:transform(
$content as map:map,
$context as map:map
) as map:map*
{
let $doc := map:get($content, "value")
let $format_in := "[M]/[D]/[Y0001] [h01]:[m01]:[s01] [P]"
let $format_out := "[Y0001]-[M01]-[D01]"
let $old_date := $doc/*:root_doc/*:date/text()
let $new_date := format-date(xs:date(xdmp:parse-dateTime($format_in, $old_date)), $format_out)
let $new_doc := mem:node-replace($doc/*:root_doc/*:date,element {fn:QName($ns, "date")}{$new_date})
let $_ := map:put($content, "value", $new_doc)
return $content
};

After modifying the xquery i am unable to get the BindVariables(vairblename and binding)

After modifying the xquery i am unable to get the BindVariables(vairblename and binding), i have added one variable,Later it is showing empty BindVariables.
after adding one more Variable it is showing 2 variables only, it should display 3 variables. Anyone Pls help to resolve this issue.
this is the xquery i am using:
`(:: pragma bea:global-element-parameter parameter="$createDeadheadReservationRQ1" element="ns1:CreateDeadheadReservationRQ" location="../../wsdl/imported/CreateDeadheadReservation/2.0/CreateDeadheadReserv ationMsg.xsd" ::)
(:: pragma bea:global-element-return element="ns2:PassengerDetailsRQ" location="../../wsdl/imported/PassengerDetailsRQ/v2.0.0/PassengerDetails2.0.0RQ. xsd" ::)
(:: pragma bea:global-element-parameter parameter="$CreateDeadHeadReservationAdapterSelectRS" element="ns3:RefTickettypeCollection" location="../../wsdl/CreateDeadHeadReservationAdapter/CreateDeadHeadReservationA dapter_table.xsd" ::)
declare namespace ns2 = "http://webservices.sabre.com/sabreXML/2011/10";
declare namespace ns3 ="http://xmlns.oracle.com/pcbpel/adapter/db/top/CreateDeadHeadReservationAdapter ";
declare namespace ns1 = "urn:www.virginaustralia.com:model:schema:reservation-management";
declare namespace flightCategory = "urn:www.virginaustralia.com:model:schema:network-operations";
declare namespace xf = "http://tempuri.org/CreateDeadheadReservationOSB/xquery/request/CreateDeadheadRe servationRQ_to_PassengerDetailsRQ/";
declare variable $rankCode_DL3UJ :=("CPT","FO","FM");
declare variable $rankCode_DL4UJ :=("CRFO","CL");
declare variable $type :=("DOM","ISH");
declare function local:getboardingPriorityCode($flightType as element(), $rankCode as element()) as xs:string
{
if(data($flightType) = $type) then
'DHD'
else
if(data($rankCode) = $rankCode_DL3UJ) then
'DL3UJ'
else if(data($rankCode) = $rankCode_DL4UJ) then
'DL4UJ'
else
'DL5UJ'
};
declare function local:getPreferredClass($flightType as element(flightCategory:Code)) as xs:string
{
if(data($flightType) = $type) then
'Y'
else
'J'
};
declare function xf:createDeadheadReservationRQ-to- PassengerDetailsRQ($createDeadheadReservationRQ1 as element(ns1:CreateDeadheadReservationRQ),
$airBookRS as element(flightCategory:GetFlightCategoryRS),$CreateDeadHeadReservationAdapterSel ectRS as element(ns3:RefTickettypeCollection))
as element(ns2:PassengerDetailsRQ) {
<ns2:PassengerDetailsRQ Version = "2.0.0">
<ns2:SpecialReqDetails>
<ns2:AddRemarkRQ>
<ns2:RemarkInfo>
<ns2:Remark Type = "General">
<ns2:Text>{ data($createDeadheadReservationRQ1/ns1:SpecialRequestDetails/ns1:Remark) }</ns2:Text>
</ns2:Remark>
</ns2:RemarkInfo>
</ns2:AddRemarkRQ>
<ns2:SpecialServiceRQ>
<ns2:SpecialServiceInfo>
<ns2:SecureFlight SegmentNumber = "A" SSR_Code = "DOCS">
<ns2:PersonName DateOfBirth = "{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:DateOfBirth) }"
Gender = "{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:Gender) }"
NameNumber = "1.1">
<ns2:GivenName>{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:GivenName) }</ns2:GivenName>
<ns2:Surname>{ data($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Surname) }</ns2:Surname>
</ns2:PersonName>
<ns2:VendorPrefs>
<ns2:Airline Hosted = "true"/>
</ns2:VendorPrefs>
</ns2:SecureFlight>
<ns2:Service SSR_Code = "{ data($createDeadheadReservationRQ1/ns1:SpecialRequestDetails/ns1:RankBasedSSR) }">
<ns2:VendorPrefs>
<ns2:Airline Hosted = "true"/>
</ns2:VendorPrefs>
</ns2:Service>
</ns2:SpecialServiceInfo>
</ns2:SpecialServiceRQ>
</ns2:SpecialReqDetails>
<ns2:TravelItineraryAddInfoRQ>
<ns2:AgencyInfo>
<ns2:Ticketing TicketType = "{fn:concat('7PS-', local:getboardingPriorityCode($airBookRS/flightCategory:ServiceType/flightCategory:Code, $createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:RankCode), local:getPreferredClass($airBookRS/flightCategory:ServiceType/flightCategory:Code))}"/>
</ns2:AgencyInfo>
<ns2:CustomerInfo>
<ns2:PersonName>
<ns2:GivenName>{
xs:string(fn:concat($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:GivenName,' ',
$createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Title))
}</ns2:GivenName>
<ns2:Surname>{ xs:string($createDeadheadReservationRQ1/ns1:PassengerInfo/ns1:PassengerName/ns1:Surname) }</ns2:Surname>
</ns2:PersonName>
</ns2:CustomerInfo>
</ns2:TravelItineraryAddInfoRQ>
</ns2:PassengerDetailsRQ>
};
declare variable $createDeadheadReservationRQ1 as element(ns1:CreateDeadheadReservationRQ) external;
declare variable $airBookRS as element(flightCategory:GetFlightCategoryRS) external;
declare variable $CreateDeadHeadReservationAdapterSelectRS as element(ns3:RefTickettypeCollection) external;
xf:createDeadheadReservationRQ-to- PassengerDetailsRQ($createDeadheadReservationRQ1, $airBookRS, $CreateDeadHeadReservationAdapterSelectRS)
Check if you write something like - var xqueryResVariable := concat (airbookRs,createDeadheadl,newVaribale) (where xqueryResVariable is the response given out from xquery), makes the newVaribale visible in the binding section.
The concept is - If you haven't used any variable to form the response (xml) that xquery will return, all those unused variables will not show up in the binding section.

Add "blocking" to Swift for-loop

I am using Swift in a project, and using SQLite.swift for database handling. I am trying to retrieve the most recent entry from my database like below:
func returnLatestEmailAddressFromEmailsTable() -> String{
let dbPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let db = Database("\(dbPath)/db.sqlite3")
let emails = db["emails"]
let email = Expression<String>("email")
let time = Expression<Int>("time")
var returnEmail:String = ""
for res in emails.limit(1).order(time.desc) {
returnEmail = res[email]
println("from inside: \(returnEmail)")
}
return returnEmail
}
I am trying to test the returned string from the above function like this:
println("from outside: \(returnLatestEmailAddressFromEmailsTable())")
Note how I print the value from both inside and outside of the function. Inside, it works every single time. I am struggling with the "from outside:" part.
Sometimes the function returns the correct email, but sometimes it returns "" (presumably, the value was not set in the for loop).
How can I add "blocking" functionality so calling returnLatestEmailAddressFromEmailsTable() will always first evaluate the for loop, and only after this return the value?

Resources