spring -
package com.ksj.empmanager.service;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ksj.lib.ExcelBuilder;
import com.ksj.lib.MailHelper;
import com.ksj.lib.PagingHelper;
import com.ksj.lib.QuartzHelper;
import com.ksj.lib.ValidatorTemp;
import com.ksj.mybatis.EmpMapper;
@Service
public class EmpServiceImpl implements BbsService{
@Autowired
EmpMapper mapper;
@Autowired
PagingHelper pagingHelper;
@Autowired
MailHelper mailHelper;
@Autowired
QuartzHelper quartzHelper;
@Autowired
ValidatorTemp validator;
@Override
public int getTotalAtcCnt(HashMap<String, Object> params) {
return mapper.getTotalAtcCnt(params);
}
@Override
public ArrayList<HashMap<String, Object>> getAtcList(HashMap<String, Object> params) {
return mapper.getAtcList(params);
}
public ArrayList<HashMap<String, Object>> getAtcListForEmpCode(HashMap<String, Object> params) {
return mapper.getAtcListForEmpCode(params);
}
@Override
public HashMap<String, Object> getAtc(HashMap<String, Object> params) {
return mapper.getAtc(params);
}
public HashMap<String, Object> getMail(HashMap<String, Object> params) {
return mapper.getMail(params);
}
public void setMail(HashMap<String, Object> params) {
mapper.setMail(params);
}
@Override
public void insertAtc(HashMap<String, Object> params) {
mapper.insertAtc(params);
}
@Override
public void updateAtc(HashMap<String, Object> params) {
mapper.updateAtc(params);
}
@Override
public void deleteAtc(HashMap<String, Object> params) {
mapper.deleteAtc(params);
}
// 직급 리스트 받기
public List<Object> getPosList(){
return mapper.getPosList();
}
// 직원코드 확인( 본인의 empSeq를 제외 )
public ArrayList<HashMap<String, Object>> checkEmpCode(HashMap<String, Object> params) {
return mapper.checkEmpCode(params);
}
/*************************
* 게시판 페이징 데이터 params에 넣기
*************************/
@Override
public HashMap<String, Object> getPagingHelper(HashMap<String, Object> params) {
return pagingHelper.getPagingHelper(params);
}
/*************************
* EMPM 유효성 체크
*************************/
public boolean checkValidForEMPM(HashMap<String, Object> params){
boolean isValid = true;
String empCode = params.get("empCode").toString();
String empName = params.get("empName").toString();
String posCode = params.get("posCode").toString();
String email = params.get("email").toString();
String phone = params.get("phone1").toString().trim()
+ "-"
+ params.get("phone2").toString().trim()
+ "-"
+ params.get("phone3").toString().trim();
//유효성 검사
if (!validator.isEmail(email)
|| !validator.isPhone(phone)
|| !validator.isNum(empCode) || !validator.checkLength(empCode,1,3)
|| !validator.isChar(empName) || !validator.checkLength(empName,2,20)
|| !validator.isNum(posCode)){
isValid = false;
}
return isValid;
}
/*************************
* setEmail 유효성 체크
*************************/
public boolean checkValidForSetMail(HashMap<String, Object> params){
boolean isValid = true;
String mailTo = params.get("mailTo").toString();
String timeToSend = params.get("timeToSend").toString();
//유효성 검사
if (!validator.isEmail(mailTo)
|| !validator.isTime(timeToSend) || !validator.checkLength(timeToSend,8,8)){
isValid = false;
}
return isValid;
}
/*************************
* 웹페이지에 메세지 출력
*************************/
public void printMsg(HttpServletResponse resp, String msg) throws IOException{
resp.setCharacterEncoding("UTF-8");
PrintWriter writer = resp.getWriter();
writer.println("<script type='text/javascript'>");
writer.println("alert('" +msg+ "');");
writer.println("history.back();");
writer.println("</script>");
writer.flush();
}
/*************************
* EMPM 엑셀 구성에 필요한 것들을 Map에 저장 후 반환
*************************/
public Map readyToBuildExcel(HashMap<String, Object> params, Map<String, Object> model){
// 페이징
params.put("ttAtcCnt", getTotalAtcCnt(params));
params.put("hmAtcN", 999);
getPagingHelper(params);
// 0 붙여서 세자리로 채우기
ArrayList<HashMap<String, Object>> empMembers = getAtcList(params);
for (int i=0; i<empMembers.size(); i++){
String empCode = empMembers.get(i).get("EMPCODE").toString();
for (int j=0; j<2; j++){
if (empCode.length()<3) empCode = "0" + empCode;
}
empMembers.get(i).put("EMPCODE", empCode);
}
// 엑셀에서의 컬럼명과 순서 정하기
ArrayList<HashMap<String, String>> columns = new ArrayList<HashMap<String, String>>();
columns.add(new HashMap<String, String>());
columns.get(0).put("columnName", "직원번호");
columns.get(0).put("keyName", "EMPCODE");
columns.add(new HashMap<String, String>());
columns.get(1).put("columnName", "이름");
columns.get(1).put("keyName", "EMPNAME");
columns.add(new HashMap<String, String>());
columns.get(2).put("columnName", "직급");
columns.get(2).put("keyName", "POSNAME");
columns.add(new HashMap<String, String>());
columns.get(3).put("columnName", "이메일");
columns.get(3).put("keyName", "EMAIL");
columns.add(new HashMap<String, String>());
columns.get(4).put("columnName", "전화번호");
columns.get(4).put("keyName", "PHONE");
// 엑셀 파일명
String fileName = (params.get("fileName") != null && params.get("fileName") != "") ?
params.get("fileName").toString() : "test_members";
// 엑셀 시트 이름 (원시트 체제로 임시로 만듬)
String sheetName = "members_list";
// excelView에 들어갈 재료 설정
model.put("datas", empMembers);
model.put("columns", columns);
model.put("fileName", fileName);
model.put("sheetName", sheetName);
model.put("columnWidth", 30);
return model;
}
/*************************
* 쿼츠 시간 변경
*************************/
public boolean setQuartzCron(HashMap<String, Object> params){
return quartzHelper.setQuartzCron(params);
}
/*************************
* 쿼츠의 정해진 시간에 실행되는 메소드 구현하는 곳
*************************/
public void myExecute() {
Map<String, Object> model = new HashMap<String, Object>();
HashMap<String, Object> params = new HashMap<String, Object>();
readyToBuildExcel(params, model);
ExcelBuilder excelBuilder = new ExcelBuilder();
File file = new File("");
try {
file = excelBuilder.getExcelFile(new HSSFWorkbook(), model);
} catch (Exception e1) {
e1.printStackTrace();
}
String mailTo = getMail(params).get("MAILTO").toString();
try {
mailHelper.sendMail(mailTo, "직원 목록입니다.", "안녕하세요.\n 김수중입니다.\n 직원 목록을 엑셀파일(xls)로 첨부하였습니다.", file);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}