-
Notifications
You must be signed in to change notification settings - Fork 5
/
CarBean.java
142 lines (114 loc) · 3.15 KB
/
CarBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.genericsystem.examplejsf.bean;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.genericsystem.carcolor.model.Car;
import org.genericsystem.carcolor.model.CarColor;
import org.genericsystem.carcolor.model.Color;
import org.genericsystem.carcolor.model.Power;
import org.genericsystem.cdi.Engine;
import org.genericsystem.common.Generic;
@Named
@RequestScoped
public class CarBean {
@Inject
private Engine engine;
private Generic car;
private Generic power;
private Generic color;
private Generic carColor;
private String newCarName;
private Integer newCarPower;
@PostConstruct
public void init() {
car = engine.find(Car.class);
power = engine.find(Power.class);
color = engine.find(Color.class);
carColor = engine.find(CarColor.class);
color.setInstance("Blue");
color.setInstance("Orange");
color.setInstance("White");
color.setInstance("Yellow");
color.setInstance("red");
// runScript(engine);
}
public List<Generic> getCars() {
return car.getSubInstances().stream().collect(Collectors.toList());
}
public ValueExpressionWrapper getPower(Generic instance) {
return new ValueExpressionWrapper() {
@Override
public String getValue() {
// Power is a property constraint
return Objects.toString(instance.getValues(power).first());
}
@Override
public void setValue(String value) {
// The value power must be an integer due the InstanceValueClassConstraint
instance.setHolder(power, Integer.parseInt(value));
}
};
}
public ValueExpressionWrapper getColor(Generic instance) {
return new ValueExpressionWrapper() {
@Override
public void setValue(String value) {
// The value color is a string to convert in Generic
Generic searchedColor = color.setInstance(value);
instance.setLink(carColor, "link", searchedColor);
}
@Override
public String getValue() {
Generic link = instance.getLinks(carColor).first();
return (link != null) ? (String) link.getTargetComponent().getValue() : null;
}
};
}
public String addCar() {
car.setInstance(newCarName).setHolder(power, newCarPower);
return "#";
}
public String update() {
return "#";
}
public String deleteCar(Generic car) {
car.remove();
return "#";
}
public String flush() {
engine.getCurrentCache().flush();
return "#";
}
public String clear() {
engine.getCurrentCache().clear();
return "#";
}
public static interface ValueExpressionWrapper {
public String getValue();
public void setValue(String value);
}
public String getNewCarName() {
return newCarName;
}
public void setNewCarName(String newCarName) {
this.newCarName = newCarName;
}
public Integer getNewCarPower() {
return newCarPower;
}
public void setNewCarPower(Integer newCarPower) {
this.newCarPower = newCarPower;
}
// void runScript(Root engine) {
// color.setInstance("Blue");
// color.setInstance("Orange");
// color.setInstance("White");
// color.setInstance("Yellow");
// color.setInstance("red");
//
// }
}