適材適所

WindowsやPowerShellやネットワーク、IBMなどのシステム系の話やポイ活など気になったことも載せているブログです。

JavaでNotesカレンダーの情報を取得する

Notesを外部から操作したい

組織に所属していると大体グループウェアを使ってメールなどを管理されているかと思います。

グループウェアの走りとして第一線をかけてきたNotesですが、時代の流れの中、すでに生きる化石と言われることも・・・。

とは言っても組織で使っている以上、使い倒さないともったいない!!

使ってみると結構便利で、グループウェアのはしりということもあって、メールからタスク管理、スケジュール管理までこなすことができます。

Notesのカレンダー情報を取得したい

そんなNotesですが、組織の予定はNotesで管理していて、プライベートの予定はGoogleカレンダーを使っている、なんて人もいるかと思います。

せっかくなら同期したいと思った次第です。

今時のシステムなら外部連携が簡単にできますが、Notesでそれを実現するためには自作するかサードパーティーに頼らざるを得ませんでした。

しかし組織のポリシーの問題もあり、サードパーティーのアプリケーションをインストールすることもできないので自作することとしました。

その時に書きなぐったNotesカレンダーを取得するJavaプログラムの紹介です。

Notesへアクセスするための事前準備

NotesにはJavaのAPIが準備されています。

他にもCOMオブジェクトもあるので、VBAなどからも操作することができます。

www.tekizai.net

Notesへアクセスするためには、大前提として、NotesクライアントかNotesサーバーがインストールされている必要があります。

Javaで操作するには、Lotus Notesから提供されているNotes.jarのライブラリを使います。

Notesがインストールされている環境であれば、インストールディレクトリのどこかに存在するかと思います。

実行する際は、同じくLotus Notesから提供されるJREを使います。

私の環境だと、「C:\Program Files (x86)\Java」の中にあるJavaを使いました。

また、Notes.jar内部で参照しているnlsxbe.dllというライブラリを読み込めるようにするため、

環境変数PATHにnlsxbe.dllの存在するディレクトリを設定する必要があります。

参考サイト Legacy Communities - IBM Community

実装

NotesManagerクラスという名前で実装しました。

loadFromNotesCalenderというメソッドでカレンダーの内容をList形式で返すというものです。

Listの要素はScheduleという、カレンダーのひとつの予定を表すクラスです。

環境によって可変となる値はプロパティファイルを作ってコードの外に逃がしています。

コード

Notes.properties

 
Server=Notesサーバーのパス
Database=メールDBのパス(○○.nsf)
Password=Notesのパスワード

NotesManagerクラス

 
//NotesManager.java

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;

import lotus.domino.Database;
import lotus.domino.DateTime;
import lotus.domino.Document;
import lotus.domino.NotesCalendar;
import lotus.domino.NotesCalendarEntry;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.NotesThread;
import lotus.domino.Session;;

public class NotesManager {
    private static Properties properties;
    private static final String FILE_PATH="Notes.properties";

    public NotesManager() throws IOException {
        InputStream in =NotesManager.class.getClassLoader().getResourceAsStream(FILE_PATH);
        properties=new Properties();
        properties.load(in);
    }

    public ArrayList<Schedule> loadFromNotesCalender(){
        //スレッドの開始
        NotesThread.sinitThread();
        Session session;
        Database db;
        ArrayList<Schedule> list=new ArrayList<Schedule>();

        //Sessionの開始
        try {
            session = NotesFactory.createSession((String)null,(String)null,properties.getProperty("Password"));
            db = session.getDatabase(properties.getProperty("Server"), properties.getProperty("Database"));

            //カレンダーのデータを取得
            NotesCalendar nc=session.getCalendar(db);
            DateTime start = session.createDateTime((new Date()));
            Calendar c=Calendar.getInstance();
            c.setTime(new Date());
            SimpleDateFormat sdf=new SimpleDateFormat();
            c.add(Calendar.YEAR, 1);
            DateTime end = session.createDateTime(sdf.format(c.getTime()));
            DateTimeFormatter dtf=new DateTimeFormatterBuilder().toFormatter();
            Vector<NotesCalendarEntry> entries=nc.getEntries(start,end);

            //カレンダーから開始・終了日時と題名を取得する
            for(int i=0;i<entries.size();i++) {
                NotesCalendarEntry cale=entries.elementAt(i);
                Document doc =cale.getAsDocument();
                Schedule schedule=new Schedule(doc);
                list.add(schedule);
            }
            System.out.println("Notesカレンダーからの取得数:"+entries.size());
        } catch (NotesException e) {
            e.printStackTrace();
        }finally {
            NotesThread.stermThread();
        }
        return list;
    }
}

Notesの予定をカプセル化するクラス

 
//Schedule.java

import java.util.Calendar;
import lotus.domino.DateTime;
import lotus.domino.Document;
import lotus.domino.NotesException;

public class Schedule {
    private Calendar start;
    private Calendar end;
    private String subject;
    private String location;
    private boolean allDay;

    public Schedule(Document doc) throws NotesException {
        Calendar startDate=Calendar.getInstance();
        startDate.setTime(((DateTime) doc.getItemValueDateTimeArray("StartDateTime").firstElement()).toJavaDate());
        Calendar endDate=Calendar.getInstance();
        endDate.setTime(((DateTime) doc.getItemValueDateTimeArray("EndDateTime").firstElement()).toJavaDate());
        String subject=doc.getItemValueString("Subject");
        String location=doc.getItemValueString("Location");
        this.start = startDate;
        this.end = endDate;
        this.subject = subject;
        this.location = location;
        //全日のいい判定が見つからずアイコンで判定
        if(doc.getItemValueString("$IconSwitcher").equals("AllDayEvent")) {
            allDay=true;
        }else {
            allDay=false;
        }
    }
    public Calendar getStart() {
        return start;
    }
    public Calendar getEnd() {
        return end;
    }
    public String getSubject() {
        return subject;
    }
    public String getLocation() {
        return location;
    }
    public boolean isAllDay() {
        return allDay;
    }
    @Override
    public String toString() {
        return "Schedule [start=" + start + ", end=" + end + ", subject=" + subject + ", location=" + location
                + ", allDay=" + allDay + "]";
    }
}

終わりに

JavaでNotesカレンダーの情報を取得するプログラムの紹介でした。

最近は下火になりつつあるNotesですが、もしかすると誰かの役に立つかも知れませんのでWebの海の中に置いておくことにします・・・。

ここまでお読みいただき、ありがとうございました。