JAVA调用IBM的Lotus Notes
目前IBM的Lotus Notes是已经比较小众,很少人在用了,刚好公司系统要调用Lotus Notes的数据,所以就记录下,接下来我们直接上代码。
1、引入对应的依赖 因为但是没有找到对应的依赖,所以我直接下载了Jar包 Notes.jar和NCSO.jar
<dependency>
<groupId>com.Notes</groupId>
<artifactId>Notes</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/Notes.jar</systemPath>
</dependency>
<dependency>
<groupId>com.Ncso</groupId>
<artifactId>Ncso</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/NCSO.jar</systemPath>
</dependency>
2、创建notes.proterties配置文件,保存Notes 系统的路径
#例如
TEST_URL=apps\\test.nsf
#你的Notes服务器登陆密码
KEY_NOTES_PWD=TEST
#你的Notes服务器地址
EMAIL_SERVER_URL=TEST
3、创建PropertieUtil.Class工具类,读取notes.proterties中Notes系统的路径,并且取得Notes连接
import lombok.extern.slf4j.Slf4j;
import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.Session;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Slf4j
public class PropertieUtil {
public static String KEY_NOTES_PWD = "KEY_NOTES_PWD";
public static String KEY_SERVER_URL = "KEY_SERVER_URL";
public static String TEST_URL = "TEST_URL";
static Properties proties = null;
static{
proties = new Properties();
InputStream is = PropertieUtil.class.getClassLoader().getResourceAsStream("notes.properties");
try {
proties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Database getDbConnection(String proKey) throws NotesException {
return new PropertieUtil().getSession().getDatabase(proties.getProperty(KEY_SERVER_URL),proties.getProperty(proKey));
}
public static Database getDbByNotesUrl(String notesUrl) throws NotesException {
return new PropertieUtil().getSession().getDatabase(proties.getProperty(KEY_SERVER_URL),proties.getProperty(notesUrl));
}
public Session getSession() throws NotesException {
return NotesFactory.createSession((String)null,(String)null, (String) proties.get(KEY_NOTES_PWD));
}
public static String getPropertyByKey(String keyStr) {
return proties.getProperty(keyStr);
}
public static Database getEmailDbConnection(String proKey) throws NotesException {
return new PropertieUtil().getSession().getDatabase(proties.getProperty(EMAIL_SERVER_URL),proties.getProperty(proKey));
}
}
3、与Notes系统交互
①获取Notes系统View数据关键代码示例
public Map<String, String> getData(String param) throws Exception {
Map<String, String> map = new HashMap<>(16);
NotesThread.sinitThread();
Database db = PropertieUtil.getDbConnection(PropertieUtil.TEST_URL);
if (!db.isOpen()) {
throw new Exception("Can not find database in Notes");
}
View view = db.getView("test\\test");
Vector<String> vector = new Vector<>();
vector.addElement(param);
Document document = view.getDocumentByKey(vector);
String staffNo = document.getItemValueString("fStaffNo");
map.put("staffNo ", staffNo );
return map;
}
②获取Notes系统中的附件关键代码示例
Database attDb = PropertieUtil.getDbConnection(PropertieUtil.ATTACHMENT_URL);
View attView = attDb.getView("Attachment\\SortByKey");
Vector<String> vector = new Vector<>();
vector.addElement("frmSO");
vector.addElement(attDocSeqNo);
Document attDoc = attView.getDocumentByKey(vector, true);
if (null == attDoc) {
throw new Exception("Can not find this record in Attachment Notes!");
}
RichTextItem richTextItem = getRichTextItem(attDoc, "AF_ParAttachment");
List<String> newAttachList = getRichItemPath(richTextItem, attachPath);
public RichTextItem getRichTextItem(Document docCur, String fieldName) throws NotesException {
RichTextItem richItem = (RichTextItem) docCur.getFirstItem(fieldName);
if (richItem == null) {
richItem = docCur.createRichTextItem(fieldName);
}
return richItem;
}
public List<String> getRichItemPath(RichTextItem richItem, String path) throws NotesException {
List<String> attachNameList = new ArrayList<String>();
if (richItem != null) {
Enumeration<?> elements = richItem.getEmbeddedObjects().elements();
while (elements.hasMoreElements()) {
EmbeddedObject eo = (EmbeddedObject) elements.nextElement();
if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
if (eo.getSource().toUpperCase().startsWith("EMAIL")){
continue;
}
String filePath = MyWebUtils.getPathBySys(path) + File.separator + eo.getSource();
eo.extractFile(filePath);
attachNameList.add(eo.getSource());
}
}
}
return attachNameList;
}
③调用Notest发送邮件关键代码示例
public boolean sendMail(Database dbCur,Document docCur,String sendBy,Vector<String> sendto,Vector<String> copyTo,String comment,String emailSubject,String form,String body) throws NotesException {
NotesMail mail = new NotesMail();
mail.setSendBy(sendBy);
mail.setSubject(emailSubject);
mail.setBody(body);
mail.setSendto(sendto);
mail.setCopyto(copyTo);
mail.setForm(form);
mail.setDoc(docCur);
mail.setComment(comment);
mail.setDocLink(true);
mailWithAttah(mail, dbCur);
return true;
}
public boolean mailWithAttah(NotesMail mail, Database db){
try {
View viParam = db.getView("cfgParameter");
viParam.refresh();
Document docParameter = viParam.getFirstDocument();
RichTextItem itmBodyEnd = null;
if (docParameter != null) {
itmBodyEnd = (RichTextItem) docParameter.getFirstItem("AF_MailAppendages");
}
docMail = db.createDocument();
docMail.replaceItemValue("Form", mail.getForm());
if (mail.getIcon()!=0) {docMail.replaceItemValue("_ViewIcon", mail.getIcon());}
docMail.replaceItemValue("Sendto", mail.getSendto());
docMail.replaceItemValue("Copyto", mail.getCopyto());
docMail.replaceItemValue("BlindCopyTo", mail.getBlindCopyto());
docMail.replaceItemValue("Subject", mail.getSubject());
rtItem = docMail.createRichTextItem("Body");
if (mail.getComment()!=""){
rtItem.appendText(mail.getComment());
rtItem.addNewLine(1);
}
if (mail.getBody()!=""){
rtItem.appendText(mail.getBody());
rtItem.addNewLine(1);
}
if (mail.isDocLink()){
if (mail.getDoc()!=null){
rtItem.addNewLine(2);
rtItem.appendText("Please click the link to open the document ==> ");
rtItem.appendDocLink(mail.getDoc(), "");
}
}
if (itmBodyEnd != null){
rtItem.addNewLine(1);
rtItem.appendRTItem(itmBodyEnd);
}
docMail.send(false);
} catch (NotesException e) {
e.printStackTrace();
return false;
}
return true;
}
|