231 lines
6.2 KiB
Java
231 lines
6.2 KiB
Java
package nlib.bbs.service.impl;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import nlib.bbs.service.ArticleVO;
|
|
import nlib.bbs.service.AttachFileService;
|
|
import nlib.bbs.service.AttachFileVO;
|
|
import nlib.bbs.service.QnaService;
|
|
import nlib.cmm.service.NlibProperty;
|
|
import nlib.util.StringUtil;
|
|
import nlib.util.UUID;
|
|
|
|
@Service("qnaService")
|
|
public class QnaServiceImpl extends BoardServiceImpl implements QnaService
|
|
{
|
|
static Logger log = LoggerFactory.getLogger(QnaServiceImpl.class);
|
|
|
|
@Resource(name="qnaDAO")
|
|
QnaDAO qnaDAO;
|
|
|
|
@Resource(name="attachFileService")
|
|
AttachFileService attachFileService;
|
|
|
|
@Override
|
|
public BoardDAO getBoardDAO() {
|
|
return qnaDAO;
|
|
}
|
|
|
|
|
|
/**
|
|
* Q&A 게시글을 등록한다.
|
|
*
|
|
* @param reqVO
|
|
* @return
|
|
*/
|
|
public ArticleVO insertArticle(ArticleVO articleVO) throws Exception {
|
|
articleVO.setArticleId(UUID.getNlibCommonID("NQ", 35));
|
|
int ret = qnaDAO.insertArticle(articleVO);
|
|
|
|
if(ret < 1) {
|
|
throw new Exception("등록에 실패하였습니다.");
|
|
}
|
|
|
|
articleVO.setResultCode("S001");
|
|
|
|
if(articleVO.getAttachFileCnt() > 0) {
|
|
addAttachFiles(articleVO);
|
|
}
|
|
|
|
articleVO.setResultMessage("정상적으로 등록되었습니다." + (StringUtil.isNotEmpty(articleVO.getResultMessage())? "\\\\n " + articleVO.getResultMessage() : ""));
|
|
|
|
return articleVO;
|
|
}
|
|
|
|
/**
|
|
* 첨부파일을 추가한다.
|
|
*
|
|
* @param articleVO
|
|
* @throws Exception
|
|
*/
|
|
public void addAttachFiles(ArticleVO articleVO) throws Exception {
|
|
int savedCnt = 0;
|
|
List<AttachFileVO> attachFileList = articleVO.getAttachFiles();
|
|
|
|
// 최대 개수
|
|
int maxFileCount = NlibProperty.getInt("fileupload.max.files", 5);
|
|
|
|
// 첨부파일그룹 등록
|
|
int groupCnt = attachFileService.insertAttachFileGroup(attachFileList.get(0));
|
|
|
|
if(groupCnt > 0) {
|
|
|
|
int curFileCount = attachFileService.countAttachFiles(articleVO.getBdAttachFileId());
|
|
int addingCount = maxFileCount - curFileCount;
|
|
|
|
// 첨부파일 등록
|
|
if(addingCount > 0) {
|
|
for(int i=0; i<attachFileList.size(); i++) {
|
|
|
|
if(savedCnt >= addingCount) {
|
|
String message = "최대 허용 개수가 도달하여 등록을 중단합니다.";
|
|
log.error(message);
|
|
articleVO.setResultMessage(message);
|
|
break;
|
|
}
|
|
|
|
AttachFileVO attachFileVO = attachFileList.get(i);
|
|
int retAttFile = attachFileService.insertAttachFile(attachFileVO);
|
|
savedCnt += retAttFile;
|
|
if(retAttFile < 1) {
|
|
log.error("insertArticle > 첨부파일 등록에 실패하였습니다 : " + attachFileVO.getOrignlFileNm());
|
|
}
|
|
}
|
|
}
|
|
|
|
if(savedCnt != articleVO.getAttachFileCnt()) {
|
|
articleVO.setResultMessage("총 " + articleVO.getAttachFileCnt() + "개 파일 중 " + savedCnt + "개 파일만 정상등록되었습니다.");
|
|
}
|
|
|
|
} else {
|
|
articleVO.setResultMessage("게시글은 등록되었으나, 첨부파일그룹 등록에 실패하여 첨부파일 등록처리가 수행되지 않았습니다.");
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 첨부파일을 삭제한다.
|
|
*
|
|
* @param articleVO
|
|
* @throws Exception
|
|
*/
|
|
public void removeAttachFiles(ArticleVO articleVO) throws Exception {
|
|
int savedCnt = 0;
|
|
List<AttachFileVO> attachFileList = articleVO.getRemovedAttachFiles();
|
|
|
|
if(attachFileList == null || attachFileList.size() < 1) return;
|
|
|
|
String attachFileId = attachFileList.get(0).getAttachFileId();
|
|
|
|
if(StringUtil.isNotEmpty(attachFileId)) {
|
|
|
|
// 첨부파일 삭제
|
|
for(int i=0; i<attachFileList.size(); i++) {
|
|
AttachFileVO attachFileVO = attachFileList.get(i);
|
|
int retAttFile = attachFileService.deleteAttachFile(attachFileVO);
|
|
savedCnt += retAttFile;
|
|
if(retAttFile < 1) {
|
|
log.error("insertArticle > 첨부파일 삭제에 실패하였습니다 : " + attachFileVO.getOrignlFileNm());
|
|
}
|
|
}
|
|
|
|
if(savedCnt != attachFileList.size()) {
|
|
articleVO.setResultMessage(String.format("총 %d개 파일 중 %개 파일만 정상 삭제되었습니다.", attachFileList.size(), savedCnt));
|
|
}
|
|
|
|
} else {
|
|
articleVO.setResultMessage("첨부파일그룹ID 정보가 누락되어 첨부파일 삭제가 불가합니다.");
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Q&A 게시글을 수정한다.
|
|
*
|
|
* @param reqVO
|
|
* @return
|
|
*/
|
|
public int updateArticle(ArticleVO articleVO) throws Exception {
|
|
|
|
int updateRet = qnaDAO.updateArticle(articleVO);
|
|
|
|
// 첨부파일 삭제
|
|
List<AttachFileVO> removedAttachFiles = articleVO.getRemovedAttachFiles();
|
|
if(removedAttachFiles != null && removedAttachFiles.size() > 0) {
|
|
removeAttachFiles(articleVO);
|
|
}
|
|
|
|
// 첨부파일 추가
|
|
List<AttachFileVO> addedFiles = articleVO.getAttachFiles();
|
|
if(addedFiles != null && addedFiles.size() > 0) {
|
|
addAttachFiles(articleVO);
|
|
}
|
|
|
|
return updateRet;
|
|
}
|
|
|
|
/**
|
|
* Q&A 게시글을 삭제한다.
|
|
*
|
|
* @param reqVO
|
|
* @return
|
|
*/
|
|
public int deleteArticle(ArticleVO articleVO) throws Exception {
|
|
int ret = qnaDAO.deleteArticle(articleVO);
|
|
attachFileService.deleteAttachFileGroup(articleVO.getBdAttachFileId());
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* 해당 QNA 글을 변경할 수 있는지 여부를 확인한다.
|
|
*
|
|
* @param articleVO
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public String canModify(ArticleVO articleVO) throws Exception {
|
|
|
|
if(StringUtil.isEmpty(articleVO.getLoginedMbInfoId())) {
|
|
return "로그인 후, 이용하실 수 있습니다.";
|
|
}
|
|
|
|
ArticleVO savedArticleVO = selectArticle(articleVO);
|
|
|
|
if(!"Y".equals(savedArticleVO.getMyQnaYn())) {
|
|
return "본인의 글만 변경할 수 있습니다.";
|
|
}
|
|
|
|
if("Y".equals(savedArticleVO.getAnswerYn())) {
|
|
return "이미 답변완료된 글은 변경할 수 없습니다.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 해당 QNA 글을 읽을 수 있는지 여부를 확인한다.
|
|
*
|
|
* @param articleVO
|
|
* @return
|
|
* @throws Exception
|
|
*/
|
|
public String canRead(ArticleVO articleVO) throws Exception {
|
|
|
|
ArticleVO savedArticleVO = selectArticle(articleVO);
|
|
|
|
if("Y".equals(savedArticleVO.getMyQnaYn())) return null;
|
|
|
|
if("Y".equals(savedArticleVO.getSecretYn())) {
|
|
return "다른 사용자의 비밀글은 조회할 수 없습니다.";
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |