Skip to content

Commit 893d39b

Browse files
committed
fix DataType.java
1 parent 377fbd0 commit 893d39b

21 files changed

Lines changed: 220 additions & 77 deletions

flow-engine-framework/src/main/java/com/codingapi/flow/form/DataType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
public enum DataType {
44
STRING,
5-
NUMBER,
5+
LONG,
6+
INTEGER,
7+
DOUBLE,
68
BOOLEAN
79
}

flow-engine-framework/src/main/java/com/codingapi/flow/form/FormData.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,34 +114,34 @@ private FlowForm getSubFormMeta(String formCode) {
114114
}
115115

116116
/**
117-
* 校验formData
117+
* 校验formData
118118
*/
119119
public void verify() {
120-
for (FormField formField:flowForm.getFields()){
120+
for (FormField formField : flowForm.getFields()) {
121121
Object value = this.dataBody.get(formField.getCode());
122122
Object defaultValue = formField.getDefaultValue();
123-
if(value==null && defaultValue!=null){
124-
this.dataBody.set(formField.getCode(),defaultValue);
123+
if (value == null && defaultValue != null) {
124+
this.dataBody.set(formField.getCode(), defaultValue);
125125
}
126-
if(formField.isRequired()){
127-
if(value==null && defaultValue==null){
126+
if (formField.isRequired()) {
127+
if (value == null && defaultValue == null) {
128128
throw FlowValidationException.fieldNotFound(formField.getName());
129129
}
130130
}
131131
}
132132

133-
if(flowForm.getSubForms()!=null) {
133+
if (flowForm.getSubForms() != null) {
134134
for (FlowForm subForm : flowForm.getSubForms()) {
135-
List<DataBody> subDataList = this.getSubDataBody(subForm.getCode());
136-
for (DataBody subData:subDataList) {
135+
List<DataBody> subDataList = this.getSubDataBody(subForm.getCode());
136+
for (DataBody subData : subDataList) {
137137
for (FormField formField : subForm.getFields()) {
138138
Object value = subData.get(formField.getCode());
139139
Object defaultValue = formField.getDefaultValue();
140-
if(value==null && defaultValue!=null){
141-
subData.set(formField.getCode(),defaultValue);
140+
if (value == null && defaultValue != null) {
141+
subData.set(formField.getCode(), defaultValue);
142142
}
143-
if(formField.isRequired()){
144-
if(value==null && defaultValue==null){
143+
if (formField.isRequired()) {
144+
if (value == null && defaultValue == null) {
145145
throw FlowValidationException.fieldNotFound(formField.getName());
146146
}
147147
}
@@ -199,7 +199,9 @@ public DataBody set(String key, Object value) {
199199
*/
200200
public Object get(String key) {
201201
String id = flowForm.getCode() + "." + key;
202-
return this.data.get(id);
202+
DataType dataType = this.fieldTypes.get(id);
203+
Object value = this.data.get(id);
204+
return ValueConvertorContext.getInstance().convert(dataType,value);
203205
}
204206

205207
/**
@@ -218,4 +220,5 @@ public Map<String, Object> toMapData() {
218220
}
219221
}
220222

223+
221224
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codingapi.flow.form;
2+
3+
public interface IValueConvertor {
4+
5+
boolean support(DataType dataType);
6+
7+
Object getValue(Object value);
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.codingapi.flow.form;
2+
3+
import com.codingapi.flow.form.convertor.*;
4+
import lombok.Getter;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class ValueConvertorContext {
10+
11+
private final List<IValueConvertor> convertors;
12+
13+
@Getter
14+
private final static ValueConvertorContext instance = new ValueConvertorContext();
15+
16+
private ValueConvertorContext(){
17+
this.convertors = new ArrayList<>();
18+
this.init();
19+
}
20+
21+
public void addConvert(IValueConvertor valueConvertor){
22+
this.convertors.add(valueConvertor);
23+
}
24+
25+
public void clear(){
26+
this.convertors.clear();
27+
}
28+
29+
private void init(){
30+
this.convertors.add(new StringValueConvertor());
31+
this.convertors.add(new IntegerValueConvertor());
32+
this.convertors.add(new BooleanValueConvertor());
33+
this.convertors.add(new LongValueConvertor());
34+
this.convertors.add(new DoubleValueConvertor());
35+
}
36+
37+
public Object convert(DataType dataType,Object value){
38+
for (IValueConvertor convertor:this.convertors){
39+
if(convertor.support(dataType)){
40+
return convertor.getValue(value);
41+
}
42+
}
43+
return value;
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.flow.form.convertor;
2+
3+
import com.codingapi.flow.form.DataType;
4+
import com.codingapi.flow.form.IValueConvertor;
5+
6+
public class BooleanValueConvertor implements IValueConvertor {
7+
8+
@Override
9+
public boolean support(DataType dataType) {
10+
return dataType==DataType.BOOLEAN;
11+
}
12+
13+
@Override
14+
public Object getValue(Object value) {
15+
return String.valueOf(value).equals("true");
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.flow.form.convertor;
2+
3+
import com.codingapi.flow.form.DataType;
4+
import com.codingapi.flow.form.IValueConvertor;
5+
6+
public class DoubleValueConvertor implements IValueConvertor {
7+
8+
@Override
9+
public boolean support(DataType dataType) {
10+
return dataType==DataType.DOUBLE;
11+
}
12+
13+
@Override
14+
public Object getValue(Object value) {
15+
return Double.parseDouble(String.valueOf(value));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.flow.form.convertor;
2+
3+
import com.codingapi.flow.form.DataType;
4+
import com.codingapi.flow.form.IValueConvertor;
5+
6+
public class IntegerValueConvertor implements IValueConvertor {
7+
8+
@Override
9+
public boolean support(DataType dataType) {
10+
return dataType==DataType.INTEGER;
11+
}
12+
13+
@Override
14+
public Object getValue(Object value) {
15+
return Integer.parseInt(String.valueOf(value));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.flow.form.convertor;
2+
3+
import com.codingapi.flow.form.DataType;
4+
import com.codingapi.flow.form.IValueConvertor;
5+
6+
public class LongValueConvertor implements IValueConvertor {
7+
8+
@Override
9+
public boolean support(DataType dataType) {
10+
return dataType==DataType.LONG;
11+
}
12+
13+
@Override
14+
public Object getValue(Object value) {
15+
return Long.parseLong(String.valueOf(value));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingapi.flow.form.convertor;
2+
3+
import com.codingapi.flow.form.DataType;
4+
import com.codingapi.flow.form.IValueConvertor;
5+
6+
public class StringValueConvertor implements IValueConvertor {
7+
8+
@Override
9+
public boolean support(DataType dataType) {
10+
return dataType==DataType.STRING;
11+
}
12+
13+
@Override
14+
public Object getValue(Object value) {
15+
return String.valueOf(value);
16+
}
17+
}

flow-engine-framework/src/test/java/com/codingapi/flow/form/FormDataTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void dataTest() {
1818
.name("请假流程")
1919
.code("leave")
2020
.addField("请假人", "name", DataType.STRING)
21-
.addField("请假天数", "days", DataType.NUMBER)
21+
.addField("请假天数", "days", DataType.INTEGER)
2222
.addField("请假事由", "reason", DataType.STRING)
2323
.addSubForm(FlowFormBuilder.builder()
2424
.name("审批记录")

0 commit comments

Comments
 (0)