|
|
@ -2,49 +2,134 @@ package cokr.xit.interfaces.lntris;
|
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
|
|
|
import cokr.xit.interfaces.filejob.service.bean.FileJobBean;
|
|
|
|
import cokr.xit.interfaces.filejob.service.bean.FileJobBean;
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends FileJobBean {
|
|
|
|
public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends FileJobBean {
|
|
|
|
|
|
|
|
private DataFileSupport<T> reader = new DataFileSupport<T>()
|
|
|
|
|
|
|
|
.setSupplier(interfaceInfoSupplier())
|
|
|
|
|
|
|
|
.setCharset(Charset.forName("UTF-8"));
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract Supplier<T> interfaceInfoSupplier();
|
|
|
|
protected abstract Supplier<T> interfaceInfoSupplier();
|
|
|
|
protected abstract Consumer<T> insertInterfaceInfo();
|
|
|
|
protected abstract Consumer<T> insertInterfaceInfo();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String receiptDates() {
|
|
|
|
|
|
|
|
return config("receiptDates");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private int receiptCount() {
|
|
|
|
|
|
|
|
String val = config("receiptCount");
|
|
|
|
|
|
|
|
int count = Integer.parseInt(ifEmpty(val, "0"));
|
|
|
|
|
|
|
|
return Math.max(0, count);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean contains(String str, List<String> tokens) {
|
|
|
|
|
|
|
|
for (String token: tokens)
|
|
|
|
|
|
|
|
if (str.contains(token))
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<String> getHeads(String[] dates) {
|
|
|
|
|
|
|
|
String interfaceID = interfaceInfoSupplier().get().interfaceID(),
|
|
|
|
|
|
|
|
head = interfaceID + "_";
|
|
|
|
|
|
|
|
if (!isEmpty(dates))
|
|
|
|
|
|
|
|
return Stream.of(dates)
|
|
|
|
|
|
|
|
.map(date -> {
|
|
|
|
|
|
|
|
return head + date;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.sorted()
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String rctDates = receiptDates();
|
|
|
|
|
|
|
|
if (!isEmpty(rctDates)) {
|
|
|
|
|
|
|
|
int offset = Integer.parseInt(rctDates);
|
|
|
|
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
|
|
|
|
return getDates(new Date(), offset).stream()
|
|
|
|
|
|
|
|
.map(date -> head + dateFormat.format(date))
|
|
|
|
|
|
|
|
.sorted()
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
|
|
|
return List.of(head);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
throw runtimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**수신 연계정보 파일을 읽어들여 저장한다.
|
|
|
|
/**수신 연계정보 파일을 읽어들여 저장한다.
|
|
|
|
* 시스템 코드를 지정하지 않으면 해당 단체의 모든 시스템의 수신전문을 처리한다.
|
|
|
|
* 시스템 코드를 지정하지 않으면 해당 단체의 모든 시스템의 수신전문을 처리한다.
|
|
|
|
* @param orgs 자치단체 코드 목록
|
|
|
|
* @param orgs 자치단체 코드 목록
|
|
|
|
* @param sysCodes 시스템 코드 목록
|
|
|
|
* @param sysCodes 시스템 코드 목록
|
|
|
|
|
|
|
|
* @param dates 수신일자
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public List<T> read(String[] orgs, String... sysCodes) {
|
|
|
|
public List<T> read(String[] orgs, String[] sysCodes, String... dates) {
|
|
|
|
String interfaceID = interfaceInfoSupplier().get().interfaceID();
|
|
|
|
List<String>
|
|
|
|
List<String> tails = InterfaceConfig.locals(orgs).stream()
|
|
|
|
heads = getHeads(dates),
|
|
|
|
.flatMap(org -> org.getOrganizationSystems(sysCodes).stream())
|
|
|
|
tails = InterfaceConfig.locals(orgs).stream()
|
|
|
|
.map(orgSys -> "@" + orgSys)
|
|
|
|
.flatMap(org -> org.getOrganizationSystems(sysCodes).stream())
|
|
|
|
.toList();
|
|
|
|
.map(orgSys -> "@" + orgSys)
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
log().debug("Reading\n{}", String.join("\n", heads.stream().map(head -> "\t" + head + "*.*").toList()));
|
|
|
|
|
|
|
|
|
|
|
|
List<Path> paths = getReceivedFilePaths(path -> {
|
|
|
|
ArrayList<T> result = new ArrayList<>();
|
|
|
|
String str = path.toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String tail: tails) {
|
|
|
|
while (true) {
|
|
|
|
if (str.contains(interfaceID) && str.contains(tail))
|
|
|
|
List<T> processed = processReceived(heads, tails);
|
|
|
|
return true;
|
|
|
|
if (processed.isEmpty()) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result.addAll(processed);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
protected List<T> processReceived(List<String> heads, List<String> tails) {
|
|
|
|
});
|
|
|
|
List<Path> paths = getReceivedFilePaths(
|
|
|
|
|
|
|
|
path -> {
|
|
|
|
|
|
|
|
String str = path.toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return contains(str, heads)
|
|
|
|
|
|
|
|
&& contains(str, tails);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
receiptCount()
|
|
|
|
|
|
|
|
);
|
|
|
|
if (paths.isEmpty()) return Collections.emptyList();
|
|
|
|
if (paths.isEmpty()) return Collections.emptyList();
|
|
|
|
|
|
|
|
|
|
|
|
List<FileStatus> fileStatus = processReceived(paths);
|
|
|
|
List<FileStatus> fileStatus = paths.stream()
|
|
|
|
|
|
|
|
.map(path -> {
|
|
|
|
|
|
|
|
FileStatus status = new FileStatus().setPath(path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
reader
|
|
|
|
|
|
|
|
.read(path)
|
|
|
|
|
|
|
|
.getMessages()
|
|
|
|
|
|
|
|
.forEach(info -> {
|
|
|
|
|
|
|
|
InterfaceConfig.databaseActive(() -> insertInterfaceInfo().accept(info));
|
|
|
|
|
|
|
|
List<T> msgs = (List<T>)status.get("messages");
|
|
|
|
|
|
|
|
if (msgs == null)
|
|
|
|
|
|
|
|
status.set("messages", msgs = new ArrayList<>());
|
|
|
|
|
|
|
|
msgs.add(info);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
status.setCause(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
|
|
|
|
Map<Boolean, List<FileStatus>> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류
|
|
|
|
Map<Boolean, List<FileStatus>> successFail = fileStatus.stream() //처리 결과를 성공 / 실패로 분류
|
|
|
|
.collect(Collectors.groupingBy(file -> file.isSuccess()));
|
|
|
|
.collect(Collectors.groupingBy(FileStatus::isSuccess));
|
|
|
|
|
|
|
|
|
|
|
|
List<FileStatus> success = successFail.get(true);
|
|
|
|
List<FileStatus> success = successFail.get(true);
|
|
|
|
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동
|
|
|
|
move(FileStatus.getPaths(success), successDir()); // 성공 디렉토리로 이동
|
|
|
@ -54,32 +139,4 @@ public abstract class InterfaceInfoReader<T extends InterfaceInfo<?, ?>> extends
|
|
|
|
Collections.emptyList() :
|
|
|
|
Collections.emptyList() :
|
|
|
|
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList();
|
|
|
|
success.stream().flatMap(status -> ((List<T>)status.get("messages")).stream()).toList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected List<FileStatus> processReceived(List<Path> paths) {
|
|
|
|
|
|
|
|
DataFileSupport<T> reader = new DataFileSupport<T>()
|
|
|
|
|
|
|
|
.setSupplier(interfaceInfoSupplier())
|
|
|
|
|
|
|
|
.setCharset(Charset.forName("UTF-8"));
|
|
|
|
|
|
|
|
return isEmpty(paths) ?
|
|
|
|
|
|
|
|
Collections.emptyList() :
|
|
|
|
|
|
|
|
paths.stream()
|
|
|
|
|
|
|
|
.map(path -> {
|
|
|
|
|
|
|
|
FileStatus status = new FileStatus().setPath(path);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
reader
|
|
|
|
|
|
|
|
.read(path)
|
|
|
|
|
|
|
|
.getMessages()
|
|
|
|
|
|
|
|
.forEach(info -> {
|
|
|
|
|
|
|
|
InterfaceConfig.databaseActive(() -> insertInterfaceInfo().accept(info));
|
|
|
|
|
|
|
|
List<T> msgs = (List<T>)status.get("messages");
|
|
|
|
|
|
|
|
if (msgs == null)
|
|
|
|
|
|
|
|
status.set("messages", msgs = new ArrayList<>());
|
|
|
|
|
|
|
|
msgs.add(info);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
status.setCause(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|