Java tutorial
/* * AttachBO.java 2010-11-29 * * Copyright 2010 Guangzhou Donghong Software Technology Inc. * Licensed under the www.jxstar.org */ package org.jxstar.service.studio; import java.io.File; import java.util.Map; import org.apache.commons.fileupload.FileItem; import org.jxstar.control.action.RequestContext; import org.jxstar.dao.DaoParam; import org.jxstar.service.BoException; import org.jxstar.service.BusinessObject; import org.jxstar.service.define.FunDefineDao; import org.jxstar.util.DateUtil; import org.jxstar.util.FileUtil; import org.jxstar.util.ImageResize; import org.jxstar.util.MapUtil; import org.jxstar.util.config.SystemVar; import org.jxstar.util.key.KeyCreator; import org.jxstar.util.resource.JsMessage; /** * ??? * upload.file.path -- ?C:/JXSTARDOC * upload.file.maxsize -- ?(M)?10 * * ??????'(2)'?? * * @author TonyTan * @version 1.0, 2010-11-29 */ public class AttachBO extends BusinessObject { private static final long serialVersionUID = 1L; //??? private static final String IMAGE_BIG = "_image_big"; /** * * @param attachId -- ID * @return */ public String downAttach(String attachId, RequestContext requestContext) { if (attachId == null || attachId.length() == 0) { setMessage(JsMessage.getValue("systembo.attachbo.paramerror")); return _returnFaild; } //? Map<String, String> mpAttach = queryAttach(attachId); if (mpAttach.isEmpty()) { _log.showDebug("not attach data id: " + attachId); return _returnSuccess; } //? String is_highimage = requestContext.getRequestValue("is_highimage", "0"); mpAttach.put("is_highimage", is_highimage); //??? try { byte[] bytes = queryAttachContent(mpAttach); requestContext.setReturnBytes(bytes); } catch (BoException e) { setMessage(JsMessage.getValue("systembo.attachbo.downerror")); return _returnFaild; } // String contentType = mpAttach.get("content_type"); requestContext.setRequestValue("ContentType", contentType); //?? String attachPath = mpAttach.get("attach_path"); String fileName = FileUtil.getFileName(attachPath); requestContext.setRequestValue("Attachment", fileName); return _returnSuccess; } /** * * @param mpAttach -- ??attach_path, table_name * @return * @throws BoException */ public byte[] queryAttachContent(Map<String, String> mpAttach) throws BoException { //?? String attachPath = mpAttach.get("attach_path"); if (attachPath == null || attachPath.length() == 0) { throw new BoException(JsMessage.getValue("systembo.attachbo.filenameerror")); } //????? String systemPath = SystemVar.getValue("upload.file.path", "D:/ATTACHDOC"); String tableName = mpAttach.get("table_name"); String fileName = FileUtil.getFileName(attachPath); attachPath = systemPath + "/" + tableName + "/" + fileName; _log.showDebug("---------??" + attachPath); //??? String is_highimage = MapUtil.getValue(mpAttach, "is_highimage", "0"); String content_type = MapUtil.getValue(mpAttach, "content_type"); if (is_highimage.equals("1") && content_type.indexOf("image") >= 0) { String[] orgNames = fileName.split("\\."); String hFileName = orgNames[0] + IMAGE_BIG + fileName.substring(fileName.indexOf(".")); String hAttachPath = systemPath + "/" + tableName + "/" + hFileName; _log.showDebug("---------??" + hAttachPath); //? File ftmp = new File(hAttachPath); boolean isexists = ftmp.exists(); if (isexists) { attachPath = hAttachPath; } else { _log.showDebug("---------???"); } } byte[] bytes = FileUtil.fileToBytes(attachPath); if (bytes == null) {//bytes.length == 0?0 throw new BoException(JsMessage.getValue("systembo.attachbo.downerror")); } else { return bytes; } } /** * ? * @param attachIds -- ID * @return */ public String deleteAttach(String[] attachIds) { if (attachIds == null || attachIds.length == 0) { setMessage(JsMessage.getValue("systembo.attachbo.paramerror")); return _returnFaild; } for (int i = 0, n = attachIds.length; i < n; i++) { String ret = deleteAttach(attachIds[i]); if (ret.equals(_returnFaild)) { return _returnFaild; } } return _returnSuccess; } /** * ? * @param attachId -- ID * @return */ public String deleteAttach(String attachId) { //? Map<String, String> mpAttach = queryAttach(attachId); if (mpAttach.isEmpty()) { _log.showDebug("not attach data id: " + attachId); return _returnSuccess; } //?? String fileName = mpAttach.get("attach_path"); if (fileName == null || fileName.length() == 0) { setMessage(JsMessage.getValue("systembo.attachbo.filenameerror")); return _returnFaild; } //????? String systemPath = SystemVar.getValue("upload.file.path", "D:/ATTACHDOC"); String tableName = mpAttach.get("table_name"); fileName = FileUtil.getFileName(fileName); fileName = systemPath + "/" + tableName + "/" + fileName; _log.showDebug("---------??" + fileName); File file = new File(fileName); if (file.exists() && !file.delete()) { setMessage(JsMessage.getValue("systembo.attachbo.deleteerror")); return _returnFaild; } // String sql = "delete from sys_attach where attach_id = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(attachId); if (!_dao.update(param)) { setMessage(JsMessage.getValue("systembo.attachbo.deleteerror")); return _returnFaild; } //? clearFieldValue(mpAttach); //???xxxx_image_big.xxx?? String use_resize = SystemVar.getValue("sys.attach.use_resize", "0"); if (use_resize.equals("1")) { File tofile = FileUtil.postfix(file, IMAGE_BIG); if (tofile.exists()) { _log.showDebug("............." + tofile.getName()); tofile.delete(); } } return _returnSuccess; } /** * ?? * ????????????ID? * * @param requestContext -- ? * @return */ public String saveAttach(RequestContext requestContext) { //?ID String dataId = requestContext.getRequestValue("dataid"); //ID String dataFunId = requestContext.getRequestValue("datafunid"); if (dataId.length() == 0 || dataFunId.length() == 0) { setMessage(JsMessage.getValue("systembo.attachbo.dataerror")); return _returnFaild; } // String attachId = ""; try { attachId = insertRecord(dataId, dataFunId, requestContext); } catch (BoException e) { _log.showError(e); setMessage(e.getMessage()); return _returnFaild; } FileItem item = getAttachItem(requestContext); //??? String orgName = FileUtil.getFileName(item.getName()); //??insertRecord String filePath = requestContext.getRequestValue("save_path"); //???? String fileName = filePath + orgName; _log.showDebug("---------???" + fileName + "; size:" + item.getSize()); FileUtil.createPath(filePath); File file = FileUtil.getValidFile(fileName); try { item.write(file); } catch (Exception e) { _log.showError(e); setMessage(JsMessage.getValue("systembo.attachbo.savefileerror")); return _returnFaild; } //?????????? String saveName = file.getName(); if (!saveName.equals(orgName)) { if (!updatePath(attachId, saveName)) {//??filePath? setMessage(JsMessage.getValue("systembo.attachbo.savefileerror")); return _returnFaild; } } //? saveType(attachId, requestContext); //???xxxx_image_big.xxx?? String imageSize = requestContext.getRequestValue("imagesize");// String isImageResize = requestContext.getRequestValue("is_imageresize");//?? if (isImageResize.equals("1")) { saveImageResize(file, imageSize); } String json = "{attachId:'" + attachId + "'}"; requestContext.setReturnData(json); return _returnSuccess; } /** * ?? * @param file * @param requestContext * @return */ private boolean saveImageResize(File file, String imageSize) { if (imageSize.length() == 0) { imageSize = SystemVar.getValue("sys.attach.resize", "600"); } float tosize = Float.parseFloat(imageSize); //??? String orgname = file.getName(); String orgpath = file.getParent(); File tofile = FileUtil.postfix(file, "_image_big"); boolean b = file.renameTo(tofile); if (b) { _log.showDebug(".............?" + tofile.getName()); } else { _log.showDebug("............." + tofile.getName()); } //? boolean ret = ImageResize.resizeImage(tofile, file, tosize); _log.showDebug(".............?" + ret + ";" + tofile.getName()); if (!ret) {//??? String orgstr = orgpath + "/" + orgname; _log.showDebug(".............??" + orgstr); File orgfile = new File(orgstr); tofile.renameTo(orgfile); } return true; } /** * * @param dataId -- ?ID * @param dataFunId -- ?ID * @param requestContext * @return * @throws BoException */ private String insertRecord(String dataId, String dataFunId, RequestContext requestContext) throws BoException { //?ID String attachId = KeyCreator.getInstance().createKey("sys_attach"); //??? String attachName = requestContext.getRequestValue("attach_name"); //? String attachField = requestContext.getRequestValue("attach_field"); //?? Map<String, String> mpFun = FunDefineDao.queryFun(dataFunId); //??? String tableName = mpFun.get("table_name"); //??? String funName = mpFun.get("fun_name"); //? String systemPath = SystemVar.getValue("upload.file.path", "D:/ATTACHDOC"); //?? FileItem item = getAttachItem(requestContext); if (item == null || item.isFormField()) { throw new BoException(JsMessage.getValue("systembo.attachbo.noupload")); } String fileName = FileUtil.getFileName(item.getName()); if (fileName == null || fileName.length() == 0) { throw new BoException(JsMessage.getValue("systembo.attachbo.filenameerror")); } //? String attachPath = systemPath + "/" + tableName + "/"; requestContext.setRequestValue("save_path", attachPath); attachPath += fileName; String contentType = item.getContentType(); //?? Map<String, String> mpUser = requestContext.getUserInfo(); String userId = MapUtil.getValue(mpUser, "user_id"); //?ID if (userId.length() == 0) { userId = requestContext.getRequestValue("user_id"); if (userId.length() > 0) { mpUser = queryUser(userId); } } String userName = MapUtil.getValue(mpUser, "user_name"); StringBuilder sbsql = new StringBuilder(); sbsql.append("insert into sys_attach ("); sbsql.append("attach_id, table_name, data_id, attach_name, content_type, attach_field, fun_id,"); sbsql.append("fun_name, attach_path, upload_date, upload_user, add_userid, add_date"); sbsql.append(") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); DaoParam param = _dao.createParam(sbsql.toString()); param.addStringValue(attachId); param.addStringValue(tableName); param.addStringValue(dataId); param.addStringValue(attachName); param.addStringValue(contentType); param.addStringValue(attachField); param.addStringValue(dataFunId); param.addStringValue(funName); param.addStringValue(fileName);//??attachPath? param.addDateValue(DateUtil.getTodaySec()); param.addStringValue(userName); param.addStringValue(userId); param.addDateValue(DateUtil.getTodaySec()); if (!_dao.update(param)) { throw new BoException(JsMessage.getValue("systembo.attachbo.newerror")); } return attachId; } /** * ?file */ private FileItem getAttachItem(RequestContext request) { String myField = "attach_path"; //? String attachField = request.getRequestValue("attach_field"); if (attachField.length() > 0) { myField = attachField; } FileItem item = (FileItem) request.getRequestObject(myField); if (item == null || item.isFormField()) { //?? String table = request.getRequestValue("table_name"); item = (FileItem) request.getRequestObject(table + "__" + attachField); } return item; } /** * * @param attachId -- ID * @param path -- ?? * @return */ private boolean updatePath(String attachId, String path) { String sql = "update sys_attach set attach_path = ? where attach_id = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(path); param.addStringValue(attachId); return _dao.update(param); } /** * ? * @param attachId -- ID * @return */ private Map<String, String> queryAttach(String attachId) { String sql = "select table_name, data_id, content_type, attach_field, fun_id, attach_path " + "from sys_attach where attach_id = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(attachId); return _dao.queryMap(param); } // private boolean clearFieldValue(Map<String, String> mpAttach) { //? String attachField = mpAttach.get("attach_field"); if (attachField.length() == 0) return true; String dataId = mpAttach.get("data_id"); String tableName = mpAttach.get("table_name"); String funId = mpAttach.get("fun_id"); //?? Map<String, String> mpFun = FunDefineDao.queryFun(funId); //??? String keyName = mpFun.get("pk_col"); String sql = "update " + tableName + " set " + attachField + " = '' where " + keyName + " = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(dataId); return _dao.update(param); } /** * ?ReportXlsUtil.printCellImage() * @param attachId -- ID * @return */ public Map<String, String> queryAttach(String dataId, String tableName, String fieldName) throws BoException { if (dataId == null || dataId.length() == 0 || tableName == null || tableName.length() == 0 || fieldName == null || fieldName.length() == 0) { throw new BoException(JsMessage.getValue("systembo.attachbo.paramerror")); } String sql = "select table_name, data_id, content_type, attach_field, fun_id, attach_path " + "from sys_attach where table_name = ? and data_id = ? and attach_field = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(tableName); param.addStringValue(dataId); param.addStringValue(fieldName); return _dao.queryMap(param); } //?ID public Map<String, String> queryUser(String userId) { String sql = "select user_id, user_name from sys_user where user_id = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(userId); return _dao.queryMap(param); } //?????insertRecord private boolean saveType(String attachId, RequestContext request) { String typeData = request.getRequestValue("attach_type"); if (typeData.length() == 0) return true; String sql = "update sys_attach set attach_type = ? where attach_id = ?"; DaoParam param = _dao.createParam(sql); param.addStringValue(typeData); param.addStringValue(attachId); return _dao.update(param); } }