Custom code questions.

0 votes
Hello,
 
In the code below, how do you enable storage read and write? 
The guide asks ... 
 
Thank you. 
 
 
 
package com.shephertz.app42.paas.customcode.sample;
 
import java.util.HashMap;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import com.shephertz.app42.paas.customcode.Executor;
import com.shephertz.app42.paas.customcode.HttpRequestObject;
import com.shephertz.app42.paas.customcode.HttpResponseObject;
import com.shephertz.app42.paas.sdk.java.ServiceAPI;
import com.shephertz.app42.paas.sdk.java.log.LogService;
 
 
public class MyCustomCode implements Executor {
 
private ServiceAPI sp = new ServiceAPI(  
    "",
     "");
 
 
private final int HTTP_STATUS_SUCCESS = 200;
 
private String moduleName = "App42CustomCodeTest";
 
 
/** 
* Write your custom code inside this method 
*/
@Override
public HttpResponseObject execute(HttpRequestObject request) {
JSONObject body = request.getBody();
 
// Build Log Service For logging in Your Code
LogService logger = sp.buildLogService();
logger.debug(" Recieved Request Body : :" + body.toString(), moduleName);
 
// Write Your Custom Code Here
// ......//
 
logger.info("Running Custom Code Hello World  ", moduleName);
 
// Create JSON Response Based on Your business logic
JSONObject jsonResponse = new JSONObject();
try {
jsonResponse.put("name", "App42CustomCodeTest_by doojin code!!");
//....//
//....//
} catch (JSONException e) {
// Do exception Handling for JSON Parsing
}
// Return JSON Response and Status Code
return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);
}
 
}
 
 

 

related to an answer for: Compensation once a day
asked Sep 16, 2014 in App42 Cloud API-BaaS by jeongdoojin8 (10 points)

1 Answer

0 votes

Hello,

For using any service inside Custom code, you have to build the service using its builder method first, just like buildLogService as shown in below snippet, you can build Storage Service too. Using that service reference you can call its helper method like findDocumentByKeyValue or findDocumentById method in custom code as explained below:

 

package com.shephertz.app42.paas.customcode.sample;

 

import java.util.HashMap;

 

import org.json.JSONException;

import org.json.JSONObject;

 

import com.shephertz.app42.paas.customcode.Executor;

import com.shephertz.app42.paas.customcode.HttpRequestObject;

import com.shephertz.app42.paas.customcode.HttpResponseObject;

import com.shephertz.app42.paas.sdk.java.ServiceAPI;

import com.shephertz.app42.paas.sdk.java.storage.Storage;

import com.shephertz.app42.paas.sdk.java.storage.StorageService;

 

 

public class MyCustomCode implements Executor {

 

private ServiceAPI sp = new ServiceAPI(    

    "",

     "");

 

 

private final int HTTP_STATUS_SUCCESS = 200;

 

 

/** 

* Write your custom code inside this method 

*/

@Override

public HttpResponseObject execute(HttpRequestObject request) {

JSONObject body = request.getBody();

 

// Build Storage Service For logging in Your Code

StorageService storageService = sp.buildStorageService();

 

// Write Your Custom Code Here

// ......//

 

Storage storage = storageService.insertJSONDocument("test", "query",

                                                "{\"UniqueIdOfUser\":\"1234\",\"Info\":\"Login id of user.\"}");

 

// Create JSON Response Based on Your business logic

JSONObject jsonResponse = new JSONObject();

try {

jsonResponse.put("response", storage.toString());

//....//

//....//

} catch (JSONException e) {

// Do exception Handling for JSON Parsing

}

// Return JSON Response and Status Code

return new HttpResponseObject(HTTP_STATUS_SUCCESS, jsonResponse);

}

 

Let us know if it helps.

Thanks,

Himanshu Sharma

 

answered Sep 16, 2014 by sushil (11 points)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...