KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > IssueTemplateInfoPeer


1 package org.tigris.scarab.om;
2
3 import java.util.List JavaDoc;
4 import java.util.Comparator JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import org.apache.torque.util.Criteria;
8 import org.tigris.scarab.services.cache.ScarabCache;
9
10 // Local classes
11
import org.tigris.scarab.om.Module;
12
13 /**
14  * You should add additional methods to this class to meet the
15  * application requirements. This class will only be generated as
16  * long as it does not already exist in the output directory.
17  */

18 public class IssueTemplateInfoPeer
19     extends org.tigris.scarab.om.BaseIssueTemplateInfoPeer
20 {
21
22     private static final String JavaDoc GET_TEMPLATES =
23         "getTemplates";
24     static final String JavaDoc GET_USER_TEMPLATES =
25         "getUserTemplates";
26     static final String JavaDoc GET_MODULE_TEMPLATES =
27         "getModuleTemplates";
28     static final String JavaDoc TEMPLATE_PEER =
29         "IssueTemplateInfoPeer";
30
31     // query types
32
public static final String JavaDoc TYPE_PRIVATE = "private";
33     public static final String JavaDoc TYPE_GLOBAL = "global";
34     public static final String JavaDoc TYPE_ALL = "all";
35
36     /**
37      * List of Issue Template objects associated with this module.
38      * And issue type, either created by current user, or project-scoped.
39      * i.e., all templates this user has permission to see.
40      */

41     public static List JavaDoc getTemplates(Module module, IssueType issueType,
42                                     ScarabUser user, String JavaDoc sortColumn,
43                                     String JavaDoc sortPolarity, String JavaDoc type)
44         throws Exception JavaDoc
45     {
46         List JavaDoc templates = null;
47         Serializable JavaDoc[] key = {TEMPLATE_PEER, GET_TEMPLATES, module, null,
48             issueType, user, sortColumn, sortPolarity, type};
49         Object JavaDoc obj = ScarabCache.get(key);
50         if (obj == null)
51         {
52             Criteria crit = new Criteria()
53                 .add(IssuePeer.MODULE_ID, module.getModuleId())
54                 .add(IssuePeer.DELETED, 0)
55                 .addJoin(ActivitySetPeer.TRANSACTION_ID,
56                          ActivityPeer.TRANSACTION_ID)
57                 .addJoin(IssuePeer.ISSUE_ID,
58                          ActivityPeer.ISSUE_ID)
59                 .add(IssuePeer.TYPE_ID, issueType.getTemplateId())
60                 .addJoin(IssueTemplateInfoPeer.ISSUE_ID,
61                          IssuePeer.ISSUE_ID);
62             crit.setDistinct();
63
64             Criteria.Criterion cGlob = crit.getNewCriterion(
65                 IssueTemplateInfoPeer.SCOPE_ID, Scope.MODULE__PK,
66                 Criteria.EQUAL);
67             cGlob.and(crit.getNewCriterion(IssueTemplateInfoPeer.APPROVED,
68                                            Boolean.TRUE, Criteria.EQUAL));
69             Criteria.Criterion cPriv = crit.getNewCriterion(
70                 ActivitySetPeer.CREATED_BY, user.getUserId(),
71                 Criteria.EQUAL);
72             cPriv.and(crit.getNewCriterion(
73                 IssueTemplateInfoPeer.SCOPE_ID, Scope.PERSONAL__PK,
74                 Criteria.EQUAL));
75
76             if (TYPE_PRIVATE.equals(type))
77             {
78                 crit.add(cPriv);
79             }
80             else if (TYPE_GLOBAL.equals(type))
81             {
82                 crit.add(cGlob);
83             }
84             else
85             {
86                 // All templates
87
cGlob.or(cPriv);
88                 crit.add(cGlob);
89             }
90
91             // Add sort criteria
92
if (sortColumn.equals("desc"))
93             {
94                 addSortOrder(crit, IssueTemplateInfoPeer.DESCRIPTION,
95                              sortPolarity);
96             }
97             else if (sortColumn.equals("avail"))
98             {
99                 crit.addJoin(IssueTemplateInfoPeer.SCOPE_ID,
100                              ScopePeer.SCOPE_ID);
101                 addSortOrder(crit, ScopePeer.SCOPE_NAME, sortPolarity);
102             }
103             else if (!sortColumn.equals("user"))
104             {
105                 // sort by name
106
addSortOrder(crit, IssueTemplateInfoPeer.NAME, sortPolarity);
107             }
108             templates = IssueTemplateInfoPeer.doSelect(crit);
109             ScarabCache.put(templates, key);
110         }
111         else
112         {
113             templates = (List JavaDoc)obj;
114         }
115         if (sortColumn.equals("user"))
116         {
117             templates = sortByCreatingUser(templates, sortPolarity);
118         }
119         return templates;
120     }
121
122     public static List JavaDoc getUserTemplates(ScarabUser user, Module module,IssueType issueType)
123         throws Exception JavaDoc
124     {
125         List JavaDoc templates = null;
126         Object JavaDoc obj = ScarabCache.get(TEMPLATE_PEER, GET_USER_TEMPLATES, user);
127         if (obj == null)
128         {
129             Criteria crit = new Criteria()
130             .addJoin(IssueTemplateInfoPeer.ISSUE_ID,
131                          IssuePeer.ISSUE_ID)
132             .add(IssuePeer.DELETED, 0)
133                 .add(IssuePeer.MODULE_ID, module.getModuleId())
134             .addJoin(ActivitySetPeer.TRANSACTION_ID,
135                      ActivityPeer.TRANSACTION_ID)
136                 .addJoin(IssuePeer.ISSUE_ID,
137                          ActivityPeer.ISSUE_ID)
138             .add(IssuePeer.TYPE_ID, issueType.getTemplateId())
139             .add(ActivitySetPeer.CREATED_BY, user.getUserId());
140             templates = IssueTemplateInfoPeer.doSelect(crit);
141             ScarabCache.put(templates, TEMPLATE_PEER, GET_USER_TEMPLATES, user);
142         }
143         else
144         {
145             templates = (List JavaDoc)obj;
146         }
147         return templates;
148     }
149
150
151     public static List JavaDoc getModuleTemplates(Module module)
152         throws Exception JavaDoc
153     {
154         List JavaDoc templates = null;
155         Object JavaDoc obj = ScarabCache.get(TEMPLATE_PEER, GET_MODULE_TEMPLATES, module);
156         if (obj == null)
157         {
158             Criteria crit = new Criteria()
159             .addJoin(IssueTemplateInfoPeer.ISSUE_ID,
160                          IssuePeer.ISSUE_ID)
161             .add(IssuePeer.DELETED, 0)
162             .add(IssuePeer.MODULE_ID, module.getModuleId())
163             .add(IssueTemplateInfoPeer.SCOPE_ID, Scope.MODULE__PK);
164             templates = IssueTemplateInfoPeer.doSelect(crit);
165             ScarabCache.put(templates, TEMPLATE_PEER, GET_USER_TEMPLATES, module);
166         }
167         else
168         {
169             templates = (List JavaDoc)obj;
170         }
171         return templates;
172     }
173
174     private static Criteria addSortOrder(Criteria crit,
175                        String JavaDoc sortColumn, String JavaDoc sortPolarity)
176     {
177         if (sortPolarity.equals("desc"))
178         {
179             crit.addDescendingOrderByColumn(sortColumn);
180         }
181         else
182         {
183             crit.addAscendingOrderByColumn(sortColumn);
184         }
185         return crit;
186     }
187
188     private static List JavaDoc sortByCreatingUser(List JavaDoc result,
189                                            String JavaDoc sortPolarity)
190         throws Exception JavaDoc
191     {
192         final int polarity = ("asc".equals(sortPolarity)) ? 1 : -1;
193         Comparator JavaDoc c = new Comparator JavaDoc()
194         {
195             public int compare(Object JavaDoc o1, Object JavaDoc o2)
196             {
197                 int i = 0;
198                 try
199                 {
200                     i = polarity *
201                         ((Issue)o1).getCreatedBy().getFirstName()
202                          .compareTo(((Issue)o2).getCreatedBy().getFirstName());
203                 }
204                 catch (Exception JavaDoc e)
205                 {
206                     //
207
}
208                 return i;
209              }
210         };
211         Collections.sort(result, c);
212         return result;
213     }
214
215 }
216
Popular Tags