import lotus.domino.*;
import java.util.Vector;
import java.util.Enumeration;
import java.io.*;
import java.io.File;
import jxl.*;
import jxl.write.*;
import java.io.PrintWriter;
import java.text.*;
public class JavaAgent extends AgentBase {
private String CurUserName = "";
public PublicFunction F = new PublicFunction();
public FileOperate FileOprateObject = new FileOperate();
public boolean delDocByKey(Database db,String Strdate) throws Exception{
DocumentCollection DateDc = null;
View SearchSubView = null;
Vector SearchKey = new Vector();
try{
SearchSubView = db.getView("v_DatelySearch");
SearchKey.addElement(Strdate);
DateDc = SearchSubView.getAllDocumentsByKey(SearchKey,true);
if ( DateDc.getCount() >0 ){
DateDc.removeAll(true);
}
SearchSubView.refresh();
if (DateDc != null){
DateDc.recycle();
}
if (SearchSubView != null){
SearchSubView.recycle();
}
SearchKey = null;
return true;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
public Document newDeptDoc(Database db,Vector DataVector,String CurUserName) throws Exception{
Vector DeptSearchKey = new Vector();
String ParentDeptNo =null;
String DeptNo = null;
String ParentCompanyNo = null;
Document doc = null;
try{
if (DataVector.elementAt(5).toString().equals("")){
ParentDeptNo = new String("RootDepartMent");//所属部门编号
}else{
ParentDeptNo = new String(DataVector.elementAt(5).toString());//所属部门编号
}
DeptNo = new String(DataVector.elementAt(4).toString());
ParentCompanyNo = new String(DataVector.elementAt(7).toString());
DeptSearchKey.addElement(ParentCompanyNo);
DeptSearchKey.addElement(ParentDeptNo);
DeptSearchKey.addElement(DeptNo);
doc = F.getSingleDocByKey(db,"v_Key_f_or_department",DeptSearchKey,true);
if (doc == null){
doc = db.createDocument();
}
doc.replaceItemValue("Form", DataVector.elementAt(0).toString());//表单名称
doc.replaceItemValue("SORTBY", DataVector.elementAt(1).toString());//序号
doc.replaceItemValue("TXTDEPARTMENTNAME_SHOW",DataVector.elementAt(2).toString());//部门全称
doc.replaceItemValue("TXTSHORTDEPARTMENTNAME", DataVector.elementAt(3).toString());//部门简称
doc.replaceItemValue("TXTDEPARTMENTNAME",DeptNo );//部门编号
doc.replaceItemValue("ParentDepartNumber", ParentDeptNo);//所属部门编号
if (DataVector.elementAt(6).toString().equals("")){
doc.replac
eItemValue("ParentDepartNumber_Show", "RootDepartMent");//所属部门名称
}else{
doc.replaceItemValue("ParentDepartNumber_Show", DataVector.elementAt(6).toString());//所属部门名称
}
doc.replaceItemValue("ParentCompanyNumber", ParentCompanyNo);//所属公司编号
doc.replaceItemValue("DisAble","");//部门是否可用
doc.replaceItemValue("Creater",CurUserName);
doc.replaceItemValue("AllReaders","*");
doc.replaceItemValue("Sys_admin","系统管理员");
doc.replaceItemValue("SYS_SYSTEM","工作门户系统管理员群组");
doc.replaceItemValue("SYS_Operation","行政管理员");
doc.replaceItemValue("Replicate_SYS","LocalDomainServers");
F.setitemproperty(doc,"Creater","AR");
F.setitemproperty(doc,"SYS_SYSTEM","AR");
F.setitemproperty(doc,"Replicate_SYS","AR");
F.setitemproperty(doc,"AllReaders","R");
F.setitemproperty(doc,"SYS_Operation","AR");
F.setitemproperty(doc,"Sys_admin","AR");
doc.save(true,false);
return doc;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
public void NotesMain() {
String TmpDocUNID="";
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
Database db = agentContext.getCurrentDatabase();
Document doc = agentContext.getDocumentContext();
CurUserName = agentContext.getEffectiveUserName();
Vector objnames = session.evaluate("@AttachmentNames",doc);
if (doc.hasEmbedded()==true){
System.out.println(objnames.size());
System.out.println(objnames.firstElement());
EmbeddedObject eo = doc.getAttachment(objnames.firstElement().toString());
// String FilePath="//local//TmpUploadFile//TmpDeptInfo.xls"; //Linux
String FilePath="\\Lotus\\Domino\\data\\app\\TmpUploadFile\\TmpDeptInfo.xls"; //Windows
//FileOperate FileOprateObject = new FileOperate();
File file = new File(FilePath);
//解放文档到服务器,并删除文档附件
if (file.exists()==true){
System.out.println("文件已经存在!");
FileOprateObject.delFile(FilePath);
eo.extractFile(FilePath);//+ eo.getSource();
eo.remove();
doc.save(true,false);
}else{
eo.extractFile(FilePath);//+ eo.getSource()
eo.remove();
doc.save(true,false);
}
TmpDocUNID=doc.getUniversalID();
jxl.Workbook rwb = null;
InputStream is = new FileInputStream(FilePath);
rwb = Workbook.getWorkbook(is);
Sheet rs = rwb.getSheet(0);
//获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
//获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
//System.out.print(rsRows);
//获取指定单元格的对象引用
Vector DeptDataVector = new Vector();
for(int i=1;i
DeptDataVector.clear();
}
DeptDataVector.addElement("f_or_department");
for(int j=0;j
DeptDataVector.addElement(cell.getContents());
System.out.print(cell.getContents()+" ");
}
if (newDeptDoc(db,DeptDataVector,agentContext.getEffectiveUserName()) !=null){
System.out.println("部门导入成功!");
}
}
String PrintPath = "[" + db.getFilePath()+ "/f_importDeptInfo?OpenForm]";
PrintWriter pw = getAgentOutput();
pw.println(PrintPath);
if(doc !=null){
doc.recycle();
}
if (agentContext !=null){agentContext.recycle();}
if(session !=null){
session.recycle();
Session Tmpsession = getSession();
AgentContext TmpagentContext = Tmpsession.getAgentContext();
Database Tmpdb = session.getDatabase(Tmpsession.getServerName(),"oadata/AIS_Org.nsf");
if (Tmpdb == null){
System.out.println("找不到数据库!");
if (TmpagentContext !=null){TmpagentContext.recycle();}
if (Tmpsession !=null){Tmpsession.recycle();}
System.exit(0);
}
Document Tmpdoc = Tmpdb.getDocumentByUNID(TmpDocUNID);
if (Tmpdoc!=null){
Tmpdoc.remove(true);
}
if (Tmpdoc !=null){Tmpdoc.recycle();}
if (Tmpdb !=null){Tmpdb.recycle();}
if (TmpagentContext !=null){TmpagentContext.recycle();}
if (Tmpsession !=null){Tmpsession.recycle();}
}
if(rwb!=null
){
rwb=null;
}
if(rs!=null){
rs=null;
}
//删除服务器上的上传文件
if (file.exists()==true){
FileOprateObject.delFile(FilePath);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}