BoardService.java
8.96 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.daeucna.board.service;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.daeucna.board.common.PagingUtil;
import com.daeucna.board.common.ResultUtil;
import com.daeucna.board.dao.BoardDao;
import com.daeucna.board.domain.BoardDto;
import com.daeucna.board.domain.BoardFileForm;
import com.daeucna.board.domain.BoardForm;
import com.daeucna.board.domain.CommonDto;
import com.daeucna.board.domain.CommonForm;
@Transactional(readOnly = true)
@Service
public class BoardService {
protected final Logger logger = LoggerFactory.getLogger(BoardService.class);
@Autowired
private BoardDao boardDao;
/** 게시판 - 목록 조회 */
public ResultUtil getBoardList(BoardForm boardForm) throws Exception {
ResultUtil resultUtil = new ResultUtil();
CommonDto commonDto = new CommonDto();
int totalCount = boardDao.getBoardCnt(boardForm);
if (totalCount != 0) {
CommonForm commonForm = new CommonForm();
commonForm.setFunction_name(boardForm.getFunction_name());
commonForm.setCurrent_page_no(boardForm.getCurrent_page_no());
commonForm.setCount_per_page(10);
commonForm.setCount_per_list(12);
commonForm.setTatal_list_count(totalCount);
commonDto = PagingUtil.setPageUtil(commonForm);
}
boardForm.setLimit(commonDto.getLimit());
boardForm.setOffset(commonDto.getOffset());
List<BoardDto> list = boardDao.getBoardList(boardForm);
HashMap<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("list", list);
resultMap.put("totalCount", totalCount);
resultMap.put("pagination", commonDto.getPagination());
resultUtil.setData(resultMap);
resultUtil.setState("SUCCESS");
return resultUtil;
}
/** 게시판 - 목록 조회 */
/*
* public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {
*
* return boardDao.getBoardList(boardForm); }
*/
/** 게시판 - 상세 조회 */
public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {
logger.debug("==================== getBoardDetail START ====================");
BoardDto boardDto = new BoardDto();
String searchType = boardForm.getSearch_type();
if ("S".equals(searchType)) {
int updateCnt = boardDao.updateBoardHits(boardForm);
if (updateCnt > 0) {
boardDto = boardDao.getBoardDetail(boardForm);
}
} else {
boardDto = boardDao.getBoardDetail(boardForm);
}
BoardFileForm boardFileForm = new BoardFileForm();
boardFileForm.setBoard_seq(boardForm.getBoard_seq());
boardDto.setFiles(boardDao.getBoardFileList(boardFileForm));
logger.debug("==================== getBoardDetail END ====================");
return boardDto;
}
/** 게시판 - 등록 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public BoardDto insertBoard(BoardForm boardForm) throws Exception {
BoardDto boardDto = new BoardDto();
int insertCnt = 0;
int boardReRef = boardDao.getBoardReRef(boardForm);
boardForm.setBoard_re_ref(boardReRef);
insertCnt = boardDao.insertBoard(boardForm);
//insertCnt = boardDao.insertBoardFail(boardForm);
List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);
for (BoardFileForm boardFileForm : boardFileList) {
boardDao.insertBoardFile(boardFileForm);
}
if (insertCnt > 0) {
boardDto.setResult("SUCCESS");
} else {
boardDto.setResult("FAIL");
}
return boardDto;
}
/** 게시판 - 첨부파일 정보 조회 */
public List<BoardFileForm> getBoardFileInfo(BoardForm boardForm) throws Exception {
List<MultipartFile> files = boardForm.getFiles();
List<BoardFileForm> boardFileList = new ArrayList<BoardFileForm>();
BoardFileForm boardFileForm = new BoardFileForm();
int boardSeq = boardForm.getBoard_seq();
String fileName = null;
String fileExt = null;
String fileNameKey = null;
String fileSize = null;
// 파일이 저장될 Path 설정
String filePath = "C:\\board\\file";
if (files != null && files.size() > 0) {
File file = new File(filePath);
// 디렉토리가 없으면 생성
if (file.exists() == false) {
file.mkdirs();
}
for (MultipartFile multipartFile : files) {
fileName = multipartFile.getOriginalFilename();
fileExt = fileName.substring(fileName.lastIndexOf("."));
// 파일명 변경(uuid로 암호화) + 확장자
fileNameKey = getRandomString() + fileExt;
fileSize = String.valueOf(multipartFile.getSize());
// 설정한 Path에 파일 저장
file = new File(filePath + "/" + fileNameKey);
multipartFile.transferTo(file);
boardFileForm = new BoardFileForm();
boardFileForm.setBoard_seq(boardSeq);
boardFileForm.setFile_name(fileName);
boardFileForm.setFile_name_key(fileNameKey);
boardFileForm.setFile_path(filePath);
boardFileForm.setFile_size(fileSize);
boardFileList.add(boardFileForm);
}
}
return boardFileList;
}
/** 게시판 - 삭제 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public BoardDto deleteBoard(BoardForm boardForm) throws Exception {
BoardDto boardDto = new BoardDto();
int deleteCnt = boardDao.deleteBoard(boardForm);
if (deleteCnt > 0) {
boardDto.setResult("SUCCESS");
} else {
boardDto.setResult("FAIL");
}
return boardDto;
}
/** 게시판 - 수정 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public BoardDto updateBoard(BoardForm boardForm) throws Exception {
BoardDto boardDto = new BoardDto();
int updateCnt = boardDao.updateBoard(boardForm);
String deleteFile = boardForm.getDelete_file();
if (!"".equals(deleteFile)) {
String[] deleteFileInfo = deleteFile.split("!");
int boardSeq = Integer.parseInt(deleteFileInfo[0]);
int fileNo = Integer.parseInt(deleteFileInfo[1]);
BoardFileForm deleteBoardFileForm = new BoardFileForm();
deleteBoardFileForm.setBoard_seq(boardSeq);
deleteBoardFileForm.setFile_no(fileNo);
boardDao.deleteBoardFile(deleteBoardFileForm);
}
List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);
for (BoardFileForm boardFileForm : boardFileList) {
boardDao.insertBoardFile(boardFileForm);
}
if (updateCnt > 0) {
boardDto.setResult("SUCCESS");
} else {
boardDto.setResult("FAIL");
}
return boardDto;
}
/** 게시판 - 답글 등록 */
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public BoardDto insertBoardReply(BoardForm boardForm) throws Exception {
BoardDto boardDto = new BoardDto();
BoardDto boardReplayInfo = boardDao.getBoardReplyInfo(boardForm);
boardForm.setBoard_seq(boardReplayInfo.getBoard_seq());
boardForm.setBoard_re_lev(boardReplayInfo.getBoard_re_lev());
boardForm.setBoard_re_ref(boardReplayInfo.getBoard_re_ref());
boardForm.setBoard_re_seq(boardReplayInfo.getBoard_re_seq());
int insertCnt = 0;
insertCnt += boardDao.updateBoardReSeq(boardForm);
insertCnt += boardDao.insertBoardReply(boardForm);
if (insertCnt > 0) {
boardDto.setResult("SUCCESS");
} else {
boardDto.setResult("FAIL");
}
return boardDto;
}
/** 32글자의 랜덤한 문자열(숫자포함) 생성 */
public static String getRandomString() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
}