1 package com.atlassian.jira.config;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.apache.commons.collections.map.ListOrderedMap;
10 import org.apache.log4j.Category;
11 import org.ofbiz.core.entity.EntityUtil;
12 import org.ofbiz.core.entity.GenericValue;
13
14 import com.atlassian.core.ofbiz.CoreFactory;
15 import com.atlassian.jira.ComponentManager;
16 import com.atlassian.jira.issue.IssueConstant;
17 import com.atlassian.jira.issue.worklogtype.WorklogType;
18 import com.atlassian.jira.issue.worklogtype.WorklogTypeImpl;
19 import com.atlassian.jira.security.JiraAuthenticationContext;
20 import com.atlassian.jira.util.EasyList;
21 import com.atlassian.jira.util.map.EasyMap;
22
23 public class DefaultExtendedConstantsManager extends DefaultConstantsManager implements ExtendedConstantsManager {
24
25 private static final Category log = Category.getInstance(DefaultExtendedConstantsManager.class);
26
27 private List worklogTypes;
28
29 private Map worklogTypeObjectsMap;
30
31 public DefaultExtendedConstantsManager(JiraAuthenticationContext authenticationContext) {
32 super(authenticationContext);
33 }
34
35 public GenericValue getConstant(String constantType, String id) {
36 if (WORKLOGTYPE_CONSTANT_TYPE.equalsIgnoreCase(constantType)) {
37 return getWorklogType(id);
38 } else
39 return super.getConstant(constantType, id);
40 }
41
42 public IssueConstant getConstantObject(String constantType, String id) {
43 if (WORKLOGTYPE_CONSTANT_TYPE.equalsIgnoreCase(constantType)) {
44 return getWorklogTypeObject(id);
45 } else
46 return super.getConstantObject(constantType, id);
47 }
48
49 public Collection getConstantObjects(String constantType) {
50 if (WORKLOGTYPE_CONSTANT_TYPE.equalsIgnoreCase(constantType)) {
51 return getWorklogTypeObjects();
52 } else
53 return super.getConstantObjects(constantType);
54 }
55
56 public void refresh() {
57 worklogTypes = null;
58 worklogTypeObjectsMap = null;
59 super.refresh();
60 }
61
62 public IssueConstant getIssueConstant(GenericValue issueConstantGV) {
63 if (issueConstantGV == null) {
64 return null;
65 }
66
67 if (WORKLOGTYPE_CONSTANT_TYPE.equals(issueConstantGV.getEntityName())) {
68 return getWorklogTypeObject(issueConstantGV.getString("id"));
69 }
70 return super.getIssueConstant(issueConstantGV);
71 }
72
73 public WorklogType getWorklogTypeObject(String id) {
74 return (WorklogType) getWorklogTypeObjectsMap().get(id);
75 }
76
77 public Collection getWorklogTypes() {
78 initWorklogTypes();
79 return worklogTypes;
80 }
81
82 public Collection getWorklogTypeObjects() {
83 initWorklogTypes();
84 return Collections.unmodifiableCollection(worklogTypeObjectsMap.values());
85 }
86
87 private Map getWorklogTypeObjectsMap() {
88 initWorklogTypes();
89 return worklogTypeObjectsMap;
90 }
91
92 private synchronized void initWorklogTypes() {
93 if (worklogTypes == null) {
94 worklogTypes = getConstants(WORKLOGTYPE_CONSTANT_TYPE);
95 worklogTypeObjectsMap = Collections.synchronizedMap(new ListOrderedMap());
96 if (worklogTypes!=null) {
97 for (Iterator iterator = worklogTypes.iterator(); iterator.hasNext();) {
98 GenericValue worklogTypeGV = (GenericValue) iterator.next();
99 worklogTypeObjectsMap.put(worklogTypeGV.getString("id"), new WorklogTypeImpl(worklogTypeGV, ComponentManager.getInstance().getTranslationManager(),
100 ComponentManager.getInstance().getJiraAuthenticationContext()));
101 }
102 }
103 }
104 }
105
106 public GenericValue getWorklogType(String id) {
107 return getConstant((List) getWorklogTypes(), id);
108 }
109
110 public synchronized void refreshWorklogTypes() {
111 worklogTypes = null;
112 getWorklogTypes();
113 }
114
115
116
117 protected static final List ORDER_BY_LIST = EasyList.build("sequence ASC");
118
119 protected GenericValue getConstant(List constants, String id) {
120 if (id == null) {
121 return null;
122 }
123
124 return EntityUtil.getOnly(EntityUtil.filterByAnd(constants, EasyMap.build("id", id)));
125 }
126
127 protected List getConstants(String type) {
128 return getConstantsWithSort(type, ORDER_BY_LIST);
129 }
130
131 protected List getConstantsWithSort(String type, List sortList) {
132 try {
133 List gvs = CoreFactory.getGenericDelegator().findAll(type, sortList);
134 return Collections.unmodifiableList(Collections.synchronizedList(gvs));
135 } catch (Exception e) {
136 log.error("Error getting constants of type: " + type + " : " + e, e);
137 }
138
139 return null;
140 }
141
142 }