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
    评论
对于LotusScript,除了Rich-Text域外的元素,如文本、单选框、列表框、复选框等,你可以用几乎相同的代码取得它们的值。例如:如果有一个“Location”域,不论它是何种类型,你都可以用下面的LotusScript代码取得它的值:   fieldVals = doc.Location   或者这样:   fieldVals = doc.GetItemValue("Location")   在LotusScript中,域的类型对于你要取值(值数组)的代码并不重要。但是在JavaScript中,不同类型的域除了显示选项(比如单选框、复选框或者文本)外,并不像在Notes里那样,它们是不同的类型的对象,每一个都要用不同的方式去引用。其实,那也不是绝对的,有些对象是相似的,但是引用过程并不像在LotusScript里那么流畅。   在JavaScript中,没有所谓的Rich-Text域,在HTML中更没有。Notes里提供了一个可以放在浏览器里的富文本Java(不是JavaScript)小程序,从而可以得到富文本的一些功能,但是你并不能用JavaScript来对它编程,而且它也不是一个真正的HTML对象类型。   更让Notes开发人员惊讶的是,在Web上还没有数字型或时间型的域。HTML的域都是文本型的。尽管你能用它们来收集数字信息,如数量或单价,而保存的数据依然是文本。为了像数字一样使用它,你必须把它转换成数字类型。另一个区别是在Web上没有计算域,但并不意味着你不能在你的表单里加入计算域。你可以加入计算域,计算值将会在Web页中显示,除非域是隐藏的。关键是即使域就在那里显示,而HTML通常的处理是没有定义域。如果你把test域改成计算域而不是可编辑的,在测试时你会发现其值是取不到的,我们可以对比下计算域和可编辑域生成的html代码:   计算域的时候生成的代码(js是取不到值的):      action="/weboa/ggxx/Dinner.nsf/test?OpenForm&Seq=1" name="_test">

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值