Devesh,

While you do have 'declare option xdmp:update "true"', it's in a library 
module; this option needs to be in the main module to take effect. When 
extending the REST API, the default assumptions are the GET requests are 
queries, while, PUT, POST, and DELETE are updates. You can override this 
behavior with a transaction mode annotation on the function itself. See 
Controlling Transaction 
Mode<http://docs.marklogic.com/guide/rest-dev/extensions#id_17130> in the REST 
Application Developer's Guide<http://docs.marklogic.com/guide/rest-dev> for 
details.

Since you're doing something that changes the database, you should probably use 
POST or PUT rather than GET.

Dave.

--
Dave Cassel
Developer Community Manager
MarkLogic Corporation<http://www.marklogic.com/>
Cell:  +1-484-798-8720


From: <Tyagi>, Devesh <[email protected]<mailto:[email protected]>>
Reply-To: MarkLogic Developer Discussion 
<[email protected]<mailto:[email protected]>>
Date: Thursday, October 23, 2014 at 12:43 PM
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: [MarkLogic Dev General] XDMP-UPDATEFUNCTIONFROMQUERY - Cannot apply an 
update function from a query.


Hi,


I was trying to create an extension of dls API in Java. To achieve this I tried 
the following code with the examples in the Java API cookbook as the guide :


Java code :

MANAGER CLASS EXTENDING RESOURCEMANAGER

static public class dlsManager extends ResourceManager {
static final public String NAME = "dlsQuery";
private XMLDocumentManager docMgr;
String uri = "/resourceExtension/demo2.xml";

public dlsManager(DatabaseClient client) {
super();

// a Resource Manager must be initialized by a Database Client
client.init(NAME, this);

// the Dictionary Manager delegates some services to a document manager
docMgr = client.newXMLDocumentManager();
}
public void createDoc()throws ResourceNotFoundException, 
ForbiddenUserException, FailedRequestException {
StringBuilder builder = new StringBuilder();
builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
builder.append("<topLevel>\n");
builder.append("<level1>a</level1>");
builder.append("<level2>b</level2>");
builder.append("</topLevel>\n");

StringHandle writeHandle = new StringHandle();
writeHandle.set(builder.toString());

// delegate
docMgr.write(uri,writeHandle);

}
public void manageDoc()throws ResourceNotFoundException, 
ForbiddenUserException, FailedRequestException {
RequestParameters params = new RequestParameters();
params.add("uri", uri);
XMLStreamReaderHandle readHandle = new XMLStreamReaderHandle();

ResourceServices services = getServices();
services.get(params, readHandle);
}

}
TO INSTALL THE RESOURCE EXTENSION
public static void installResourceExtensionShortcut(DatabaseClient client) 
throws IOException {
// create a manager for resource extensions
ResourceExtensionsManager resourceMgr = 
client.newServerConfigManager().newResourceExtensionsManager();

// specify metadata about the resource extension
ExtensionMetadata metadata = new ExtensionMetadata();
metadata.setTitle("dls services to manage docs");
metadata.setDescription("to support checkin/checkout");
metadata.setProvider("MarkLogic");
metadata.setVersion("0.1");

// acquire the resource extension source code
InputStream sourceStream = Util.openStream(
"scripts"+File.separator+dlsManager.NAME+".xqy");
if (sourceStream == null)
throw new IOException("Could not read example resource extension");

// write the resource extension to the database
resourceMgr.writeServicesAs(dlsManager.NAME, sourceStream, metadata,
new MethodParameters(MethodType.GET));

System.out.println("(Shortcut) Installed the resource extension on the server");
}
public static void installResourceExtensionStrongTyped(DatabaseClient client) 
throws IOException {
// create a manager for resource extensions
ResourceExtensionsManager resourceMgr = 
client.newServerConfigManager().newResourceExtensionsManager();

// specify metadata about the resource extension
ExtensionMetadata metadata = new ExtensionMetadata();
metadata.setTitle("dls services to manage docs");
metadata.setDescription("to support checkin/checkout");
metadata.setProvider("MarkLogic");
metadata.setVersion("0.1");

// acquire the resource extension source code
InputStream sourceStream = Util.openStream(
"scripts"+File.separator+dlsManager.NAME+".xqy");
if (sourceStream == null)
throw new IOException("Could not read example resource extension");

// create a handle on the extension source code
InputStreamHandle handle = new InputStreamHandle();
handle.set(sourceStream);

// write the resource extension to the database
resourceMgr.writeServices(dlsManager.NAME, handle, metadata,
new MethodParameters(MethodType.GET));

System.out.println("(Strong Typed) Installed the resource extension on the 
server");
}

// use the resource manager
public static void useResource(String host, int port, String user, String 
password, Authentication authType)
throws ResourceNotFoundException, ForbiddenUserException, 
FailedRequestException {
// create the client
DatabaseClient client = DatabaseClientFactory.newClient(host, port, user, 
password, authType);
dlsManager mgr = new dlsManager(client);
mgr.createDoc();
client.release();
client = DatabaseClientFactory.newClient(host, port, user, password, authType);
mgr = new dlsManager(client);
mgr.manageDoc();

client.release();
}
Xquery code(dlsQuery.xqy):

xquery version "1.0-ml";

(: Copyright 2011-2014 MarkLogic Corporation.  All Rights Reserved. :)

module namespace dlsQuery = "http://marklogic.com/rest-api/resource/dlsQuery";;

import module namespace dls = "http://marklogic.com/xdmp/dls";
      at "/MarkLogic/dls.xqy";

declare option xdmp:update "true";

declare function dlsQuery:get(
    $context as map:map,
    $params  as map:map
) as document-node()*
{
let $uri := map:get($params,"uri")
return
dls:document-manage($uri,
                      fn:false(),
                      "example of managed doc from resourceextension")

};

The result which I get on executing this is

(Shortcut) Installed the resource extension on the server
(Strong Typed) Installed the resource extension on the server
Exception in thread "main" com.marklogic.client.FailedRequestException: Local 
message: failed to read resource at resources/dlsQuery: Internal Server Error. 
Server Message: XDMP-UPDATEFUNCTIONFROMQUERY: 
Q{http://marklogic.com/rest-api/resource/dlsQuery}get#2($context, 
$service-params) -- Cannot apply an update function from a query.  See the 
MarkLogic server error log for further detail.
at 
com.marklogic.client.impl.JerseyServices.checkStatus(JerseyServices.java:3738)
at 
com.marklogic.client.impl.JerseyServices.getResource(JerseyServices.java:2798)
at 
com.marklogic.client.impl.ResourceServicesImpl.get(ResourceServicesImpl.java:52)
at 
com.marklogic.client.impl.ResourceServicesImpl.get(ResourceServicesImpl.java:48)
at demo.ResourceExtension$dlsManager.manageDoc(ResourceExtension.java:118)

I also referred to the blog at 
http://blog.davidcassel.net/2011/10/queries-and-updates/​ but to no avail. Can 
anyone please suggest a solution to this? Any help would be appreciated.

Regards,
Devesh Tyagi


"This e-mail and any attachments transmitted with it are for the sole use of 
the intended recipient(s) and may contain confidential , proprietary or 
privileged information. If you are not the intended recipient, please contact 
the sender by reply e-mail and destroy all copies of the original message. Any 
unauthorized review, use, disclosure, dissemination, forwarding, printing or 
copying of this e-mail or any action taken in reliance on this e-mail is 
strictly prohibited and may be unlawful."
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to