KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.util.List JavaDoc;
50 import java.util.Arrays JavaDoc;
51 import java.util.Date JavaDoc;
52
53 import org.apache.turbine.TemplateContext;
54 import org.apache.turbine.Turbine;
55
56 import org.apache.torque.TorqueException;
57 import org.apache.torque.util.Criteria;
58 import org.apache.torque.om.Persistent;
59
60 import org.tigris.scarab.services.security.ScarabSecurity;
61 import org.tigris.scarab.services.cache.ScarabCache;
62 import org.tigris.scarab.tools.localization.L10NKeySet;
63 import org.tigris.scarab.om.Module;
64 import org.tigris.scarab.om.ModuleManager;
65 import org.tigris.scarab.util.ScarabException;
66 import org.tigris.scarab.util.Email;
67 import org.tigris.scarab.util.EmailContext;
68 import org.tigris.scarab.util.Log;
69
70 /**
71  * This class manages the Query table.
72  *
73  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
74  * @version $Id: Query.java 9222 2004-11-02 10:22:39Z dabbous $
75  */

76 public class Query
77     extends org.tigris.scarab.om.BaseQuery
78     implements Persistent
79 {
80     private static final String JavaDoc GET_R_QUERY_USER =
81         "getRQueryUser";
82
83     /**
84      * A local reference, so that getScarabUser is guaranteed to return
85      * the same instance as was passed to setScarabUser
86      */

87     private ScarabUser scarabUser;
88     
89     /**
90      * Get the value of scarabUser.
91      * @return value of scarabUser.
92      */

93     public ScarabUser getScarabUser()
94         throws TorqueException
95     {
96         ScarabUser user = this.scarabUser;
97         if (user == null)
98         {
99             user = super.getScarabUser();
100         }
101         
102         return user;
103     }
104     
105     /**
106      * Set the value of scarabUser.
107      * @param v Value to assign to scarabUser.
108      */

109     public void setScarabUser(ScarabUser v)
110         throws TorqueException
111     {
112         this.scarabUser = v;
113         super.setScarabUser(v);
114     }
115     
116     /**
117      * Throws UnsupportedOperationException. Use
118      * <code>getModule()</code> instead.
119      *
120      * @return a <code>ScarabModule</code> value
121      */

122     public ScarabModule getScarabModule()
123     {
124         throw new UnsupportedOperationException JavaDoc(
125             "Should use getModule"); //EXCEPTION
126
}
127
128     /**
129      * Throws UnsupportedOperationException. Use
130      * <code>setModule(Module)</code> instead.
131      *
132      */

133     public void setScarabModule(ScarabModule module)
134     {
135         throw new UnsupportedOperationException JavaDoc(
136             "Should use setModule(Module). Note module cannot be new."); //EXCEPTION
137
}
138
139     /**
140      * Use this instead of setScarabModule. Note: module cannot be new.
141      */

142     public void setModule(Module me)
143         throws TorqueException
144     {
145         if (me == null)
146         {
147             setModuleId((Integer JavaDoc)null);
148         }
149         else
150         {
151             Integer JavaDoc id = me.getModuleId();
152             if (id == null)
153             {
154                 throw new TorqueException("Modules must be saved prior to " +
155                     "being associated with other objects."); //EXCEPTION
156
}
157             setModuleId(id);
158         }
159     }
160
161     /**
162      * Module getter. Use this method instead of getScarabModule().
163      *
164      * @return a <code>Module</code> value
165      */

166     public Module getModule()
167         throws TorqueException
168     {
169         Module module = null;
170         Integer JavaDoc id = getModuleId();
171         if ( id != null )
172         {
173             module = ModuleManager.getInstance(id);
174         }
175         
176         return module;
177     }
178
179     /**
180      * A new Query object
181      */

182     public static Query getInstance()
183     {
184         return new Query();
185     }
186
187     public boolean canDelete(ScarabUser user)
188         throws Exception JavaDoc
189     {
190         // can delete a query if they have delete permission
191
// Or if is their personal query
192
return (user.hasPermission(ScarabSecurity.ITEM__DELETE, getModule())
193                 || (user.getUserId().equals(getUserId())
194                    && (getScopeId().equals(Scope.PERSONAL__PK))));
195     }
196
197     public boolean canEdit(ScarabUser user)
198         throws Exception JavaDoc
199     {
200         return canDelete(user);
201     }
202
203     /**
204      * Either returns true, if no email problem occured,
205      * or throws an appropriate Exception. Note: If an email
206      * exception occured, the query is still saved.
207      * @param user
208      * @param module
209      * @param context
210      * @return
211      * @throws Exception
212      */

213     public boolean saveAndSendEmail(ScarabUser user, Module module,
214                                     TemplateContext context)
215         throws Exception JavaDoc
216     {
217         // If it's a module scoped query, user must have Item | Approve
218
// permission, Or its Approved field gets set to false
219
Exception JavaDoc exception=null; // temporary store a thrown exception
220

221         if (getScopeId().equals(Scope.PERSONAL__PK)
222             || user.hasPermission(ScarabSecurity.ITEM__APPROVE, module))
223         {
224             setApproved(true);
225         }
226         else
227         {
228             setApproved(false);
229
230             // Send Email to the people with module edit ability so
231
// that they can approve the new template
232
if (context != null)
233             {
234                 String JavaDoc template = Turbine.getConfiguration().
235                     getString("scarab.email.requireapproval.template",
236                               "RequireApproval.vm");
237
238                 ScarabUser[] toUsers = module
239                     .getUsers(ScarabSecurity.ITEM__APPROVE);
240
241                 if (Log.get().isDebugEnabled())
242                 {
243                     if (toUsers == null || toUsers.length ==0)
244                     {
245                         Log.get().debug("No users to approve query.");
246                     }
247                     else
248                     {
249                         Log.get().debug("Users to approve query: ");
250                         for (int i=0; i<toUsers.length; i++)
251                         {
252                             Log.get().debug(toUsers[i].getEmail());
253                         }
254                     }
255                 }
256                 
257                 EmailContext ectx = new EmailContext();
258                 ectx.setUser(user);
259                 ectx.setModule(module);
260                 ectx.setDefaultTextKey("NewQueryRequiresApproval");
261
262                 String JavaDoc fromUser = "scarab.email.default";
263                 try
264                 {
265                     Email.sendEmail(ectx,
266                         module,
267                         fromUser,
268                         module.getSystemEmail(),
269                         Arrays.asList(toUsers),
270                         null,
271                         template);
272                 }
273                 catch (Exception JavaDoc e)
274                 {
275                     exception = e;
276                 }
277             }
278         }
279         if (getMITList() != null)
280         {
281             getMITList().save();
282             // it would be good if this updated our list id, but it doesn't
283
// happen automatically so reset it.
284
setMITList(getMITList());
285         }
286         save();
287         if(exception != null)
288         {
289             throw exception;
290         }
291         return true;
292     }
293
294     public MITList getMITList()
295         throws TorqueException
296     {
297         MITList mitlist = super.getMITList();
298         if (mitlist == null)
299         {
300             mitlist = MITListManager.getSingleItemList(getModule(),
301                 getIssueType(), null);
302         }
303         return mitlist;
304     }
305
306     /**
307      * Subscribes user to query.
308      */

309     public void subscribe(ScarabUser user, Integer JavaDoc frequencyId)
310         throws Exception JavaDoc
311     {
312         RQueryUser rqu = getRQueryUser(user);
313         rqu.setSubscriptionFrequency(frequencyId);
314         rqu.setIsSubscribed(true);
315         rqu.save();
316     }
317
318     /**
319      * Unsubscribes user from query.
320      */

321     public void unSubscribe(ScarabUser user)
322         throws Exception JavaDoc
323     {
324         RQueryUser rqu = getRQueryUser(user);
325         if (rqu.getIsdefault())
326         {
327             rqu.setIsSubscribed(false);
328             rqu.save();
329         }
330         else
331         {
332             rqu.delete(user);
333         }
334     }
335
336
337     /**
338      * Gets RQueryUser object for this query and user.
339      */

340     public RQueryUser getRQueryUser(ScarabUser user)
341         throws Exception JavaDoc
342     {
343         RQueryUser result = null;
344         Object JavaDoc obj = ScarabCache.get(this, GET_R_QUERY_USER, user);
345         if (obj == null)
346         {
347             Criteria crit = new Criteria();
348             crit.add(RQueryUserPeer.QUERY_ID, getQueryId());
349             crit.add(RQueryUserPeer.USER_ID, user.getUserId());
350             List JavaDoc rqus = RQueryUserPeer.doSelect(crit);
351             if (!rqus.isEmpty())
352             {
353                 result = (RQueryUser)rqus.get(0);
354             }
355             else
356             {
357                 result = new RQueryUser();
358                 result.setQuery(this);
359                 result.setUserId(user.getUserId());
360             }
361             ScarabCache.put(result, this, GET_R_QUERY_USER, user);
362         }
363         else
364         {
365             result = (RQueryUser)obj;
366         }
367         return result;
368     }
369
370     /**
371      * Checks permission and approves or rejects query. If query
372      * is approved, query type set to "module", else set to "personal".
373      */

374     public void approve(ScarabUser user, boolean approved)
375          throws Exception JavaDoc
376     {
377         Module module = getModule();
378
379         if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module))
380         {
381             setApproved(true);
382             if (!approved)
383             {
384                 setScopeId(Scope.PERSONAL__PK);
385             }
386             save();
387         }
388         else
389         {
390             throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction);
391         }
392     }
393
394
395     /**
396      * Checks if user has permission to delete query.
397      * Only the creating user can delete a personal query.
398      * Only project owner or admin can delete a project-wide query.
399      */

400     public void delete(ScarabUser user)
401          throws Exception JavaDoc
402     {
403         Module module = getModule();
404         if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module)
405           || (user.getUserId().equals(getUserId())
406              && getScopeId().equals(Scope.PERSONAL__PK)))
407         {
408             // Delete user-query maps.
409
List JavaDoc rqus = getRQueryUsers();
410             for (int i=0; i<rqus.size(); i++)
411             {
412                 RQueryUser rqu = (RQueryUser)rqus.get(i);
413                 rqu.delete(user);
414             }
415             setDeleted(true);
416             save();
417             
418         }
419         else
420         {
421             throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction);
422         }
423     }
424
425     /**
426      * Checks if user has permission to delete query.
427      * Only the creating user can delete a personal query.
428      * Only project owner or admin can delete a project-wide query.
429      */

430     public void copyQuery(ScarabUser user)
431          throws Exception JavaDoc
432     {
433          Query newQuery = new Query();
434          newQuery.setName(getName() + " (copy)");
435          newQuery.setDescription(getDescription());
436          newQuery.setValue(getValue());
437          newQuery.setModuleId(getModuleId());
438          newQuery.setIssueTypeId(getIssueTypeId());
439          newQuery.setListId(getListId());
440          newQuery.setApproved(getApproved());
441          newQuery.setCreatedDate(new Date JavaDoc());
442          newQuery.setUserId(user.getUserId());
443          newQuery.setScopeId(getScopeId());
444          newQuery.save();
445
446          RQueryUser rqu = getRQueryUser(user);
447          if (rqu != null)
448          {
449              RQueryUser rquNew = new RQueryUser();
450              rquNew.setQueryId(newQuery.getQueryId());
451              rquNew.setUserId(user.getUserId());
452              rquNew.setSubscriptionFrequency(rqu.getSubscriptionFrequency());
453              rquNew.setIsdefault(rqu.getIsdefault());
454              rquNew.setIsSubscribed(rqu.getIsSubscribed());
455              rquNew.save();
456          }
457     }
458 }
459
Popular Tags