拆分模块
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ecep.contract.model.Bank;
|
||||
import com.ecep.contract.service.BankService;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class BankStringConverter extends EntityStringConverter<Bank> {
|
||||
|
||||
@Lazy
|
||||
@Autowired
|
||||
BankService service;
|
||||
public BankStringConverter() {
|
||||
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
// #2
|
||||
setInitialized(project -> service.findById(project.getId()));
|
||||
setSuggestion(service::search);
|
||||
setFromString(service::findByName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.ecep.contract.model.ContractGroup;
|
||||
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
public class ContractGroupStringConverter extends StringConverter<ContractGroup> {
|
||||
|
||||
private List<ContractGroup> dataset;
|
||||
|
||||
public ContractGroupStringConverter() {
|
||||
}
|
||||
|
||||
public ContractGroupStringConverter(List<ContractGroup> dataset) {
|
||||
this.dataset = dataset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(ContractGroup group) {
|
||||
return group == null ? "All" : ContractGroup.toString(group);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContractGroup fromString(String string) {
|
||||
if (dataset == null) {
|
||||
return null;
|
||||
}
|
||||
if (!StringUtils.hasText(string)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (ContractGroup group : dataset) {
|
||||
if (group == null) {
|
||||
continue;
|
||||
}
|
||||
if (ContractGroup.toString(group).equals(string)) {
|
||||
return group;
|
||||
}
|
||||
if (group.getCode().equals(string)) {
|
||||
return group;
|
||||
}
|
||||
if (group.getName().contains(string)) {
|
||||
return group;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,12 @@
|
||||
package com.ecep.contract.ds.contract;
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.service.ContractService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ecep.contract.ds.contract.service.ContractService;
|
||||
import com.ecep.contract.model.Contract;
|
||||
import com.ecep.contract.util.EntityStringConverter;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class ContractStringConverter extends EntityStringConverter<Contract> {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ecep.contract.model.Employee;
|
||||
import com.ecep.contract.service.EmployeeService;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class EmployeeStringConverter extends EntityStringConverter<Employee> {
|
||||
@Lazy
|
||||
@Autowired
|
||||
private EmployeeService service;
|
||||
|
||||
public EmployeeStringConverter() {
|
||||
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
setInitialized(project -> service.findById(project.getId()));
|
||||
setSuggestion(service::search);
|
||||
setFromString(service::findByName);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.controlsfx.control.textfield.AutoCompletionBinding;
|
||||
import org.hibernate.Hibernate;
|
||||
|
||||
import com.ecep.contract.model.BasedEntity;
|
||||
import com.ecep.contract.model.NamedEntity;
|
||||
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
public class EntityStringConverter<T> extends StringConverter<T> {
|
||||
|
||||
private Function<T, T> initialized;
|
||||
private Function<String, T> fromString;
|
||||
private Function<T, String> formater;
|
||||
private Function<String, List<T>> suggestion;
|
||||
|
||||
@Override
|
||||
public String toString(T cc) {
|
||||
if (cc == null) {
|
||||
return "-";
|
||||
}
|
||||
cc = prefixObject(cc);
|
||||
return toPrettyString(cc);
|
||||
}
|
||||
|
||||
public T prefixObject(T cc) {
|
||||
if (cc == null) {
|
||||
return null;
|
||||
}
|
||||
if (initialized != null && !Hibernate.isInitialized(cc)) {
|
||||
cc = initialized.apply(cc);
|
||||
}
|
||||
return cc;
|
||||
}
|
||||
|
||||
public String toPrettyString(T cc) {
|
||||
if (formater != null) {
|
||||
return formater.apply(cc);
|
||||
}
|
||||
if (cc == null) {
|
||||
return null;
|
||||
}
|
||||
if (cc instanceof BasedEntity e) {
|
||||
return e.toPrettyString();
|
||||
}
|
||||
if (cc instanceof NamedEntity e) {
|
||||
return e.getName();
|
||||
}
|
||||
return cc.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T fromString(String string) {
|
||||
if (fromString == null) {
|
||||
return null;
|
||||
}
|
||||
return fromString.apply(string);
|
||||
}
|
||||
|
||||
public void setFromString(Function<String, T> fromString) {
|
||||
this.fromString = fromString;
|
||||
}
|
||||
|
||||
public void setInitialized(Function<T, T> initialized) {
|
||||
this.initialized = initialized;
|
||||
}
|
||||
|
||||
public void setFormater(Function<T, String> formater) {
|
||||
this.formater = formater;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置自动补全的数据提供方法
|
||||
*
|
||||
* @param suggestion 数据提供方法
|
||||
*/
|
||||
public void setSuggestion(Function<String, List<T>> suggestion) {
|
||||
this.suggestion = suggestion;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求自动补全数据
|
||||
*
|
||||
* @param request 请求
|
||||
* @return 补全数据
|
||||
*/
|
||||
public List<T> suggest(AutoCompletionBinding.ISuggestionRequest request) {
|
||||
return suggestion.apply(request.getUserText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
package com.ecep.contract.ds.project.service;
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import com.ecep.contract.model.Project;
|
||||
import com.ecep.contract.service.ProjectService;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.ecep.contract.model.Project;
|
||||
import com.ecep.contract.util.EntityStringConverter;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class ProjectStringConverter extends EntityStringConverter<Project> {
|
||||
|
||||
Reference in New Issue
Block a user