package com.test.core.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import au.com.bytecode.opencsv.CSVReader;
import com.test.core.conf.SystemConf;
import com.test.core.expcetion.ServiceException;
import com.test.orgucpassport.web.model.OrgUcPassportModel;
public final class CSVUtils {
private static Logger LOG = Logger.getLogger(CSVUtils.class);
public static final Integer BINDS_COLUMNS = 3;
public static final Integer UNBINDS_COLUMNS = 1;
public static final String FILL_ERROR = "对不起,读取文件失败或者内容为空,请检查后上传!";
public static final String HEADER_ERROR = "对不起,文件表头没有严格符合模板,请检查后重新上传!";
public static final String BODY_ERROR = "对不起,文件数据内容没有严格符合模板,请检查后重新上传!";
private CSVUtils() {
}
public static List<OrgUcPassportModel> readCSVWhenBinds(File CSVFileBinds)
throws ServiceException, IOException {
List<String[]> csvData = getCSVData(CSVFileBinds);
// 除去尾部空行
excludingTail(csvData);
checkCSVFormatWhenBinds(csvData);
return readCSVBodyWhenBinds(csvData);
}
public static List<OrgUcPassportModel> readCSVWhenUnbinds(
File CSVFileUnbinds) throws ServiceException {
List<String[]> csvData = getCSVData(CSVFileUnbinds);
// 除去尾部空行
excludingTail(csvData);
checkCSVFormatWhenUnbinds(csvData);
return readCSVBodyWhenUnbinds(csvData);
}
public static List<OrgUcPassportModel> readCSVBodyWhenBinds(
List<String[]> csvData) throws ServiceException {
List<OrgUcPassportModel> CSVForm = new ArrayList<OrgUcPassportModel>();
try {
int rowCount = 1;
for (String[] CSVBody : csvData) {
// 跳过表头,从第二行开始读取
if (rowCount == 1) {
rowCount++;
continue;
}
OrgUcPassportModel model = new OrgUcPassportModel();
model.setPassportName(CSVBody[0].trim());
model.setExpiredTime(DateUtils.parseDate(CSVBody[1].trim(),
new String[] { SystemConf.DATE_FORMAT }));
model.setOrgId(Long.valueOf(CSVBody[2].trim()));
CSVForm.add(model);
}
} catch (ParseException e) {
new ServiceException(BODY_ERROR);
}
return CSVForm;
}
public static List<OrgUcPassportModel> readCSVBodyWhenUnbinds(
List<String[]> csvData) throws ServiceException {
List<OrgUcPassportModel> CSVForm = new ArrayList<OrgUcPassportModel>();
int rowCount = 1;
for (String[] bodyData : csvData) {
// 跳过表头,从第二行开始读取
if (rowCount == 1) {
rowCount++;
continue;
}
OrgUcPassportModel model = new OrgUcPassportModel();
model.setPassportName(bodyData[0].trim());
CSVForm.add(model);
}
return CSVForm;
}
public static void checkCSVFormatWhenBinds(List<String[]> csvData)
throws ServiceException {
checkCSVHeaderWhenBinds(csvData);
checkCSVBodyWhenBinds(csvData);
}
public static void checkCSVFormatWhenUnbinds(List<String[]> csvData)
throws ServiceException {
checkCSVHeaderWhenUnbinds(csvData);
checkCSVBodyWhenUnbinds(csvData);
}
public static void checkCSVHeaderWhenBinds(List<String[]> csvData)
throws ServiceException {
checkCSVData(csvData);
String[] CSVHeader = csvData.get(0);
if (CSVHeader == null || CSVHeader.length <= 0) {
throw new ServiceException(HEADER_ERROR);
}
if (!BINDS_COLUMNS.equals(CSVHeader.length)) {
throw new ServiceException(HEADER_ERROR);
}
checkCSVCells(CSVHeader);
// 表头第二列必须是关于时间的提示,而不能是时间(避免上传文件没有表头):
String timeColumn = CSVHeader[1].trim();
if (RegexpUtils.isHardRegexpValidate(timeColumn,
RegexpUtils.DATE_BARS_REGEXP)) {
throw new ServiceException(HEADER_ERROR);
}
// 表头第三列必须是关于机构ID 的提示,而不能是机构ID(避免上传文件没有表头):
String orgColumn = CSVHeader[2].trim();
if (RegexpUtils.isHardRegexpValidate(orgColumn,
RegexpUtils.POSITIVE_INTEGER_REGEXP)) {
throw new ServiceException(HEADER_ERROR);
}
}
public static void checkCSVHeaderWhenUnbinds(List<String[]> csvData)
throws ServiceException {
checkCSVData(csvData);
String[] CSVHeader = csvData.get(0);
if (CSVHeader == null || CSVHeader.length <= 0) {
throw new ServiceException(HEADER_ERROR);
}
if (!UNBINDS_COLUMNS.equals(CSVHeader.length)) {
throw new ServiceException(HEADER_ERROR);
}
checkCSVCells(CSVHeader);
}
public static void checkCSVBodyWhenBinds(List<String[]> csvData)
throws ServiceException {
checkCSVData(csvData);
int rowCount = 1;
for (String[] bodyData : csvData) {
// 跳过表头,从第二行开始读取
if (rowCount == 1) {
rowCount++;
continue;
}
if (bodyData.length <= 0) {
throw new ServiceException(BODY_ERROR);
}
if (!BINDS_COLUMNS.equals(bodyData.length)) {
throw new ServiceException(BODY_ERROR);
}
// 保证每个单元格中的都有数据
checkCSVCells(bodyData);
// 第二列必须是时间格式
String time = bodyData[1].trim();
if (!RegexpUtils.isHardRegexpValidate(time,
RegexpUtils.DATE_BARS_REGEXP)) {
throw new ServiceException(BODY_ERROR);
}
// 第三列必须是正整数(>0)
String orgId = bodyData[2].trim();
if (!RegexpUtils.isHardRegexpValidate(orgId,
RegexpUtils.POSITIVE_INTEGER_REGEXP)) {
throw new ServiceException(BODY_ERROR);
}
}
}
public static void checkCSVBodyWhenUnbinds(List<String[]> csvData)
throws ServiceException {
checkCSVData(csvData);
int rowCount = 1;
for (String[] bodyData : csvData) {
// 跳过表头,从第二行开始读取
if (rowCount == 1) {
rowCount++;
continue;
}
if (bodyData.length <= 0) {
throw new ServiceException(BODY_ERROR);
}
if (!UNBINDS_COLUMNS.equals(bodyData.length)) {
throw new ServiceException(BODY_ERROR);
}
// 保证每个单元格中的都有数据
checkCSVCells(bodyData);
}
}
public static void checkCSVData(List<String[]> csvData)
throws ServiceException {
if (csvData == null || csvData.size() <= 0) {
throw new ServiceException(FILL_ERROR);
}
}
public static List<String[]> getCSVData(File CSVFile)
throws ServiceException {
if (CSVFile == null) {
throw new ServiceException(FILL_ERROR);
}
List<String[]> csvData = null;
InputStream is = null;
InputStreamReader isr = null;
CSVReader reader = null;
try {
is = new FileInputStream(CSVFile);
isr = new InputStreamReader(is, "UTF-8");
reader = new CSVReader(isr, ',');
csvData = reader.readAll();
} catch (FileNotFoundException e) {
LOG.warn("", e);
throw new ServiceException(FILL_ERROR, e);
} catch (UnsupportedEncodingException e) {
LOG.warn("", e);
throw new ServiceException(FILL_ERROR, e);
} catch (IOException e) {
LOG.warn("", e);
throw new ServiceException(FILL_ERROR, e);
} finally {
// 关闭文件流
try {
reader.close();
} catch (IOException e) {
LOG.warn("", e);
}
IOUtils.closeQuietly(isr);
IOUtils.closeQuietly(is);
}
return csvData;
}
public static void excludingTail(List<String[]> csvData) {
List<String[]> toExclud = new ArrayList<String[]>();
// 从最后一行开始检查
for (int tail = csvData.size() - 1; tail >= 0; tail--) {
String[] row = csvData.get(tail);
if (row != null && row.length > 0) {
Boolean isBlank = true;
for (String cell : row) {
if (!StringUtils.isBlank(cell)) {
// 保证每一个单元格不为空
isBlank = false;
break;
}
}
if (!isBlank) {
break;
}
}
toExclud.add(row);
}
// 清除尾部空行
csvData.removeAll(toExclud);
}
public static void checkCSVCells(String[] row) throws ServiceException {
for (String cell : row) {
if (StringUtils.isBlank(cell)) {
throw new ServiceException(BODY_ERROR);
}
}
}
}