Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dlshouwen.jspc.zwpc.controller; import com.dlshouwen.core.base.config.CONFIG; import com.dlshouwen.core.base.extra.grid.model.Pager; import com.dlshouwen.core.base.extra.grid.utils.PagerPropertyUtils; import com.dlshouwen.core.base.http.FileUploadClient; import com.dlshouwen.core.base.model.AjaxResponse; import com.dlshouwen.core.base.model.OperationType; import com.dlshouwen.core.base.model.SessionUser; import com.dlshouwen.core.base.utils.AttributeUtils; import com.dlshouwen.core.base.utils.CodeTableUtils; import com.dlshouwen.core.base.utils.DateUtils; import com.dlshouwen.core.base.utils.GUID; import com.dlshouwen.core.base.utils.LogUtils; import com.dlshouwen.core.system.dao.UserDao; import com.dlshouwen.core.system.model.User; import com.dlshouwen.core.system.model.UserVO; import com.dlshouwen.jspc.zjpc.dao.ExpreviewDao; import com.dlshouwen.jspc.zjpc.model.Expreview; import com.dlshouwen.jspc.zwpc.dao.EvalItemDao; import com.dlshouwen.jspc.zwpc.model.EvalItem; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * ? * * @author cuixubin */ @Controller @RequestMapping("/jspc/zwpc/zwpc") public class SelfEvaluateController { /** * */ private String basePath = "jspc/zwpc/"; /** * ?? */ private EvalItemDao dao; /** * ?? */ private UserDao userDao; /** * ?? */ private ExpreviewDao expDao; /** * ?? * * @param dao ?? */ @Resource(name = "evalItemDao") public void setDao(EvalItemDao dao) { this.dao = dao; } /** * ?? * * @param dao ?? */ @Resource(name = "userDao") public void setUserDao(UserDao userDao) { this.userDao = userDao; } /** * ?? * * @param expDao */ @Resource(name = "expreviewDao") public void setExpDao(ExpreviewDao expDao) { this.expDao = expDao; } /** * ? * * @param model ? * @param request * @param expectation ?? * @return basePath + 'main' * @throws Exception */ @RequestMapping(value = "", method = RequestMethod.GET) public String toDeclarationPage(Model model, HttpServletRequest request, String expectation) throws Exception { // ?? Expreview exp = new Expreview(); model.addAttribute("expreview", exp); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "declaration"; } /** * ?? * * @param request * @return * @throws Exception */ @RequestMapping(value = "/listPage", method = RequestMethod.GET) public String toEvalItemListPage(HttpServletRequest request) throws Exception { // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "evalList"; } /** * ?? * * @param gridPager ? * @param request * @param response ? * @throws Exception */ @RequestMapping(value = "/list", method = RequestMethod.POST) public void getEvalList(String gridPager, HttpServletRequest request, HttpServletResponse response) throws Exception { SessionUser sUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); // Pager Pager pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager)); // ?? expDao.getExpreviewByUserId(sUser.getUser_id(), pager, request, response); // ? LogUtils.updateOperationLog(request, OperationType.SEARCH, "??"); } /** * ? * * @param qualified ?id * @param model ? * @param request * @return basePath + 'main' * @throws Exception */ @RequestMapping(value = "/{qualified}/application", method = RequestMethod.GET) public String toApplyPage(@PathVariable String qualified, Model model, HttpServletRequest request) throws Exception { // session?? SessionUser sUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); User user = userDao.getUserById(sUser.getUser_id()); // ?? Expreview exp = new Expreview(); exp.setQualified(qualified); exp.setId(new GUID().toString()); exp.setUser_id(user.getUser_id()); exp.setUser_name(user.getUser_name()); exp.setStatus("4");//? 4?? exp.setCreate_date(new Date()); expDao.insertExpreview(exp); // ?? UserVO UVO = new UserVO(user, exp); model.addAttribute("UVO", UVO); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "application"; } /** * * * @param evalItem * @param request * @param bindingResult ? * @param response * @throws Exception */ @RequestMapping(value = "/addEvalItem", method = RequestMethod.POST) @ResponseBody @Transactional public void addEvalItem(@Valid EvalItem evalItem, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) throws Exception { // AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); if (bindingResult.hasErrors()) { ajaxResponse.bindingResultHandler(bindingResult); LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + AjaxResponse.getBindingResultMessage(bindingResult) + ""); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } if (StringUtils.isNotEmpty(evalItem.getEvalResult_id()) && StringUtils.isNotEmpty(evalItem.getType())) { SessionUser sUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); evalItem.setCreator(sUser.getUser_id()); evalItem.setEditor(sUser.getUser_id()); String attachName = uploadFile(request, "upfile"); evalItem.setAttach(attachName); evalItem.setCreateDate(new Date()); evalItem.setEditDate(new Date()); dao.insertItem(evalItem); } // AJAX? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); } /** * * * @param evalItem * @param request * @param bindingResult ? * @return AJAX? * @throws Exception */ @RequestMapping(value = "/updateEvalItem", method = RequestMethod.POST) @ResponseBody @Transactional public void updateEvalItem(@Valid EvalItem evalItem, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) throws Exception { // AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); if (bindingResult.hasErrors()) { ajaxResponse.bindingResultHandler(bindingResult); LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + AjaxResponse.getBindingResultMessage(bindingResult) + ""); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } if (StringUtils.isNotEmpty(evalItem.getEvalResult_id()) && StringUtils.isNotEmpty(evalItem.getType())) { SessionUser sUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); evalItem.setEditor(sUser.getUser_id()); String attachName = uploadFile(request, "upfile"); evalItem.setAttach(attachName); evalItem.setEditDate(new Date()); dao.updateEvalItem(evalItem); } // AJAX? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); } /** * ? * * @param expId_itemType * @param request * @return AJAX? * @throws Exception */ @RequestMapping(value = "/{expId_itemType}/refreshItems", method = RequestMethod.POST) @ResponseBody @Transactional public AjaxResponse refreshItems(@PathVariable String expId_itemType, HttpServletRequest request) throws Exception { // AJAX? AjaxResponse aresp = new AjaxResponse(); String expId = null; String itemType = null; if (StringUtils.isNotEmpty(expId_itemType)) { expId = expId_itemType.split("_")[0]; itemType = expId_itemType.split("_")[1]; } List<EvalItem> etList = new ArrayList<EvalItem>(); if (StringUtils.isNotEmpty(expId) && StringUtils.isNotEmpty(itemType)) { etList = dao.getByExpIdAndItemType(expId, itemType); } String html = getItemForHtml(etList, request.getServletContext()); Map map = new HashMap(); map.put("ITEM_DATE", html); map.put("ITEM_TYPE", itemType); aresp.setSuccess(true); aresp.setSuccessMessage("??"); aresp.setExtParam(map); return aresp; } /** * ?? * * @param expId * @param request * @return AJAX? * @throws Exception */ @RequestMapping(value = "/{expId}/getAllItems", method = RequestMethod.POST) @ResponseBody @Transactional public AjaxResponse getAllItems(@PathVariable String expId, HttpServletRequest request) throws Exception { // AJAX? AjaxResponse aresp = new AjaxResponse(); // ?? List<EvalItem> evalItems = new ArrayList<EvalItem>(); evalItems = dao.getAllByExpId(expId); // List ET[] = new ArrayList[8]; for (int i = 0; i < ET.length; i++) { ET[i] = new ArrayList<EvalItem>(); } for (EvalItem item : evalItems) { if (item.getType().equals("JSYR")) {// ET[0].add(item); } else if (item.getType().equals("KCJX")) {// ET[1].add(item); } else if (item.getType().equals("JYJXYJ")) {// ET[2].add(item); } else if (item.getType().equals("YXL")) {//? ET[3].add(item); } else if (item.getType().equals("JYJXJL")) {//?? ET[4].add(item); } else if (item.getType().equals("JXJYJL")) {//? ET[5].add(item); } else if (item.getType().equals("ZYJSZC")) {//? ET[6].add(item); } else if (item.getType().equals("QT")) {// ET[7].add(item); } } Map map = new HashMap(); map.put("JSYR", getItemForHtml(ET[0], request.getServletContext())); map.put("KCJX", getItemForHtml(ET[1], request.getServletContext())); map.put("JYJXYJ", getItemForHtml(ET[2], request.getServletContext())); map.put("YXL", getItemForHtml(ET[3], request.getServletContext())); map.put("JYJXJL", getItemForHtml(ET[4], request.getServletContext())); map.put("JXJYJL", getItemForHtml(ET[5], request.getServletContext())); map.put("ZYJSZC", getItemForHtml(ET[6], request.getServletContext())); map.put("QT", getItemForHtml(ET[7], request.getServletContext())); aresp.setExtParam(map); aresp.setSuccess(true); aresp.setSuccessMessage("?"); return aresp; } /** * ?(?) * * @param expId id * @param model ? * @param request * @return basePath + 'applyDetail' * @throws Exception */ @RequestMapping(value = "/{expId}/toDetailPage", method = RequestMethod.GET) public String toApplyDetailPage(@PathVariable String expId, Model model, HttpServletRequest request) throws Exception { String userid = null; if (StringUtils.isNotEmpty(expId)) { userid = expDao.getExpreviewById(expId).getUser_id(); } // ?? User temUser = userDao.getUserById(userid); Expreview temExp = expDao.getExpreviewById(expId); UserVO UVO = new UserVO(temUser, temExp); model.addAttribute("UVO", UVO); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "applyDetail"; } /** * ? * * @param expId id * @param model ? * @param request * @return basePath + 'applyDetail' * @throws Exception */ @RequestMapping(value = "/{expId}/toResultPage", method = RequestMethod.GET) public String toResultPage(@PathVariable String expId, Model model, HttpServletRequest request) throws Exception { String userid = null; if (StringUtils.isNotEmpty(expId)) { userid = expDao.getExpreviewById(expId).getUser_id(); } // ?? User temUser = userDao.getUserById(userid); Expreview temExp = expDao.getExpreviewById(expId); UserVO UVO = new UserVO(temUser, temExp); model.addAttribute("UVO", UVO); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "applyDetail"; } /** * * * ?iditem * * @param itemId * @param request * @return * @throws java.lang.Exception */ @RequestMapping(value = "/deleteItem", method = RequestMethod.POST) @ResponseBody public AjaxResponse deleteItem(String itemId, HttpServletRequest request) throws Exception { AjaxResponse ajaxResponse = new AjaxResponse(); // ? if (StringUtils.isNotEmpty(itemId)) { dao.deleteItem(itemId); } ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("?"); return ajaxResponse; } /** * * * ?? * * @param expId * @param expStatu * @param request * @return * @throws java.lang.Exception */ @RequestMapping(value = "/submitItems", method = RequestMethod.POST) @ResponseBody public AjaxResponse submitItems(String expId, String expStatu, HttpServletRequest request) throws Exception { AjaxResponse ajaxResponse = new AjaxResponse(); // ? if (StringUtils.isNotEmpty(expId)) { Expreview exp = expDao.getExpreviewById(expId); exp.setStatus("0");//? expDao.updateExpreview(exp); } ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("???"); //URL?? Map map = new HashMap(); if ("notNumber".equals(expStatu)) { map.put("URL", basePath + "zwpc"); } else { map.put("URL", basePath + "zwpc/listPage"); } ajaxResponse.setExtParam(map); return ajaxResponse; } /** * * * @param expId_itemType id * @param request * @return basePath + 'toAddEvalItemPage' * @throws Exception */ @RequestMapping(value = "/{expId_itemType}/toAddEvalItemPage", method = RequestMethod.GET) public String toAddEvalItemPage(@PathVariable String expId_itemType, HttpServletRequest request, Model model) throws Exception { EvalItem evalItem = null; String jspName = "addEvalItem"; if (expId_itemType.contains("_")) { evalItem = new EvalItem(); evalItem.setEvalItem_id(new GUID().toString()); String expId = null; String itemType = null; if (StringUtils.isNotEmpty(expId_itemType)) { expId = expId_itemType.split("_")[0]; itemType = expId_itemType.split("_")[1]; } evalItem.setEvalResult_id(expId); evalItem.setType(itemType); } else { evalItem = dao.getEvalItemById(expId_itemType); jspName = "updateEvalItem"; } model.addAttribute("evalItem", evalItem); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, ""); return basePath + jspName; } /** * ?? * * @param request * @return basePath + 'toUserInfoPage' * @throws Exception */ @RequestMapping(value = "/toUserInfoPage", method = RequestMethod.GET) public String toUserInfoPage(HttpServletRequest request) throws Exception { // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "userInfo"; } /** * * * @param applyIds ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/deleteApply", method = RequestMethod.POST) @ResponseBody public AjaxResponse deleteApply(String applyIds, HttpServletRequest request) throws Exception { // ? StringBuffer deleteApplayIds = new StringBuffer(); // ?? int totalCount = applyIds.split(",").length; int expNowCount = 0; // ??? StringBuffer successIds = new StringBuffer(); StringBuffer failureIds = new StringBuffer(); // ?? for (String applyId : applyIds.split(",")) { // ?? boolean isNowExp = expDao.getExpreviewById(applyId).getStatus().equals("0"); if (isNowExp) { expNowCount++; failureIds.append(applyId).append(","); continue; } successIds.append(applyId).append(","); // ? deleteApplayIds.append(applyId).append(","); } if (deleteApplayIds.length() > 0) { deleteApplayIds.deleteCharAt(deleteApplayIds.length() - 1); // expDao.deleteExpreview(deleteApplayIds.toString()); } // ??? StringBuffer message = new StringBuffer(); message.append("").append(totalCount).append("?<br /><br />"); message.append("?").append(totalCount - expNowCount).append("?<br />"); if (expNowCount > 0) { message.append("").append(expNowCount) .append("?<br />"); } // ? AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage(message.toString()); ajaxResponse.setData(deleteApplayIds.toString().replaceAll("'", "")); // ? LogUtils.updateOperationLog(request, OperationType.DELETE, "?" + successIds.toString() + "" + failureIds.toString()); return ajaxResponse; } // @RequestMapping(value = "/downloadFile", method = RequestMethod.GET) public void download(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=utf-8"); String itemId = request.getParameter("itemId"); String path = dao.getEvalItemById(itemId).getAttach(); String fileName = path.substring(path.lastIndexOf("/") + 1); File f = new File(path); if (f.exists()) { FileInputStream filein = new FileInputStream(f); byte[] b = new byte[1024]; try { int len = filein.read(b); if (fileName != null) { fileName = new String(fileName.getBytes(), "ISO8859-1"); if (fileName != "") { response.addHeader("Content-Disposition", "attachment;filename=" + fileName); } } while (len > 0) { response.getOutputStream().write(b, 0, len); len = filein.read(b); } } catch (Exception e) { System.out.println(e.getStackTrace() + e.getMessage()); } finally { response.getOutputStream().flush(); response.getOutputStream().close(); filein.close(); } } else { response.getWriter().print("????"); } } private String uploadFile(HttpServletRequest request, String fname) throws IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile(fname); String fileName = multipartFile.getOriginalFilename(); String filePath = ""; if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) { JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream()); if (jobj.getString("responseMessage").equals("OK")) { filePath = jobj.getString("fpath"); } } /* if (fileName.trim().length() > 0) { int pos = fileName.lastIndexOf("."); fileName = fileName.substring(pos); String fileDirPath = request.getSession().getServletContext().getRealPath("/"); String path = fileDirPath.substring(0, fileDirPath.lastIndexOf(File.separator)) + CONFIG.UPLOAD_FILE_PATH; Date date = new Date(); fileName = String.valueOf(date.getTime()) + fileName; File file = new File(path); if (!file.exists()) { file.mkdirs(); } file = new File(path + "/" + fileName); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } path = path + "/" + fileName; FileOutputStream fos = null; InputStream s = null; try { fos = new FileOutputStream(file); s = multipartFile.getInputStream(); byte[] buffer = new byte[1024]; int read = 0; while ((read = s.read(buffer)) != -1) { fos.write(buffer, 0, read); } fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } if (s != null) { s.close(); } } catch (Exception e) { e.printStackTrace(); } } path = path.replaceAll("\\\\", "/"); filePath = path; } */ return filePath; } private String getItemForHtml(List<EvalItem> items, ServletContext sc) { String result = "", preHtml; String beizhu = ""; if (!items.equals(null) && items.size() > 0) { for (int i = 0; i < items.size(); i++) { preHtml = "<li class='item-table-list-li'>" + "<div class='item-table-list-box'>" + "<i class='fa fa-edit' style='display:none;' onclick=\'editItem(\"" + items.get(i).getEvalItem_id() + "\",\"" + items.get(i).getType() + "\")\'></i>" + "<i class='fa fa-times' style='display:none;' id='" + items.get(i).getEvalItem_id() + "' onclick=\'deleteItem(\"" + items.get(i).getEvalItem_id() + "\")\'></i>" + "<table class='table table-responsive item-table-list' width='100%' border='0' cellspacing='0' cellpadding='0'>" + "<tr>"; String bDate = DateUtils.getYearMonth(items.get(i).getBeginDate()); String eDate = DateUtils.getYearMonth(items.get(i).getEndDate()); String level = CodeTableUtils.getValue(sc, "ryjb", items.get(i).getLevel()); String grade = CodeTableUtils.getValue(sc, "hjdj", items.get(i).getGrade()); level = StringUtils.isEmpty(level) ? "" : level; grade = StringUtils.isEmpty(grade) ? "" : grade; result += preHtml; result += "<td class='first_td'>" + bDate + "-" + eDate + "</td>"; result += "<td>" + items.get(i).getName() + "</td>"; result += "<td>" + level + grade + "</td>"; // result += "<td><a target='_blank' href='" + sc.getContextPath() + "/jspc/zwpc/zwpc/downloadFile?itemId=" + items.get(i).getEvalItem_id() + "' class='table-tc-fj-open' ></a></td>"; if (StringUtils.isNotEmpty(items.get(i).getAttach())) { String sourceURL = AttributeUtils.getAttributeContent(sc, "source_webapp_file_postion"); result += "<td><a target='_blank' href='" + sourceURL + items.get(i).getAttach() + "' class='table-tc-fj-open' ></a></td>"; } else { result += "<td> </td>"; } beizhu = items.get(i).getDescription(); beizhu = StringUtils.isEmpty(beizhu) ? "" : beizhu; if ("".equals(beizhu)) { result += "</tr></table><div class='exp_description'>" + beizhu + "</div></li>"; } else { result += "</tr></table><div class='exp_description' title='" + beizhu + "'>" + beizhu + "</div></li>"; } } } else { result = "<div class='div_zanwu'></div>"; } return result; } }