⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addressbookaction.java

📁 j2ee权限验证
💻 JAVA
字号:
package com.gemt.privilege.struts;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class AddressBookAction extends DispatchAction {

	/*
	public Collection getUnitPerson() throws SQLException {
		Collection col = new ArrayList();
		final String sql = "select a.id,a.ppnm,c.unid,c.untdty,c.offtel,c.fax,c.mobile,a.hmtel,c.srtno,a.delst,a.lddscd,a.sex,a.educ,a.birth,0 as hasit from ftd0802_ppin a,ftd0807_ppun c where c.ppid = a.id order by c.unid,c.srtno";
		java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.16:1521:szwrb", "szdba", "szwrb");
		java.sql.PreparedStatement pstmt = con.prepareStatement(sql);
		java.sql.ResultSet rs = pstmt.executeQuery();
		
		return col;
	}*/
	
	public Map<String, Collection<PersonInfo>> getUnitPersonByAlid(long alid) throws SQLException {
		Map<String, Collection<PersonInfo>> map = new HashMap<String, Collection<PersonInfo>>();
		Collection<PersonInfo> col = null;
		final String sql = "select a.id,a.ppnm,c.unid,c.untdty,c.offtel,c.fax,c.mobile,a.hmtel,c.srtno,a.delst,a.lddscd,a.sex,a.educ,a.birth,nvl((select 1 from FTD0806_ALPP b,ftd0807_ppun d where b.ppid=d.ppid and d.unid=b.unid and a.id=b.ppid and alid=?),0) as hasit from ftd0802_ppin a,ftd0807_ppun c where c.ppid = a.id order by c.unid,c.srtno";
		java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.16:1521:szwrb", "szdba", "szwrb");
		java.sql.PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setLong(1, alid);
		java.sql.ResultSet rs = pstmt.executeQuery();
		
		long unid = -2;
		
		while(rs.next()) {
			
				System.out.println(rs.getLong("unid") + " " + rs.getLong("id"));
			
			PersonInfo personInfo = new PersonInfo();
			personInfo.setId(rs.getLong("id"));
			personInfo.setPpnm(rs.getString("ppnm"));
			personInfo.setUnid(rs.getLong("unid"));
			personInfo.setUntdty(rs.getString("untdty"));
			personInfo.setOfftel(rs.getString("offtel"));
			personInfo.setFax(rs.getString("fax"));
			personInfo.setMobile(rs.getString("mobile"));
			personInfo.setHmtel(rs.getString("hmtel"));
			personInfo.setSrtno(rs.getString("srtno"));
			personInfo.setDelst(rs.getString("delst"));
			personInfo.setLddscd(rs.getString("lddscd"));
			personInfo.setSex(rs.getString("sex"));
			personInfo.setEduc(rs.getString("educ"));
			personInfo.setBirth(rs.getString("birth"));
			personInfo.setHasit(rs.getInt("hasit"));
			if(rs.getLong("unid") == unid) {
				
				if(col == null) col = new ArrayList<PersonInfo>();
				col.add(personInfo);
			}
			else {
				if(unid != -2) {
					System.out.println("当前unid = " + unid);
					map.put(""+unid, col);
					//col = new ArrayList<PersonInfo>();
				}
				col = new ArrayList<PersonInfo>();
				col.add(personInfo);
			}
			unid = rs.getLong("unid");
		}
		if(unid != -2) {
			map.put(""+unid, col);
			col = new ArrayList<PersonInfo>();
		}

		if(rs != null) rs.close();
		if(pstmt != null) pstmt.close();
		if(con != null) con.close();		
		return map;
	}
	
	public String getXmlTree() throws SQLException {
		
		Map<String, Collection<PersonInfo>> map = getUnitPersonByAlid(20166);
		
		final String sql = "select a.id,a.ndnm,a.pid,a.srtno,a.ndprp,a.unid,level from FTD0805_NDREL a start with pid=0 connect by prior id = pid";
		java.sql.DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
		java.sql.Connection con = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.16:1521:szwrb", "szdba", "szwrb");
		java.sql.PreparedStatement pstmt = con.prepareStatement(sql);
		java.sql.ResultSet rs = pstmt.executeQuery();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version='1.0' encoding='UTF-8'?><tree id='0'>");
		int level=0;
		while(rs.next()) {
			if(level == rs.getInt("level")) {
				sb.append("</item>");
				sb.append("<item level=\"" + rs.getLong("level") + "\" text=\""+ rs.getString("ndnm") +"\" id=\"" + rs.getLong("id") + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\">");
				if(rs.getString("unid") != null && !"".equals(rs.getString("unid").trim())) {
					Collection<PersonInfo> personInfoCol = map.get(rs.getString("unid"));
					if(personInfoCol != null)
					for(PersonInfo personInfo : personInfoCol) {
						final String yyy = personInfo.getHasit()==1?"checked=\"1\"":"";
						sb.append("<item " + yyy + " level=\"" + rs.getLong("level")+1 + "\" unid=\"" + rs.getString("unid") + "\" text=\""+ personInfo.getPpnm() +"\" id=\"" + personInfo.getId() + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\"/>");
					}
				}
				
			}
			else if(rs.getInt("level") > level) {
				sb.append("<item level=\"" + rs.getLong("level") + "\" text=\""+ rs.getString("ndnm") +"\" id=\"" + rs.getLong("id") + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\">");
				if(rs.getString("unid") != null && !"".equals(rs.getString("unid").trim())) {
					Collection<PersonInfo> personInfoCol = map.get(rs.getString("unid"));
					if(personInfoCol != null)
					for(PersonInfo personInfo : personInfoCol) {
						final String yyy = personInfo.getHasit()==1?"checked=\"1\"":"";
						sb.append("<item " + yyy + " level=\"" + rs.getLong("level")+1 + "\" unid=\"" + rs.getString("unid") + "\" text=\""+ personInfo.getPpnm() +"\" id=\"" + personInfo.getId() + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\"/>");
					}
				}
			}
			else {
				for(int i =0; i < level + 1 - rs.getInt("level"); i++)
					sb.append("</item>");
				sb.append("<item level=\"" + rs.getLong("level") + "\" text=\""+ rs.getString("ndnm") +"\" id=\"" + rs.getLong("id") + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\">");
				if(rs.getString("unid") != null && !"".equals(rs.getString("unid").trim())) {
					Collection<PersonInfo> personInfoCol = map.get(rs.getString("unid"));
					if(personInfoCol != null)
					for(PersonInfo personInfo : personInfoCol) {
						final String yyy = personInfo.getHasit()==1?"checked=\"1\"":"";
						sb.append("<item " + yyy + " level=\"" + rs.getLong("level")+1 + "\" unid=\"" + rs.getString("unid") + "\" text=\""+ personInfo.getPpnm() +"\" id=\"" + personInfo.getId() + "\" open=\"1\" im0=\"books_close.gif\" im1=\"tombs.gif\" im2=\"tombs.gif\" call=\"1\" select=\"1\"/>");
					}
				}
			}
			
			level = rs.getInt("level");
		}
		
		for(int j =0; j <level; j++)
			sb.append("</item>");
		sb.append("</tree>");
		
		if(rs != null) rs.close();
		if(pstmt != null) pstmt.close();
		if(con != null) con.close();
		return sb.toString();
	}
	
	public ActionForward displayTree(
		      ActionMapping mapping, ActionForm form, HttpServletRequest request,
		      HttpServletResponse response) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter(); 
		out.print(getXmlTree());
		out.flush();
		out.close();
		return null;//mapping.findForward("success");
	}
	
	public static void main(String[] args) {
		
		AddressBookAction addressBook = new AddressBookAction();
		try {
			
			/*
			Map<String, Collection<PersonInfo>> map = addressBook.getUnitPersonByAlid(20166);
			Set keyset = map.keySet();
			for(Iterator it = keyset.iterator(); it.hasNext();) {
				Collection<PersonInfo> col = map.get((String)it.next());
				if(col != null) {
					for(PersonInfo personInfo : col) {
						//if(personInfo.getUnid() == 6541)
						System.out.println("人员和单位" + personInfo.getId() + " " + personInfo.getUnid());
					}
				}
				
			}*/
			System.out.println(addressBook.getXmlTree());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -