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

📄 functionmanageimpl.java

📁 本代码是一个权限管理系统源代码
💻 JAVA
字号:
/**
 * 
 */
package com.seavision.PermissionManage.components;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import net.sf.hibernate.HibernateException;

import com.seavision.PermissionManage.help.Constants;
import com.seavision.PermissionManage.help.HibernateUtil;
import com.seavision.PermissionManage.vo.FunctionVO;

/**
 * @author Administrator
 * 
 */
public class FunctionManageImpl implements FunctionManage {
	/**
	 * 1.获取功能点名称列表
	 * 
	 * @return List 从function表中读取信息, 封装到FunctionVO中。成功返回List,失败返回null。
	 */
	public List getFunctionNameList() {
		// 建立数据库连接
		Connection conn = HibernateUtil.getConnection();
		List result = new ArrayList();

		String sql = "select functionName from functionn group by functionName order by functionName";
		Statement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			while (rs.next()) {
				FunctionVO functionVo = new FunctionVO();
				//functionVo.setFunctionId(rs.getInt("functionId"));
				// System.out.println("cunzai"+rs.getInt("functionId"));
				functionVo.setFunctionName(rs.getString("functionName"));
				//functionVo.setUrl(rs.getString("url"));
				result.add(functionVo);

			}
			// System.out.println("ceshi___"+result.size());
			return result;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (stm != null)
					stm.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * 2.上传功能点
	 * 
	 * @param path
	 *            将数据写入数据库, 成功返回success,失败返回false
	 */
	public String upLoadFunction(String path) {
		boolean commit = true;
		try {
			HibernateUtil.beginTransaction();
		} catch (HibernateException e) {
			e.printStackTrace();
		}

		// 清除旧的数据库表
		deleteFunction();
		// 读取指定路径文件
		// readFunctionFile(path);
		List result = readFunctionFile(path);
		// 将内容写入数据库
		for (int i = 0; i < result.size(); i++) {
			FunctionVO functionvo = (FunctionVO) result.get(i);
			String str = saveFunction(functionvo);
			System.out.println("save=====" + str);
			if (str != "success") {
				commit = false;
			}
		}
		try {
			HibernateUtil.endTransaction(commit);
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		if (commit) {
			return Constants.SUCCESS;
		} else {
			return Constants.FAILURE;
		}

	}

	/**
	 * 3.下载功能点
	 * 
	 * @param patch
	 * @制定文件存放的路径进行下载, 成功返回success,失败返回false
	 */
	public String downLoadFunction(String patch) {

		String str = "";
		String str2 = "\r\n";
		Connection conn = HibernateUtil.getConnection();
		String sql = "select functionName,url from functionn";
		System.out.println("sql=" + sql);
		try {
			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			while (rs.next()) {
				// 从数据库读出数据
				str = str + rs.getString("url") + "="
						+ rs.getString("functionName") + ";" + str2;
				// 调用方法把数据写入文档

			}
			str = str + str2;
			download(str, patch);

		} catch (Exception e) {
			e.printStackTrace();
			return Constants.FAILURE;
		} finally {
			try {
				conn.close();

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return Constants.SUCCESS;
	}

	/**
	 * 写入文件
	 * 
	 * @param str
	 * @param path
	 * 
	 */
	private void download(String str, String path) {
		String path2 = path + ".properties";// 默认扩展名为.properties
		if (str == null || str.length() < 0)
			throw new IllegalArgumentException(
					"There is no specified destination file (1140).");
		try {
			java.io.File file = new java.io.File(path2);// 指定文件
			// 建立输出流
			FileOutputStream out = new FileOutputStream(file);
			byte[] b = str.getBytes();
			// 写入文件
			out.write(b);

		} catch (IOException ioexception) {
			ioexception.fillInStackTrace();

		}
	}

	/**
	 * 4.读取功能点文件
	 * 
	 * @param path
	 * @return void 从制定路径读取文件
	 */
	public List readFunctionFile(String path) {
		List result = new ArrayList();
		String s = new String();
		String s1 = "";
		s = path.substring(0, path.lastIndexOf("\\"));
		s1 = path.substring(s.lastIndexOf("\\") + 1);

		if (s1 == null)
			throw new IllegalArgumentException(
					"There is no specified destination file (1140).");
		try {
			java.io.File file = new java.io.File(s1);
			// System.out.println("kjdfkjf");
			FileInputStream fileInputstream = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fileInputstream);
			BufferedReader br = new BufferedReader(isr);
			while (true) {
				// System.out.println("kjdfkjf");
				String str = br.readLine();
				// System.out.println("kjdfkjf===="+str);
				if (str == null) {
					break;
				}
				String[] oneCode = str.split("=");
				if (oneCode == null || oneCode.length != 2) {
					continue;
				}
				String cs = oneCode[0]
						.substring(0, oneCode[0].lastIndexOf("/"));
				oneCode[0] = oneCode[0].substring(cs.lastIndexOf("/") + 1);
				// System.out.println("ceshi--->"+oneCode[0]);
				oneCode[1] = oneCode[1].substring(0, oneCode[1].length() - 1);
				// System.out.println("ceshi--->"+oneCode[1]);
				FunctionVO functionVo = new FunctionVO();
				functionVo.setFunctionName(oneCode[1]);
				functionVo.setUrl(oneCode[0]);
				// System.out.println("fun===="+functionVo.getUrl());
				result.add(functionVo);
			}
			br.close();
			return result;
		} catch (IOException ioexception) {
			ioexception.fillInStackTrace();
		}
		return result;
	}

	/**
	 * 5.删除所有功能点
	 * 
	 * @return 根据参数从表function中删除数据。成功返回SUCCESS,失败返回FALSE。
	 */
	public String deleteFunction() {
		String str = "";
		Connection conn = HibernateUtil.getConnection();
		String sql = "delete from functionn";
		try {
			Statement stm = conn.createStatement();
			stm.execute(sql);
			str = "sucess";
		} catch (Exception e) {
			e.printStackTrace();
			str = "false";
		}
		return str;
	}

	/**
	 * 6.保存所有功能点
	 * 
	 * @param functionVo
	 *            将对象FunctionVO新增到表function中, 成功返回SUCCESS,失败返回FALSE。
	 */
	public String saveFunction(FunctionVO functionVo) {
		String str = HibernateUtil.save(functionVo);
		return str;
	}

	/**
	 * 7.根据用户编号获取用户所有功能点
	 * 
	 * @param userId
	 * @return 根据参数从function、role表中读取信息, 封装到FunctionVO中。成功返回List,失败返回Null。
	 */
	public List getFunctionByUserId(String userId) {
		Connection conn = HibernateUtil.getConnection();
		List result = new ArrayList();
		String sql = "select a.functionId,a.functionName,a.url"
				+ " from functionn a,functionAndRole b,userAndRole c "
				+ "where a.functionId=b.functionId and b.roleId=c.userName and c.userName="
				+ userId;
		System.out.println("sql=" + sql);
		try {
			Statement stm = conn.createStatement();
			ResultSet rs = stm.executeQuery(sql);
			while (rs.next()) {
				FunctionVO functionVo = new FunctionVO();
				functionVo.setFunctionName(rs.getString("functionName"));
				functionVo.setUrl(rs.getString("url"));
				functionVo.setFunctionId(rs.getInt("functionId"));
				result.add(functionVo);
			}
		} catch (Exception e) {
			e.getStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;

	}

	public List getFunctionName() {
		// 建立数据库连接
		Connection conn = HibernateUtil.getConnection();
		List result = new ArrayList();

		String sql = "select * from functionn";
		Statement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			// if (rs.next()){
			// System.out.println("cunzai");
			// }else{
			// System.out.println("bucnaz");
			// }
			while (rs.next()) {
				FunctionVO functionVo = new FunctionVO();
				functionVo.setFunctionId(rs.getInt("functionId"));
				// System.out.println("cunzai"+rs.getInt("functionId"));
				functionVo.setFunctionName(rs.getString("functionName"));
				functionVo.setUrl(rs.getString("url"));
				result.add(functionVo);

			}
			// System.out.println("ceshi___"+result.size());
			return result;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {

				stm.close();
				rs.close();

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	// public Vector getFunctionURL(){
	// Vector vector = new Vector();
	// String sql = "select url from function";
	// Connection con = HibernateUtil.getConnection();
	// Statement stm = null;
	// ResultSet rs = null;
	// try {
	// stm = con.createStatement();
	// rs = stm.executeQuery(sql);
	// while(rs.next()){
	// vector.addElement((String)rs.getString("url"));
	// }
	// } catch (SQLException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return vector;
	// }
	public List getFunctionURL() {
		List list = new ArrayList();
		List functionList = new ArrayList();
		String sql = "from FunctionVO";

		try {
			list = HibernateUtil.queryHQL(sql);
			for (int i = 0; i < list.size(); i++) {
				FunctionVO functionVO = new FunctionVO();
				functionVO = (FunctionVO) list.get(i);
				functionList.add(functionVO.getUrl());
			}

		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return functionList;
	}
}

⌨️ 快捷键说明

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