This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal


May 17, 2017, 7:51 AM
9 Posts

Java agent - convert JSON to Notes documents

  • Category: Domino Designer
  • Platform: All Platforms
  • Release: 9.0.1
  • Role: Developer
  • Tags: agent,java,json
  • Replies: 2

I need to read mesasges from Websphere MQ that are in JSON format. The JSON should be converted to Notes documents. Does anyone has any guidelines / instructions which I can use?

May 17, 2017, 12:21 PM
323 Posts
And how do they map?

When you get to developing some scripts for importing, there are decisions you need to make.

Any objects within the main JSON object would need to be mapped to something: Notes documents contain individual fields with no subfields, excepting (up to 64k of) multiple values for a field.

What'd you like to do with the JSON?

Clearly the JSON could be pushed directly into a richtext object if you want that.

May 17, 2017, 7:06 PM
82 Posts
Here's a sample to get you started ...
import lotus.domino.*;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import org.json.JSONObject;
import org.json.JSONException;

import java.util.*;


public class JavaAgent extends AgentBase {
        JSONObject MESSAGE_SELECTION = null;
        String jsonName="";
        public void NotesMain() {
                try {
                        Session session = getSession();
                        AgentContext agentContext = session.getAgentContext();
                        Document doc = agentContext.getDocumentContext();  //incoming mail message
                        //Document doc = agentContext.getUnprocessedDocuments().getFirstDocument();  //used for debug
                        removeItems(doc);
                        if (doc.getItemValueString("Subject").equals("")){
                                doc.replaceItemValue("Subject", "**NO DEFECT TITLE**");
                        }
                        doc.replaceItemValue("Form", "Defect");
                        doc.replaceItemValue("Status",        "Needs Triage");
                        Vector defectID = session.evaluate("@Unique");
                        doc.replaceItemValue("DefectID",defectID.toString().replaceAll("[^A-Za-z0-9 ]", ""));
                        Item readbody = doc.getFirstItem("Body");
                        String bodyText = scrubText(readbody.getText().replaceAll("[\n]",""));
                        String jsonText = "";
                        int  defInfoPosStart, defInfoPosEnd ;
                        defInfoPosStart = bodyText.indexOf ("DefectInfoStart") + 15 ;
                        defInfoPosEnd = bodyText.indexOf ("DefectInfoEnd");        
                        jsonText = bodyText.substring (defInfoPosStart, defInfoPosEnd) ;

                        try{
                                MESSAGE_SELECTION = new JSONObject(jsonText);
                                Iterator props = MESSAGE_SELECTION.keys();
                                while (props.hasNext()){
                                        jsonName = (String)props.next();
                                        doc.replaceItemValue(jsonName, MESSAGE_SELECTION.getString(jsonName));
                                }
                                doc.computeWithForm(false,false);
                                doc.save();
                        }catch(JSONException Je){
                                Je.printStackTrace();
                        }

                } catch(Exception e) {
                        e.printStackTrace();
                }
        }

        /*
         * remove all ascii 13 char from the json string that will be passed to JSONObject constructor
         */
        public String scrubText(String jsonText){
                String returnText="";
                for (int count=0; count < jsonText.length(); count++)
                {
                        if ((int)jsonText.charAt(count)!=13){
                                returnText += jsonText.charAt(count);
                        }
                }
                return returnText;
        }

        /*
         * Strip all unnecessary items from the Note
         */
        public void removeItems(Document mailDoc){
                String[] keeperItems = {"From","INetFrom","Body","Subject","$FILE"};
                try{
                        Vector items = mailDoc.getItems();
                        for (int i=0; i<items.size(); i++) {
                                Item item = (Item)items.elementAt(i);
                                if (Arrays.asList(keeperItems).indexOf(item.getName())<0){
                                        item.remove();
                                }
                        }
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
       
        public String encodeURL(String url) {
                try {
                        return URLEncoder.encode(url, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                        return url;
                }

       
}

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal