-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulletinInsertAction.java
More file actions
169 lines (136 loc) · 4.9 KB
/
BulletinInsertAction.java
File metadata and controls
169 lines (136 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.itgroup.busi.action.bulletin;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.itgroup.busi.entities.Bulletin;
import com.itgroup.busi.service.IBulletinService;
import com.itgroup.busi.util.ueditor.Uploader;
import com.itgroup.core.action.BaseAction;
import com.itgroup.core.action.file.FileAction;
import com.itgroup.core.entities.File;
import com.itgroup.core.entities.User;
import com.itgroup.core.exception.BusinessException;
import com.itgroup.core.exception.ParamException;
import com.itgroup.core.service.IFileService;
/**
*
* @author zy
* 公告新增
*/
@Controller
public class BulletinInsertAction extends BaseAction{
private static final Logger log = Logger.getLogger(BulletinInsertAction.class);
@Resource(name="fileService")
private IFileService fileService;
@Resource(name="bulletinService")
private IBulletinService bulletinService;
@InitBinder("bulletin")
public void initBinder2(WebDataBinder binder) {
binder.setFieldDefaultPrefix("bulletin.");
}
/**
* 新增公告准备
* @Description: TODO
* @param model
* @return String
* @exception:
* @author:
* @time: 2017年4月21日 下午5:39:00
*/
@RequestMapping("busi/bulletin/bulletinInsert!prepare.action")
public String bulletinInsertPrepare(ModelMap model){
return "/WEB-INF/busi/bulletin/bulletinInsert.jsp";
}
/**
* 新增公告
* @param model
* @param bulletin
* @param request
* @return
*/
@ResponseBody
@RequestMapping("busi/bulletin/bulletinInsert!insert.action")
public JSONObject bulletinInsertSubmit(ModelMap model,Bulletin bulletin,HttpServletRequest request){
JSONObject object = new JSONObject();
object.put("code", 1);
object.put("msg", "新增成功");
User user = getLoginUser();
bulletin.setPublishUser(user);
try{
//把图片信息保存到表中
saveImageFile(bulletin,request);
bulletinService.insertBulletinTransaction(bulletin);
}catch(BusinessException e){
e.printStackTrace();
object.put("code", 0);
object.put("reason", "新增公告失败,原因:"+e.getMessage());
}catch(ParamException e){
e.printStackTrace();
object.put("code", 0);
object.put("reason", "新增公告失败,原因:"+e.getMessage());
}
return object;
}
/**
* 把图片信息保存到session中
* @Description:
* @exception:
* @author: zy
* @time: 2017年5月10日 上午12:49:16
*/
public void saveImageFile(Bulletin bulletin,HttpServletRequest request){
List<Uploader> uploaderList = (List<Uploader>)request.getSession().getAttribute("uploaderObjectList");
if(uploaderList == null && bulletin.getContent()!=null && bulletin.getContent().contains("<img")){
throw new ParamException("您新建的公告中包含图片,登陆超时,无法提交公告,请重新上传图片或者删除图片并提交");
}
if(uploaderList != null){
for(Uploader uploader:uploaderList){
if(uploader!=null && bulletin.getContent()!=null && bulletin.getContent().contains("<img")){
File file = new File();
//查询数据库判断是否已经存在此记录
String location = uploader.getFileName();
file.setLocation(location);
Map<String, Object> condition = new HashMap<>();
condition.put("model", file);
//获取总记录数
int totalCount = fileService.getWithConditionCount(condition);
if(totalCount>0){
continue;
}
User loginUser = getLoginUser();
file.setCreateUser(loginUser!=null?loginUser.getId():null);
file.setCreateDate(new Date());
file.setLocation(uploader.getFileName());
//图片url
file.setUrl(uploader.getUrl());
//略缩图url
file.setThumburl(FileAction.domain+"/"+uploader.getFileName()+FileAction.thumbPicture);
file.setBucket(uploader.getBucket());
//源文件名
String fileOldName = uploader.getOriginalName();
int docIndex = fileOldName.lastIndexOf(".");
//文件名
String fileName = fileOldName.substring(0, docIndex);
//文件后缀
String subfix = fileOldName.substring(docIndex+1, fileOldName.length());
file.setFileName(fileName);
long size = 1;
file.setSize(size);
file.setSubfix(subfix);
fileService.add(file);
}
}
}
}
}