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 package com.mindtree.techworks.insight.preferences.model;
26
27 import java.io.Serializable;
28 import java.util.HashMap;
29 import java.util.Iterator;
30
31
32
33
34
35
36
37
38
39
40 public class Preference implements Serializable {
41
42
43
44
45 private static final long serialVersionUID = -9070247330487678727L;
46
47
48
49
50 public static final String ID_SEPERATOR = "@";
51
52
53
54
55 private String id;
56
57
58
59
60 private String name;
61
62
63
64
65
66
67
68
69
70
71 private boolean isUserModifiable;
72
73
74
75
76
77 private Preference parentPreference;
78
79
80
81
82 private HashMap childPreferences;
83
84
85
86
87 private HashMap preferenceAttributes;
88
89
90
91
92 private String displayClass;
93
94
95
96
97
98 private boolean isAggregated;
99
100
101
102
103
104 private transient PreferenceModelManager preferenceModelManager;
105
106
107
108
109
110
111
112
113 public Preference (String id, boolean isUserModifiable, boolean isAggregated) {
114
115 this.id = id;
116 this.isUserModifiable = isUserModifiable;
117 this.isAggregated = isAggregated;
118 preferenceAttributes = new HashMap();
119 }
120
121
122
123
124
125
126
127
128
129 public Preference (String id, boolean isUserModifiable,
130 boolean isAggregated, String name) {
131
132 this(id, isUserModifiable, isAggregated);
133 this.name = name;
134 }
135
136
137
138
139
140 public boolean isAggregated () {
141
142 return isAggregated;
143 }
144
145
146
147
148 public String getId () {
149
150 return id;
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164 public String getCompleteId () {
165
166 if (null == parentPreference) {
167 return id;
168 } else {
169 return parentPreference.getCompleteId() + ID_SEPERATOR + id;
170 }
171 }
172
173
174
175
176 public boolean isUserModifiable () {
177
178 return isUserModifiable;
179 }
180
181
182
183
184 public String getName () {
185
186 return name;
187 }
188
189
190
191
192
193
194
195
196
197
198
199 public boolean addPreferenceAttribute (
200 PreferenceAttribute preferenceAttribute) {
201
202 if (preferenceAttributes.containsKey(preferenceAttribute.getId())) {
203 return false;
204 } else if (!this.equals(preferenceAttribute.getParent())) {
205 return false;
206 } else {
207 preferenceAttributes.put(preferenceAttribute.getId(),
208 preferenceAttribute);
209 if (null != preferenceModelManager) {
210 if (null != preferenceModelManager) {
211 preferenceModelManager.preferenceAttributeChanged(this.getCompleteId(), preferenceAttribute.getId(), PreferenceModelManager.OPERATION_PREF_ATTR_ADDED);
212 }
213 }
214 return true;
215 }
216 }
217
218
219
220
221
222
223
224
225
226 public boolean registerPreferenceModelManager (
227 PreferenceModelManager manager) {
228
229 if (null == preferenceModelManager) {
230 preferenceModelManager = manager;
231
232 if (null != childPreferences) {
233 Iterator childPreferenceItr = childPreferences.values()
234 .iterator();
235 while (childPreferenceItr.hasNext()) {
236 Preference childPreference = (Preference) childPreferenceItr
237 .next();
238 childPreference.registerPreferenceModelManager(manager);
239 }
240 }
241 return true;
242 } else {
243 return false;
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 public boolean deregisterPreferenceModelManager (
257 PreferenceModelManager manager) {
258
259 if (null != preferenceModelManager
260 && preferenceModelManager.equals(manager)) {
261 preferenceModelManager = null;
262
263 if (null != childPreferences) {
264 Iterator childPreferenceItr = childPreferences.values()
265 .iterator();
266 while (childPreferenceItr.hasNext()) {
267 Preference childPreference = (Preference) childPreferenceItr
268 .next();
269 childPreference.deregisterPreferenceModelManager(manager);
270 }
271 }
272 return true;
273 } else {
274 return false;
275 }
276 }
277
278
279
280
281
282
283
284 void notifyAttributeValueChange (String attributeId, String newValue) {
285
286
287 if (null != preferenceModelManager
288 && preferenceAttributes.containsKey(attributeId)) {
289 preferenceModelManager.attributeValueChanged(getCompleteId(),
290 attributeId, newValue);
291 }
292 }
293
294
295
296
297
298
299
300
301
302 public PreferenceAttribute getPreferenceAttributeById (String attributeId) {
303
304 if (preferenceAttributes.containsKey(attributeId)) {
305 return (PreferenceAttribute) preferenceAttributes.get(attributeId);
306 } else if (null != parentPreference) {
307 return parentPreference.getPreferenceAttributeById(attributeId);
308 } else {
309 throw new ArrayIndexOutOfBoundsException(
310 "Preference Attribute not present");
311 }
312 }
313
314
315
316
317
318
319
320
321
322 public boolean isPreferenceAttributePresent (String attributeId) {
323
324 if (preferenceAttributes.containsKey(attributeId)) {
325 return true;
326 } else if (null != parentPreference) {
327 return parentPreference.isPreferenceAttributePresent(attributeId);
328 } else {
329 return false;
330 }
331 }
332
333
334
335
336
337
338 public Iterator iteratePreferenceAttributes () {
339
340 return preferenceAttributes.values().iterator();
341 }
342
343
344
345
346
347
348
349 public Iterator iteratePreferenceAttributeIds () {
350
351 return preferenceAttributes.keySet().iterator();
352 }
353
354
355
356
357
358
359
360
361
362 public Preference getPreferenceById (String preferenceId) {
363
364 if (null != childPreferences) {
365 if (childPreferences.containsKey(preferenceId)) {
366 return (Preference) childPreferences.get(preferenceId);
367 } else if (preferenceId.indexOf(ID_SEPERATOR) > 0) {
368 int parentLocation = preferenceId.indexOf(ID_SEPERATOR);
369 String parentKey = preferenceId.substring(0, parentLocation);
370
371 if (childPreferences.containsKey(parentKey)) {
372 return ((Preference) childPreferences.get(parentKey)).getPreferenceById(preferenceId.substring(parentLocation + 1));
373 } else {
374 throw new ArrayIndexOutOfBoundsException("Preference not present");
375 }
376 } else {
377 throw new ArrayIndexOutOfBoundsException("Preference not present");
378 }
379 } else {
380 throw new ArrayIndexOutOfBoundsException("Preference not present");
381 }
382 }
383
384
385
386
387
388
389
390
391
392 public boolean isPreferencePresent (String preferenceId) {
393
394 if (null != childPreferences) {
395 return childPreferences.containsKey(preferenceId);
396 } else {
397 return false;
398 }
399
400 }
401
402
403
404
405
406
407
408 public Iterator iterateChildPreferences () {
409
410 if (null != childPreferences) {
411 return childPreferences.values().iterator();
412 } else {
413 return null;
414 }
415 }
416
417
418
419
420
421
422
423 public Iterator iterateChildPreferenceIds () {
424
425 if (null != childPreferences) {
426 return childPreferences.keySet().iterator();
427 } else {
428 return null;
429 }
430 }
431
432
433
434
435
436
437
438 public PreferenceInfo getPreferenceInfo () {
439
440 PreferenceInfo preferenceInfo = new PreferenceInfo();
441 preferenceInfo.setId(this.id);
442 preferenceInfo.setName(this.name);
443 preferenceInfo.setUserModifiable(this.isUserModifiable);
444 preferenceInfo.setAggregated(this.isAggregated);
445 preferenceInfo.setCompleteId(getCompleteId());
446
447
448 if (null != childPreferences) {
449 for (Iterator childPrefItr = childPreferences.values().iterator(); childPrefItr
450 .hasNext();) {
451 preferenceInfo
452 .addChildPreferenceInfo(((Preference) childPrefItr
453 .next()).getPreferenceInfo());
454 }
455 }
456
457 return preferenceInfo;
458 }
459
460
461
462
463 public String getDisplayClass () {
464
465 return displayClass;
466 }
467
468
469
470
471 public void setDisplayClass (String displayClass) {
472
473 this.displayClass = displayClass;
474 }
475
476
477
478
479
480
481 protected void setParent (Preference parentPreference) {
482
483 this.parentPreference = parentPreference;
484 }
485
486
487
488
489
490
491
492
493 public boolean addChildPreference (Preference preference) {
494
495 if (null == childPreferences) {
496 childPreferences = new HashMap();
497 }
498
499 if (childPreferences.containsKey(preference.getId())) {
500 return false;
501 } else {
502 childPreferences.put(preference.getId(), preference);
503 preference.setParent(this);
504 if (null != preferenceModelManager) {
505 preference
506 .registerPreferenceModelManager(preferenceModelManager);
507
508 preferenceModelManager.childPreferenceChanged(this
509 .getCompleteId(), preference.getId(),
510 PreferenceModelManager.OPERATION_CHILD_PREF_ADDED);
511 }
512 return true;
513 }
514 }
515
516
517
518
519
520
521 public boolean removePreference(Preference preference) {
522 if (preference.parentPreference == this) {
523 if (null != childPreferences && childPreferences.containsKey(preference.getId())) {
524 childPreferences.remove(preference.getId());
525 preference.deregisterPreferenceModelManager(this.preferenceModelManager);
526 preference.parentPreference = null;
527
528 if (null != preferenceModelManager) {
529 preferenceModelManager
530 .childPreferenceChanged(
531 this.getCompleteId(),
532 preference.getId(),
533 PreferenceModelManager.OPERATION_CHILD_PREF_REMOVED);
534 }
535 return true;
536 } else {
537 return false;
538 }
539 } else {
540 return false;
541 }
542 }
543
544
545
546
547
548
549 public boolean removePreferenceAttribute(PreferenceAttribute preferenceAttribute) {
550 if (preferenceAttribute.getParent().equals(this) && this.preferenceAttributes.containsValue(preferenceAttribute)) {
551 this.preferenceAttributes.remove(preferenceAttribute.getId());
552 if (null != preferenceModelManager) {
553 preferenceModelManager.preferenceAttributeChanged(this.getCompleteId(), preferenceAttribute.getId(), PreferenceModelManager.OPERATION_PREF_ATTR_REMOVED);
554 }
555 return true;
556 } else {
557 return false;
558 }
559 }
560
561
562
563 public void setName(String name) {
564 this.name = name;
565 }
566 }