Commit 2e0e6f96 authored by Nguyen Ha's avatar Nguyen Ha

fix sonar

parent d712ba6d
...@@ -46,6 +46,7 @@ import javax.persistence.criteria.CriteriaQuery; ...@@ -46,6 +46,7 @@ import javax.persistence.criteria.CriteriaQuery;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -103,8 +104,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -103,8 +104,7 @@ public class CustomerServiceImpl implements CustomerService {
ResultDTO resultDTO = new ResultDTO(); ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); Session session = null;
session.beginTransaction();
if (DataUtil.isNullOrZero(companySiteId)) { if (DataUtil.isNullOrZero(companySiteId)) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
...@@ -113,6 +113,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -113,6 +113,8 @@ public class CustomerServiceImpl implements CustomerService {
} }
try { try {
session = sessionFactory.openSession();
session.beginTransaction();
// StringBuilder sqlStrBuilder = new StringBuilder(); // StringBuilder sqlStrBuilder = new StringBuilder();
// sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "campaign-customer-detail-by-params")); // sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "campaign-customer-detail-by-params"));
...@@ -179,7 +181,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -179,7 +181,7 @@ public class CustomerServiceImpl implements CustomerService {
} }
Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Order.desc(sort))); Pageable pageable = PageRequest.of(page, pageSize, Sort.by(Sort.Order.desc(sort)));
if (pageable != null) { if (pageable != null) {
query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize()); query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
query.setMaxResults(pageable.getPageSize()); query.setMaxResults(pageable.getPageSize());
} }
...@@ -193,7 +195,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -193,7 +195,7 @@ public class CustomerServiceImpl implements CustomerService {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR); resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally { } finally {
session.close(); if(session != null) session.close();
} }
return resultDTO; return resultDTO;
...@@ -224,8 +226,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -224,8 +226,7 @@ public class CustomerServiceImpl implements CustomerService {
ResultDTO resultDTO = new ResultDTO(); ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); Session session = null;
session.beginTransaction();
if (DataUtil.isNullOrZero(companySiteId)) { if (DataUtil.isNullOrZero(companySiteId)) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
...@@ -234,6 +235,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -234,6 +235,8 @@ public class CustomerServiceImpl implements CustomerService {
} }
try { try {
session = sessionFactory.openSession();
session.beginTransaction();
// StringBuilder sqlStrBuilder = new StringBuilder(); // StringBuilder sqlStrBuilder = new StringBuilder();
// sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "campaign-customer-detail-by-params")); // sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "campaign-customer-detail-by-params"));
...@@ -339,7 +342,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -339,7 +342,7 @@ public class CustomerServiceImpl implements CustomerService {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR); resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally { } finally {
session.close(); if(session != null) session.close();
} }
return resultDTO; return resultDTO;
...@@ -429,8 +432,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -429,8 +432,7 @@ public class CustomerServiceImpl implements CustomerService {
ResultDTO resultDTO = new ResultDTO(); ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); Session session = null;
session.beginTransaction();
if (DataUtil.isNullOrZero(companySiteId)) { if (DataUtil.isNullOrZero(companySiteId)) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
...@@ -439,6 +441,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -439,6 +441,8 @@ public class CustomerServiceImpl implements CustomerService {
} }
try { try {
session = sessionFactory.openSession();
session.beginTransaction();
// StringBuilder sqlStrBuilder = new StringBuilder(); // StringBuilder sqlStrBuilder = new StringBuilder();
// sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "get-customer-detail-by-id")); // sqlStrBuilder.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "get-customer-detail-by-id"));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -494,7 +498,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -494,7 +498,7 @@ public class CustomerServiceImpl implements CustomerService {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR); resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally { } finally {
session.close(); if(session != null) session.close();
} }
return resultDTO; return resultDTO;
...@@ -522,8 +526,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -522,8 +526,7 @@ public class CustomerServiceImpl implements CustomerService {
ResultDTO resultDTO = new ResultDTO(); ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); Session session = null;
session.beginTransaction();
if (DataUtil.isNullOrZero(companySiteId)) { if (DataUtil.isNullOrZero(companySiteId)) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
...@@ -532,6 +535,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -532,6 +535,8 @@ public class CustomerServiceImpl implements CustomerService {
} }
try { try {
session = sessionFactory.openSession();
session.beginTransaction();
// StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
// sb.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "search-campaign-customer-by-params")); // sb.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "search-campaign-customer-by-params"));
...@@ -594,7 +599,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -594,7 +599,7 @@ public class CustomerServiceImpl implements CustomerService {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR); resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally { } finally {
session.close(); if(session != null) session.close();
} }
return resultDTO; return resultDTO;
...@@ -759,8 +764,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -759,8 +764,7 @@ public class CustomerServiceImpl implements CustomerService {
ResultDTO resultDTO = new ResultDTO(); ResultDTO resultDTO = new ResultDTO();
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); Session session = null;
session.beginTransaction();
if (DataUtil.isNullOrEmpty(searchCustomerRequestDTO.getCompanySiteId())) { if (DataUtil.isNullOrEmpty(searchCustomerRequestDTO.getCompanySiteId())) {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
...@@ -769,6 +773,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -769,6 +773,8 @@ public class CustomerServiceImpl implements CustomerService {
} }
try { try {
session = sessionFactory.openSession();
session.beginTransaction();
// StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
// sb.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "search-campaign-customer-by-params")); // sb.append(SQLBuilder.getSqlQueryById(SQLBuilder.SQL_MODULE_CAMPAIGN_MNG, "search-campaign-customer-by-params"));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
...@@ -865,7 +871,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -865,7 +871,7 @@ public class CustomerServiceImpl implements CustomerService {
resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR); resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
resultDTO.setDescription(Constants.ApiErrorDesc.ERROR); resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
} finally { } finally {
session.close(); if(session != null) session.close();
} }
return resultDTO; return resultDTO;
...@@ -885,16 +891,11 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -885,16 +891,11 @@ public class CustomerServiceImpl implements CustomerService {
try { try {
List<CustomerContact> data = customerContactRepository.findByCustomerIdAndAndContactTypeAndContact(customer.getCustomerId(), customer.getContactType(), customer.getContact(), SQLBuilder.buildPageable(customer)); List<CustomerContact> data = customerContactRepository.findByCustomerIdAndAndContactTypeAndContact(customer.getCustomerId(), customer.getContactType(), customer.getContact(), SQLBuilder.buildPageable(customer));
if (customer != null) { result.setErrorCode(Constants.ApiErrorCode.SUCCESS);
result.setErrorCode(Constants.ApiErrorCode.SUCCESS); result.setDescription("customer contact data");
result.setDescription("customer contact data"); result.setListData(data);
result.setListData(data); result.setTotalRow(customerContactRepository.findByCustomerIdAndAndContactTypeAndContact(customer.getCustomerId(), customer.getContactType(), customer.getContact(), null).size());
result.setTotalRow(customerContactRepository.findByCustomerIdAndAndContactTypeAndContact(customer.getCustomerId(), customer.getContactType(), customer.getContact(), null).size());
} else {
result.setErrorCode(Constants.ApiErrorCode.ERROR);
result.setDescription("customer contact data null");
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -940,7 +941,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -940,7 +941,7 @@ public class CustomerServiceImpl implements CustomerService {
@Override @Override
@Transactional(DataSourceQualify.CCMS_FULL) @Transactional(DataSourceQualify.CCMS_FULL)
public Map<String, Object> readAndValidateCustomer(String path, List<CustomizeFields> dynamicHeader, UserSession userSession, Long customerListId) { public Map<String, Object> readAndValidateCustomer(String path, List<CustomizeFields> dynamicHeader, UserSession userSession, Long customerListId) throws IOException{
LOGGER.info("------------READ AND VALIDATE--------------"); LOGGER.info("------------READ AND VALIDATE--------------");
Locale locale = new Locale("vi", "VN"); Locale locale = new Locale("vi", "VN");
DataFormatter dataFormat = new DataFormatter(); DataFormatter dataFormat = new DataFormatter();
...@@ -948,6 +949,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -948,6 +949,7 @@ public class CustomerServiceImpl implements CustomerService {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int failedCount = 0; int failedCount = 0;
XSSFWorkbook workbook = null;
try { try {
//<editor-fold desc="Khởi tạo mảng header tĩnh" defaultstate="collapsed"> //<editor-fold desc="Khởi tạo mảng header tĩnh" defaultstate="collapsed">
...@@ -971,7 +973,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -971,7 +973,7 @@ public class CustomerServiceImpl implements CustomerService {
File file = new File(path); File file = new File(path);
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(fis); workbook = new XSSFWorkbook(fis);
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(3); Row row = sheet.getRow(3);
...@@ -1056,11 +1058,11 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1056,11 +1058,11 @@ public class CustomerServiceImpl implements CustomerService {
&& rawDataList.get(i)[4] != null && rawDataList.get(i)[4] != null
&& !rawDataList.get(i)[4].toString().trim().equals("")) { && !rawDataList.get(i)[4].toString().trim().equals("")) {
String str = validatePhone(rawDataList.get(i)[2].toString().trim(), locale); String str = validatePhone(rawDataList.get(i)[2].toString().trim(), locale);
str.concat(validateLength(BundleUtils.getLangString("customer.secondPhone", locale).split("#")[0], rawDataList.get(i)[3].toString(), 50, locale)); str += (validateLength(BundleUtils.getLangString("customer.secondPhone", locale).split("#")[0], rawDataList.get(i)[3].toString(), 50, locale));
if (!validateEmail(rawDataList.get(i)[4].toString().trim())) { if (!validateEmail(rawDataList.get(i)[4].toString().trim())) {
str.concat(BundleUtils.getLangString("customer.emailInvalid", locale)); str += (BundleUtils.getLangString("customer.emailInvalid", locale));
} else { } else {
str.concat(validateDuplicateEmail(rawDataList.get(i)[4].toString().trim(), locale)); str += (validateDuplicateEmail(rawDataList.get(i)[4].toString().trim(), locale));
} }
sb.append(validateLength(BundleUtils.getLangString("customer.email", locale).split("#")[0], rawDataList.get(i)[4].toString(), 50, locale)); sb.append(validateLength(BundleUtils.getLangString("customer.email", locale).split("#")[0], rawDataList.get(i)[4].toString(), 50, locale));
sb.append(str); sb.append(str);
...@@ -1265,6 +1267,8 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1265,6 +1267,8 @@ public class CustomerServiceImpl implements CustomerService {
LOGGER.info(e.getMessage()); LOGGER.info(e.getMessage());
result.put("message", "validate-error"); result.put("message", "validate-error");
return result; return result;
}finally {
if (workbook != null) workbook.close();
} }
} }
...@@ -1335,11 +1339,12 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1335,11 +1339,12 @@ public class CustomerServiceImpl implements CustomerService {
@Override @Override
@Transactional(DataSourceQualify.CCMS_FULL) @Transactional(DataSourceQualify.CCMS_FULL)
public byte[] buildTemplate(Long companySiteId) { public byte[] buildTemplate(Long companySiteId) throws IOException {
LOGGER.info("-----------BUILD TEMPLATE-----------"); LOGGER.info("-----------BUILD TEMPLATE-----------");
Locale locale = new Locale("vi", "VN"); Locale locale = new Locale("vi", "VN");
XSSFWorkbook workbook = null;
try { try {
XSSFWorkbook workbook = new XSSFWorkbook(); workbook = new XSSFWorkbook();
CreationHelper creationHelper = workbook.getCreationHelper(); CreationHelper creationHelper = workbook.getCreationHelper();
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
Sheet sheet = workbook.createSheet("IMPORT"); Sheet sheet = workbook.createSheet("IMPORT");
...@@ -1478,11 +1483,12 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -1478,11 +1483,12 @@ public class CustomerServiceImpl implements CustomerService {
workbook.write(os); workbook.write(os);
os.flush(); os.flush();
os.close(); os.close();
workbook.close();
return os.toByteArray(); return os.toByteArray();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
return null; return null;
}finally {
if (workbook != null) workbook.close();
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment