MarkLogic: Committing multiple statements within a single transaction - nosql

I want to version a document, for which we are followed below approach to check-out and check-in the document in a single transaction.
(:--------------------------- XQuery Starts ---------------------------:)
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare function local:ManageDocument($docUri)
{
let $query := fn:concat('
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare variable $docUri as xs:string external;
dls:document-manage($docUri,fn:false(),fn:concat("First Version of ", $docUri))'
)
return xdmp:eval(
$query,
(xs:QName("docUri"), $docUri),
<options xmlns="xdmp:eval">
<prevent-deadlocks>true</prevent-deadlocks>
</options>
)
};
let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged := dls:document-is-managed($docUri)
let $manageDoc := if($isManaged) then() else local:ManageDocument($docUri)
let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut := if($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())
return $manageDoc
(:--------------------------- XQuery Ends ---------------------------:)
However, it is throwing below exception:
[1.0-ml] XDMP-PREVENTDEADLOCKS: xdmp:eval("
xquery version "1.0-
ml";
declare ...", (fn:QName("","docUri"), "/searchable
/as-2018-1981_standard.pdf.xml"), <options xmlns="xdmp:eval"><prevent-
deadlocks>true</prevent-deadlocks></options>) -- Processing an update from an
update with different-transaction isolation could deadlock
To overcome this I modified the XQuery to solve our purpose:
(:--------------------------- New XQuery Starts ---------------------------:)
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare function local:ManageDocument($docUri)
{
let $query := fn:concat('
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare variable $docUri as xs:string external;
dls:document-manage($docUri,fn:false(),fn:concat("First Version of ", $docUri))'
)
return xdmp:eval(
$query,
(xs:QName("docUri"), $docUri),
<options xmlns="xdmp:eval">
<prevent-deadlocks>true</prevent-deadlocks>
</options>
)
};
declare function local:CheckouotDocument($docUri)
{
let $query := fn:concat('
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare variable $docUri as xs:string external;
(: dls:document-checkout($docUri, fn:true(), "updating doc", 3600) :)
dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())
')
return xdmp:eval(
$query,
(xs:QName("docUri"), $docUri),
<options xmlns="xdmp:eval">
<prevent-deadlocks>true</prevent-deadlocks>
</options>
)
};
let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged := dls:document-is-managed($docUri)
let $manageDoc := if($isManaged) then() else local:ManageDocument($docUri)
let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut := if($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else local:CheckouotDocument($docUri)
return $manageDoc
(:--------------------------- New XQuery Ends ---------------------------:)
Above XQuery is working as expected, however, it would be great if anyone can help me in solving my purpose in more efficient and simplified manner.

You could simplify your code a bit by using xdmp:invoke-function() instead of constructing strings for xdmp:eval(), and avoid repeating the options by declaring a variable:
xquery version "1.0-ml";
declare namespace html = "http://www.w3.org/1999/xhtml";
import module namespace dls = "http://marklogic.com/xdmp/dls" at "/MarkLogic/dls.xqy";
declare variable $OPTIONS :=
<options xmlns="xdmp:eval">
<prevent-deadlocks>true</prevent-deadlocks>
</options>;
declare function local:ManageDocument($docUri)
{
xdmp:invoke-function(function() {
dls:document-manage($docUri, fn:false(), "First Version of "||$docUri)
}, $OPTIONS)
};
declare function local:CheckouotDocument($docUri)
{
xdmp:invoke-function(function() {
(: dls:document-checkout($docUri, fn:true(), "updating doc", 3600) :)
dls:document-checkout-update-checkin($docUri, element {"ROOT"} {"HELLO WORLD!"}, "document-checkout-update-checkin", fn:true())
}, $OPTIONS)
};
let $docUri := "/searchable/as-2018-1981_standard.pdf.xml"
let $isManaged := dls:document-is-managed($docUri)
let $manageDoc := if ($isManaged) then() else local:ManageDocument($docUri)
let $chechoutStatus := dls:document-checkout-status($docUri)
let $checkOut := if ($chechoutStatus and $isManaged) then (fn:error(xs:QName('Error'), "Already Editing")) else local:CheckouotDocument($docUri)
return $manageDoc

Related

Custom command line parameter - distinguish nonexistence from blank? [duplicate]

I am new to Inno Setup and I have already read the documentation. Now I know that Inno Setup can accept different/custom parameter and could be processed via Pascal script. But the problem is, I don't know how to write in Pascal.
I am hoping I could get help about the coding.
I'd like to pass /NOSTART parameter to my setup file which while tell the setup to disable(uncheck) the check mark on "Launch " and if /NOSTART is not provided, it it will enable(check) the check mark "Launch "
or if possible, that Launch page is not required and do everything via code.
Since you can't imperatively modify flags for section entries and directly accessing the RunList would be quite a dirty workaround, I'm using for this two postinstall entries, while one has no unchecked flag specified and the second one has. So, the first entry represents the checked launch check box and the second one unchecked launch check box. Which one is used is controlled by the Check parameter function, where is checked if a command line tail contains /NOSTART parameter.
Also, I've used a little more straightforward function for determining if a certain parameter is contained in the command line tail. It uses the CompareText function to compare text in a case insensitive way. You can replace it with CompareStr function, if you want to compare the parameter text in a case sensitive way. Here is the script:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[Run]
Filename: "calc.exe"; Description: "Launch calculator"; \
Flags: postinstall nowait skipifsilent; Check: LaunchChecked
Filename: "calc.exe"; Description: "Launch calculator"; \
Flags: postinstall nowait skipifsilent unchecked; Check: not LaunchChecked
[Code]
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
function LaunchChecked: Boolean;
begin
Result := not CmdLineParamExists('/NOSTART');
end;
and so a little research read and read .. i got my answer.
here's my code (except the "GetCommandLineParam")
[Code]
{
var
StartNow: Boolean;
}
function GetCommandLineParam(inParam: String): String;
var
LoopVar : Integer;
BreakLoop : Boolean;
begin
{ Init the variable to known values }
LoopVar :=0;
Result := '';
BreakLoop := False;
{ Loop through the passed in arry to find the parameter }
while ( (LoopVar < ParamCount) and
(not BreakLoop) ) do
begin
{ Determine if the looked for parameter is the next value }
if ( (ParamStr(LoopVar) = inParam) and
( (LoopVar+1) <= ParamCount )) then
begin
{ Set the return result equal to the next command line parameter }
Result := ParamStr(LoopVar+1);
{ Break the loop }
BreakLoop := True;
end;
{ Increment the loop variable }
LoopVar := LoopVar + 1;
end;
end;
{
function InitializeSetup(): Boolean;
var
NOSTART_Value : String;
begin
NOSTART_Value := GetCommandLineParam('/NOSTART');
if(NOSTART_Value = 'false') then
begin
StartNow := True
end
else
begin
StartNow := False
end;
Result := True;
end;
}
procedure CurStepChanged(CurStep: TSetupStep);
var
Filename: String;
ResultCode: Integer;
NOSTART_Value : String;
begin
if CurStep = ssDone then
begin
NOSTART_Value := GetCommandLineParam('/NOSTART');
if(NOSTART_Value = 'false') then
begin
Filename := ExpandConstant('{app}\{#MyAppExeName}');
Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
end
end;
end;
a code update. Thanks to #TLama
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Break;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
Filename: String;
ResultCode: Integer;
NOSTART_Value : String;
RunApp : Boolean;
begin
if CurStep = ssDone then
begin
RunApp := CmdLineParamExists('/START');
if(RunApp = True) then
begin
Filename := ExpandConstant('{app}\{#MyAppExeName}');
Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
end
// NOSTART_Value := GetCommandLineParam('/START');
// if(NOSTART_Value = 'true') then
// begin
// Filename := ExpandConstant('{app}\{#MyAppExeName}');
// Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
//end
end;
end;
How about the following, easy to read
; Script generated by the Inno Script Studio Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "Install Specialty Programs"
#define MyAppVersion "1.0"
#define MyAppPublisher ""
[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{5}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\{#MyAppName}
DisableDirPage=yes
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputDir=P:\_Development\INNO Setup Files\Specialty File Install
OutputBaseFilename=Specialty File Install
Compression=lzma
SolidCompression=yes
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Files]
Source: "P:\_Development\INNO Setup Files\Specialty File Install\Files\0.0 - Steps.docx"; DestDir: "c:\support\Specialty Files"; Tasks: V00Step
[Tasks]
Name: "Office2013"; Description: "Running Office 2013"; Flags: checkablealone unchecked
Name: "Office2016"; Description: "Running Office 2016"; Flags: checkablealone unchecked
Name: "V00Step"; Description: "Steps To Follow (Read Me)"; Flags: exclusive
[Run]
Filename: "C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE"; Parameters: """c:\support\Specialty Files\0.0 - Steps.docx"""; Description: "Run if Office 2013 is installed"; Tasks: V00Step AND Office2013
Filename: "C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EXE"; Parameters: """c:\support\Specialty Files\0.0 - Steps.docx"""; Description: "Run if Office 2016 is installed"; Tasks: V00Step AND Office2016

RegWrite not writing to the registry

ValueType := A_Args[1]
KeyName := A_Args[2]
ValueName := A_Args[3]
ValueData := A_Args[4]
Loop, %0%
params .= A_Space %A_Index%
; https://autohotkey.com/docs/Run#RunAs
full_command_line := DllCall("GetCommandLine", "str")
if !(A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")) {
try {
if A_IsCompiled
Run *RunAs "%A_ScriptFullPath%" "%params%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%" "%params%"
}
ExitApp
}
RegWrite, % ValueType, % KeyName, % ValueName, % ValueData
Why is RegWrite not writing to the registry when I pass parameters to the script?
A_LastError codes
Code 87 means an invalid parameter. What are you passing to RegWrite?
Here's one function I use for debugging. If isCondition is true it shows a custom error message and stops everything.
fAbort(isCondition, sFuncName, sNote, dVars:="") {
If isCondition {
sAbortMessage := % sFuncName ": " sNote
. "`n`nA_LineNumber: """ A_LineNumber """`nErrorLevel: """ ErrorLevel """`nA_LastError: """ A_LastError """`n"
For sName, sValue in dVars
sAbortMessage .= "`n" sName ": """ sValue """"
MsgBox, 16,, % sAbortMessage
ExitApp
}
}
After a RegWrite it could be used like this:
fAbort(ErrorLevel ; 1, if RegWrite unsuccessful.
, "Script or function name here" ; Could use A_ThisFunc for current function name.
, "Registry write unsuccessful." ; Your custom message here.
, { x: "blabla", y: 13 } ; Additional vars you want to see in the msgbox.
)

How to post data with a ContentType of 'multipart/form-data' in Delphi REST?

I am trying to send a request to a REST API using multipart/form-data as the content type.
I always get "HTTP/1.1 500 Internal Error" as a response.
I tried sending requests to methods that require application/x-www-form-urlencoded and had success, though.
How can I achieve getting a success response from my API using multipart/form-data?
Here is my code:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
Input: TIdMultipartFormDataStream;
begin
Input := TIdMultipartFormDataStream.Create;
Input.Clear;
Input.AddFormField('Email', 'tugba.xx#allianz.com.tr');
Input.AddFormField('Password', 'xxxx');
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
RESTRequest1 := TRESTRequest.Create(nil);
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddBody(Input,TRESTContentType.ctMULTIPART_FORM_DATA);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
end;
Embarcadero's REST component has its own built in multipart/form-data capabilities via the TRESTRequest.AddParameter() method:
procedure TForm10.Button1Click(Sender: TObject);
var
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
strImageJSON : string;
begin
RESTClient1 := TRESTClient.Create('http://192.168.1.172:81/');
try
RESTRequest1 := TRESTRequest.Create(nil);
try
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'api/Mobile/MobileLoginControl';
RESTRequest1.AddParameter('Email', 'tugba.xx#allianz.com.tr', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.AddParameter('Password', 'xxxx', TRESTRequestParameterKind.pkREQUESTBODY);
RESTRequest1.Client := RESTClient1;
RESTRequest1.Execute;
strImageJSON := RESTRequest1.Response.Content;
finally
RESTRequest1.Free;
end;
finally
RESTClient1.Free;
end;
end;
You don't need to use use Indy's TIdMultiPartFormDataStream, especially when you are not using Indy's TIdHTTP.

dsRESTConnection component won't allow me change the .username and .password after the first connect

In my Delphi 10.3.1 application server (which is a "DataSnap REST Application"), I have put some logging as follows:
procedure TWebModule1.DSAuthenticationManager1UserAuthenticate(
Sender: TObject; const Protocol, Context, User, Password: string;
var valid: Boolean; UserRoles: TStrings);
begin
valid := (User > '') and (Password > ''); // pass-through
if Valid then
begin
MyWebUtils.LogMessage('Authentication, ' + User + ', ' + Password +
', ' + protocol + ', ' + context);
TDSSessionManager.GetThreadSession.PutData('USERNAME', User);
end;
end;
In my client (on Android), which uses a dsRestConnection, I have a procedure to test the connection, which I call, for example in FormShow of the main form.
function TClientModuleRest.TestConnect(var ErrorMsg: String): Boolean;
var
returned: String;
begin
Result := True;
try
DSRestConnection.Username := 'Admin';
DSRestConnection.Password := 'Test';
DSRestConnection.TestConnection([toNoLoginPrompt]);
except
on e: exception do
begin
Result := False;
ErrorMsg := e.Message;
end
end;
end;
This works well.
The problem occurs when I call the first Server method.
function TClientModuleRest.ValidUser(username, password: String;
var Response: String): Integer;
var
Server: TServerMethodsRClient;
Valid: Boolean;
IniFile: TStringList;
FileName: TFileName;
begin
Result := 0;
DSRestConnection.UserName := username;
DSRestConnection.Password := password;
try
Server := TServerMethodsRClient.Create(DSRestConnection);
try
Valid := Server.ValidUser(username, password, Response);
if not Valid then
raise Exception.Create(response);
Result := 1;
finally
Server.Free;
end;
except
on e: exception do
begin
Response := e.Message;
Result := -1; // an error occured
end;
end;
end;
So the problem is that the setting of the .username and .password doesn't 'take'. In other words, my conclusion is that the .username and `.password can only be set before the first connect, and not thereafter.
The server log file shows that even though the ValidUser function was called with the username and password values that were passed to it, the authentication is receiving the username and password from the first (test) connect.
Is this by design? Or am I missing something? Thanks.

using xdmp:eval in REST Service in Marklogic

I am using the below REST service to insert the document(from DB Ingestion) into Rest DB which is configured for this service. I am using POST method and sending parameters in form-data.
URL: http://{hostname}:8010/v1/addDocument.
The issue is that the xml($DirectoryListing) picked from Ingestion DB is getting automatically serialized into text, so I am not getting the xml for insertion.
This is working fine when this code is directly ran on Qconsole, but from rest service call its not working.
ML Version I am using is 7.0-2.3​
Please help me on this.
let $PrefixURI := "/docs/"
let $Path := fn:concat('/processing/',$transId,'/validDocs/')
let $_ := xdmp:log(fn:concat("Path",$Path))
let $DirectoryListing as node() :=
xdmp:invoke-function(
function() {
xdmp:directory($Path)
} ,
<options xmlns="xdmp:eval">
<database>{xdmp:database("Ingestion")}</database>
</options>
)
let $logs :=
element docsUris {
for $FileEntry in $DirectoryListing
let $Filename := functx:substring-after-last(xdmp:node-uri($FileEntry),'/')
let $docUri := fn:concat($PrefixURI, $Filename)
let $_ := xdmp:log(fn:concat("uri",$docUri))
let $contents := $FileEntry
let $_ := xdmp:log(fn:concat("adding document",$contents))
return
(: Insert the document :)
(dls:document-insert-and-manage(
$docUri,
fn:false(),
$contents,
"created",
(xdmp:permission("dls-user", "read"),
xdmp:permission("dls-user", "update")),
"historic"),
xdmp:document-add-collections(
$docUri,
"latest"),
xdmp:document-remove-collections(
$docUri, "historic")
)