refactor(converter): 重构StringConverter实现并统一处理null值
重构各实体类的StringConverter实现,移除EntityStringConverter基类,改为直接实现StringConverter接口 在各Service中提供对应的StringConverter实例,统一处理null值情况 更新ComboBoxUtils使用Service提供的StringConverter
This commit is contained in:
@@ -1,29 +1,25 @@
|
||||
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.service.ProductTypeService;
|
||||
import com.ecep.contract.vo.ProductTypeVo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class ProductTypeStringConverter extends EntityStringConverter<ProductTypeVo> {
|
||||
@Lazy
|
||||
@Autowired
|
||||
private ProductTypeService service;
|
||||
|
||||
public ProductTypeStringConverter() {
|
||||
public class ProductTypeStringConverter extends StringConverter<ProductTypeVo> {
|
||||
private final ProductTypeService service;
|
||||
|
||||
public ProductTypeStringConverter(ProductTypeService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
setInitialized(type -> service.findById(type.getId()));
|
||||
setSuggestion(service::search);
|
||||
@Override
|
||||
public String toString(ProductTypeVo type) {
|
||||
return type == null ? "" : type.getCode() + " " + type.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductTypeVo fromString(String string) {
|
||||
return service.findByName(string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import javafx.util.StringConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -9,21 +10,21 @@ import com.ecep.contract.vo.ProductUsageVo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class ProductUsageStringConverter extends EntityStringConverter<ProductUsageVo> {
|
||||
@Lazy
|
||||
@Autowired
|
||||
public class ProductUsageStringConverter extends StringConverter<ProductUsageVo> {
|
||||
private ProductUsageService service;
|
||||
|
||||
public ProductUsageStringConverter() {
|
||||
|
||||
public ProductUsageStringConverter(ProductUsageService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
setInitialized(usage -> service.findById(usage.getId()));
|
||||
setSuggestion(service::search);
|
||||
@Override
|
||||
public String toString(ProductUsageVo usage) {
|
||||
return usage == null ? "" : usage.getCode() + " " + usage.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductUsageVo fromString(String string) {
|
||||
return service.findByName(string);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,30 +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.service.ProjectIndustryService;
|
||||
import com.ecep.contract.vo.ProjectIndustryVo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
@Lazy
|
||||
@Component
|
||||
public class ProjectIndustryStringConverter extends EntityStringConverter<ProjectIndustryVo> {
|
||||
@Lazy
|
||||
@Autowired
|
||||
public class ProjectIndustryStringConverter extends StringConverter<ProjectIndustryVo> {
|
||||
private ProjectIndustryService service;
|
||||
|
||||
public ProjectIndustryStringConverter() {
|
||||
|
||||
public ProjectIndustryStringConverter(ProjectIndustryService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
setInitialized(industry -> service.findById(industry.getId()));
|
||||
setSuggestion(service::search);
|
||||
@Override
|
||||
public String toString(ProjectIndustryVo object) {
|
||||
if (object == null) {
|
||||
return "";
|
||||
}
|
||||
return object.getCode() + " " + object.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectIndustryVo fromString(String string) {
|
||||
if (string == null || string.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return service.findByName(string);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import com.ecep.contract.service.ProjectSaleTypeService;
|
||||
import com.ecep.contract.vo.ProjectSaleTypeVo;
|
||||
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
public class ProjectSaleTypeStringConverter extends StringConverter<ProjectSaleTypeVo> {
|
||||
private final ProjectSaleTypeService service;
|
||||
|
||||
public ProjectSaleTypeStringConverter(ProjectSaleTypeService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(ProjectSaleTypeVo type) {
|
||||
return type == null ? "" : type.getCode() + " " + type.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectSaleTypeVo fromString(String string) {
|
||||
return service.findByName(string);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.ecep.contract.converter;
|
||||
|
||||
import com.ecep.contract.service.ProjectTypeService;
|
||||
import com.ecep.contract.vo.ProjectTypeVo;
|
||||
import javafx.util.StringConverter;
|
||||
|
||||
/**
|
||||
* ProjectTypeVo的StringConverter实现,用于JavaFX控件中的显示和转换
|
||||
*/
|
||||
public class ProjectTypeStringConverter extends StringConverter<ProjectTypeVo> {
|
||||
private final ProjectTypeService service;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param service ProjectTypeService实例
|
||||
*/
|
||||
public ProjectTypeStringConverter(ProjectTypeService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ProjectTypeVo对象转换为字符串
|
||||
*
|
||||
* @param object ProjectTypeVo对象
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
@Override
|
||||
public String toString(ProjectTypeVo object) {
|
||||
return object == null ? "" : object.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换为ProjectTypeVo对象
|
||||
*
|
||||
* @param string 字符串
|
||||
* @return 转换后的ProjectTypeVo对象
|
||||
*/
|
||||
@Override
|
||||
public ProjectTypeVo fromString(String string) {
|
||||
if (string == null || string.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return service.findByName(string);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user