package kr.wisestone.owl.service.impl; import com.google.common.collect.Lists; import kr.wisestone.owl.common.ExcelConditionCheck; import kr.wisestone.owl.constant.Constants; import kr.wisestone.owl.constant.MsgConstants; import kr.wisestone.owl.domain.CompanyField; import kr.wisestone.owl.domain.HostingField; import kr.wisestone.owl.exception.OwlRuntimeException; import kr.wisestone.owl.mapper.HostingFieldMapper; import kr.wisestone.owl.repository.HostingFieldRepository; import kr.wisestone.owl.service.HostingFieldService; import kr.wisestone.owl.service.WorkspaceService; import kr.wisestone.owl.util.ConvertUtil; import kr.wisestone.owl.vo.HostingFieldVo; import kr.wisestone.owl.vo.ExportExcelAttrVo; import kr.wisestone.owl.vo.ExportExcelVo; import kr.wisestone.owl.vo.ResPage; import kr.wisestone.owl.web.condition.HostingFieldCondition; import kr.wisestone.owl.web.form.HostingFieldForm; import kr.wisestone.owl.web.view.ExcelView; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class HostingFieldServiceImpl extends AbstractServiceImpl> implements HostingFieldService { @Autowired private HostingFieldRepository hostingFieldRepository; @Autowired private HostingFieldMapper hostingFieldMapper; @Autowired private WorkspaceService workspaceService; @Autowired private ExcelView excelView; @Autowired private ExcelConditionCheck excelConditionCheck; @Override protected JpaRepository getRepository() { return this.hostingFieldRepository; } // Hosting 추가 @Override public HostingField add(HostingFieldForm HostingFieldForm) { if (HostingFieldForm.getTelList() != null && HostingFieldForm.getTelList().size() > 0) { String[] tels = ConvertUtil.ToArray(HostingFieldForm.getTelList()); HostingFieldForm.setTel(Arrays.toString(tels)); } if (HostingFieldForm.getEmailList() != null && HostingFieldForm.getEmailList().size() > 0) { String[] emails = ConvertUtil.ToArray(HostingFieldForm.getEmailList()); HostingFieldForm.setEmail(Arrays.toString(emails)); } HostingField hostingField = ConvertUtil.copyProperties(HostingFieldForm, HostingField.class); if (hostingField.getCode() != null && !hostingField.getCode().equals("")) { hostingFieldRepository.saveAndFlush(hostingField); } else { throw new OwlRuntimeException(this.messageAccessor.getMessage(MsgConstants.HOSTING_CODE_NOT_ENTER)); } return hostingField; } // Hosting 목록을 가져온다. @Override public List find(Map resJsonData, HostingFieldCondition condition, Pageable pageable) { condition.setPage(pageable.getPageNumber() * pageable.getPageSize()); condition.setPageSize(pageable.getPageSize()); List> results = this.hostingFieldMapper.find(condition); Long totalHostingCount = this.hostingFieldMapper.count(condition); return this.convertHostingVoToMap(results, totalHostingCount, pageable, resJsonData); } public Map find(Long id) { return this.hostingFieldMapper.findById(id); } // Hosting 상세 조회한다. @Override public void detail(Map resJsonData, HostingFieldCondition hostingFieldCondition) { HostingFieldVo HostingFieldVo = new HostingFieldVo(); Long hostingId = hostingFieldCondition.getId(); if (hostingId != null) { HostingField HostingField = this.getHosting(hostingId); HostingFieldVo = ConvertUtil.copyProperties(HostingField, HostingFieldVo.class); } resJsonData.put(Constants.REQ_KEY_CONTENT, HostingFieldVo); } // Hosting 정로를 수정한다. @Override public void modify(HostingFieldForm HostingFieldForm) { if (HostingFieldForm.getTelList() != null && HostingFieldForm.getTelList().size() > 0) { String[] tels = ConvertUtil.ToArray(HostingFieldForm.getTelList()); HostingFieldForm.setTel(Arrays.toString(tels)); } if (HostingFieldForm.getEmailList() != null && HostingFieldForm.getEmailList().size() > 0) { String[] emails = ConvertUtil.ToArray(HostingFieldForm.getEmailList()); HostingFieldForm.setEmail(Arrays.toString(emails)); } HostingField HostingField = ConvertUtil.copyProperties(HostingFieldForm, HostingField.class); hostingFieldRepository.saveAndFlush(HostingField); } // Hosting를 삭제한다. @Override public void remove(HostingFieldForm HostingFieldForm) { if (HostingFieldForm.getRemoveIds().size() < 1) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.COMPANY_REMOVE_NOT_SELECT)); } for (Long id : HostingFieldForm.getRemoveIds()) { this.hostingFieldRepository.deleteById(id); } this.hostingFieldRepository.flush(); } // Hosting 목록을 엑셀로 다운로드 한다. @Override public ModelAndView downloadExcel(HttpServletRequest request, Model model) { ModelAndView modelAndView = this.workspaceService.checkUseExcelDownload(model); if (modelAndView != null) { return modelAndView; } Map conditions = new HashMap<>(); // 엑셀 다운로드에 필요한 검색 조건 정보를 추출하고 검색 조건 추출에 오류가 발생하면 경고를 표시해준다. modelAndView = this.excelConditionCheck.checkCondition(conditions, request, model); if (modelAndView != null) { return modelAndView; } HostingFieldCondition hostingFieldCondition = HostingFieldCondition.make(conditions); List> results = this.hostingFieldMapper.find(hostingFieldCondition); List hostingFieldVos = ConvertUtil.convertListToListClass(results, HostingFieldVo.class); // code_ko_KR 에 code명 설정 ExportExcelVo excelInfo = new ExportExcelVo(); excelInfo.setFileName(this.messageAccessor.message("Hosting 목록")); excelInfo.addAttrInfos(new ExportExcelAttrVo("name", this.messageAccessor.message("Hosting.HostingName"), 6, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("code", this.messageAccessor.message("Hosting.HostingCode"), 6, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("manager", this.messageAccessor.message("Hosting.HostingManager"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("tel", this.messageAccessor.message("Hosting.HostingTel"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("email", this.messageAccessor.message("Hosting.HostingEmail"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("url", this.messageAccessor.message("Hosting.HostingUrl"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.addAttrInfos(new ExportExcelAttrVo("memo", this.messageAccessor.message("Hosting.HostingMemo"), 10, ExportExcelAttrVo.ALIGN_CENTER)); excelInfo.setDatas(hostingFieldVos); model.addAttribute(Constants.EXCEL, excelInfo); return new ModelAndView(this.excelView); } // 검색 결과를 HostingVo 로 변환한다. private List convertHostingVoToMap(List> results, Long totalHostingCount, Pageable pageable, Map resJsonData) { List hostingFieldVos = Lists.newArrayList(); for (Map result : results) { HostingFieldVo HostingFieldVo = ConvertUtil.convertMapToClass(result, HostingFieldVo.class); hostingFieldVos.add(HostingFieldVo); } int totalPage = (int) Math.ceil((totalHostingCount - 1) / pageable.getPageSize()) + 1; resJsonData.put(Constants.RES_KEY_CONTENTS, hostingFieldVos); resJsonData.put(Constants.REQ_KEY_PAGE_VO, new ResPage(pageable.getPageNumber(), pageable.getPageSize(), totalPage, totalHostingCount)); return hostingFieldVos; } // Hosting ID 로 조회한다 @Override public HostingField getHosting(Long id) { if (id == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.HOSTING_NOT_EXIST)); } HostingField HostingField = this.findOne(id); if (HostingField == null) { throw new OwlRuntimeException( this.messageAccessor.getMessage(MsgConstants.HOSTING_NOT_EXIST)); } return HostingField; } }