WorkflowUtilities.java

  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. package org.itracker.model.util;

  19. import org.apache.log4j.Logger;
  20. import org.itracker.core.resources.ITrackerResources;
  21. import org.itracker.model.NameValuePair;

  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Locale;
  25. import java.util.Map;

  26. /**
  27.  * Contains utilities used when displaying and processing workflow and field events
  28.  */
  29. public class WorkflowUtilities {

  30.     /**
  31.      * Fires for each field when building the form.  Mainly used to build dynamic list options.
  32.      */
  33.     public static final int EVENT_FIELD_ONPOPULATE = 1;
  34.     /**
  35.      * 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.
  36.      */
  37.     public static final int EVENT_FIELD_ONSORT = 2;
  38.     /**
  39.      * Fires to set the current value of a form field.  This will overwrite any data in the form field pulled from the database.
  40.      */
  41.     public static final int EVENT_FIELD_ONSETDEFAULT = 3;
  42.     /**
  43.      * Fires on validation of the form field.
  44.      */
  45.     public static final int EVENT_FIELD_ONVALIDATE = 4;
  46.     /**
  47.      * Fires after validation, but before the data is committed to the database.
  48.      */
  49.     public static final int EVENT_FIELD_ONPRESUBMIT = 5;
  50.     /**
  51.      * Fires after all data is submitted to the db for all fields. Performed right before the response is sent.
  52.      */
  53.     public static final int EVENT_FIELD_ONPOSTSUBMIT = 6;

  54.     private static final Logger logger = Logger.getLogger(WorkflowUtilities.class);

  55.     public WorkflowUtilities() {
  56.     }

  57.     /**
  58.      * Returns a title of workflow event, according to selected locale.
  59.      *
  60.      * @param value  is an identifier of incoming event.
  61.      * @param locale is a selected locale.
  62.      * @return a name of event or something like "MISSING KEY: <resourceBundleKey>".
  63.      */
  64.     public static String getEventName(int value, Locale locale) {
  65.         final String eventName = getEventName(Integer.toString(value), locale);
  66.         return eventName;
  67.     }

  68.     public static String getEventName(String value, Locale locale) {
  69.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_WORKFLOW_EVENT + value, locale);
  70.     }

  71.     /**
  72.      * Returns an array of pairs (eventName, eventId), where eventName
  73.      * is an event title, according to selected locale.
  74.      *
  75.      * @param locale is a selected locale.
  76.      * @return an array of pairs (eventName, eventId), which is never null.
  77.      */
  78.     public static NameValuePair[] getEvents(Locale locale) {
  79.         NameValuePair[] eventNames = new NameValuePair[5];
  80.         eventNames[0] = new NameValuePair(getEventName(EVENT_FIELD_ONPOPULATE, locale), Integer.toString(EVENT_FIELD_ONPOPULATE));
  81. //        eventNames[] = new NameValuePair(getEventName(EVENT_FIELD_ONSORT, locale), Integer.toString(EVENT_FIELD_ONSORT));
  82.         eventNames[1] = new NameValuePair(getEventName(EVENT_FIELD_ONSETDEFAULT, locale), Integer.toString(EVENT_FIELD_ONSETDEFAULT));
  83.         eventNames[2] = new NameValuePair(getEventName(EVENT_FIELD_ONVALIDATE, locale), Integer.toString(EVENT_FIELD_ONVALIDATE));
  84.         eventNames[3] = new NameValuePair(getEventName(EVENT_FIELD_ONPRESUBMIT, locale), Integer.toString(EVENT_FIELD_ONPRESUBMIT));
  85.         eventNames[4] = new NameValuePair(getEventName(EVENT_FIELD_ONPOSTSUBMIT, locale), Integer.toString(EVENT_FIELD_ONPOSTSUBMIT));
  86.         return eventNames;
  87.     }

  88.     /**
  89.      * Select a list of NameValuePair objects from provided map object according
  90.      * to fieldId selector. Typesafe version of #getListOptions(Map, Integer)
  91.      *
  92.      * @param listOptions is a map, with stored NameValuePair objects lists
  93.      *                    associated with specific integer id.
  94.      * @param fieldId     is a selector from map.
  95.      * @return a list of objects, which may be empty, but never null.
  96.      */
  97.     public static List<NameValuePair> getListOptions(Map<Integer, List<NameValuePair>> listOptions, int fieldId) {
  98.         return getListOptions(listOptions, Integer.valueOf(fieldId));
  99.     }

  100.     /**
  101.      * Select a list of NameValuePair objects from provided map object according
  102.      * to fieldId selector.
  103.      *
  104.      * @param listOptions is a map, with stored NameValuePair objects lists
  105.      *                    associated with specific integer id.
  106.      * @param fieldId     is a selector from map.
  107.      * @return a list of objects, which may be empty, but never null.
  108.      */
  109.     @SuppressWarnings("unchecked")
  110.     public static List<NameValuePair> getListOptions(Map listOptions, Integer fieldId) {
  111.         List<NameValuePair> options = new ArrayList<NameValuePair>();

  112.         if (listOptions != null && listOptions.size() != 0 && fieldId != null) {
  113.             Object mapOptions = listOptions.get(fieldId);
  114.             if (mapOptions != null) {
  115.                 options = (List<NameValuePair>) mapOptions;
  116.             }
  117.         }

  118.         return options;
  119.     }

  120. }