<%@ page language="java"
	import="java.sql.*,java.io.*,javax.servlet.*,javax.servlet.http.*,java.text.SimpleDateFormat,java.util.Date;"
	pageEncoding="gb2312"%>
<%
		//流转	
		String flg= request.getParameter("flg");
		String id=request.getParameter("id");	
    	if(flg!=null&&flg.length()>0&&id!=null&&id.length()>0){
    	    flg=new String(flg.getBytes("iso-8859-1"),"utf-8");
     		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
			String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
				+ this.getServletContext().getRealPath("demodata/") + "\\demo.mdb";
			Connection conn = DriverManager.getConnection(strUrl);
			Statement stmt = conn.createStatement();
    		stmt.executeUpdate("Update word set Status = '"+flg+"' where id="+id);
			stmt.close();
			conn.close();
   		}
   		
   		//新建文件
   		else if(request.getParameter("action")!=null){
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
			String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
				+ this.getServletContext().getRealPath("demodata/") + "\\demo.mdb";
			Connection conn = DriverManager.getConnection(strUrl);
			Statement stmt = conn.createStatement();
			ResultSet rs=stmt.executeQuery("select Max(ID) from word");
            int newID = 1;
            if (rs.next())
            {
                newID =  Integer.parseInt(rs.getString(1))+1;
            }
            rs.close();

            String fileName = "aabb" + newID + ".doc";
            String FileSubject = "请输入文档主题";
            String getFile=(String)request.getParameter("FileSubject");
            if ( getFile!=null&&getFile.length()>0)
                FileSubject =new String(getFile.getBytes("iso-8859-1"));
                out.print(FileSubject);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			// new Date()为获取当前系统时间
            String strsql = "Insert into word(ID,FileName,Subject,SubmitTime,Status) values(" + newID
                + ",'" + fileName + "','" + FileSubject + "','" + df.format(new Date()) + "','在线编辑')";
            stmt.executeUpdate(strsql);
            stmt.close();
            conn.close();
            
            //拷贝文件
			String oldPath=getServletContext().getRealPath("doc/" + request.getParameter("TemplateName"));
			String newPath=getServletContext().getRealPath("doc/" + fileName);
			try { 
           		int bytesum = 0; 
           		int byteread = 0; 
           		File oldfile = new File(oldPath);
           		if (oldfile.exists()) { //文件存在时 
               		InputStream inStream = new FileInputStream(oldPath); //读入原文件 
               		FileOutputStream fs = new FileOutputStream(newPath); 
               		byte[] buffer = new byte[1444]; 
               		int length; 
               		while ( (byteread = inStream.read(buffer)) != -1) { 
                   		bytesum += byteread; //字节数 文件大小 
                   		System.out.println(bytesum); 
                   		fs.write(buffer, 0, byteread); 
               		} 
               		inStream.close(); 
           		} 
       		} 
      		catch (Exception e) { 
           		System.out.println("复制单个文件操作出错"); 
           		e.printStackTrace(); 
       		} 

  			response.sendRedirect("word2.jsp?id=" + newID);
   		} 	
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<link rel="stylesheet" href="css/style.css" type="text/css"></link>
		<title>在线演示示例</title>
		<script type="text/javascript">
			function onColor(dd){
				dd.style.backgroundColor = "#D7FFEE";
			}
			function offColor(dd){
				dd.style.backgroundColor="";
			}
			function getFocus(){
				var str=document.getElementById("FileSubject").value;
				if(str=="请输入文档主题"){
					document.getElementById("FileSubject").value="";
				}
			}
			function lostFocus(){
				var str=document.getElementById("FileSubject").value;
					if(str.length<=0){
						document.getElementById("FileSubject").value="请输入文档主题";
					}
			}												
		</script>
	</head>
	<body>
		<!--header-->
		<div class="zz-headBox br-5 clearfix">
			<div class="zz-head mc">
				<!--logo-->
				<div class="logo fl">
					<a href="#"> <img src="images/logo.png" alt="" /> </a>
				</div>
				<!--logo end-->
				<ul class="head-rightUl fr">
					<li><a href="http://www.zhuozhengsoft.com" target="_blank">卓正网站</a></li>
                <li><a href="http://www.zhuozhengsoft.com/poask/index.asp" target="_blank">客户问吧</a></li>
                <li class="bor-0"><a href="http://www.zhuozhengsoft.com/contact-us.html" target="_blank">联系我们</a></li>
				</ul>
			</div>
		</div>
		<!--header end-->
		<!--content-->
		<div class="zz-content mc clearfix pd-28">
			<div class="demo mc">
				<h2 class="fs-16">
					PageOffice 在文档流转中的应用
				</h2>
				<h3 class="fs-12">
					演示说明:
				</h3>
				
				<p>
					提示1：正确运行本演示前，您首先要确定您的机器上已安装Microsoft Office。
				</p>
				<p>
					提示2：如果您是第一次使用PageOffice，在打开文件的时候会提示安装控件，请点允许安装。
				</p>
				
			</div>
			<div class="demo mc">
				<h3 class="fs-12">
					新建文件
				</h3>
				<form id="form1" action="index.jsp?action=create" method="post">
					<table class="text" cellspacing="0" cellpadding="0" border="0">
						<tr>
							<td style="font-size: 9pt" align="left">
								<select name="TemplateName" style='width: 240px;'>
									<option value='redhead00.doc' selected="selected">
										-- 空白模板 --
									</option>
									<option value='redhead01.doc'>
										公司公文模板
									</option>
									<option value='redhead02.doc'>
										政府红头文件模板
									</option>
								</select>
							</td>							
							<td align="center">
								<input name="FileSubject" id="FileSubject" type="text" onfocus="getFocus()"
									onblur="lostFocus()" class="boder" style="width: 180px;"
									value="请输入文档主题" />
							</td>
							<td style="width: 221px;">
								&nbsp;
								<input type="image" style="width: 92px;" id="imgBtn"
									src="images/newword.gif" alt="" />

							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="zz-talbeBox mc">
				<h2 class="fs-12">
					文档列表
				</h2>
				<table class="zz-talbe">
					<thead>
						<tr>
							<th width="7%">
								类型
							</th>
							<th width="22%">
								文档名称
							</th>
							<th width="13%">
								创建日期
							</th>
							<th width="10%">
								无痕模式
							</th>
							<th width="19%">
								留痕模式
							</th>
							<th width="10%">
								核稿
							</th>
							<th width="10%">
								只读
							</th>
							<th width="8%">
								Html
							</th>
						</tr>
					</thead>
					<tbody>
						<%
							Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
							String strUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="
									+ this.getServletContext().getRealPath("demodata/")+ "\\demo.mdb";
							Connection conn = DriverManager.getConnection(strUrl);
							Statement stmt = conn.createStatement();
							ResultSet rs = stmt.executeQuery("select * from word order by id desc");

							while (rs.next()) {
								String str0 = rs.getString(1);
								if (str0 != null && str0.length() > 0) {
						%>				
						<tr onmouseover='onColor(this);' onmouseout='offColor(this);'>
							<td>
								<img src='images/office-1.jpg' />
							</td>
							<td><%=rs.getString(3)%></td>
							<%
								//显示时间
										String str4 = rs.getString(4);
										if (str4 != null && str4.length() > 0) {
							%>
							<td><%=new SimpleDateFormat("yyyy/MM/dd")
								.format(new SimpleDateFormat("yyyy-MM-dd")
										.parse(str4))%></td>
							<%
								} else {
							%>
							<td>
								&nbsp;
							</td>
							<%
								}
										String str6 = rs.getString(6);

										if (str6 != null && str6.length() > 0)  {
											int n = 0;
											if (str6.equals("在线编辑"))
												n = 1;
											else if (str6.equals("张三批阅"))
												n = 2;
											else if (str6.equals("李四批阅"))
												n = 3;
											else if (str6.equals("文员清稿"))
												n = 4;
											else if (str6.equals("正式发文"))
												n = 5;

											switch (n) {
												case 0:
												case 1 :
							%><td colspan=4>
								<a href="word2.jsp?id=<%=str0 %>"><span
									style='color: Blue;'>在线编辑</span> </a> →
								<a href="word.jsp?id=<%=str0 %>&user=张三">张三批阅 </a> →
								<a href="word.jsp?id=<%=str0 %>&user=LiSi">李四批阅</a> →
								<a href="word1.jsp?id=<%=str0 %>">文员清稿</a> →
								<a href="word3.jsp?id=<%=str0 %>">正式发文</a>
							</td>
							<%
														break;
												case 2 :
							%>
							<td colspan=4>
								<a href="word2.jsp?id=<%=str0 %>"><span style=' color:Green;'>在线编辑</span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=ZhangSan"><span
									style='color: Blue;'>张三批阅</span> </a> →
								<a href="word.jsp?id=<%=str0 %>&user=LiSi">李四批阅</a> →	
								<a href="word1.jsp?id=<%=str0 %>">文员清稿</a> →
								<a href="word3.jsp?id=<%=str0 %>">正式发文</a>
							</td>
							<%
														break;
												case 3 :
							%><td colspan=4>
								<a href="word2.jsp?id=<%=str0 %>"><span style=' color:Green;'>在线编辑</span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=ZhangSan"><span style=' color:Green;'>张三批阅</span> </a> →
								<a href="word.jsp?id=<%=str0 %>&user=LiSi"><span
									style='color: Blue;'>李四批阅</span> </a> →
								<a href="word1.jsp?id=<%=str0 %>">文员清稿</a> →
								<a href="word3.jsp?id=<%=str0 %>">正式发文</a>
							</td>
							<%
														break;
												case 4 :
							%>
							<td colspan=4>
								<a href="word2.jsp?id=<%=str0 %>"><span style=' color:Green;'>在线编辑</span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=ZhangSan"><span style=' color:Green;'>张三批阅 </span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=LiSi"><span style=' color:Green;'>李四批阅</span></a> →
								<a href="word1.jsp?id=<%=str0 %>"><span
									style='color: Blue;'>文员清稿</span> </a> →
								<a href="word3.jsp?id=<%=str0 %>">正式发文</a>
							</td>
							<%
														break;
												case 5 :
							%><td colspan=4>
								<a href="word2.jsp?id=<%=str0 %>"><span style=' color:Green;'>在线编辑</span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=ZhangSan"><span style=' color:Green;'>张三批阅 </span></a> →
								<a href="word.jsp?id=<%=str0 %>&user=LiSi"><span style=' color:Green;'>李四批阅</span></a> →
								<a href="word1.jsp?id=<%=str0 %>"><span style=' color:Green;'>文员清稿</span></a> →
								<a href="word3.jsp?id=<%=str0 %>"><span
									style='color: Blue;'>正式发文 </span> </a>
							</td>
							<%
														break;
											}
										} else {
							%>

							<td colspan=4>
								<a href='###'><span style='color: Blue;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
								</a>
								<a href=''>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
									&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
								<a href=''>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
								<a href=''>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
							</td>
							<%
								}
							%>
							<td>
							<%  
								String str5 = rs.getString(5);
								if (str5 != null && str5.length() > 0) { 
							%>
									<a style="color:blue;" target="_blank" href="doc/<%= str5 %>">Html</a>
							<%
								}
								else{
							%>
									Html
							<% 
								}
							%>
							</td>
						</tr>
						<%
								}
							}
							rs.close();
							stmt.close();
							conn.close();
						%>
					</tbody>
				</table>
			</div>
		</div>
		<!--content end-->
		<!--footer-->
		<div class="login-footer clearfix">
			Copyright &copy; 2013 北京卓正志远软件有限公司
		</div>
		<!--footer end-->
	</body>
</html>
