001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2005 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.util.Arrays;
050: import org.apache.torque.TorqueException;
051:
052: import org.apache.turbine.TemplateContext;
053: import org.apache.turbine.Turbine;
054:
055: import org.apache.torque.om.Persistent;
056:
057: import org.tigris.scarab.om.Module;
058: import org.tigris.scarab.services.security.ScarabSecurity;
059: import org.tigris.scarab.tools.localization.L10NKeySet;
060: import org.tigris.scarab.util.Email;
061: import org.tigris.scarab.util.EmailContext;
062: import org.tigris.scarab.util.ScarabException;
063:
064: /**
065: * This class represents the IssueTemplateInfo object.
066: *
067: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
068: * @version $Id: IssueTemplateInfo.java 10214 2006-07-22 09:39:48Z dabbous $
069: */
070: public class IssueTemplateInfo extends BaseIssueTemplateInfo implements
071: Persistent {
072:
073: /**
074: * A new IssueTemplateInfo object
075: */
076: public static IssueTemplateInfo getInstance() {
077: return new IssueTemplateInfo();
078: }
079:
080: public boolean canDelete(ScarabUser user) throws TorqueException {
081: // can delete a template if they have delete permission
082: // Or if is their personal template
083: boolean hasPermission = user.hasPermission(
084: ScarabSecurity.ITEM__DELETE, getIssue().getModule());
085: boolean isCreatedBySelf = user.getUserId().equals(
086: getIssue().getCreatedBy().getUserId());
087: boolean hasScopePersonal = getScopeId().equals(
088: Scope.PERSONAL__PK);
089: return (hasPermission || (isCreatedBySelf && hasScopePersonal));
090: }
091:
092: public boolean canEdit(ScarabUser user) throws TorqueException {
093: return canDelete(user);
094: }
095:
096: public void saveAndSendEmail(final ScarabUser user,
097: final Module module, final TemplateContext context)
098: throws TorqueException, ScarabException {
099: // If it's a module scoped template, user must have Item | Approve
100: // permission, Or its Approved field gets set to false
101: boolean success = true;
102: final Issue issue = IssueManager.getInstance(getIssueId());
103:
104: // If it's a module template, user must have Item | Approve
105: // permission, or its Approved field gets set to false
106: if (getScopeId().equals(Scope.PERSONAL__PK)
107: || user.hasPermission(ScarabSecurity.ITEM__APPROVE,
108: module)) {
109: setApproved(true);
110: } else {
111: setApproved(false);
112: issue.save();
113:
114: // Send Email to the people with module edit ability so
115: // that they can approve the new template
116: if (context != null) {
117: final String template = Turbine
118: .getConfiguration()
119: .getString(
120: "scarab.email.requireapproval.template",
121: "RequireApproval.vm");
122:
123: ScarabUser[] toUsers = module
124: .getUsers(ScarabSecurity.MODULE__EDIT);
125: // if the module doesn't have any users, then we need to add at
126: // least ourselves...
127: if (toUsers.length == 0) {
128: toUsers = new ScarabUser[1];
129: toUsers[0] = user;
130: }
131:
132: final EmailContext ectx = new EmailContext();
133: ectx.setUser(user);
134: ectx.setModule(module);
135: ectx.setDefaultTextKey("NewTemplateRequiresApproval");
136:
137: final String fromUser = "scarab.email.default";
138: try {
139: Email.sendEmail(ectx, module, fromUser, module
140: .getSystemEmail(), Arrays.asList(toUsers),
141: null, template);
142: } catch (Exception e) {
143: save(); // Not shure about this, but i think it's ok,
144: // because we already did an issue.save(), see above
145: throw new ScarabException(
146: L10NKeySet.ExceptionGeneral, e);
147: }
148: }
149: }
150: save();
151: }
152:
153: /*
154: * Checks permission and approves or rejects template. If template
155: * is approved,template type set to "module", else set to "personal".
156: */
157: public void approve(final ScarabUser user, final boolean approved)
158: throws TorqueException, ScarabException {
159: final Module module = getIssue().getModule();
160:
161: if (user.hasPermission(ScarabSecurity.ITEM__APPROVE, module)) {
162: setApproved(true);
163: if (!approved) {
164: setScopeId(Scope.PERSONAL__PK);
165: }
166: save();
167: } else {
168: throw new ScarabException(
169: L10NKeySet.YouDoNotHavePermissionToAction);
170: }
171: }
172:
173: }
|