Configuration.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;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.commons.lang.builder.ToStringBuilder;
  21. import org.itracker.model.util.IssueUtilities;

  22. import java.io.Serializable;
  23. import java.util.Comparator;

  24. /**
  25.  * A configuration item.
  26.  *
  27.  * @author ready
  28.  */
  29. public class Configuration extends AbstractEntity implements Comparable<Entity> {

  30.     public static enum Type implements IntCodeEnum<Type> {
  31.         initialized(-1, 0),
  32.         locale(1, 0),
  33.         status(2, IssueUtilities.FIELD_STATUS),
  34.         severity(3, IssueUtilities.FIELD_SEVERITY),
  35.         resolution(4, IssueUtilities.FIELD_RESOLUTION),
  36.         customfield(5, 0);


  37.         private final Integer code;
  38.         private final Integer legacyCode;

  39.         private Type(Integer code, Integer legacyCode) {
  40.             this.code = code;
  41.             this.legacyCode = legacyCode;
  42.         }

  43.         public Integer getCode() {
  44.             return code;
  45.         }

  46.         public Type fromCode(Integer code) {
  47.             return Type.valueOf(code);
  48.         }

  49.         public Integer getLegacyCode() {
  50.             return legacyCode;
  51.         }

  52.         public static Type valueOf(Integer code) {
  53.             for (Type val: values()) {
  54.                 if (val.code.compareTo(code) == 0) {
  55.                     return val;
  56.                 }
  57.             }
  58.             throw new IllegalArgumentException("Unknown code : " + code);
  59.         }
  60.         /**
  61.          * Returns the key for a particular configuration item. This is made up of a
  62.          * static part based on the type of configuration item, and the unique value
  63.          * of the configuration item.
  64.          *
  65.          * @param configuration the Configuration to return the key for
  66.          * @return the key for the item
  67.          */
  68.         public String getLanguageKey(final Configuration configuration) {
  69.             if (configuration != null) {
  70.                 final Type type = configuration.getType();
  71.                 String key = "itracker." + type.name() + ".";

  72.                 if (type == Type.locale) {
  73.                     key += "name.";
  74.                 }

  75.                 key += configuration.getValue();

  76.                 if (type == Type.customfield) {
  77.                     key += ".label";
  78.                 }
  79.                 return key;
  80.             }
  81.             return "";
  82.         }
  83.         public String getTypeLanguageKey() {
  84.             final String base = "itracker.web.attr.";

  85.             return base + name();
  86.         }



  87.     }

  88.     public static final ConfigurationOrderComparator CONFIGURATION_ORDER_COMPARATOR = new ConfigurationOrderComparator();
  89.     /**
  90.      *
  91.      */
  92.     private static final long serialVersionUID = 1L;
  93.     /**
  94.      * PENDING: this field doesn't exist in the database!?
  95.      * <p/>
  96.      * <p>
  97.      * TODO : every configuration item should have a name, similar to a Java
  98.      * property in a properties file. A description would be nice to have too.
  99.      * name + version should be the natural key. (note: we shouldn't allow 2
  100.      * configuration items with the same name and version, but with different
  101.      * types).
  102.      * </p>
  103.      * <p/>
  104.      * <p>
  105.      * But since <code>name</code> is nullable, only the type and value can be
  106.      * used as natural key at the moment. This should be a temporary situation,
  107.      * because the value is allowed to change.
  108.      * </p>
  109.      */
  110.     private String name;

  111.     /**
  112.      * ITracker version in which this configuration item was added.
  113.      */
  114.     private String version;

  115.     /**
  116.      * The real type of the value stored as a string.
  117.      */
  118.     private Type type;

  119.     /**
  120.      * The configuration value as a string.
  121.      */
  122.     private String value;

  123.     /**
  124.      * Display order.
  125.      * <p/>
  126.      * <p>
  127.      * Several instances may have the same display order.
  128.      * </p>
  129.      */
  130.     private int order;

  131.     /**
  132.      * Default constructor (required by Hibernate).
  133.      * <p/>
  134.      * <p>
  135.      * PENDING: should be <code>private</code> so that it can only be used by
  136.      * Hibernate, to ensure that the fields which form an instance's identity
  137.      * are always initialized/never <tt>null</tt>.
  138.      * </p>
  139.      */
  140.     public Configuration() {
  141.     }

  142.     public Configuration(Type type, String value) {
  143.         setType(type);
  144.         setValue(value);
  145.     }

  146.     public Configuration(Type type, NameValuePair pair) {
  147.         this(type, pair.getValue());
  148.         setName(pair.getName());
  149.     }

  150.     public Configuration(Type type, String value, String version) {
  151.         this(type, value);
  152.         setVersion(version);
  153.     }

  154.     public Configuration(Type type, String value, int order) {
  155.         this(type, value);
  156.         setOrder(order);
  157.     }

  158.     public Configuration(Type type, String value, String version, int order) {
  159.         this(type, value, version);
  160.         setOrder(order);
  161.     }

  162.     public String getName() {
  163.         return name;
  164.     }

  165.     public void setName(String name) {
  166.         this.name = name;
  167.     }

  168.     public int getOrder() {
  169.         return order;
  170.     }

  171.     public void setOrder(int order) {
  172.         this.order = order;
  173.     }

  174.     public Type getType() {
  175.         return type;
  176.     }

  177.     public void setType(Type type) {
  178.         this.type = type;
  179.     }

  180.     public String getValue() {
  181.         return value;
  182.     }

  183.     public void setValue(String value) {
  184.         if (value == null) {
  185.             throw new IllegalArgumentException("null value");
  186.         }
  187.         this.value = value;
  188.     }

  189.     public String getVersion() {
  190.         return version;
  191.     }

  192.     public void setVersion(String version) {
  193.         if (version == null) {
  194.             throw new IllegalArgumentException("null version");
  195.         }
  196.         this.version = version;
  197.     }

  198.     /**
  199.      * String composed of system ID and natural key (name and version).
  200.      */
  201.     @Override
  202.     public String toString() {
  203.         return new ToStringBuilder(this).append("id", getId()).append("type", getType())
  204.                 .append("name", getName()).append("version", getVersion()).append(
  205.                         "value", getValue()).toString();

  206.     }

  207.     public static final class ConfigurationOrderComparator implements
  208.             Comparator<Configuration>, Serializable {
  209.         /**
  210.          *
  211.          */
  212.         private static final long serialVersionUID = 1L;

  213.         public int compare(Configuration o1, Configuration o2) {
  214.             return new CompareToBuilder().append(o1.getOrder(), o2.getOrder()).append(o1.getValue(), o2.getValue())
  215.                     .toComparison();
  216.         }


  217.     }

  218. }