add bulk upload pengadaan
This commit is contained in:
@@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.sql.Timestamp;
|
||||
@@ -118,4 +119,9 @@ public class CommonService {
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
public boolean hasExcelFormat(MultipartFile file) {
|
||||
String type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||
return type.equals(file.getContentType());
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,350 @@
|
||||
package com.iconplus.smartproc.service.drppengadaan;
|
||||
|
||||
import com.iconplus.smartproc.exception.BusinessException;
|
||||
import com.iconplus.smartproc.helper.model.EmptyResponse;
|
||||
import com.iconplus.smartproc.helper.service.BaseService;
|
||||
import com.iconplus.smartproc.model.entity.*;
|
||||
import com.iconplus.smartproc.model.request.DrpPengadaanRequest;
|
||||
import com.iconplus.smartproc.repository.*;
|
||||
import com.iconplus.smartproc.service.CommonService;
|
||||
import com.iconplus.smartproc.util.Constants;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class UploadBulkUploadPengadaanService implements BaseService<DrpPengadaanRequest, EmptyResponse> {
|
||||
|
||||
private final DrpPengadaanRepository drpPengadaanRepository;
|
||||
private final CommonService commonService;
|
||||
private final LokasiRepository lokasiRepository;
|
||||
private final JenisPengadaanRepository jenisPengadaanRepository;
|
||||
private final UnitInisiatorRepository unitInisiatorRepository;
|
||||
private final SumberDanaRepository sumberDanaRepository;
|
||||
private final SupplyPositioningMatrixRepository supplyPositioningMatrixRepository;
|
||||
private final MetodePengadaanRepository metodePengadaanRepository;
|
||||
private final MetodePenyampaianRepository metodePenyampaianRepository;
|
||||
private final JenisKontrakRepository jenisKontrakRepository;
|
||||
private final StrategiPengadaanRepository strategiPengadaanRepository;
|
||||
private final DrpRepository drpRepository;
|
||||
|
||||
public UploadBulkUploadPengadaanService(DrpPengadaanRepository drpPengadaanRepository,
|
||||
CommonService commonService,
|
||||
LokasiRepository lokasiRepository,
|
||||
JenisPengadaanRepository jenisPengadaanRepository,
|
||||
UnitInisiatorRepository unitInisiatorRepository,
|
||||
SumberDanaRepository sumberDanaRepository,
|
||||
SupplyPositioningMatrixRepository supplyPositioningMatrixRepository,
|
||||
MetodePengadaanRepository metodePengadaanRepository,
|
||||
MetodePenyampaianRepository metodePenyampaianRepository,
|
||||
JenisKontrakRepository jenisKontrakRepository,
|
||||
StrategiPengadaanRepository strategiPengadaanRepository,
|
||||
DrpRepository drpRepository) {
|
||||
this.drpPengadaanRepository = drpPengadaanRepository;
|
||||
this.commonService = commonService;
|
||||
this.lokasiRepository = lokasiRepository;
|
||||
this.jenisPengadaanRepository = jenisPengadaanRepository;
|
||||
this.unitInisiatorRepository = unitInisiatorRepository;
|
||||
this.sumberDanaRepository = sumberDanaRepository;
|
||||
this.supplyPositioningMatrixRepository = supplyPositioningMatrixRepository;
|
||||
this.metodePengadaanRepository = metodePengadaanRepository;
|
||||
this.metodePenyampaianRepository = metodePenyampaianRepository;
|
||||
this.jenisKontrakRepository = jenisKontrakRepository;
|
||||
this.strategiPengadaanRepository = strategiPengadaanRepository;
|
||||
this.drpRepository = drpRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmptyResponse execute(DrpPengadaanRequest input) throws IOException {
|
||||
|
||||
var isExcelFormat = commonService.hasExcelFormat(input.getFile());
|
||||
if (!isExcelFormat) {
|
||||
throw new BusinessException(Constants.ERR_CODE_10014,
|
||||
Constants.ERR_TTL_10014,
|
||||
Constants.ERR_MSG_10014);
|
||||
}
|
||||
|
||||
var listDataExcel = transformFile(input);
|
||||
validateDrpRequest(listDataExcel);
|
||||
|
||||
List<DrpPengadaan> drpPengadaanList = new ArrayList<>();
|
||||
|
||||
List<Lokasi> lokasiList = getListLokasi(listDataExcel);
|
||||
List<JenisPengadaan> jenisPengadaanList = getJenisPengadaans(listDataExcel);
|
||||
List<UnitInisiator> unitInisiatorList = getUnitInisiators(listDataExcel);
|
||||
List<SumberDana> sumberDanaList = getSumberDanas(listDataExcel);
|
||||
List<SupplyPositioningMatrix> supplyPositioningMatrixList = getSupplyPositioningMatrices(listDataExcel);
|
||||
List<MetodePengadaan> metodePengadaanList = getMetodePengadaans(listDataExcel);
|
||||
List<MetodePenyampaian> metodePenyampaianList = getMetodePenyampaians(listDataExcel);
|
||||
List<JenisKontrak> jenisKontrakList = getJenisKontraks(listDataExcel);
|
||||
List<StrategiPengadaan> strategiPengadaanList = getStrategiPengadaans(listDataExcel);
|
||||
List<Integer> listDrp = listDataExcel.stream().map(DrpPengadaanRequest::getTahun).collect(Collectors.toList());
|
||||
List<Drp> drpList = drpRepository.getListDrpByList(listDrp);
|
||||
|
||||
|
||||
for (DrpPengadaanRequest drpPengadaanRequest : listDataExcel) {
|
||||
|
||||
DrpPengadaan drpPengadaanEntity = DrpPengadaan.builder()
|
||||
.namaPengadaan(drpPengadaanRequest.getNamaPengadaan())
|
||||
.nomor(drpPengadaanRequest.getNomor())
|
||||
.pagu(drpPengadaanRequest.getPagu())
|
||||
.rencanaTanggal(drpPengadaanRequest.getRencanaTanggal())
|
||||
.targetTanggal(drpPengadaanRequest.getTargetTanggal())
|
||||
.hpe(drpPengadaanRequest.getHpe())
|
||||
.isDelete(false)
|
||||
.build();
|
||||
|
||||
lokasiList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getLokasi(), drpPengadaanRequest.getLokasi())) {
|
||||
drpPengadaanEntity.setLokasiId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
jenisPengadaanList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getJenisPengadaan(), drpPengadaanRequest.getJenisPengadaan())) {
|
||||
drpPengadaanEntity.setJenisPengadaanId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
unitInisiatorList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getUnitInisiator(), drpPengadaanRequest.getUnitInisiator())) {
|
||||
drpPengadaanEntity.setUnitInisiatorId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
sumberDanaList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getSumberDana(), drpPengadaanRequest.getSumberDana())) {
|
||||
drpPengadaanEntity.setSumberDanaId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
supplyPositioningMatrixList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getSupplyPositioningMatrix(), drpPengadaanRequest.getSupplyPositioningMatrix())) {
|
||||
drpPengadaanEntity.setSupplyPositioningMatrixId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
metodePengadaanList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getMetodePengadaan(), drpPengadaanRequest.getMetodePengadaan())) {
|
||||
drpPengadaanEntity.setMetodePengadaanId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
metodePenyampaianList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getMetodePenyampaian(), drpPengadaanRequest.getMetodePenyampaian())) {
|
||||
drpPengadaanEntity.setMetodePenyampaianId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
jenisKontrakList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getJenisKontrak(), drpPengadaanRequest.getJenisKontrak())) {
|
||||
drpPengadaanEntity.setJenisKontrakId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
strategiPengadaanList.forEach(c-> {
|
||||
if (StringUtils.equalsIgnoreCase(c.getStrategiPengadaan(), drpPengadaanRequest.getStrategiPengadaan())) {
|
||||
drpPengadaanEntity.setStrategiPengadaanId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
drpList.forEach(c-> {
|
||||
if (c.getTahun().equals(drpPengadaanRequest.getTahun())) {
|
||||
drpPengadaanEntity.setDrpId(c.getId());
|
||||
}
|
||||
});
|
||||
|
||||
drpPengadaanList.add(drpPengadaanEntity);
|
||||
}
|
||||
|
||||
drpPengadaanRepository.saveAll(drpPengadaanList);
|
||||
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
private void validateDrpRequest(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var drpPengadaan = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getNamaPengadaan)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<DrpPengadaan> drpPengadaans = drpPengadaanRepository.getDrpPengadaanByListNamaPengadaan(drpPengadaan);
|
||||
if (ObjectUtils.isNotEmpty(drpPengadaans)) {
|
||||
List<String> existDrpPengadaan = drpPengadaans.stream().map(DrpPengadaan::getNamaPengadaan).collect(Collectors.toList());
|
||||
throw new BusinessException(Constants.ERR_CODE_10015,
|
||||
Constants.ERR_TTL_10015,
|
||||
String.format(Constants.ERR_MSG_10015, existDrpPengadaan));
|
||||
}
|
||||
}
|
||||
|
||||
private List<StrategiPengadaan> getStrategiPengadaans(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listStrategiPengadaan = listDataExcel.stream().map(DrpPengadaanRequest::getStrategiPengadaan).collect(Collectors.toList());
|
||||
return strategiPengadaanRepository.getListStrategiPengadaanByList(listStrategiPengadaan);
|
||||
}
|
||||
|
||||
private List<JenisKontrak> getJenisKontraks(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var jenisKontraks = listDataExcel.stream().map(DrpPengadaanRequest::getJenisKontrak).collect(Collectors.toList());
|
||||
return jenisKontrakRepository.getListJenisKontrakByList(jenisKontraks);
|
||||
}
|
||||
|
||||
private List<MetodePenyampaian> getMetodePenyampaians(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listMetodePenyampaian = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getMetodePenyampaian)
|
||||
.collect(Collectors.toList());
|
||||
return metodePenyampaianRepository.getListMetodePenyampaianByList(listMetodePenyampaian);
|
||||
}
|
||||
|
||||
private List<MetodePengadaan> getMetodePengadaans(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listMetodePengadaan = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getMetodePengadaan)
|
||||
.collect(Collectors.toList());
|
||||
return metodePengadaanRepository.getListMetodePengadaanByList(listMetodePengadaan);
|
||||
}
|
||||
|
||||
private List<SupplyPositioningMatrix> getSupplyPositioningMatrices(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listSupplyPositioningMatrix = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getSupplyPositioningMatrix)
|
||||
.collect(Collectors.toList());
|
||||
return supplyPositioningMatrixRepository.getListSupplyPositioningMatrix(listSupplyPositioningMatrix);
|
||||
}
|
||||
|
||||
private List<SumberDana> getSumberDanas(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listSumberDana = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getSumberDana)
|
||||
.collect(Collectors.toList());
|
||||
return sumberDanaRepository.getListSumberDanaByList(listSumberDana);
|
||||
}
|
||||
|
||||
private List<UnitInisiator> getUnitInisiators(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listUnitInisiator = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getUnitInisiator)
|
||||
.collect(Collectors.toList());
|
||||
return unitInisiatorRepository.getListUnitInisiatorByList(listUnitInisiator);
|
||||
}
|
||||
|
||||
private List<JenisPengadaan> getJenisPengadaans(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listJenisPengadaan = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getJenisPengadaan)
|
||||
.collect(Collectors.toList());
|
||||
return jenisPengadaanRepository.getListPengadaan(listJenisPengadaan);
|
||||
}
|
||||
|
||||
private List<Lokasi> getListLokasi(List<DrpPengadaanRequest> listDataExcel) {
|
||||
var listLokasi = listDataExcel.stream()
|
||||
.map(DrpPengadaanRequest::getLokasi)
|
||||
.collect(Collectors.toList());
|
||||
return lokasiRepository.getListLokasi(listLokasi);
|
||||
}
|
||||
|
||||
private List<DrpPengadaanRequest> transformFile(DrpPengadaanRequest input) throws IOException {
|
||||
try {
|
||||
Workbook workbook = new XSSFWorkbook(input.getFile().getInputStream());
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
|
||||
List<DrpPengadaanRequest> drpPengadaanRequestList = new ArrayList<>();
|
||||
Iterator<Row> rows = sheet.iterator();
|
||||
|
||||
int rowNumber = 0;
|
||||
while (rows.hasNext()) {
|
||||
Row currentRow = rows.next();
|
||||
|
||||
// skip header
|
||||
if (rowNumber <= 7) {
|
||||
rowNumber++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Iterator<Cell> cellsInRow = currentRow.iterator();
|
||||
DrpPengadaanRequest drpPengadaanRequest = DrpPengadaanRequest.builder().build();
|
||||
|
||||
int cellIdx = 0;
|
||||
while (cellsInRow.hasNext()) {
|
||||
Cell currentCell = cellsInRow.next();
|
||||
|
||||
switch (cellIdx) {
|
||||
case 0:
|
||||
drpPengadaanRequest.setId((long) currentCell.getNumericCellValue());
|
||||
break;
|
||||
case 1:
|
||||
drpPengadaanRequest.setNamaPengadaan(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 2:
|
||||
drpPengadaanRequest.setLokasi(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 3:
|
||||
drpPengadaanRequest.setJenisPengadaan(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 4:
|
||||
drpPengadaanRequest.setUnitInisiator(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 5:
|
||||
drpPengadaanRequest.setNomor(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 6:
|
||||
drpPengadaanRequest.setPagu(BigDecimal.valueOf(currentCell.getNumericCellValue()));
|
||||
break;
|
||||
case 7:
|
||||
drpPengadaanRequest.setSumberDana(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 8:
|
||||
drpPengadaanRequest.setSupplyPositioningMatrix(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 9:
|
||||
drpPengadaanRequest.setMetodePengadaan(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 10:
|
||||
drpPengadaanRequest.setMetodePenyampaian(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 11:
|
||||
drpPengadaanRequest.setJenisKontrak(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 12:
|
||||
drpPengadaanRequest.setStrategiPengadaan(currentCell.getStringCellValue());
|
||||
break;
|
||||
case 13:
|
||||
drpPengadaanRequest.setRencanaTanggal(new Date(currentCell.getDateCellValue().getTime()));
|
||||
break;
|
||||
case 14:
|
||||
drpPengadaanRequest.setTargetTanggal(new Date(currentCell.getDateCellValue().getTime()));
|
||||
break;
|
||||
case 15:
|
||||
drpPengadaanRequest.setHpe(BigDecimal.valueOf(currentCell.getNumericCellValue()));
|
||||
break;
|
||||
case 16:
|
||||
drpPengadaanRequest.setTahun((int) currentCell.getNumericCellValue());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
cellIdx++;
|
||||
}
|
||||
|
||||
if (drpPengadaanRequest.getId() == null || drpPengadaanRequest.getId() != 0) {
|
||||
drpPengadaanRequestList.add(drpPengadaanRequest);
|
||||
}
|
||||
|
||||
}
|
||||
workbook.close();
|
||||
return drpPengadaanRequestList;
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(Constants.ERR_CODE_10016,
|
||||
Constants.ERR_TTL_10016,
|
||||
String.format(Constants.ERR_MSG_10016, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user