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(""); out.println(""); out.println("RouteMap - Report"); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("

"); out.println(""); out.println(""); out.println(""); out.println(""); out.println("
ไม่พบข้อมูล Data Not Found ...
 
"); out.println(""); } 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; } }