KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > beans > session > SystemConfigurationBean


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.beans.session;
20
21 import java.io.*;
22 import java.net.*;
23 import java.rmi.*;
24 import java.sql.*;
25 import java.util.*;
26 import javax.ejb.*;
27 import javax.naming.*;
28 import javax.rmi.*;
29 import javax.sql.*;
30
31 import cowsultants.itracker.ejb.beans.entity.*;
32 import cowsultants.itracker.ejb.util.*;
33 import cowsultants.itracker.ejb.client.exceptions.*;
34 import cowsultants.itracker.ejb.client.interfaces.*;
35 import cowsultants.itracker.ejb.client.models.*;
36 import cowsultants.itracker.ejb.client.resources.*;
37 import cowsultants.itracker.ejb.client.util.*;
38
39 /**
40   * Implimetation of the SystemConfiguration Interface.
41   * @see SystemConfiguration
42   */

43 public class SystemConfigurationBean implements SessionBean {
44     private static Properties props = null;
45
46     InitialContext ic = null;
47
48     IDGeneratorHome idHome = null;
49     ConfigurationLocalHome cHome = null;
50     CustomFieldLocalHome cfHome = null;
51     CustomFieldValueLocalHome cfvHome = null;
52     LanguageLocalHome lHome = null;
53
54     public String JavaDoc getProperty(String JavaDoc name) {
55         return props.getProperty(name);
56     }
57
58     public String JavaDoc getProperty(String JavaDoc name, String JavaDoc defaultValue) {
59         return props.getProperty(name, defaultValue);
60     }
61
62     public boolean getBooleanProperty(String JavaDoc name, boolean defaultValue) {
63         String JavaDoc value = props.getProperty(name);
64         return (value == null ? defaultValue : new Boolean JavaDoc(value).booleanValue());
65     }
66
67     public int getIntegerProperty(String JavaDoc name, int defaultValue) {
68         String JavaDoc value = props.getProperty(name);
69         try {
70             return (value == null ? defaultValue : Integer.parseInt(value));
71         } catch(NumberFormatException JavaDoc nfe) {
72             return defaultValue;
73         }
74     }
75
76     public long getLongProperty(String JavaDoc name, long defaultValue) {
77         String JavaDoc value = props.getProperty(name);
78         try {
79             return (value == null ? defaultValue : Long.parseLong(value));
80         } catch(NumberFormatException JavaDoc nfe) {
81             return defaultValue;
82         }
83     }
84
85     public Properties getProperties() {
86         return props;
87     }
88
89     public ConfigurationModel getConfigurationItem(Integer JavaDoc id) {
90         try {
91             ConfigurationLocal configItem = cHome.findByPrimaryKey(id);
92             return configItem.getModel();
93         } catch(FinderException fe) {
94         }
95         return null;
96     }
97
98     public ConfigurationModel[] getConfigurationItemsByType(int type) {
99         ConfigurationModel[] items = new ConfigurationModel[0];
100
101         try {
102             Collection configItems = cHome.findByType(type);
103             items = new ConfigurationModel[configItems.size()];
104             int i = 0;
105             for(Iterator iter = configItems.iterator(); iter.hasNext(); i++) {
106                 items[i] = ((ConfigurationLocal) iter.next()).getModel();
107             }
108             Arrays.sort(items, new ConfigurationModel());
109         } catch(FinderException fe) {
110         }
111
112         return items;
113     }
114
115     public ConfigurationModel[] getConfigurationItemsByType(int type, Locale locale) {
116         ConfigurationModel[] items = getConfigurationItemsByType(type);
117         for(int i = 0; i < items.length; i++) {
118             if(items[i].getType() == SystemConfigurationUtilities.TYPE_STATUS) {
119                 items[i].setName(IssueUtilities.getStatusName(items[i].getValue(), locale));
120             } else if(items[i].getType() == SystemConfigurationUtilities.TYPE_SEVERITY) {
121                 items[i].setName(IssueUtilities.getSeverityName(items[i].getValue(), locale));
122             } else if(items[i].getType() == SystemConfigurationUtilities.TYPE_RESOLUTION) {
123                 items[i].setName(IssueUtilities.getResolutionName(items[i].getValue(), locale));
124             }
125         }
126         return items;
127     }
128
129     public ConfigurationModel createConfigurationItem(ConfigurationModel model) {
130         try {
131             IDGenerator idGen = idHome.create();
132             ConfigurationLocal configurationItem = cHome.create(idGen.getId(ConfigurationLocal.ID_NAME));
133             configurationItem.setModel(model);
134
135             return configurationItem.getModel();
136         } catch(CreateException ce) {
137             Logger.logDebug("Unable to create new configuration item.");
138         }
139
140         return null;
141     }
142
143     public ConfigurationModel updateConfigurationItem(ConfigurationModel model) {
144         try {
145             if(model != null) {
146                 ConfigurationLocal configurationItem = cHome.findByPrimaryKey(model.getId());
147                 configurationItem.setModel(model);
148                 return configurationItem.getModel();
149             }
150         } catch(FinderException fe) {
151         }
152
153         return null;
154     }
155
156     public ConfigurationModel[] updateConfigurationItems(ConfigurationModel[] models, int type) {
157         try {
158             removeConfigurationItems(type);
159
160             IDGenerator idGen = idHome.create();
161             for(int i = 0; i < models.length; i++) {
162                 ConfigurationLocal configurationItem = cHome.create(idGen.getId(ConfigurationLocal.ID_NAME));
163                 configurationItem.setModel(models[i]);
164                 models[i] = configurationItem.getModel();
165             }
166
167             Arrays.sort(models, new ConfigurationModel());
168             return models;
169         } catch(CreateException ce) {
170             Logger.logDebug("Unable to create new language item.");
171         }
172
173         return new ConfigurationModel[0];
174     }
175
176     public void removeConfigurationItem(Integer JavaDoc id) {
177         try {
178             cHome.remove(id);
179         } catch(RemoveException ce) {
180             Logger.logDebug("Unable to remove configuration item " + id + ".");
181         }
182     }
183
184     public void removeConfigurationItems(int type) {
185         try {
186             Collection currentItems = cHome.findByType(type);
187             for(Iterator iter = currentItems.iterator(); iter.hasNext(); ) {
188                 try {
189                     ConfigurationLocal config = (ConfigurationLocal) iter.next();
190
191                     config.remove();
192                 } catch(RemoveException re) {
193                 }
194             }
195         } catch(FinderException fe) {
196         }
197     }
198
199     public void removeConfigurationItems(ConfigurationModel model) {
200         try {
201             Vector currentIds = new Vector();
202             Collection currentItems = cHome.findByTypeAndValue(model.getType(), model.getValue());
203             for(Iterator iter = currentItems.iterator(); iter.hasNext(); ) {
204                 try {
205                     ((ConfigurationLocal) iter.next()).remove();
206                 } catch(RemoveException re) {
207                 }
208             }
209         } catch(FinderException fe) {
210         }
211     }
212
213     public boolean configurationItemExists(ConfigurationModel model) {
214         try {
215             if(model != null && model.getVersion() != null) {
216                 Collection configItems = cHome.findByTypeAndValue(model.getType(), model.getValue());
217                 if(configItems != null && configItems.size() > 0) {
218                     return true;
219                 }
220             }
221         } catch(FinderException fe) {
222         }
223
224         return false;
225     }
226
227     public boolean configurationItemUpToDate(ConfigurationModel model) {
228         try {
229             long currentVersion = 0;
230
231             if(model != null && model.getVersion() != null) {
232                 Collection configItems = cHome.findByTypeAndValue(model.getType(), model.getValue());
233                 for(Iterator iter = configItems.iterator(); iter.hasNext(); ) {
234                     ConfigurationLocal configItem = (ConfigurationLocal) iter.next();
235                     if(configItem != null) {
236                         currentVersion = Math.max(SystemConfigurationUtilities.getVersionAsLong(configItem.getVersion()), currentVersion);
237                     }
238                 }
239
240                 if(currentVersion >= SystemConfigurationUtilities.getVersionAsLong(model.getVersion())) {
241                     return true;
242                 }
243             }
244         } catch(FinderException fe) {
245         }
246
247         return false;
248     }
249
250     public void resetConfigurationCache() {
251         IssueUtilities.setResolutions(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION));
252         IssueUtilities.setSeverities(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY));
253         IssueUtilities.setStatuses(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS));
254         IssueUtilities.setCustomFields(getCustomFields());
255     }
256
257     public void resetConfigurationCache(int type) {
258         if(type == SystemConfigurationUtilities.TYPE_RESOLUTION) {
259             IssueUtilities.setResolutions(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION));
260         } else if(type == SystemConfigurationUtilities.TYPE_SEVERITY) {
261             IssueUtilities.setSeverities(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY));
262         } else if(type == SystemConfigurationUtilities.TYPE_STATUS) {
263             IssueUtilities.setStatuses(getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS));
264         } else if(type == SystemConfigurationUtilities.TYPE_CUSTOMFIELD) {
265             IssueUtilities.setCustomFields(getCustomFields());
266         }
267     }
268
269     public CustomFieldModel getCustomField(Integer JavaDoc id) {
270         try {
271             CustomFieldLocal customField = cfHome.findByPrimaryKey(id);
272             return customField.getModel();
273         } catch(FinderException fe) {
274         }
275         return null;
276     }
277
278     public CustomFieldModel[] getCustomFields() {
279         CustomFieldModel[] fields = new CustomFieldModel[0];
280
281         try {
282             Collection customFields = cfHome.findAll();
283             fields = new CustomFieldModel[customFields.size()];
284             int i = 0;
285             for(Iterator iter = customFields.iterator(); iter.hasNext(); i++) {
286                 fields[i] = ((CustomFieldLocal) iter.next()).getModel();
287             }
288         } catch(FinderException fe) {
289         }
290
291         return fields;
292     }
293
294     public CustomFieldModel[] getCustomFields(Locale locale) {
295         CustomFieldModel[] fields = getCustomFields();
296         for(int i = 0; i < fields.length; i++) {
297             fields[i].setLabels(locale);
298         }
299
300         return fields;
301     }
302
303     public CustomFieldModel createCustomField(CustomFieldModel model) {
304         try {
305             IDGenerator idGen = idHome.create();
306             CustomFieldLocal customField = cfHome.create(idGen.getId(CustomFieldLocal.ID_NAME));
307             customField.setModel(model);
308             model.setId(customField.getId());
309             if(model.getOptions().length > 0) {
310                 removeCustomFieldValues(customField.getId());
311                 CustomFieldValueModel[] newOptions = model.getOptions();
312                 for(int i = 0; i < newOptions.length; i++) {
313                     newOptions[i].setCustomFieldId(customField.getId());
314                     createCustomFieldValue(newOptions[i]);
315                 }
316             }
317
318             return customField.getModel();
319         } catch(CreateException ce) {
320             Logger.logDebug("Unable to create new custom field.", ce);
321         }
322
323         return null;
324     }
325
326     public CustomFieldModel updateCustomField(CustomFieldModel model) {
327         try {
328             if(model != null) {
329                 CustomFieldLocal customField = cfHome.findByPrimaryKey(model.getId());
330                 customField.setModel(model);
331
332                 if(model.getOptions().length > 0) {
333                     removeCustomFieldValues(customField.getId());
334                     CustomFieldValueModel[] newOptions = model.getOptions();
335                     for(int i = 0; i < newOptions.length; i++) {
336                         createCustomFieldValue(newOptions[i]);
337                     }
338                 }
339                 return customField.getModel();
340             }
341         } catch(FinderException fe) {
342         }
343
344         return null;
345     }
346
347     public void removeCustomField(Integer JavaDoc customFieldId) {
348         try {
349             CustomFieldLocal customField = cfHome.findByPrimaryKey(customFieldId);
350             customField.remove();
351         } catch(FinderException fe) {
352         } catch(RemoveException re) {
353         }
354     }
355
356     public CustomFieldValueModel getCustomFieldValue(Integer JavaDoc id) {
357         try {
358             CustomFieldValueLocal customFieldValue = cfvHome.findByPrimaryKey(id);
359             return customFieldValue.getModel();
360         } catch(FinderException fe) {
361         }
362         return null;
363     }
364
365     public CustomFieldValueModel createCustomFieldValue(CustomFieldValueModel model) {
366         if(model == null || model.getCustomFieldId() == null) {
367             return null;
368         }
369
370         try {
371             CustomFieldLocal customField = cfHome.findByPrimaryKey(model.getCustomFieldId());
372
373             IDGenerator idGen = idHome.create();
374             CustomFieldValueLocal customFieldValue = cfvHome.create(idGen.getId(CustomFieldValueLocal.ID_NAME));
375             customFieldValue.setModel(model);
376             customFieldValue.setCustomField(customField);
377             return customFieldValue.getModel();
378         } catch(CreateException ce) {
379             Logger.logDebug("Unable to create new custom field value: " + ce.getMessage());
380         } catch(FinderException fe) {
381             Logger.logDebug("Unable to create new custom field value, custom field not found: " + fe.getMessage());
382         }
383
384         return null;
385     }
386
387     public CustomFieldValueModel updateCustomFieldValue(CustomFieldValueModel model) {
388         try {
389             if(model != null) {
390                 CustomFieldValueLocal customFieldValue = cfvHome.findByPrimaryKey(model.getId());
391                 customFieldValue.setModel(model);
392                 return customFieldValue.getModel();
393             }
394         } catch(FinderException fe) {
395         }
396
397         return null;
398     }
399
400     public CustomFieldValueModel[] updateCustomFieldValues(Integer JavaDoc customFieldId, CustomFieldValueModel[] models) {
401         CustomFieldValueModel[] values = new CustomFieldValueModel[0];
402
403         if(customFieldId != null) {
404             try {
405                 CustomFieldLocal customField = cfHome.findByPrimaryKey(customFieldId);
406                 Collection currValues = customField.getValues();
407                 for(Iterator iter = currValues.iterator(); iter.hasNext(); ) {
408                     if(models == null || models.length == 0) {
409                         try {
410                             ((CustomFieldValueLocal) iter.next()).remove();
411                         } catch(RemoveException re) {
412                         }
413                     } else {
414                         CustomFieldValueLocal value = (CustomFieldValueLocal) iter.next();
415                         for(int i = 0; i < models.length; i++) {
416                             if(value.getId().equals(models[i].getId())) {
417                                 value.setModel(models[i]);
418                                 models[i] = value.getModel();
419                                 break;
420                             }
421                         }
422                     }
423                 }
424             } catch(FinderException fe) {
425             }
426         }
427
428         Arrays.sort(models, new CustomFieldValueModel());
429         return models;
430     }
431
432     public void removeCustomFieldValue(Integer JavaDoc customFieldValueId) {
433         try {
434             CustomFieldValueLocal customFieldValue = cfvHome.findByPrimaryKey(customFieldValueId);
435             customFieldValue.remove();
436         } catch(FinderException fe) {
437         } catch(RemoveException re) {
438         }
439     }
440
441     public void removeCustomFieldValues(Integer JavaDoc customFieldId) {
442         try {
443             Collection currentValues = cfvHome.findByCustomField(customFieldId);
444             for(Iterator iter = currentValues.iterator(); iter.hasNext(); ) {
445                 try {
446                     ((CustomFieldValueLocal) iter.next()).remove();
447                 } catch(RemoveException re) {
448                 }
449             }
450         } catch(FinderException fe) {
451         }
452     }
453
454     public LanguageModel getLanguageItemByKey(String JavaDoc key, Locale locale) {
455         LanguageModel model = null;
456
457         if(key != null) {
458             try {
459                 LanguageLocal languageItem = lHome.findByKeyAndLocale(key, ITrackerResources.BASE_LOCALE);
460                 model = languageItem.getModel();
461
462                 if(locale != null && ! "".equals(locale.getLanguage())) {
463                     languageItem = lHome.findByKeyAndLocale(key, locale.getLanguage());
464                     model = languageItem.getModel();
465
466                     if(! "".equals(locale.getCountry())) {
467                         languageItem = lHome.findByKeyAndLocale(key, locale.toString());
468                         model = languageItem.getModel();
469                     }
470                 }
471             } catch(FinderException fe) {
472             }
473         }
474
475         return model;
476     }
477
478     public LanguageModel[] getLanguageItemsByKey(String JavaDoc key) {
479         LanguageModel[] items = new LanguageModel[0];
480
481         try {
482             Collection languageItems = lHome.findByKey(key);
483             items = new LanguageModel[languageItems.size()];
484             int i = 0;
485             for(Iterator iter = languageItems.iterator(); iter.hasNext(); i++) {
486                 items[i] = ((LanguageLocal) iter.next()).getModel();
487             }
488         } catch(FinderException fe) {
489         }
490
491         return items;
492     }
493
494     public LanguageModel updateLanguageItem(LanguageModel model) {
495         try {
496             LanguageLocal languageItem;
497
498             try {
499                 languageItem = lHome.findByKeyAndLocale(model.getResourceKey(), model.getLocale());
500                 languageItem.setModel(model);
501             } catch(FinderException fe) {
502                 IDGenerator idGen = idHome.create();
503                 languageItem = lHome.create(idGen.getId(LanguageLocal.ID_NAME));
504                 languageItem.setModel(model);
505             }
506
507             return languageItem.getModel();
508         } catch(CreateException ce) {
509             Logger.logDebug("Unable to create new language item.", ce);
510         }
511
512         return null;
513     }
514
515     public void removeLanguageKey(String JavaDoc key) {
516         try {
517             Vector currentIds = new Vector();
518             Collection languageItems = lHome.findByKey(key);
519             for(Iterator iter = languageItems.iterator(); iter.hasNext(); ) {
520                 currentIds.add(((LanguageLocal) iter.next()).getId());
521             }
522             for(int i = 0; i < currentIds.size(); i++) {
523                 try {
524                     lHome.remove((Integer JavaDoc) currentIds.elementAt(i));
525                 } catch(RemoveException re) {
526                 }
527             }
528         } catch(FinderException ce) {
529             Logger.logDebug("Unable to find any language items with key" + key + " for removal.");
530         }
531     }
532
533     public void removeLanguageItem(LanguageModel model) {
534         try {
535             LanguageLocal languageItem = lHome.findByKeyAndLocale(model.getResourceKey(), model.getLocale());
536             lHome.remove(languageItem.getId());
537         } catch(FinderException ce) {
538         } catch(RemoveException ce) {
539             Logger.logDebug("Unable to remove language item " + model.toString() + ".");
540         }
541     }
542
543     public String JavaDoc[] getSortedKeys() {
544         String JavaDoc[] sortedKeys = new String JavaDoc[0];
545
546         try {
547             int i = 0;
548             Collection items = lHome.findByLocale(ITrackerResources.BASE_LOCALE);
549             sortedKeys = new String JavaDoc[items.size()];
550             for(Iterator iter = items.iterator(); iter.hasNext(); i++) {
551                 LanguageLocal item = (LanguageLocal) iter.next();
552                 sortedKeys[i] = item.getResourceKey();
553             }
554         } catch(FinderException fe) {
555         }
556
557         // Now sort the list of keys in a logical manner
558
Arrays.sort(sortedKeys);
559
560         return sortedKeys;
561     }
562
563     public HashMap getDefinedKeys(String JavaDoc locale) {
564         HashMap keys = new HashMap();
565
566         if(locale == null || locale.equals("")) {
567             locale = ITrackerResources.BASE_LOCALE;
568         }
569
570         try {
571             Collection items = lHome.findByLocale(locale);
572             for(Iterator iter = items.iterator(); iter.hasNext(); ) {
573                 LanguageLocal item = (LanguageLocal) iter.next();
574                 keys.put(item.getResourceKey(), item.getResourceValue());
575             }
576         } catch(FinderException fe) {
577         }
578
579         return keys;
580     }
581
582     public NameValuePairModel[] getDefinedKeysAsArray(String JavaDoc locale) {
583         NameValuePairModel[] keys = new NameValuePairModel[0];
584
585         if(locale == null || locale.equals("")) {
586             locale = ITrackerResources.BASE_LOCALE;
587         }
588
589         try {
590             int i = 0;
591             Collection items = lHome.findByLocale(locale);
592             keys = new NameValuePairModel[items.size()];
593             for(Iterator iter = items.iterator(); iter.hasNext(); i++) {
594                 LanguageLocal item = (LanguageLocal) iter.next();
595                 keys[i] = new NameValuePairModel(item.getResourceKey(), item.getResourceValue());
596             }
597         } catch(FinderException fe) {
598         }
599
600         Arrays.sort(keys, new NameValuePairModel());
601
602         return keys;
603     }
604
605     public int getNumberDefinedKeys(String JavaDoc locale) {
606         return getDefinedKeys(locale).size();
607     }
608
609     public LanguageModel[] getLanguage(Locale locale) {
610         LanguageModel[] languageArray = new LanguageModel[0];
611         HashMap language = new HashMap();
612
613         try {
614             Collection baseItems = lHome.findByLocale(ITrackerResources.BASE_LOCALE);
615             for(Iterator iterator = baseItems.iterator(); iterator.hasNext();) {
616                 LanguageLocal item = (LanguageLocal) iterator.next();
617                 language.put(item.getResourceKey(), item.getResourceValue());
618             }
619
620             if(locale != null && ! "".equals(locale.getLanguage())) {
621                 Collection languageItems = lHome.findByLocale(locale.getLanguage());
622                 for(Iterator iterator = languageItems.iterator(); iterator.hasNext();) {
623                     LanguageLocal item = (LanguageLocal) iterator.next();
624                     language.put(item.getResourceKey(), item.getResourceValue());
625                 }
626
627                 if(! "".equals(locale.getCountry())) {
628                     Collection countryItems = lHome.findByLocale(locale.toString());
629                     for(Iterator iterator = countryItems.iterator(); iterator.hasNext();) {
630                         LanguageLocal item = (LanguageLocal) iterator.next();
631                         language.put(item.getResourceKey(), item.getResourceValue());
632                     }
633                 }
634             }
635
636             languageArray = new LanguageModel[language.size()];
637             int i = 0;
638             String JavaDoc localeString = (locale == null ? ITrackerResources.BASE_LOCALE : locale.toString());
639             for(Iterator iterator = language.keySet().iterator(); iterator.hasNext(); i++) {
640                 String JavaDoc key = (String JavaDoc) iterator.next();
641                 languageArray[i] = new LanguageModel(localeString, key, (String JavaDoc) language.get(key));
642             }
643         } catch(FinderException fe) {
644         }
645
646         return languageArray;
647     }
648
649     public HashMap getAvailableLanguages() {
650         HashMap languages = new HashMap();
651
652         ConfigurationModel[] locales = getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_LOCALE);
653         for(int i = 0; i < locales.length; i++) {
654             if(ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locales[i].getValue())) {
655                 continue;
656             }
657             if(locales[i].getValue().length() == 2) {
658                 languages.put(locales[i].getValue(), new Vector());
659             }
660         }
661         for(int i = 0; i < locales.length; i++) {
662             if(ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locales[i].getValue())) {
663                 continue;
664             }
665             String JavaDoc locale = locales[i].getValue();
666             if(locale.length() == 5 && locale.indexOf('_') == 2) {
667                 String JavaDoc baseLanguage = locale.substring(0,2);
668                 Vector languageVector = (Vector) languages.get(baseLanguage);
669                 if(languageVector != null) {
670                     languageVector.addElement(locale);
671                 }
672             }
673         }
674         return languages;
675     }
676
677     public void updateLanguage(Locale locale, LanguageModel[] items) {
678         if(locale != null && items != null) {
679             ConfigurationModel configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_LOCALE, locale.toString(), props.getProperty("version"));
680             updateLanguage(locale, items, configItem);
681         }
682     }
683
684     public void updateLanguage(Locale locale, LanguageModel[] items, ConfigurationModel configItem) {
685         if(items == null || locale == null || configItem == null) {
686             return;
687         }
688
689         for(int i = 0; i < items.length; i++) {
690             if(items[i] != null) {
691                 if(items[i].getAction() == SystemConfigurationUtilities.ACTION_REMOVE) {
692                     removeLanguageItem(items[i]);
693                 } else {
694                     updateLanguageItem(items[i]);
695                 }
696             }
697         }
698         removeConfigurationItems(configItem);
699         createConfigurationItem(configItem);
700
701     }
702
703     public SystemConfigurationModel getSystemConfiguration(Locale locale) {
704         SystemConfigurationModel config = new SystemConfigurationModel();
705
706         // Load the basic system configuration
707
ConfigurationModel[] resolutions = getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION);
708         for(int i = 0; i < resolutions.length; i++) {
709             resolutions[i].setName(ITrackerResources.getString(SystemConfigurationUtilities.getLanguageKey(resolutions[i]), locale));
710         }
711         config.setResolutions(resolutions);
712
713         ConfigurationModel[] severities = getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY);
714         for(int i = 0; i < severities.length; i++) {
715             severities[i].setName(ITrackerResources.getString(SystemConfigurationUtilities.getLanguageKey(severities[i]), locale));
716         }
717         config.setSeverities(severities);
718
719         ConfigurationModel[] statuses = getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
720         for(int i = 0; i < statuses.length; i++) {
721             statuses[i].setName(ITrackerResources.getString(SystemConfigurationUtilities.getLanguageKey(statuses[i]), locale));
722         }
723         config.setStatuses(statuses);
724
725         // Now load the CustomFields
726
config.setCustomFields(IssueUtilities.getCustomFields(locale));
727
728         // Now set the system version
729
config.setVersion(props.getProperty("version"));
730
731         return config;
732     }
733
734     public boolean initializeLocale(String JavaDoc locale, boolean forceReload) {
735         boolean result = false;
736
737         ConfigurationModel localeConfig = new ConfigurationModel(SystemConfigurationUtilities.TYPE_LOCALE, locale, props.getProperty("version"));
738         if(! configurationItemUpToDate(localeConfig) || forceReload) {
739             Logger.logDebug("Loading database with locale " + locale);
740             PropertiesFileHandler localePropertiesHandler = new PropertiesFileHandler("/cowsultants/itracker/ejb/client/resources/ITracker" + (ITrackerResources.BASE_LOCALE.equals(locale) ? "" : "_" + locale) + ".properties");
741             if(localePropertiesHandler.hasProperties()) {
742                 Properties localeProperties = localePropertiesHandler.getProperties();
743                 Logger.logDebug("Locale " + locale + " contains " + localeProperties.size() + " properties.");
744                 for(Enumeration propertiesEnumeration = localeProperties.propertyNames(); propertiesEnumeration.hasMoreElements(); ) {
745                     String JavaDoc key = (String JavaDoc) propertiesEnumeration.nextElement();
746                     String JavaDoc value = localeProperties.getProperty(key);
747                     updateLanguageItem(new LanguageModel(locale, key, value));
748                 }
749                 removeConfigurationItems(localeConfig);
750                 createConfigurationItem(localeConfig);
751                 ITrackerResources.clearBundle(ITrackerResources.getLocale(locale));
752                 result = true;
753             } else {
754                 Logger.logInfo("Locale " + locale + " contained no properties.");
755             }
756         }
757         return result;
758     }
759
760     public void initializeConfiguration() {
761         // Need to eventually add in code that detects the current version of the config and update
762
// if necessary
763

764         try {
765             ConfigurationModel[] initialized = getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_INITIALIZED);
766             if(initialized == null || initialized.length != 1) {
767                 Logger.logDebug("System does not appear to be initialized, initializing system configuration.");
768                 LanguageModel[] baseLanguage = getLanguage(ITrackerResources.getLocale(ITrackerResources.BASE_LOCALE));
769                 if(baseLanguage == null || baseLanguage.length == 0) {
770                     throw new SystemConfigurationException("Languages must be initialized before the system configuration can be loaded.");
771                 }
772
773                 // Remove any previous configuration information, possibly left over from previous failed initialization
774
Logger.logDebug("Removing previous incomplete initialization information.");
775                 removeConfigurationItems(SystemConfigurationUtilities.TYPE_STATUS);
776                 removeConfigurationItems(SystemConfigurationUtilities.TYPE_SEVERITY);
777                 removeConfigurationItems(SystemConfigurationUtilities.TYPE_RESOLUTION);
778
779                 for(int i = 0; i < baseLanguage.length; i++) {
780                     if(baseLanguage[i].getResourceKey().startsWith(ITrackerResources.KEY_BASE_RESOLUTION)) {
781                         try {
782                             String JavaDoc resolutionString = baseLanguage[i].getResourceKey().substring(20);
783                             Logger.logDebug("Adding new configuration resolution value: " + resolutionString);
784                             int resolutionNumber = Integer.parseInt(resolutionString);
785                             createConfigurationItem(new ConfigurationModel(SystemConfigurationUtilities.TYPE_RESOLUTION, resolutionString, props.getProperty("version"), resolutionNumber));
786                         } catch(Exception JavaDoc e) {
787                             Logger.logError("Unable to load resolution value: " + baseLanguage[i].getResourceKey(), e);
788                         }
789                     }
790                     if(baseLanguage[i].getResourceKey().startsWith(ITrackerResources.KEY_BASE_SEVERITY)) {
791                         try {
792                             String JavaDoc severityString = baseLanguage[i].getResourceKey().substring(18);
793                             Logger.logDebug("Adding new configuration severity value: " + severityString);
794                             int severityNumber = Integer.parseInt(severityString);
795                             createConfigurationItem(new ConfigurationModel(SystemConfigurationUtilities.TYPE_SEVERITY, severityString, props.getProperty("version"), severityNumber));
796                         } catch(Exception JavaDoc e) {
797                             Logger.logError("Unable to load severity value: " + baseLanguage[i].getResourceKey(), e);
798                         }
799                     }
800                     if(baseLanguage[i].getResourceKey().startsWith(ITrackerResources.KEY_BASE_STATUS)) {
801                         try {
802                             String JavaDoc statusString = baseLanguage[i].getResourceKey().substring(16);
803                             Logger.logDebug("Adding new configuration status value: " + statusString);
804                             int statusNumber = Integer.parseInt(statusString);
805                             createConfigurationItem(new ConfigurationModel(SystemConfigurationUtilities.TYPE_STATUS, statusString, props.getProperty("version"), statusNumber));
806                         } catch(Exception JavaDoc e) {
807                             Logger.logError("Unable to load status value: " + baseLanguage[i].getResourceKey(), e);
808                         }
809                     }
810                 }
811                 createConfigurationItem(new ConfigurationModel(SystemConfigurationUtilities.TYPE_INITIALIZED, "1", props.getProperty("version")));
812             }
813         } catch(Exception JavaDoc e) {
814             Logger.logError("Unable to initialize system configuration.", e);
815         }
816     }
817
818     public void ejbCreate() {
819         if(props == null) {
820             props = new Properties();
821
822             try {
823                 ic = new InitialContext();
824                 NamingEnumeration bindings = ic.listBindings("java:comp/env");
825                 while(bindings.hasMoreElements()) {
826                     try {
827                         Binding namePair = (Binding) bindings.nextElement();
828                         Logger.logDebug("Setting property " + namePair.getName() + " from env-entry to value '" + ((Object JavaDoc) namePair.getObject()).toString() + "'");
829                         props.setProperty(namePair.getName(), ((Object JavaDoc) namePair.getObject()).toString());
830                     } catch(ClassCastException JavaDoc cce) {
831                         Logger.logError("ClassCastException while loading application properties.", cce);
832                     }
833                 }
834             } catch(NamingException ne) {
835                 Logger.logError("NamingException while loading application properties.", ne);
836             }
837
838             PropertiesFileHandler filePropertiesHandler = new PropertiesFileHandler();
839             filePropertiesHandler.addProperties("/itrackerApplication.properties");
840             filePropertiesHandler.addProperties("/itrackerVersion.properties");
841             Properties fileProps = filePropertiesHandler.getProperties();
842             for(Enumeration e = fileProps.propertyNames(); e.hasMoreElements(); ) {
843                 String JavaDoc propName = (String JavaDoc) e.nextElement();
844                 Logger.logDebug("Setting property " + propName + " from file to value '" + fileProps.getProperty(propName) + "'");
845                 props.setProperty(propName, fileProps.getProperty(propName));
846             }
847
848             props.setProperty("start_time_millis", Long.toString(new java.util.Date JavaDoc().getTime()));
849
850             try {
851                 InitialContext ic = new InitialContext();
852                 try {
853                     DataSource ds = (DataSource) ic.lookup(props.getProperty("default_ds", SystemConfigurationUtilities.DEFAULT_DATASOURCE));
854                 } catch(NamingException testne) {
855                     DataSource ds = (DataSource) ic.lookup("java:/" + props.getProperty("default_ds"));
856                     props.setProperty("default_ds", "java:/" + props.getProperty("default_ds"));
857                     Logger.logWarn("Error in datasource, changed to '" + props.getProperty("default_ds") + "'");
858                 }
859             } catch(NamingException ne) {
860                 Logger.logError("NamingException while checking DataSource.", ne);
861             }
862         }
863
864         try {
865             if(ic == null) {
866                 ic = new InitialContext();
867             }
868
869             Object JavaDoc idRef = ic.lookup("java:comp/env/" + IDGenerator.JNDI_NAME);
870             idHome = (IDGeneratorHome) PortableRemoteObject.narrow(idRef, IDGeneratorHome.class);
871
872             cHome = (ConfigurationLocalHome) ic.lookup("java:comp/env/" + ConfigurationLocal.JNDI_NAME);
873             cfHome = (CustomFieldLocalHome) ic.lookup("java:comp/env/" + CustomFieldLocal.JNDI_NAME);
874             cfvHome = (CustomFieldValueLocalHome) ic.lookup("java:comp/env/" + CustomFieldValueLocal.JNDI_NAME);
875             lHome = (LanguageLocalHome) ic.lookup("java:comp/env/" + LanguageLocal.JNDI_NAME);
876         } catch(NamingException ne) {
877             Logger.logError("Exception while creating system configuration.", ne);
878         }
879     }
880
881     public void setSessionContext(SessionContext value) {}
882     public void ejbActivate() {}
883     public void ejbPassivate() {}
884     public void ejbRemove() {}
885 }
886   
Popular Tags