Java API Client able to add studies to StudyProperties query

LabKey Support Forum
Java API Client able to add studies to StudyProperties query Jason  2012-02-10 13:56
Status: Closed
 
I am brand new to Labkey Server and client API. I have local installation of Labkey Server running and I am trying to use the Java API to add studies under a project folder. My questions are as follows:

1. Is it possible to have multiple studies under one project folder?
2. Why am I unable to execute the following code even though I am a Site Administrator? I get a 401 Unauthorized response code, but I am able to select and update from the same schema/table.

Connection cn = new Connection("http://localhost:8080/labkey", "email address goes here", "password goes here");

InsertRowsCommand cmd = new InsertRowsCommand("study", "StudyProperties");

Map<String, Object> row = new HashMap<String, Object>();
row.put("Label", "This is a study label");   
row.put("Investigator", "Smith, John");           

cmd.addRow(row);
        
SaveRowsResponse resp;
try {
    resp = cmd.execute(cn, "Project folder name goes here");
    System.out.println(resp.getRowsAffected());
}
catch (CommandException e) {           
    System.out.println(e.getStatusCode());
    System.out.println(e.getMessage());
    System.out.println(e.getResponseText());
}

Any help would be greatly appreciated!
 
 
Matthew Bellew responded:  2012-02-10 14:26
Hi Jason,

It's great you got this far. However the Java API is focused mostly on querying and updating data, and not higher level operations like 'create a study'. Without investigating, I suspect this error is because there is no study-defined yet, so it won't allow you to update the study properties.

I think you might want to look into the documentation for our "study archive" format. The study archive is a .zip of files containing the study data and meta-data.

   https://www.labkey.org/wiki/home/Documentation/page.view?name=importExportStudy

You can then simply create a new folder and import. We support one study per folder, but multiple folders per project, so multiple studies per project are supported. To get started you can look at the demo study archive.

   https://www.labkey.org/wiki/home/Documentation/page.view?name=setupDemoStudy

and checkout the tutorials.

I hope that helps you get started,
Matt
 
Jason responded:  2012-03-02 12:40
I am trying to integrate LabKey with another application. I need to be able to create studies in LabKey from the other application in real time. So, the import study archive won't work unfortunately. Is there another API or method I can try?

Thanks,
Jason
 
Matthew Bellew responded:  2012-03-05 11:14
I'm pretty sure we can get this to work pretty well, but I think this requires a long answer. First, you can always call our URLs directly to invoke the functionality of the server. There are cases when useful functionality has not yet been wrapped as a documented javascript API, or where it's awkward to wrap it.

In this case folder creation can be accomplished by posting to "/{contextPath}/admin/{parent folder path}/createFolder.api". This action expects there parameters:

        String name;
        String folderType
        String activeModules[]
        String defaultModule

Likewise a study can be imported either by posting a study archive file or a path to study archive file using "{contextPath}/study/{path}/importStudy.api" (or importStudyFromPipeline.api).

ImportStudy just takes a multi-part encode POST with one file for the study archive.

NOTE that in 12.1 we are releasing a "folder archive" feature similar to the study archive feature. This might be useful as you can create a template folder and then export it. This would capture folder type, active modules, default web part layouts, etc.

Then you would simply post the folder archive, followed by the study archive.

It might be easiest to prototype this with and HTML/Javascript page, using form posts, then translate the result into working Java code. Feel free to follow up with me in email or on the phone to get this working.

Matt