Search code examples
javajsonxpages

XPages - Calling Java methods


What is wrong with this java code? I am not able to call this in XPage.

I am trying to create a JSON string so that I can refer it in my Javascript variables for client-side sorting.

/**
 * 
 */
package com.company.apse.AccReq;

/**
 * @author Arun
 *
 */

import java.io.*;

import lotus.domino.*;
import com.ibm.domino.services.util.JsonWriter;
import com.ibm.xsp.extlib.util.ExtLibUtil; 

public class AccReqJson {

    /**
     * @param args
     */
    public static String mainly(String[] args) {
        // TODO Auto-generated method stub
        try{
            System.out.println("calling mainly");
            Database database = ExtLibUtil.getCurrentDatabase();
            View view = database.getView("(All_AccessRequests_SingleEntry)");

            //get access request document
            Document document = view.getFirstDocument();
            StringWriter sw = new StringWriter();
            JsonWriter g = new JsonWriter(sw,true);
            //we need to loop through all the documents
            while (document != null){

                int j = 0;  
                //now loop through all the 80 fields for user name/email/access
                for (j=1;j<=80;j++){
                    //System.out.println(document.getItemValueString((String) ("UserName_"+j)));
                    if (!document.getItemValueString((String) ("UserName_"+j)).equalsIgnoreCase("")){
                        //now begin writing JSON

                        g.startArrayItem();             
                        g.startObject();

                        //finally add unid of document
                        g.startProperty("UNID");
                        g.outStringLiteral(document.getUniversalID());
                        g.endProperty();

                        //finally add unid of document
                        g.startProperty("J");
                        g.outStringLiteral((String)(""+ j));
                        g.endProperty();

                        //Get Access Type Type (Existing HCL, New HCL, New Client, Change Access)
                        g.startProperty("AccessRequestFor");
                        if (j<=40){
                            g.outStringLiteral("Existing HCL"); 
                        }
                        else if(j<=50){
                            g.outStringLiteral("New Client");
                        }
                        else if(j<=60){
                            g.outStringLiteral("New HCL");
                        }
                        else{
                            g.outStringLiteral("Change Access");
                        }

                        g.endProperty();

                        //get APSE Name
                        g.startProperty("APSE");
                        g.outStringLiteral(document.getItemValueString("APSEName"));
                        g.endProperty();

                        //get User Name
                        g.startProperty("UserName");
                        g.outStringLiteral(document.getItemValueString((String) ("UserName_"+j)));
                        g.endProperty();

                        //get Email
                        g.startProperty("UserEmail");
                        g.outStringLiteral(document.getItemValueString((String) ("Email_"+j)));
                        g.endProperty();

                        //get Access Type
                        String tmpAccess=""; 
                        String tmpExtraAccess="";
                        tmpAccess = document.getItemValueString((String) ("UserAccess_"+j));
                        if (tmpAccess.indexOf("<br />")!=-1)
                        {
                            if (tmpAccess.lastIndexOf("<br />")!= tmpAccess.indexOf("<br />")){
                                tmpExtraAccess = tmpAccess.substring(tmpAccess.indexOf("<br />")+6, tmpAccess.length()-1);  
                            }
                            tmpAccess = tmpAccess.substring(0, tmpAccess.indexOf("<br />"));
                        }

                        g.startProperty("UserAccess");
                        g.outStringLiteral(tmpAccess);
                        g.endProperty();

                        g.startProperty("UserExtraAccess");
                        g.outStringLiteral(tmpExtraAccess.replaceAll("<br />",", "));
                        g.endProperty();

                        //Raised Date
                        g.startProperty("AccessRequestDate");
                        g.outStringLiteral(document.getItemValueDateTimeArray("ACCESSREQUESTEDDATE").firstElement().toString());
                        g.endProperty();

                        //Raised By
                        g.startProperty("AccessRequestor");
                        g.outStringLiteral(document.getItemValueString("ACCESSREQUESTER"));
                        g.endProperty();

                        //Remedy Ticket Number
                        g.startProperty("RemedyRef");
                        g.outStringLiteral(document.getItemValueString("MagicRef"));
                        g.endProperty();



                        g.endObject();        
                        g.endArrayItem();   
                    }
                }
            //get next document
            document = view.getNextDocument(document);  
            }
            System.out.println(sw.toString());
            return sw.toString();
        }

        catch(Exception ex) {
            // tbd: handle exception
            System.out.println(ex.getStackTrace().toString());
            return "An Error occured. Check with IT team.";
        }
    }
}

Solution

  • Do you try to call a static function from an object? Have you tried it without the keyword static ?
    And you could remove the "String[] args" ....