/src/main/java/com/dionetechnology/jira/plugins/multicascadeselect/OptionsMap.java

https://bitbucket.org/ram_tandukar/multi-cascade-select · Java · 239 lines · 136 code · 41 blank · 62 comment · 22 complexity · 0b839fc62343215a2896ffb8160b1e2e MD5 · raw file

  1. package com.dionetechnology.jira.plugins.multicascadeselect;
  2. import com.atlassian.jira.issue.customfields.option.Options;
  3. import com.atlassian.jira.issue.customfields.option.Option;
  4. import java.util.*;
  5. /**
  6. * This class, implementing Java Map, associates a level (integer) to a Map
  7. * Forked from https://github.com/sourcesense/jira-multi-level-cascade-select
  8. */
  9. public class OptionsMap implements Map {
  10. private TreeMap map = new TreeMap(new OptionsMapKeyComparator());
  11. /*
  12. * this OptionMap is created to obtain the map of all the levels, does not contain the selecion of the user*/
  13. public OptionsMap(Options options) {
  14. super();
  15. putOptions(options);
  16. }
  17. public void putOptions(Options options) {
  18. transformOptionsToMap(options, 0, null);
  19. }
  20. /**
  21. * Converts from int to String, returning null for zero.
  22. * @param level
  23. * @return
  24. */
  25. private String getKeyFromLevel(int level) {
  26. return level == 0 ? null : String.valueOf(level);
  27. }
  28. /**
  29. * If the Map contains the level in input (string built from the Integer)
  30. * returns the map parentId -> option list
  31. * @param key
  32. * @return
  33. */
  34. private Map safeGetMap(String key) {
  35. if (!map.containsKey(key)) {
  36. map.put(key, new TreeMap(new OptionsMapKeyComparator()));
  37. }
  38. return (Map) map.get(key);
  39. }
  40. /**
  41. * checks that the list of Options in input is not empty and then it populate the result map,
  42. * descending through the levels
  43. * @param options
  44. * @param level
  45. * @param parentId
  46. * @return
  47. */
  48. private Map transformOptionsToMap(List options, int level, String parentId) {
  49. if (options != null && !options.isEmpty()) {
  50. Map mapLevel = safeGetMap(getKeyFromLevel(level));
  51. mapLevel.put(parentId, options);
  52. for (Iterator iterator = options.iterator(); iterator.hasNext();) {
  53. Option option = (Option) iterator.next();
  54. transformOptionsToMap(option.getChildOptions(), level + 1, getOptionKey(option));
  55. }
  56. }
  57. return map;
  58. }
  59. /**
  60. * returns the String ID of an Option
  61. * @param option
  62. * @return
  63. */
  64. private String getOptionKey(Option option) {
  65. if (option != null) {
  66. return option.getOptionId().toString();
  67. }
  68. return null;
  69. }
  70. /**
  71. * returns the ID of the parent of the Option
  72. * @param option
  73. * @return
  74. */
  75. private String getParentOptionKey(Option option) {
  76. if (option != null) {
  77. return getOptionKey(option.getParentOption());
  78. }
  79. return null;
  80. }
  81. /**
  82. * puts an option in an input level, if the level is empty, a new option list is created and populated
  83. * with the input option
  84. * @param level
  85. * @param option
  86. */
  87. public void putOptionInLevel(String level, Option option) {
  88. Map mapLevel = safeGetMap(level);
  89. String parentKey = getParentOptionKey(option);
  90. List options = (List) mapLevel.get(parentKey);
  91. if (options == null) {
  92. options = new ArrayList();
  93. mapLevel.put(parentKey, options);
  94. }
  95. if (!options.contains(option)) {
  96. options.add(option);
  97. }
  98. }
  99. public int size() {
  100. return map.size();
  101. }
  102. public boolean isEmpty() {
  103. return map.isEmpty();
  104. }
  105. public boolean containsKey(Object key) {
  106. return map.containsKey(key);
  107. }
  108. /**
  109. * checks that the input value belong to the input level.
  110. * In the level it checks that the input value belong at least to one child list
  111. * @param level
  112. * @param value
  113. * @return
  114. */
  115. public boolean containsValueInLevel(Object level, String value) {
  116. Map levelMap = (Map) map.get(level);
  117. if (levelMap != null) {
  118. //mi prendo tutti i parentId e li scansiono uno per uno
  119. for (Iterator parentKeyIterator = levelMap.keySet().iterator(); parentKeyIterator.hasNext();) {
  120. Object parentKey = parentKeyIterator.next();
  121. List options = (List) levelMap.get(parentKey);
  122. //mi ottengo la lista di opzioni figlie del parentId
  123. if (options != null) {
  124. //per ognuna verifico se soddisfa l'inpu
  125. for (Iterator optionsIterator = options.iterator(); optionsIterator.hasNext();) {
  126. Option option = (Option) optionsIterator.next();
  127. if (option.getValue().equalsIgnoreCase(value)) {
  128. return true;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136. public boolean containsValue(Object value) {
  137. return map.containsValue(value);
  138. }
  139. public Object get(Object key) {
  140. return map.get(key);
  141. }
  142. public Object put(Object key, Object value) {
  143. return map.put(key, value);
  144. }
  145. public Object remove(Object key) {
  146. return map.remove(key);
  147. }
  148. public void putAll(Map t) {
  149. map.putAll(t);
  150. }
  151. public void clear() {
  152. map.clear();
  153. }
  154. public Set keySet() {
  155. return map.keySet();
  156. }
  157. public Collection values() {
  158. return map.values();
  159. }
  160. public Set entrySet() {
  161. return map.entrySet();
  162. }
  163. public String toString() {
  164. return map.toString();
  165. }
  166. public Map getLevel(String level) {
  167. return safeGetMap(level);
  168. }
  169. /**
  170. * return all the options inside a level
  171. * @param level
  172. * @return
  173. */
  174. public List getOptionsInLevel(String level) {
  175. Map mapLevel = getLevel(level);
  176. List options = new ArrayList();
  177. for(Iterator mapIterator=mapLevel.keySet().iterator();mapIterator.hasNext();) {
  178. Object mapKey = mapIterator.next();
  179. options.addAll((Collection) mapLevel.get(mapKey));
  180. }
  181. return options;
  182. }
  183. public static String getFirstLevelKey() {
  184. return null;
  185. }
  186. /**
  187. * return the next level from the input one.
  188. * Remember that the level is a String.
  189. * @param level
  190. * @return
  191. */
  192. public static String getNextLevelKey(String level) {
  193. if (level==null) {
  194. return "1";
  195. }
  196. return String.valueOf(Integer.parseInt(level)+1);
  197. }
  198. }