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 org.itracker.model.util;
20
21 import org.apache.log4j.Logger;
22 import org.itracker.core.resources.ITrackerResources;
23 import org.itracker.model.NameValuePair;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29
30 /**
31 * Contains utilities used when displaying and processing workflow and field events
32 */
33 public class WorkflowUtilities {
34
35 /**
36 * Fires for each field when building the form. Mainly used to build dynamic list options.
37 */
38 public static final int EVENT_FIELD_ONPOPULATE = 1;
39 /**
40 * NOT CURRENTLY IMPLEMENTED. Use the onPopulate event instead. In the future, this event may be implemented to allow for list sorting after list value population.
41 */
42 public static final int EVENT_FIELD_ONSORT = 2;
43 /**
44 * Fires to set the current value of a form field. This will overwrite any data in the form field pulled from the database.
45 */
46 public static final int EVENT_FIELD_ONSETDEFAULT = 3;
47 /**
48 * Fires on validation of the form field.
49 */
50 public static final int EVENT_FIELD_ONVALIDATE = 4;
51 /**
52 * Fires after validation, but before the data is committed to the database.
53 */
54 public static final int EVENT_FIELD_ONPRESUBMIT = 5;
55 /**
56 * Fires after all data is submitted to the db for all fields. Performed right before the response is sent.
57 */
58 public static final int EVENT_FIELD_ONPOSTSUBMIT = 6;
59
60 private static final Logger logger = Logger.getLogger(WorkflowUtilities.class);
61
62 public WorkflowUtilities() {
63 }
64
65 /**
66 * Returns a title of workflow event, according to selected locale.
67 *
68 * @param value is an identifier of incoming event.
69 * @param locale is a selected locale.
70 * @return a name of event or something like "MISSING KEY: <resourceBundleKey>".
71 */
72 public static String getEventName(int value, Locale locale) {
73 final String eventName = getEventName(Integer.toString(value), locale);
74 return eventName;
75 }
76
77 public static String getEventName(String value, Locale locale) {
78 return ITrackerResources.getString(ITrackerResources.KEY_BASE_WORKFLOW_EVENT + value, locale);
79 }
80
81 /**
82 * Returns an array of pairs (eventName, eventId), where eventName
83 * is an event title, according to selected locale.
84 *
85 * @param locale is a selected locale.
86 * @return an array of pairs (eventName, eventId), which is never null.
87 */
88 public static NameValuePair[] getEvents(Locale locale) {
89 NameValuePaireValuePair">NameValuePair[] eventNames = new NameValuePair[5];
90 eventNames[0] = new NameValuePair(getEventName(EVENT_FIELD_ONPOPULATE, locale), Integer.toString(EVENT_FIELD_ONPOPULATE));
91 // eventNames[] = new NameValuePair(getEventName(EVENT_FIELD_ONSORT, locale), Integer.toString(EVENT_FIELD_ONSORT));
92 eventNames[1] = new NameValuePair(getEventName(EVENT_FIELD_ONSETDEFAULT, locale), Integer.toString(EVENT_FIELD_ONSETDEFAULT));
93 eventNames[2] = new NameValuePair(getEventName(EVENT_FIELD_ONVALIDATE, locale), Integer.toString(EVENT_FIELD_ONVALIDATE));
94 eventNames[3] = new NameValuePair(getEventName(EVENT_FIELD_ONPRESUBMIT, locale), Integer.toString(EVENT_FIELD_ONPRESUBMIT));
95 eventNames[4] = new NameValuePair(getEventName(EVENT_FIELD_ONPOSTSUBMIT, locale), Integer.toString(EVENT_FIELD_ONPOSTSUBMIT));
96 return eventNames;
97 }
98
99 /**
100 * Select a list of NameValuePair objects from provided map object according
101 * to fieldId selector. Typesafe version of #getListOptions(Map, Integer)
102 *
103 * @param listOptions is a map, with stored NameValuePair objects lists
104 * associated with specific integer id.
105 * @param fieldId is a selector from map.
106 * @return a list of objects, which may be empty, but never null.
107 */
108 public static List<NameValuePair> getListOptions(Map<Integer, List<NameValuePair>> listOptions, int fieldId) {
109 return getListOptions(listOptions, Integer.valueOf(fieldId));
110 }
111
112 /**
113 * Select a list of NameValuePair objects from provided map object according
114 * to fieldId selector.
115 *
116 * @param listOptions is a map, with stored NameValuePair objects lists
117 * associated with specific integer id.
118 * @param fieldId is a selector from map.
119 * @return a list of objects, which may be empty, but never null.
120 */
121 @SuppressWarnings("unchecked")
122 public static List<NameValuePair> getListOptions(Map listOptions, Integer fieldId) {
123 List<NameValuePair> options = new ArrayList<NameValuePair>();
124
125 if (listOptions != null && listOptions.size() != 0 && fieldId != null) {
126 Object mapOptions = listOptions.get(fieldId);
127 if (mapOptions != null) {
128 options = (List<NameValuePair>) mapOptions;
129 }
130 }
131
132 return options;
133 }
134
135 }