|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.ericmignot.modeler.data; |
|
19 |
| |
|
20 |
| import java.util.ArrayList; |
|
21 |
| import java.util.HashMap; |
|
22 |
| import java.util.Iterator; |
|
23 |
| import java.util.List; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class Model |
|
32 |
| { |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| protected Map categories; |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| protected List listeners; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
168
| public Model()
|
|
52 |
| { |
|
53 |
168
| this.clearModel();
|
|
54 |
168
| this.listeners = new ArrayList();
|
|
55 |
| } |
|
56 |
| |
|
57 |
171
| public void clearModel()
|
|
58 |
| { |
|
59 |
171
| this.categories = new HashMap();
|
|
60 |
| } |
|
61 |
1
| public String getXML()
|
|
62 |
| { |
|
63 |
1
| String toReturn = "<BusinessObjects>";
|
|
64 |
1
| BusinessObject[] bos = getSortedBusinessObjects();
|
|
65 |
1
| for (int i=0; i < bos.length; i++)
|
|
66 |
| { |
|
67 |
3
| if (bos[i] instanceof Link)
|
|
68 |
| { |
|
69 |
1
| toReturn += this.adjustLinkXML((Link) bos[i]);
|
|
70 |
| } |
|
71 |
| else |
|
72 |
| { |
|
73 |
2
| toReturn += bos[i].getXML();
|
|
74 |
| } |
|
75 |
| } |
|
76 |
1
| toReturn += "</BusinessObjects>";
|
|
77 |
1
| return toReturn;
|
|
78 |
| } |
|
79 |
| |
|
80 |
2
| protected String adjustLinkXML(Link link)
|
|
81 |
| { |
|
82 |
2
| String xml = link.getXML();
|
|
83 |
2
| String toBeReplaced = link.getReferenceEndXML();
|
|
84 |
| |
|
85 |
2
| BusinessObject linkEnd = this.getRealLinkEnd(link);
|
|
86 |
2
| String replaceBy = "<Reference Name='End' Type='"
|
|
87 |
| + linkEnd.getClass().getName() |
|
88 |
| + "' Id='" |
|
89 |
| + linkEnd.getId() |
|
90 |
| + "' />"; |
|
91 |
| |
|
92 |
2
| return xml.replaceAll(toBeReplaced, replaceBy);
|
|
93 |
| } |
|
94 |
| |
|
95 |
10
| public BusinessObject[] getBusinessObjects()
|
|
96 |
| { |
|
97 |
10
| List bos = new ArrayList();
|
|
98 |
10
| Iterator iterator = this.categories.keySet().iterator();
|
|
99 |
10
| while (iterator.hasNext())
|
|
100 |
| { |
|
101 |
17
| String category = (String) iterator.next();
|
|
102 |
17
| for (int i=0; i < this.getBusinessObjectCount(category); i++)
|
|
103 |
| { |
|
104 |
34
| BusinessObject bo = this.getBusinessObjectByIndex(category, i);
|
|
105 |
34
| if (this.isBusinessObjectPublic(bo))
|
|
106 |
| { |
|
107 |
30
| bos.add(bo);
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| } |
|
111 |
10
| return (BusinessObject[]) bos.toArray(new BusinessObject[0]);
|
|
112 |
| } |
|
113 |
| |
|
114 |
38
| protected boolean isBusinessObjectPublic(BusinessObject bo)
|
|
115 |
| { |
|
116 |
38
| if (bo instanceof SimplePoint)
|
|
117 |
| { |
|
118 |
3
| return false;
|
|
119 |
| } |
|
120 |
35
| if (bo instanceof Link)
|
|
121 |
| { |
|
122 |
10
| Link link = (Link) bo;
|
|
123 |
10
| if (link.getLinkStart() == null)
|
|
124 |
| { |
|
125 |
1
| return false;
|
|
126 |
| } |
|
127 |
9
| if (link.getLinkStart() instanceof SimplePoint)
|
|
128 |
| { |
|
129 |
2
| return false;
|
|
130 |
| } |
|
131 |
| } |
|
132 |
32
| return true;
|
|
133 |
| } |
|
134 |
| |
|
135 |
7
| public BusinessObject[] getSortedBusinessObjects()
|
|
136 |
| { |
|
137 |
7
| BusinessObject[] bos = this.getBusinessObjects();
|
|
138 |
7
| for (int i=0; i <bos.length; i++)
|
|
139 |
| { |
|
140 |
22
| for (int j=i+1; j<bos.length; j++)
|
|
141 |
| { |
|
142 |
26
| if (this.getReferencingBos(bos[j]).contains(bos[i]))
|
|
143 |
| { |
|
144 |
5
| BusinessObject tmp = bos[j];
|
|
145 |
5
| bos[j] = bos[i];
|
|
146 |
5
| bos[i] = tmp;
|
|
147 |
| } |
|
148 |
| } |
|
149 |
| } |
|
150 |
7
| return bos;
|
|
151 |
| } |
|
152 |
| |
|
153 |
93
| public List getReferencingBos(BusinessObject bo)
|
|
154 |
| { |
|
155 |
93
| List toReturn = new ArrayList();
|
|
156 |
93
| Iterator iterator = this.categories.values().iterator();
|
|
157 |
93
| while (iterator.hasNext())
|
|
158 |
| { |
|
159 |
185
| List bos = (List) iterator.next();
|
|
160 |
185
| for (int i = 0; i < bos.size(); i ++)
|
|
161 |
| { |
|
162 |
272
| BusinessObject current = (BusinessObject) bos.get(i);
|
|
163 |
272
| if (current.hasReference(bo))
|
|
164 |
| { |
|
165 |
52
| toReturn.add(current);
|
|
166 |
| } |
|
167 |
| } |
|
168 |
| } |
|
169 |
93
| return toReturn;
|
|
170 |
| } |
|
171 |
6
| public List getLinksFromBusinessObject(BusinessObject bo)
|
|
172 |
| { |
|
173 |
6
| List toReturn = new ArrayList();
|
|
174 |
6
| List references = this.getReferencingBos(bo);
|
|
175 |
6
| for (int i=0; i<references.size(); i++)
|
|
176 |
| { |
|
177 |
11
| BusinessObject reference = (BusinessObject) references.get(i);
|
|
178 |
11
| if (reference instanceof Link
|
|
179 |
| && ((Link) reference).getLinkStart()==bo |
|
180 |
| ) |
|
181 |
| { |
|
182 |
6
| toReturn.add(reference);
|
|
183 |
| } |
|
184 |
| } |
|
185 |
6
| return toReturn;
|
|
186 |
| } |
|
187 |
5
| public List getLinksToBusinessObject(BusinessObject bo)
|
|
188 |
| { |
|
189 |
5
| List toReturn = new ArrayList();
|
|
190 |
5
| List references = this.getReferencingBos(bo);
|
|
191 |
5
| for (int i=0; i<references.size(); i++)
|
|
192 |
| { |
|
193 |
9
| BusinessObject reference = (BusinessObject) references.get(i);
|
|
194 |
9
| if (reference instanceof Link
|
|
195 |
| && ((Link) reference).getLinkEnd()==bo |
|
196 |
| ) |
|
197 |
| { |
|
198 |
5
| toReturn.add(reference);
|
|
199 |
| } |
|
200 |
| } |
|
201 |
5
| return toReturn;
|
|
202 |
| } |
|
203 |
4
| protected BusinessObject getRealLinkEnd(Link link)
|
|
204 |
| { |
|
205 |
4
| BusinessObject linkEnd = link.getLinkEnd();
|
|
206 |
4
| while (linkEnd instanceof SimplePoint)
|
|
207 |
| { |
|
208 |
5
| List references = this.getLinksFromBusinessObject(linkEnd);
|
|
209 |
5
| linkEnd = ((Link) references.get(0)).getLinkEnd();
|
|
210 |
| } |
|
211 |
4
| return linkEnd;
|
|
212 |
| } |
|
213 |
1
| protected BusinessObject getRealLinkStart(Link link)
|
|
214 |
| { |
|
215 |
1
| BusinessObject linkStart = link.getLinkStart();
|
|
216 |
1
| while (linkStart instanceof SimplePoint)
|
|
217 |
| { |
|
218 |
1
| List references = this.getLinksToBusinessObject(linkStart);
|
|
219 |
1
| linkStart = ((Link) references.get(0)).getLinkStart();
|
|
220 |
| } |
|
221 |
1
| return linkStart;
|
|
222 |
| } |
|
223 |
23
| public Link getRootLink(Link link)
|
|
224 |
| { |
|
225 |
23
| Link toReturn = link;
|
|
226 |
23
| while (toReturn.getLinkStart() instanceof SimplePoint)
|
|
227 |
| { |
|
228 |
3
| List references = this.getLinksToBusinessObject(toReturn.getLinkStart());
|
|
229 |
3
| toReturn = (Link) references.get(0);
|
|
230 |
| } |
|
231 |
23
| return toReturn;
|
|
232 |
| } |
|
233 |
| |
|
234 |
411
| public void addBusinessObject(BusinessObject bo)
|
|
235 |
| { |
|
236 |
411
| this.addBusinessObject(bo.getClass().getName(), bo);
|
|
237 |
| } |
|
238 |
416
| public void addBusinessObject(String category, BusinessObject bo)
|
|
239 |
| { |
|
240 |
416
| if (this.categories.containsKey(category))
|
|
241 |
| { |
|
242 |
178
| List bos = (List) this.categories.get(category);
|
|
243 |
178
| bos.add(bo);
|
|
244 |
| } |
|
245 |
| else |
|
246 |
| { |
|
247 |
238
| List bos = new ArrayList();
|
|
248 |
238
| bos.add(bo);
|
|
249 |
238
| this.categories.put(category, bos);
|
|
250 |
| } |
|
251 |
416
| this.fireAdded(bo);
|
|
252 |
| } |
|
253 |
| |
|
254 |
48
| public List removeBusinessObject(BusinessObject bo)
|
|
255 |
| { |
|
256 |
48
| return this.removeBusinessObject(bo.getClass().getName(), bo);
|
|
257 |
| } |
|
258 |
51
| protected List removeBusinessObject(String category, BusinessObject bo)
|
|
259 |
| { |
|
260 |
51
| if (this.categories.containsKey(category))
|
|
261 |
| { |
|
262 |
51
| List removedBos = new ArrayList();
|
|
263 |
51
| removedBos.add(bo);
|
|
264 |
| |
|
265 |
51
| List bos = (List) this.categories.get(category);
|
|
266 |
51
| bos.remove(bo);
|
|
267 |
51
| List referencingBos = this.getReferencingBos(bo);
|
|
268 |
51
| for (int i = 0; i<referencingBos.size(); i++)
|
|
269 |
| { |
|
270 |
13
| List removed = this.removeBusinessObject((BusinessObject) referencingBos.get(i));
|
|
271 |
13
| removedBos.addAll(removed);
|
|
272 |
| } |
|
273 |
| |
|
274 |
51
| this.fireRemoved(bo);
|
|
275 |
51
| return removedBos;
|
|
276 |
| } |
|
277 |
| else |
|
278 |
| { |
|
279 |
0
| return new ArrayList();
|
|
280 |
| } |
|
281 |
| } |
|
282 |
3
| public List removeBusinessObjectQuiet(BusinessObject bo)
|
|
283 |
| { |
|
284 |
3
| return this.removeBusinessObjectQuiet(bo.getClass().getName(), bo);
|
|
285 |
| } |
|
286 |
3
| protected List removeBusinessObjectQuiet(String category, BusinessObject bo)
|
|
287 |
| { |
|
288 |
3
| if (this.categories.containsKey(category))
|
|
289 |
| { |
|
290 |
3
| List removedBos = new ArrayList();
|
|
291 |
3
| removedBos.add(bo);
|
|
292 |
3
| List bos = (List) this.categories.get(category);
|
|
293 |
3
| bos.remove(bo);
|
|
294 |
3
| this.fireRemoved(bo);
|
|
295 |
3
| return removedBos;
|
|
296 |
| } |
|
297 |
| else |
|
298 |
| { |
|
299 |
0
| return new ArrayList();
|
|
300 |
| } |
|
301 |
| } |
|
302 |
| |
|
303 |
6
| public int getBusinessObjectCount(Class className)
|
|
304 |
| { |
|
305 |
6
| return this.getBusinessObjectCount(className.getName());
|
|
306 |
| } |
|
307 |
118
| public int getBusinessObjectCount(String category)
|
|
308 |
| { |
|
309 |
118
| if (this.categories.containsKey(category))
|
|
310 |
| { |
|
311 |
117
| List bos = (List) this.categories.get(category);
|
|
312 |
117
| return bos.size();
|
|
313 |
| } |
|
314 |
| else |
|
315 |
| { |
|
316 |
1
| return 0;
|
|
317 |
| } |
|
318 |
| } |
|
319 |
| |
|
320 |
4
| public BusinessObject getBusinessObjectByIndex(Class className, int index)
|
|
321 |
| { |
|
322 |
4
| return this.getBusinessObjectByIndex(className.getName(), index);
|
|
323 |
| } |
|
324 |
53
| public BusinessObject getBusinessObjectByIndex(String category, int index)
|
|
325 |
| { |
|
326 |
53
| if (this.categories.containsKey(category))
|
|
327 |
| { |
|
328 |
53
| List bos = (List) this.categories.get(category);
|
|
329 |
53
| return (BusinessObject) bos.get(index);
|
|
330 |
| } |
|
331 |
| else |
|
332 |
| { |
|
333 |
0
| return null;
|
|
334 |
| } |
|
335 |
| } |
|
336 |
| |
|
337 |
73
| public BusinessObject getBusinessObjectById(String category, long id)
|
|
338 |
| { |
|
339 |
73
| if (this.categories.containsKey(category))
|
|
340 |
| { |
|
341 |
72
| List bos = (List) this.categories.get(category);
|
|
342 |
72
| BusinessObject toReturn = null;
|
|
343 |
108
| for (int i = 0; i < bos.size(); i ++)
|
|
344 |
| { |
|
345 |
108
| BusinessObject current = (BusinessObject) bos.get(i);
|
|
346 |
108
| if (current.getId() == id)
|
|
347 |
| { |
|
348 |
72
| toReturn = current;
|
|
349 |
72
| break;
|
|
350 |
| } |
|
351 |
| } |
|
352 |
72
| return toReturn;
|
|
353 |
| } |
|
354 |
| else |
|
355 |
| { |
|
356 |
1
| return null;
|
|
357 |
| } |
|
358 |
| } |
|
359 |
| |
|
360 |
115
| public void register(ModelListener listener)
|
|
361 |
| { |
|
362 |
115
| this.listeners.add(listener);
|
|
363 |
| } |
|
364 |
1
| public void unregister(ModelListener listener)
|
|
365 |
| { |
|
366 |
1
| this.listeners.remove(listener);
|
|
367 |
| } |
|
368 |
| |
|
369 |
416
| protected void fireAdded(BusinessObject bo)
|
|
370 |
| { |
|
371 |
416
| for (int i = 0; i < this.listeners.size() ; i++)
|
|
372 |
| { |
|
373 |
233
| ((ModelListener) this.listeners.get(i)).businessObjectAdded(bo);
|
|
374 |
| } |
|
375 |
| } |
|
376 |
54
| protected void fireRemoved(BusinessObject bo)
|
|
377 |
| { |
|
378 |
54
| for (int i = 0; i < this.listeners.size() ; i++)
|
|
379 |
| { |
|
380 |
15
| ((ModelListener) this.listeners.get(i)).businessObjectRemoved(bo);
|
|
381 |
| } |
|
382 |
| } |
|
383 |
| } |