package com.itjob.service;

import java.util.*;

import com.itjob.dao.BaseDao;
import com.itjob.dao.model.TRight;

/**
 * 权限信息管理的业务逻辑实现类.
 */
public class RightManagerImpl implements RightManager {
	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 获得权限信息列表
	 */
	public List getRightList() throws Exception {
		List<Map> returnList = new ArrayList<Map>();
		List list = dao.hqlQuery("from TRight");
		TRight right = null;
		Map<String, String> map = null;
		for (int i = 0; i < list.size(); i++) {
			map = new HashMap<String, String>();
			right = (TRight) list.get(i);
			map.put("num", String.valueOf(i + 1));
			map.put("trId", String.valueOf(right.getTrId()));
			map.put("rightName", right.getRightName());
			map.put("rightUrl", right.getRightUrl());
			map.put("description", right.getDescription());
			returnList.add(map);
		}
		return returnList;
	}
	
	/**
	 * 保存权限信息
	 * @param right 权限信息对象
	 */
	public void saveRight(TRight right) throws Exception {
		dao.saveObject(right);
	}

	/**
	 * 根据主键ID获得权限信息对象
	 */
	public TRight getRight(long trId) throws Exception {
		TRight right = (TRight) dao.getObject(TRight.class, trId);
		return right;
	}

	/**
	 * 更新权限信息对象
	 * @param right 权限信息对象
	 */
	public void updateRight(TRight right) throws Exception {
		dao.updateObject(right);
	}
	
	/**
	 * 删除权限信息
	 * @param idArray 需要删除的一到多个权限的id数组
	 */
	public void deleteRight(String[] idArray) throws Exception {
		TRight right = null;
		for (int i = 0; i < idArray.length; i++) {
			right = (TRight) dao.getObject(TRight.class, Long.parseLong(idArray[i]));
			dao.deleteObject(right);
		}
	}
}
