http://www.jacksonlhj.cn/articles/2022/02/28/1646053714040.html
JAVA调用IBM的Lotus Notes
目前IBM的Lotus Notes是已经比较小众,很少人在用了,刚好公司系统要调用Lotus Notes的数据,所以就记录下,接下来我们直接上代码。
1、引入对应的依赖
因为但是没有找到对应的依赖,所以我直接下载了Jar包
Notes.jar和NCSO.jar
<!--Notes-->
<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;
/**
* @Value注解获取配置而文件的数据
* @author jackson
*
*/
@Slf4j
public class PropertieUtil {
public static String KEY_NOTES_PWD = "KEY_NOTES_PWD";
public static String KEY_SERVER_URL = "KEY_SERVER_URL";
//TEST_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();
}
}
/**根据properties的key获取数据库
* @throws NotesException */
public static Database getDbConnection(String proKey) throws NotesException {
return new PropertieUtil().getSession().getDatabase(proties.getProperty(KEY_SERVER_URL),proties.getProperty(proKey));
}
/**根据url获取数据库
*
* @param notesUrl
* String notes数据库Url
* @return
* @throws NotesException
*/
public static Database getDbByNotesUrl(String notesUrl) throws NotesException {
return new PropertieUtil().getSession().getDatabase(proties.getProperty(KEY_SERVER_URL),proties.getProperty(notesUrl));
}
/**创建session
* @throws NotesException
* @throws IOException */
public Session getSession() throws NotesException {
return NotesFactory.createSession((String)null,(String)null, (String) proties.get(KEY_NOTES_PWD));
}
/**
* 根据配置文件key获取value
* @param keyStr
* String key
* @return
*/
public static String getPropertyByKey(String keyStr) {
return proties.getProperty(keyStr);
}
/**根据properties的key获取数据库
* @throws NotesException */
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");
}
//Notes中所查询的View
View view = db.getView("test\\test");
//参数条件
Vector<String> vector = new Vector<>();
vector.addElement(param);
//根据参数条件获取数据
Document document = view.getDocumentByKey(vector);
//根据Notes系统中的Filed Name的类型获取数据
//Vector plantList = document.getItemValue("fPlantByCtmPN");
String staffNo = document.getItemValueString("fStaffNo");
map.put("staffNo ", staffNo );
return map;
}
②获取Notes系统中的附件关键代码示例
//附件所在的Notes URL
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);
/**
* 获取RichTextItem
*
* @param docCur Document notes文档
* @param fieldName String 附件字段名
* @return RichTextItem
* @throws NotesException
*/
public RichTextItem getRichTextItem(Document docCur, String fieldName) throws NotesException {
RichTextItem richItem = (RichTextItem) docCur.getFirstItem(fieldName);
if (richItem == null) {
richItem = docCur.createRichTextItem(fieldName);
}
return richItem;
}
/**
* 获取 所有 附件path
*
* @param richItem RichTextItem 附件对象(多附件)
* @return List<String>
* @throws NotesException
*/
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) {
//以EMAil开头的无需显示
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发送邮件关键代码示例
/**
* 发送邮件
* @param dbCur
* Database notes数据库
* @param docCur
* Document notes Document
* @param sendBy
* String 发件人
* @param sendto
* Vector<String> 收件人
* @param copyTo
* Vector<String> 抄送
* @param comment
* String 备注内容,显示在第一行
* @param emailSubject
* String 邮件主题
* @param form
* String Document 表单名
* @param body
* String 主体内容
* @return
* @throws NotesException
*/
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);//是否带富文本link
// 发送邮件
mailWithAttah(mail, dbCur);
return true;
}
/**
* 发送邮件,附带富文本内容
* @param mail 邮件对象
* @param db Notes数据库
* @return
*/
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;
}