To continue with my JDBC experience in Domino , Today I've written a class to log an activity in my application.It may not be designed as per Java Standards or conventions.However,I am willing to know what could be the better approach to design Java classes to avail high performance.Here is my code ,


import lotus.domino.*;

public class activityClass extends AgentBase
{
public Session session;
public AgentContext agentContext;
public RichTextItem activityLog;
public RichTextStyle RtStyle1;
public Document logdoc;
public Database db;
public Database logdb;


public activityClass(Session session)
{
try{
agentContext= session.getAgentContext();
db = agentContext.getCurrentDatabase();
Document doc = db.getProfileDocument("connectionSetup","");
String ldb = doc.getItemValueString("ActivityLogDbName");
logdb = session.getDatabase(db.getServer(), ldb);
String UserName=session.getUserName();

DateTime dt = session.createDateTime("Today");
dt.setNow();

RtStyle1=session.createRichTextStyle();
RtStyle1.setBold(RichTextStyle.NO);
RtStyle1.setFontSize(8);
RtStyle1.setFont(RichTextStyle.FONT_HELV);
RtStyle1.setColor(RichTextStyle.COLOR_BLACK);

if ( logdb!=null){
logdoc = logdb.createDocument();
logdoc.appendItemValue("Form", "ActivityLog");
logdoc.replaceItemValue("Activity$Status", "");
logdoc.replaceItemValue("Activity$StartDtTime", dt.getLocalTime());
logdoc.replaceItemValue("Activity$StartedBy", UserName);
logdoc.replaceItemValue("Activity$EndDtTime", "");
activityLog = logdoc.createRichTextItem("Activity$Log");
}
}catch(Exception e){ e.printStackTrace(); }


}

public void addActivity(Session session,String whatActivity)
{
try{
DateTime dt = session.createDateTime("Today");
dt.setNow();
String logMessage=dt.getLocalTime() + " - " + whatActivity;
activityLog.appendStyle(RtStyle1);
activityLog.appendText(logMessage);
activityLog.addNewLine(1);
}catch(Exception e){ e.printStackTrace(); }

}

public void endActivity(Session session,String whatActivity)
{
try{
DateTime dt = session.createDateTime("Today");
dt.setNow();
String logMessage=dt.getLocalTime() + " - " + whatActivity;
activityLog.appendStyle(RtStyle1);
activityLog.appendText(logMessage);
activityLog.addNewLine(1);
}catch(Exception e){ e.printStackTrace(); }
}

public void closeActivity(Session session)
{
try{
DateTime dt = session.createDateTime("Today");
dt.setNow();
String logMessage=dt.getLocalTime() + " - " + "Activity closed";
activityLog.appendStyle(RtStyle1);
activityLog.appendText(logMessage);
activityLog.addNewLine(1);
logdoc.replaceItemValue("Activity$EndDtTime", dt.getLocalTime());
logdoc.save(true);
}catch(Exception e){ e.printStackTrace(); }
}
}


How I call,


public class JavaAgent extends AgentBase {
public void NotesMain() {
String qs;
try {

Session session = getSession();
AgentContext agentContext = session.getAgentContext();
activityClass activityLog= new activityClass(session);

activityLog.addActivity(session,"Start processing table1.");
Table1 t1 = new Table1(db);
qs= t1.ManageTable1(db,cdoc);
if(qs!=null){
activityLog.endActivity(session,qs);
}
}

}

activityLog.closeActivity(session);
catch(Exception e) {e.printStackTrace();}
}
}


Here is my log output,





To be continued....

3 Responses to "JDBC connectivity with Oracle in Domino-Part IV"

  1. gravatar NotesSensei Says:

    YOu need to move your stuff into NotesMain(), not in the constructor.

  2. gravatar Rishi Says:

    @ NotesSensei,

    I've already tried putting all the in "NotesMain()" but ended up with same "NullPointerException".Actually while doing analysis , I've found without passing "Session" as a argument my program were unable to initialize Session.Posted the code, What do you say ??

  3. gravatar Rishi Says:

    A few suggestions from a long time Java Developer:

    Java classes use 1st letter Capitalization as a convention:

    public class activityClass extends AgentBase {
    public class ActivityClass extends AgentBase {

    Conversely variables start with lowercase letters:

    String UserName = session.getUserName();
    String userName = session.getUserName();

    as do methods:

    qs= t1.ManageTable1(db,cdoc);
    qs= t1.manageTable1(db, cdoc);

    Catching and printing Exceptions is a mistake for more reasons than I have time to explain. Suffice to say you should be throwing the checked exceptions from your methods unless you plan to do something about them.

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

    Encapsulation is an important part of writing Java objects. These variables should have private accessors.

    public Session session;
    public AgentContext agentContext;
    public RichTextItem RtActivityLog;
    public RichTextStyle RtStyle1;
    public Document logdoc;
    public Database db;
    public Database logdb;

    also I recommend you get a code formatter and Java IDE. Eclipse is free and is an excellent IDE with suitable Java code formatting abilities. It will also enable you to step through your code in debug mode so you can isolate bugs like your NullPointerException very quickly.

    Posted by Paul Hill

Leave a Reply

preload preload preload