ENDRPrint/.svn/pristine/83/8313f28470ace74ec100de4c5b1199d0d6999f2e.svn-base

215 lines
7.2 KiB
Plaintext
Raw Normal View History

2024-08-14 10:33:27 +07:00
package th.co.muangthai.endrprint.controller;
import com.google.gson.Gson;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
import org.codehaus.jackson.map.ObjectMapper;
import th.co.muangthai.endrprint.dao.serviceImp.*;
import th.co.muangthai.endrprint.dao.serviceInterface.*;
import th.co.muangthai.endrprint.model.bean.form.*;
import th.co.muangthai.endrprint.model.bean.master.EndrUserLoginBean;
import th.co.muangthai.endrprint.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
/**
* AbstractMasterController
*/
public abstract class AbstractMasterController implements Serializable {
protected Gson gsonData = JsonUtil.getGsonInstance();
ObjectMapper mapper = new ObjectMapper();
protected CommonServiceInterface commonService = new CommonServiceImp();
/**
* clear Session
*
* @param request
*/
protected void clearSession(HttpServletRequest request) {
try {
Object userBeans = request.getSession().getAttribute(TextContent.sessionLogin);
//if( null != userBeans ){
request.getSession().removeAttribute(TextContent.sessionLogin);
request.getSession().invalidate();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get Session User Login
*
* @param request
* @return Data results
*/
protected EndrUserLoginBean getUerLoginBean(HttpServletRequest request) {
EndrUserLoginBean userBeans = new EndrUserLoginBean();
try {
Object obj = request.getSession().getAttribute(TextContent.sessionLogin);
if (null != obj) {
userBeans = (EndrUserLoginBean) obj;
}
} catch (Exception e) {
e.printStackTrace();
}
return userBeans;
}
/**
* Get String Json
*
* @param dataJson
* @return String Json
*/
protected String toStringJson(String dataJson) {
try {
return new String(dataJson.getBytes("UTF-8"), "ISO-8859-1");
//return new String( dataJson.getBytes(),"TIS-620" );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* Get BigDecimal
*
* @param txt
* @return BigDecimal
*/
protected BigDecimal getBigDecimal(String txt) {
if (VSMUtil.isNotEmpty(txt)) {
return new BigDecimal(txt);
}
return null;
}
/**
* Get String Time
*
* @param date
* @return String
* @throws ParseException
*/
protected String getTimeStr(Date date) throws ParseException {
return DateUtil.toFormatString(date, "HHmmss");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
////// For Report
/**
* @param response
*/
public void dataNotFound(HttpServletResponse response) {
response.setContentType("text/html");
try {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>RouteMap - Report</title>");
out.println("</head>");
out.println("<body bgcolor=\"#9999CC\" leftmargin=\"0\" topmargin=\"0\" >");
out.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" align=\"center\">");
out.println("<tr height=\"80\">");
out.println("<td width=\"100%\" BACKGROUND =\"./images/tg_banner.gif\" align=\"center\">");
out.println("</td></tr></table><BR>");
out.println("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" align=\"center\"><form name=\"form1\">");
out.println("<tr align=\"center\"><td><strong>ไม่พบข้อมูล Data Not Found ...</strong></td></tr>");
out.println("<tr rowspan='2'><td>&nbsp;</td></tr>");
out.println("<tr align=\"center\"><td><input type=\"button\" value=\"ปิดหน้าจอ\" onClick=\"javascript:window.close();\"></td></tr>");
out.println("</form></table></body>");
out.println("</html>");
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @param request
* @param response
* @param fileName
* @param type
* @return
*/
public static HttpServletResponse encodingTextResponse(HttpServletRequest request, HttpServletResponse response, String fileName, String type) {
final String XLS = ".XLS";
final String PDF = ".PDF";
SimpleDateFormat fmt = new SimpleDateFormat("ddMMyyyy-hhmmss");
String dateTextLong = fmt.format(new Date());
try {
fileName = fileName + "-" + dateTextLong + "" + type;
String ContentDispositionType = "inline";
if (XLS.equals(type)) {
ContentDispositionType = "attachment";
}
String user_agent = request.getHeader("user-agent");
boolean isInternetExplorer = (user_agent.indexOf("MSIE") > -1);
if (isInternetExplorer) {
response.setHeader("Content-disposition", "" + ContentDispositionType + "; filename=\"" + URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20") + "\"");
} else {
response.addHeader("Content-Disposition", "" + ContentDispositionType + "; " + "filename=\"" + MimeUtility.encodeWord(fileName, "utf-8", "Q").replaceAll("\\+", "%20") + "\"");
}
return response;
} catch (UnsupportedEncodingException ence) {
// ... handle exception ...
ence.getMessage();
}
return null;
}
/**
* Get Fix Message
*
* @param str - Message Code
* @return String
*/
// protected static String getResourceBundle(String str) {
// //ResourceBundle bundle = ResourceBundle.getBundle("message");
// //return bundle.getString(str);
// return SessionUtil.getMsg(str);
// }
/**
* Get Message properties
*
* @param str - Message Code
* @return String
*/
protected static String getResourceBundleTxtMsg(String str) {
ResourceBundle bundle = ResourceBundle.getBundle("message");
return bundle.getString(str);
// return SessionUtil.getMsg(str);
}
/**
* Get Error Exception
*
* @param e - Exception
* @return Object MsgErrorForm
*/
protected MsgErrorForm getErrorForException(Exception e) {
MsgErrorForm errorForm = new MsgErrorForm();
errorForm.setDataErrorException(e);
return errorForm;
}
}