KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
50
51 import org.apache.turbine.TemplateContext;
52 import org.apache.turbine.Turbine;
53
54 import org.apache.torque.om.Persistent;
55
56 import org.tigris.scarab.om.Module;
57 import org.tigris.scarab.services.security.ScarabSecurity;
58 import org.tigris.scarab.tools.localization.L10NKeySet;
59 import org.tigris.scarab.util.Email;
60 import org.tigris.scarab.util.EmailContext;
61 import org.tigris.scarab.util.ScarabException;
62
63 /**
64  * This class represents the IssueTemplateInfo object.
65  *
66  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
67  * @version $Id: IssueTemplateInfo.java 9222 2004-11-02 10:22:39Z dabbous $
68  */

69 public class IssueTemplateInfo
70     extends org.tigris.scarab.om.BaseIssueTemplateInfo
71     implements Persistent
72 {
73
74     /**
75      * A new IssueTemplateInfo object
76      */

77     public static IssueTemplateInfo getInstance()
78     {
79         return new IssueTemplateInfo();
80     }
81
82
83     public boolean canDelete(ScarabUser user)
84         throws Exception JavaDoc
85     {
86         // can delete a template if they have delete permission
87
// Or if is their personal template
88
return (user.hasPermission(ScarabSecurity.ITEM__DELETE, getIssue().getModule())
89             || (user.getUserId().equals(getIssue().getCreatedBy().getUserId())
90                 && getScopeId().equals(Scope.PERSONAL__PK)));
91     }
92
93     public boolean canEdit(ScarabUser user)
94         throws Exception JavaDoc
95     {
96         return canDelete(user);
97     }
98
99     public void saveAndSendEmail(ScarabUser user, Module module,
100                                     TemplateContext context)
101         throws Exception JavaDoc
102     {
103         // If it's a module scoped template, user must have Item | Approve
104
// permission, Or its Approved field gets set to false
105
boolean success = true;
106         Issue issue = IssueManager.getInstance(getIssueId());
107
108         // If it's a module template, user must have Item | Approve
109
// permission, or its Approved field gets set to false
110
if (getScopeId().equals(Scope.PERSONAL__PK)
111             || user.hasPermission(ScarabSecurity.ITEM__APPROVE, module))
112         {
113             setApproved(true);
114         }
115         else
116         {
117             setApproved(false);
118             issue.save();
119
120             // Send Email to the people with module edit ability so
121
// that they can approve the new template
122
if (context != null)
123             {
124                 String JavaDoc template = Turbine.getConfiguration().
125                     getString("scarab.email.requireapproval.template",
126                               "RequireApproval.vm");
127                 
128                 ScarabUser[] toUsers = module.getUsers(ScarabSecurity.MODULE__EDIT);
129                 // if the module doesn't have any users, then we need to add at
130
// least ourselves...
131
if (toUsers.length == 0)
132                 {
133                     toUsers = new ScarabUser[1];
134                     toUsers[0] = user;
135                 }
136
137                 EmailContext ectx = new EmailContext();
138                 ectx.setUser(user);
139                 ectx.setModule(module);
140                 ectx.setDefaultTextKey("NewTemplateRequiresApproval");
141
142                 String JavaDoc fromUser = "scarab.email.default";
143                 try
144                 {
145                     Email.sendEmail(ectx,
146                         module,
147                         fromUser,
148                         module.getSystemEmail(),
149                         Arrays.asList(toUsers),
150                         null, template);
151                 }
152                 catch(Exception JavaDoc e)
153                 {
154                     save(); // Not shure about this, but i think it's ok,
155
// because we already did an issue.save(), see above
156
throw e;
157                 }
158             }
159         }
160         save();
161     }
162
163     /*
164      * Checks permission and approves or rejects template. If template
165      * is approved,template type set to "module", else set to "personal".
166      */

167     public void approve(ScarabUser user, boolean approved)
168          throws Exception JavaDoc
169     {
170         Module module = getIssue().getModule();
171
172         if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module))
173         {
174             setApproved(true);
175             if (!approved)
176             {
177                 setScopeId(Scope.PERSONAL__PK);
178             }
179             save();
180         }
181         else
182         {
183             throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction);
184         }
185     }
186
187 }
188
Popular Tags