refactor: 重构企业文件验证和移动逻辑 fix: 修复企业合规验证逻辑及路径处理问题 docs: 添加VerifyContext工具类及相关文档 style: 优化代码格式及注释
177 lines
5.0 KiB
Java
177 lines
5.0 KiB
Java
package com.ecep.contract.task;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Locale;
|
|
import java.util.logging.Level;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeansException;
|
|
|
|
import com.ecep.contract.Desktop;
|
|
import com.ecep.contract.Message;
|
|
import com.ecep.contract.MessageHolder;
|
|
import com.ecep.contract.SpringApp;
|
|
import com.ecep.contract.service.CompanyService;
|
|
import com.ecep.contract.service.EmployeeService;
|
|
import com.ecep.contract.service.SysConfService;
|
|
import com.ecep.contract.vo.EmployeeVo;
|
|
|
|
import javafx.application.Platform;
|
|
import javafx.beans.property.StringProperty;
|
|
import javafx.concurrent.Task;
|
|
import javafx.scene.control.ListCell;
|
|
import lombok.Setter;
|
|
|
|
public abstract class Tasker<T> extends Task<T> {
|
|
private static final Logger logger = LoggerFactory.getLogger(Tasker.class);
|
|
@Setter
|
|
protected java.util.function.Predicate<Message> messageHandler;
|
|
private EmployeeVo currentUser;
|
|
private HashMap<Class<?>, Object> cachedMap = new HashMap<>();
|
|
|
|
public SysConfService getConfService() {
|
|
return getBean(SysConfService.class);
|
|
}
|
|
|
|
public CompanyService getCompanyService() {
|
|
return getBean(CompanyService.class);
|
|
}
|
|
|
|
public EmployeeService getEmployeeService() {
|
|
return getBean(EmployeeService.class);
|
|
}
|
|
|
|
protected <K> K getBean(Class<K> requiredType) throws BeansException {
|
|
return getCachedBean(requiredType);
|
|
}
|
|
|
|
protected <K> K getCachedBean(Class<K> requiredType) {
|
|
@SuppressWarnings("unchecked")
|
|
K bean = (K) cachedMap.get(requiredType);
|
|
if (bean == null) {
|
|
bean = SpringApp.getBean(requiredType);
|
|
cachedMap.put(requiredType, bean);
|
|
}
|
|
return bean;
|
|
}
|
|
|
|
public EmployeeVo getCurrentUser() {
|
|
if (currentUser == null) {
|
|
currentUser = getEmployeeService().findById(Desktop.instance.getActiveEmployeeId());
|
|
}
|
|
return currentUser;
|
|
}
|
|
|
|
@Override
|
|
protected T call() throws Exception {
|
|
MessageHolderImpl holder = new MessageHolderImpl();
|
|
try {
|
|
return execute(holder);
|
|
} catch (Exception e) {
|
|
holder.error(e.getMessage());
|
|
logger.error(e.getMessage(), e);
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
protected abstract T execute(MessageHolder holder) throws Exception;
|
|
|
|
@Override
|
|
protected void updateMessage(String message) {
|
|
updateMessage(Level.INFO, message);
|
|
}
|
|
|
|
public void updateMessage(Level level, String message) {
|
|
if (messageHandler != null) {
|
|
if (messageHandler.test(new Message(level, message))) {
|
|
return;
|
|
}
|
|
}
|
|
super.updateMessage(message);
|
|
}
|
|
|
|
public void updateTitle(String title) {
|
|
super.updateTitle(title);
|
|
}
|
|
|
|
protected void skipUpdateCheckUpdateMessage(String message) {
|
|
if (Platform.isFxApplicationThread()) {
|
|
((StringProperty) messageProperty()).set(message);
|
|
} else {
|
|
Platform.runLater(() -> ((StringProperty) messageProperty()).set(message));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void scheduled() {
|
|
super.scheduled();
|
|
updateMessage(Level.FINE, "任务进入调度器");
|
|
}
|
|
|
|
@Override
|
|
protected void running() {
|
|
super.running();
|
|
updateMessage(Level.FINE, "任务开始执行");
|
|
}
|
|
|
|
@Override
|
|
protected void cancelled() {
|
|
super.cancelled();
|
|
updateMessage(Level.SEVERE, "任务被取消");
|
|
}
|
|
|
|
@Override
|
|
protected void failed() {
|
|
super.failed();
|
|
Throwable exception = getException();
|
|
if (exception == null) {
|
|
updateMessage(Level.SEVERE, "任务执行失败");
|
|
} else {
|
|
updateMessage(Level.SEVERE, "任务执行失败:" + exception.getMessage());
|
|
if (logger.isErrorEnabled()) {
|
|
logger.error(exception.getMessage(), exception);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected void succeeded() {
|
|
super.succeeded();
|
|
updateMessage(Level.FINE, "任务执行完毕");
|
|
}
|
|
|
|
public static class MessageListCell extends ListCell<Message> {
|
|
@Override
|
|
protected void updateItem(Message item, boolean empty) {
|
|
super.updateItem(item, empty);
|
|
if (item == null || empty) {
|
|
setText(null);
|
|
getStyleClass().clear();
|
|
} else {
|
|
setText(item.getMessage());
|
|
getStyleClass().removeIf(s -> s.startsWith("row-"));
|
|
getStyleClass().add("row-" + item.getLevel().getName().toLowerCase());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 错误消息处理
|
|
*/
|
|
public class MessageHolderImpl implements MessageHolder {
|
|
public MessageHolderImpl() {
|
|
}
|
|
|
|
@Override
|
|
public void addMessage(Level level, String message) {
|
|
updateMessage(level, message);
|
|
}
|
|
}
|
|
|
|
public Locale getLocale() {
|
|
return Desktop.instance.getActiveEmployee().localeProperty().get();
|
|
}
|
|
|
|
}
|