自动处理文件内容解析,日期格式错误情况下文件需要放置到errorfile文件夹下,表示文件不可用

This commit is contained in:
qiaoqinzheng 2023-12-29 20:05:11 +08:00
parent 9b365cfd8e
commit bb511d23e2
7 changed files with 164 additions and 10 deletions

View File

@ -64,6 +64,11 @@ public class SpectrumPathProperties implements Serializable {
*/
private String filesourcePath;
/**
* 错误文件存储路径
*/
private String errorFilePath;
/**
* 能谱文件存储路径以能谱系统类型/能谱类型为key以存储路径为value
*/

View File

@ -0,0 +1,12 @@
package org.jeecg.modules.exception;
/**
* 判断日期是否是正确格式
*/
public class DateFormatErrorException extends RuntimeException {
public DateFormatErrorException(String message) {
super(message);
}
}

View File

@ -1,21 +1,20 @@
package org.jeecg.modules.spectrum;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.jeecg.common.constant.StringConstant;
import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.base.entity.original.GardsSampleData;
import org.jeecg.modules.base.enums.SampleStatus;
import org.jeecg.modules.config.datasource.DataSourceSwitcher;
import org.jeecg.modules.eneity.event.FormatErrorEvent;
import org.jeecg.modules.eneity.event.RepeatErrorEvent;
import org.jeecg.modules.exception.AcquisitionBlockException;
import org.jeecg.modules.exception.FileRepeatException;
import org.jeecg.modules.exception.HeaderBlockException;
import org.jeecg.modules.exception.PHD_ReadException;
import org.jeecg.modules.exception.*;
import org.jeecg.modules.native_jni.EnergySpectrumHandler;
import org.jeecg.modules.native_jni.struct.EnergySpectrumStruct;
import org.jeecg.modules.service.ISpectrumBlockService;
@ -25,9 +24,11 @@ import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;
/**
* 样品谱(Samplephd)探测器本地谱DetbkphdQC谱Qcphd气体谱Gasbkphd
@ -99,8 +100,40 @@ public abstract class AbstractS_D_Q_G_SpectrumHandler extends AbstractSpectrumHa
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new PHD_ReadException("THE PHDFile has some blocks can't be read:"+super.spectrumFile.getAbsolutePath());
}
try {
//如果是其中单一一个为空 抛出异常
//如果两个都为空 就不抛出异常
//如果两个都不为空 进行日期格式化 格式化失败
if ((StringUtils.isBlank(sourceData.collection_start_date) && StringUtils.isNotBlank(sourceData.collection_start_time)) ||
(StringUtils.isBlank(sourceData.collection_start_time) && StringUtils.isNotBlank(sourceData.collection_start_date))) {
throw new RuntimeException();
} else if (StringUtils.isNotBlank(sourceData.collection_start_time) && StringUtils.isNotBlank(sourceData.collection_start_date)) {
DateUtils.parseDate(sourceData.collection_start_date + StringPool.SPACE + sourceData.collection_start_time);
}
if ((StringUtils.isBlank(sourceData.collection_stop_date) && StringUtils.isNotBlank(sourceData.collection_stop_time)) ||
(StringUtils.isBlank(sourceData.collection_stop_time) && StringUtils.isNotBlank(sourceData.collection_stop_date))) {
throw new RuntimeException();
} else if (StringUtils.isNotBlank(sourceData.collection_stop_time) && StringUtils.isNotBlank(sourceData.collection_stop_date)) {
DateUtils.parseDate(sourceData.collection_stop_date + StringPool.SPACE + sourceData.collection_stop_time);
}
if ((StringUtils.isBlank(sourceData.acquisition_start_date) && StringUtils.isNotBlank(sourceData.acquisition_start_time)) ||
(StringUtils.isBlank(sourceData.acquisition_start_time) && StringUtils.isNotBlank(sourceData.acquisition_start_date))) {
throw new RuntimeException();
} else if (StringUtils.isNotBlank(sourceData.acquisition_start_time) && StringUtils.isNotBlank(sourceData.acquisition_start_date)) {
DateUtils.parseDate(sourceData.acquisition_start_date + StringPool.SPACE + sourceData.acquisition_start_time);
}
} catch (Exception e) {
//将文件移动到错误文件目录
super.isDateFormatErr = true;
//发送格式化错误事件后续统计报告使用
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new DateFormatErrorException("This PHDFile contains the wrong date format content:"+super.spectrumFile.getAbsolutePath());
} finally {
this.sourceData = sourceData;
}
}
/**
* 获取文件保存相对路径
@ -144,6 +177,36 @@ public abstract class AbstractS_D_Q_G_SpectrumHandler extends AbstractSpectrumHa
super.spectrumFile = FileUtil.rename(super.spectrumFile, newFileName.toString(), true);
}
/**
* 对错误的本地能谱临时文件进行改名
*/
@Override
protected void updateErrorSpectrumFileName() throws FileNotFoundException {
StringBuilder newFileName = new StringBuilder();
newFileName.append(this.sourceData.detector_code);
newFileName.append(StringConstant.DASH);
if (StringUtils.isNotBlank(this.sourceData.acquisition_start_date) && StringUtils.isNotBlank(this.sourceData.acquisition_start_time)) {
newFileName.append(StringUtils.replace(this.sourceData.acquisition_start_date,StringConstant.SLASH,""));
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(StringUtils.replace(this.sourceData.acquisition_start_time.substring(0,this.sourceData.acquisition_start_time.lastIndexOf(":")),":",""));
} else {
newFileName.append("");
}
if (StringUtils.isNotBlank(this.sourceData.spectrum_quantity) && Double.isFinite(this.sourceData.acquisition_live_time)) {
newFileName.append(super.spectrumServiceQuotes.getNameStandUtil().GetSuffix(super.currDataType.getType(),this.sourceData.spectrum_quantity,String.valueOf(this.sourceData.acquisition_live_time)));
} else {
Random ran = new Random();
double live_time = ran.nextDouble() * 10000;
newFileName.append(super.spectrumServiceQuotes.getNameStandUtil().GetSuffix(super.currDataType.getType(),this.sourceData.spectrum_quantity,String.valueOf(live_time)));
}
if(!super.spectrumFile.exists()){
//发送格式化错误事件后续统计报告使用
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new FileNotFoundException(super.spectrumFile.getAbsolutePath()+" does not exist");
}
super.spectrumFile = FileUtil.rename(super.spectrumFile, newFileName.toString(), true);
}
/**
* 读取邮件内容#开头的标签
* @throws Exception

View File

@ -60,6 +60,8 @@ public abstract class AbstractSpectrumHandler extends AbstractChain {
* 返回调用方filesourceundelSpectrumParsingActuator的文件名称
*/
protected StringBuilder returnFileName;
protected boolean isDateFormatErr = false;
/**
* 保存当前能谱文件有哪些#开头的标签
*/
@ -148,6 +150,11 @@ public abstract class AbstractSpectrumHandler extends AbstractChain {
*/
protected abstract void updateSpectrumFileName() throws FileNotFoundException;
/**
* 对错误的本地能谱临时文件进行改名
*/
protected abstract void updateErrorSpectrumFileName() throws FileNotFoundException;
/**
* 处理原始数据
*/
@ -222,11 +229,21 @@ public abstract class AbstractSpectrumHandler extends AbstractChain {
protected void handleParseingFailFile(Exception e) throws FileNotFoundException {
if(!SpectrumSource.FORM_FILE_UNDEL.getSourceType().equals(spectrumSource) && !(e instanceof FileRepeatException)){
try {
if (isDateFormatErr) {
//修改能谱文件名称
this.updateErrorSpectrumFileName();
//解析失败会把文件移动到undeal目录
final String rootPath = spectrumServiceQuotes.getSpectrumPathProperties().getRootPath();
final String errorFilePath = spectrumServiceQuotes.getSpectrumPathProperties().getErrorFilePath();
final String finalPath = rootPath+File.separator+errorFilePath;
FileOperation.moveFile(spectrumFile,finalPath,true);
} else {
//解析失败会把文件移动到undeal目录
final String rootPath = spectrumServiceQuotes.getSpectrumPathProperties().getRootPath();
final String undealPath = spectrumServiceQuotes.getSpectrumPathProperties().getUndealPath();
final String finalPath = rootPath+File.separator+undealPath;
FileOperation.copyFile(spectrumFile,finalPath,true);
}
} catch (IOException ex) {
ex.printStackTrace();
}

View File

@ -141,6 +141,25 @@ public class AlertSpectrum extends AbstractSpectrumHandler{
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
@Override
protected void updateErrorSpectrumFileName() throws FileNotFoundException {
StringBuilder newFileName = new StringBuilder();
newFileName.append(this.sourceData.station_code);
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(super.currDataType.getType());
newFileName.append(StringConstant.DASH);
newFileName.append(StringUtils.replace(this.sourceData.date,StringConstant.SLASH,""));
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(StringUtils.replace(this.sourceData.time,":",""));
newFileName.append(super.currDataType.getSuffix());
if(!super.spectrumFile.exists()){
//发送格式化错误事件后续统计报告使用
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new FileNotFoundException(super.spectrumFile.getAbsolutePath()+" does not exist");
}
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
/**
* 处理原始数据
*/

View File

@ -136,6 +136,25 @@ public class HealthStatusSpectrum extends AbstractSpectrumHandler{
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
@Override
protected void updateErrorSpectrumFileName() throws FileNotFoundException {
StringBuilder newFileName = new StringBuilder();
newFileName.append(this.sourceData.station_code);
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(super.currDataType.getType());
newFileName.append(StringConstant.DASH);
newFileName.append(StringUtils.replace(this.sourceData.start_date,StringConstant.SLASH,""));
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(StringUtils.replace(this.sourceData.start_time,":",""));
newFileName.append(super.currDataType.getSuffix());
if(!super.spectrumFile.exists()){
//发送格式化错误事件后续统计报告使用
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new FileNotFoundException(super.spectrumFile.getAbsolutePath()+" does not exist");
}
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
/**
* 处理原始数据
*/

View File

@ -132,6 +132,25 @@ public class MetSpectrum extends AbstractSpectrumHandler{
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
@Override
protected void updateErrorSpectrumFileName() throws FileNotFoundException {
StringBuilder newFileName = new StringBuilder();
newFileName.append(this.sourceData.station_code);
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(super.currDataType.getType());
newFileName.append(StringConstant.DASH);
newFileName.append(StringUtils.replace(this.sourceData.met_start_date.get(0),StringConstant.SLASH,""));
newFileName.append(StringConstant.UNDER_LINE);
newFileName.append(StringUtils.replace(this.sourceData.met_start_time.get(0),":",""));
newFileName.append(super.currDataType.getSuffix());
if(!super.spectrumFile.exists()){
//发送格式化错误事件后续统计报告使用
spectrumServiceQuotes.getApplicationContext().publishEvent(new FormatErrorEvent());
throw new FileNotFoundException(super.spectrumFile.getAbsolutePath()+" does not exist");
}
super.spectrumFile = FileOperation.rename(super.spectrumFile,newFileName.toString(),true);
}
/**
* 处理原始数据
*/