1 | |
package org.jtheque.core.managers.module; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.module.beans.ModuleContainer; |
20 | |
import org.jtheque.core.managers.module.beans.ModuleState; |
21 | |
import org.jtheque.core.managers.state.AbstractState; |
22 | |
import org.jtheque.core.managers.state.NodeState; |
23 | |
import org.jtheque.core.managers.update.InstallationResult; |
24 | |
import org.jtheque.utils.StringUtils; |
25 | |
|
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collection; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public final class ModuleConfiguration extends AbstractState { |
35 | 0 | private final Collection<ModuleInfo> infos = new ArrayList<ModuleInfo>(20); |
36 | |
|
37 | |
private boolean discovery; |
38 | |
|
39 | |
@Override |
40 | |
public boolean isDelegated() { |
41 | 0 | return true; |
42 | |
} |
43 | |
|
44 | |
@Override |
45 | |
public void delegateLoad(Collection<NodeState> nodes) { |
46 | 0 | for (NodeState node : nodes){ |
47 | 0 | if ("module".equals(node.getName())) { |
48 | 0 | infos.add(convertToModuleInfo(node)); |
49 | 0 | } else if("discovery".equals(node.getName())){ |
50 | 0 | discovery = true; |
51 | |
} |
52 | |
} |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private static ModuleInfo convertToModuleInfo(NodeState node) { |
63 | 0 | ModuleInfo info = new ModuleInfo(node.getAttributeValue("id")); |
64 | |
|
65 | 0 | if(StringUtils.isNotEmpty(node.getAttributeValue("state"))){ |
66 | 0 | info.setState(ModuleState.valueOf(node.getIntAttributeValue("state"))); |
67 | |
} else { |
68 | 0 | for (NodeState child : node.getChildrens()) { |
69 | 0 | if ("state".equals(child.getName())) { |
70 | 0 | info.setState(ModuleState.valueOf(Integer.parseInt(child.getText()))); |
71 | |
} |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | if(info.getState() == null){ |
76 | 0 | info.setState(ModuleState.INSTALLED); |
77 | |
} |
78 | |
|
79 | 0 | return info; |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public Collection<NodeState> delegateSave() { |
84 | 0 | Collection<NodeState> states = new ArrayList<NodeState>(25); |
85 | |
|
86 | 0 | if(discovery){ |
87 | 0 | states.add(new NodeState("discovery")); |
88 | |
} |
89 | |
|
90 | 0 | for (ModuleInfo info : infos) { |
91 | 0 | states.add(convertToNodeState(info)); |
92 | |
} |
93 | |
|
94 | 0 | return states; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private static NodeState convertToNodeState(ModuleInfo info) { |
105 | 0 | NodeState state = new NodeState("module"); |
106 | |
|
107 | 0 | state.setAttribute("id", info.getModuleId()); |
108 | 0 | state.setAttribute("state", Integer.toString(info.getState().ordinal())); |
109 | |
|
110 | 0 | return state; |
111 | |
} |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
ModuleInfo getModuleInfo(String moduleName) { |
121 | 0 | for (ModuleInfo i : infos) { |
122 | 0 | if (i.getModuleId().equals(moduleName)) { |
123 | 0 | return i; |
124 | |
} |
125 | |
} |
126 | |
|
127 | 0 | return null; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public boolean isDiscovery(){ |
136 | 0 | return discovery; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public ModuleState getState(String moduleName) { |
146 | 0 | ModuleInfo info = getModuleInfo(moduleName); |
147 | |
|
148 | 0 | if (info != null) { |
149 | 0 | return info.getState(); |
150 | |
} |
151 | |
|
152 | 0 | return null; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public void setState(String moduleName, ModuleState state) { |
162 | 0 | ModuleInfo info = getModuleInfo(moduleName); |
163 | |
|
164 | 0 | if (info != null) { |
165 | 0 | info.setState(state); |
166 | |
} |
167 | 0 | } |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public boolean containsModule(ModuleContainer module) { |
176 | 0 | for (ModuleInfo i : infos) { |
177 | 0 | if (i.getModuleId().equals(module.getId())) { |
178 | 0 | return true; |
179 | |
} |
180 | |
} |
181 | |
|
182 | 0 | return false; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public void remove(ModuleContainer module) { |
191 | 0 | ModuleInfo info = getModuleInfo(module.getId()); |
192 | |
|
193 | 0 | infos.remove(info); |
194 | 0 | } |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public void add(ModuleContainer module) { |
202 | 0 | add(module.getId(), module.getState()); |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
public void add(ModuleContainer module, ModuleState state) { |
212 | 0 | add(module.getId(), state); |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public void add(InstallationResult result) { |
221 | 0 | add(result.getName(), ModuleState.INSTALLED); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
private void add(String id, ModuleState state) { |
231 | 0 | ModuleInfo info = new ModuleInfo(id); |
232 | |
|
233 | 0 | info.setState(state); |
234 | |
|
235 | 0 | infos.add(info); |
236 | 0 | } |
237 | |
} |