Java拆离Lotus Notes Document的附件

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.EmbeddedObject;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.NotesThread;
import lotus.domino.RichTextItem;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.cso.Item;

public class AutoExtract extends NotesThread {

	static Logger logger = Logger.getLogger(AutoExtract.class);

	public static void main(String argv[]) {
		AutoExtract t = new AutoExtract();
		try {
			t.start();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			t.termThread();
		}
	}

	public void runNotes() {

		Document doc = null;
		Document tmpdoc = null;
		Database db = null;
		View view = null;
		StringBuilder sb = new StringBuilder();
		Database localdb = null;
		Document tempdoc = null;
		try {

			PropertyConfigurator.configure("log4j.properties");
			String filepath = "conf.properties";
			Properties pro = new Properties();
			FileInputStream fin = new FileInputStream(filepath);
			pro.load(fin);

			String ip = pro.get("ip").toString();
			String notesid = pro.get("notesid").toString();
			String password = pro.get("password").toString();
			String port = pro.get("port").toString();
			String dbname = pro.get("dbname").toString();
			String localpath = pro.get("localpath").toString();
			String backmail = pro.get("backmail").toString();
			String localdatabase = pro.get("localdb").toString();
			String localpwd = pro.get("localpwd").toString();

			String mailswitch = pro.get("mailswitch").toString();

			Session s = NotesFactory.createSession((String) null,
					(String) null, localpwd);

			logger.debug(s.getPlatform());
			logger.debug(s.getUserName());

			localdb = s.getDatabase("", localdatabase);
			logger.debug(localdb.getFilePath());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");

			logger.debug("Start at: " + sdf.format(new Date()));

			logger.debug("ip : " + ip);
			logger.debug("notesid : " + notesid);
			logger.debug("password :" + password);
			logger.debug("port : " + port);
			logger.debug("dbname : " + dbname);
			logger.debug("localpath : " + localpath);
			logger.debug("backmail : " + backmail);
			logger.debug("mailswitch : " + mailswitch);

			File file = new File(localpath);

			if (!file.exists() && !file.isDirectory()) {
				logger.debug("Directory not exist, mkdir!");
				file.mkdir();
			}

			db = getConnection(ip, port, notesid, password, dbname);

			logger.debug(db.getTitle());

			if (db == null) {
				logger.error("db is null,return!");
				return;
			}

			view = db.getView("($Inbox)");
			logger.debug(view.getEntryCount());

			doc = view.getFirstDocument();

			while (doc != null) {
				tmpdoc = view.getNextDocument(doc);

				try {
					logger.debug("开始处理:" + doc.getItemValueString("Subject"));

					tempdoc = localdb.createDocument();
					tempdoc.createRichTextItem("Body");

					Vector<?> items = doc.getItems();
					for (int j = 0; j < items.size(); j++) {
						Item item = (Item) items.elementAt(j);
						tempdoc.replaceItemValue(item.getName(), item
								.getValueString());
					}
					RichTextItem rt = null;
					if (tempdoc.hasItem("Body")) {
						rt = (RichTextItem) tempdoc.getFirstItem("Body");
						logger.debug("包含Body域");
					} else {
						rt = tempdoc.createRichTextItem("Body");
						logger.debug("创建Body域");
					}

					RichTextItem body = (RichTextItem) doc.getFirstItem("Body");

					if (body != null) {

						Vector<?> v = body.getEmbeddedObjects();

						if (v != null) {
							logger.debug("附件个数: " + v.size());
							Enumeration<?> e = v.elements();
							while (e.hasMoreElements()) {
								EmbeddedObject eo = (EmbeddedObject) e
										.nextElement();
								if (eo.getType() == EmbeddedObject.EMBED_ATTACHMENT) {
									eo.extractFile(file.getPath()
											+ File.separator + eo.getSource());
									logger.debug("执行拆离: " + eo.getSource());

									rt.embedObject(
											EmbeddedObject.EMBED_ATTACHMENT,
											null, file.getPath()
													+ File.separator
													+ eo.getSource(), eo
													.getSource());
									logger.debug("执行附加操作");
								}
							}

						} else {
							logger.debug("v is null!");
						}

					} else {
						logger.debug("body is null!");

					}

					logger.debug("执行保存操作");

					tempdoc.save();
					tempdoc.putInFolder("($Inbox)");

					if ("1".equals(mailswitch)) {

						sb.delete(0, sb.length());

						sb.append("IPA ");
						sb.append(sdf.format(new Date()));
						sb.append(doc.getItemValueString("Subject"));
						doc.replaceItemValue("Subject", sb.toString());
						doc.replaceItemValue("SendTo", backmail);
						doc.replaceItemValue("CopyTo", "");
						doc.replaceItemValue("BlindCopyTo", "");
						doc.send();

						logger.debug("执行发送操作");
					}

					doc.remove(true);
					logger.debug("执行删除操作");
				} catch (Exception innere) {
					System.out.println(innere.getMessage());
					logger.debug(innere.getMessage());
				}
				doc.recycle();
				tempdoc.recycle();
				doc = tmpdoc;
			}

			logger.debug("Finished at: " + sdf.format(new Date()));

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getStackTrace());
		} finally {
			try {
				if (db != null) {
					db.recycle();
				}
				if (doc != null) {
					doc.recycle();
				}
				if (tmpdoc != null) {
					tmpdoc.recycle();
				}
				if (view != null) {
					view.recycle();
				}
			} catch (NotesException e) {
				// TODO Auto-generated catch block
				logger.error(e.getStackTrace());
			}
		}
	}

	public static Database getConnection(String ip, String port,
			String notesID, String password, String dbname)
			throws NotesException {
		Database db;
		String host = ip + ":" + port;
		Session s = NotesFactory.createSession(host, notesID, password);
		db = s.getDatabase("", dbname);
		if (!db.isOpen()) {
			logger.error("Error occured in getConnection, " + dbname
					+ "does not exist!");
			return null;
		} else {
			logger.debug("title of " + dbname + " is " + db.getTitle());
			return db;
		}
	}

}


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值