refactor(converter): 重构StringConverter实现并统一处理null值

重构各实体类的StringConverter实现,移除EntityStringConverter基类,改为直接实现StringConverter接口
在各Service中提供对应的StringConverter实例,统一处理null值情况
更新ComboBoxUtils使用Service提供的StringConverter
This commit is contained in:
2025-10-17 00:27:01 +08:00
parent 0b45f6eef2
commit 235269f86f
17 changed files with 184 additions and 82 deletions

View File

@@ -6,12 +6,12 @@
<parent>
<groupId>com.ecep.contract</groupId>
<artifactId>Contract-Manager</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
</parent>
<groupId>com.ecep.contract</groupId>
<artifactId>client</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
@@ -22,7 +22,7 @@
<dependency>
<groupId>com.ecep.contract</groupId>
<artifactId>common</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>

View File

@@ -92,7 +92,6 @@ public class ProjectTabSkinBase extends AbstProjectBasedTabSkin implements TabSk
controller.standardPayWayField.selectedProperty().bindBidirectional(viewModel.getStandardPayWay());
Bindings.bindBidirectional(controller.amountField.textProperty(), viewModel.getAmount(),
new NumberStringConverter());
ComboBoxUtils.initialComboBox(controller.saleTypeField, viewModel.getSaleType(), getSaleTypeService(), true);
ComboBoxUtils.initialComboBox(controller.projectTypeField, viewModel.getProjectType(), getProjectTypeService(),
true);

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -2,6 +2,8 @@ package com.ecep.contract.service;
import java.util.List;
import com.ecep.contract.converter.ProductTypeStringConverter;
import javafx.util.StringConverter;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@@ -17,6 +19,8 @@ import com.ecep.contract.vo.ProductTypeVo;
@Service
@CacheConfig(cacheNames = "product-type")
public class ProductTypeService extends QueryService<ProductTypeVo, ProductTypeViewModel> {
private final StringConverter<ProductTypeVo> stringConverter = new ProductTypeStringConverter(this);
@Cacheable(key = "#p0")
@Override
public ProductTypeVo findById(Integer id) {
@@ -61,4 +65,8 @@ public class ProductTypeService extends QueryService<ProductTypeVo, ProductTypeV
return page.getContent().getFirst();
}
@Override
public StringConverter<ProductTypeVo> getStringConverter() {
return stringConverter;
}
}

View File

@@ -1,5 +1,7 @@
package com.ecep.contract.service;
import com.ecep.contract.converter.ProductUsageStringConverter;
import javafx.util.StringConverter;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@@ -14,6 +16,7 @@ import com.ecep.contract.vo.ProductUsageVo;
@Service
@CacheConfig(cacheNames = "productUsageCache")
public class ProductUsageService extends QueryService<ProductUsageVo, ProductUsageViewModel> {
private final StringConverter<ProductUsageVo> stringConverter = new ProductUsageStringConverter(this);
@Override
@Cacheable(key = "#id")
@@ -21,6 +24,10 @@ public class ProductUsageService extends QueryService<ProductUsageVo, ProductUsa
return super.findById(id);
}
public ProductUsageVo findByName(String string) {
return findOneByProperty("name", string);
}
@Override
@Cacheable(key = "'all'")
public java.util.List<ProductUsageVo> findAll() {
@@ -47,4 +54,9 @@ public class ProductUsageService extends QueryService<ProductUsageVo, ProductUsa
return page.getContent().getFirst();
}
@Override
public StringConverter<ProductUsageVo> getStringConverter() {
return stringConverter;
}
}

View File

@@ -6,23 +6,33 @@ import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.converter.ProjectIndustryStringConverter;
import com.ecep.contract.vm.ProjectIndustryViewModel;
import com.ecep.contract.vo.ProjectIndustryVo;
import javafx.util.StringConverter;
@Service
@CacheConfig(cacheNames = "project-industry")
public class ProjectIndustryService extends QueryService<ProjectIndustryVo, ProjectIndustryViewModel> {
private final StringConverter<ProjectIndustryVo> stringConverter = new ProjectIndustryStringConverter(this);
@Cacheable(key = "#p0")
@Override
public ProjectIndustryVo findById(Integer id) {
return super.findById(id);
}
public ProjectIndustryVo findByCode(String code) {
return findOneByProperty("code", code);
}
public ProjectIndustryVo findByName(String name) {
return findOneByProperty("name", name);
}
@Cacheable(key = "'all'")
@Override
public List<ProjectIndustryVo> findAll() {
@@ -41,11 +51,4 @@ public class ProjectIndustryService extends QueryService<ProjectIndustryVo, Proj
super.delete(entity);
}
public ProjectIndustryVo findByCode(String code) {
Page<ProjectIndustryVo> page = findAll(ParamUtils.builder().equals("code", code).build(), Pageable.ofSize(1));
if (page.isEmpty()) {
return null;
}
return page.getContent().getFirst();
}
}

View File

@@ -7,13 +7,18 @@ import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.converter.ProjectSaleTypeStringConverter;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.ProjectSaleTypeViewModel;
import com.ecep.contract.vo.ProjectSaleTypeVo;
import javafx.util.StringConverter;
@Service
@CacheConfig(cacheNames = "project-sale-type")
public class ProjectSaleTypeService extends QueryService<ProjectSaleTypeVo, ProjectSaleTypeViewModel> {
private final StringConverter<ProjectSaleTypeVo> stringConverter = new ProjectSaleTypeStringConverter(this);
@Cacheable(key = "#id")
@Override
public ProjectSaleTypeVo findById(Integer id) {
@@ -28,15 +33,20 @@ public class ProjectSaleTypeService extends QueryService<ProjectSaleTypeVo, Proj
return findAll(ParamUtils.builder().equals("name", name).build(), Pageable.ofSize(1)).getContent().getFirst();
}
@Caching(evict = { @CacheEvict(key = "#p0.id") })
@Caching(evict = {@CacheEvict(key = "#p0.id")})
@Override
public ProjectSaleTypeVo save(ProjectSaleTypeVo v) {
return super.save(v);
}
@Caching(evict = { @CacheEvict(key = "#p0.id") })
@Caching(evict = {@CacheEvict(key = "#p0.id")})
@Override
public void delete(ProjectSaleTypeVo entity) {
super.delete(entity);
}
@Override
public StringConverter<ProjectSaleTypeVo> getStringConverter() {
return stringConverter;
}
}

View File

@@ -2,17 +2,22 @@ package com.ecep.contract.service;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.converter.ProjectTypeStringConverter;
import com.ecep.contract.vm.ProjectTypeViewModel;
import com.ecep.contract.vo.ProjectTypeVo;
import javafx.util.StringConverter;
@Service
@CacheConfig(cacheNames = "project-type")
public class ProjectTypeService extends QueryService<ProjectTypeVo, ProjectTypeViewModel> {
private final StringConverter<ProjectTypeVo> stringConverter;
public ProjectTypeService() {
this.stringConverter = new ProjectTypeStringConverter(this);
}
@Cacheable(key = "#p0")
@Override
@@ -21,19 +26,15 @@ public class ProjectTypeService extends QueryService<ProjectTypeVo, ProjectTypeV
}
public ProjectTypeVo findByName(String name) {
Page<ProjectTypeVo> page = findAll(ParamUtils.builder().equals("name", name).build(), Pageable.ofSize(1));
if (page.isEmpty()) {
return null;
}
return page.getContent().getFirst();
return findOneByProperty("name", name);
}
public ProjectTypeVo findByCode(String code) {
Page<ProjectTypeVo> page = findAll(ParamUtils.builder().equals("code", code).build(), Pageable.ofSize(1));
if (page.isEmpty()) {
return null;
}
return page.getContent().getFirst();
return findOneByProperty("code", code);
}
public StringConverter<ProjectTypeVo> getStringConverter() {
return stringConverter;
}
}

View File

@@ -260,6 +260,9 @@ public class QueryService<T extends IdentityEntity, TV extends IdentityViewModel
return new StringConverter<>() {
@Override
public String toString(T object) {
if (object == null) {
return "";
}
if (object instanceof NamedEntity named) {
return named.getName();
}

View File

@@ -125,9 +125,7 @@ public class ComboBoxUtils {
list.addAll(queryService.findAll());
comboBox.setItems(list);
EntityStringConverter<T> converter = new EntityStringConverter<>(list);
comboBox.setConverter(converter);
comboBox.setConverter(queryService.getStringConverter());
if (property != null) {
// 从ComboBox选择到property的单向绑定

View File

@@ -6,12 +6,12 @@
<parent>
<groupId>com.ecep.contract</groupId>
<artifactId>Contract-Manager</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
</parent>
<groupId>com.ecep.contract</groupId>
<artifactId>common</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>

View File

@@ -10,7 +10,7 @@
</parent>
<groupId>com.ecep.contract</groupId>
<artifactId>Contract-Manager</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>server</module>

View File

@@ -6,12 +6,12 @@
<parent>
<groupId>com.ecep.contract</groupId>
<artifactId>Contract-Manager</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
</parent>
<groupId>com.ecep.contract</groupId>
<artifactId>server</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
<properties>
<maven.compiler.source>${java.version}</maven.compiler.source>
@@ -22,7 +22,7 @@
<dependency>
<groupId>com.ecep.contract</groupId>
<artifactId>common</artifactId>
<version>0.0.122-SNAPSHOT</version>
<version>0.0.126-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>