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 org.apache.torque.TorqueException;
050: import org.apache.torque.om.Persistent;
051: import org.apache.torque.util.Criteria;
052: import org.tigris.scarab.services.security.ScarabSecurity;
053: import org.tigris.scarab.tools.ScarabLocalizationTool;
054: import org.tigris.scarab.tools.localization.L10NKey;
055: import org.tigris.scarab.tools.localization.L10NKeySet;
056: import org.tigris.scarab.tools.localization.L10NMessage;
057: import org.apache.fulcrum.security.util.TurbineSecurityException;
058: import org.tigris.scarab.util.Log;
059: import org.tigris.scarab.util.ScarabConstants;
060:
061: import java.util.HashSet;
062: import java.util.List;
063: import java.util.Iterator;
064: import java.util.Set;
065:
066: /**
067: * This class is for dealing with Attributes associated to Users and Modules.
068: *
069: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
070: * @author <a href="mailto:jon@collab.net">Jon Scott Stevens</a>
071: * @version $Id: RModuleUserAttribute.java 10080 2006-04-30 23:38:30Z jorgeuriarte $
072: */
073: public class RModuleUserAttribute extends BaseRModuleUserAttribute
074: implements Persistent {
075: /**
076: * Delete the record. Must have USER__EDIT_PREFERENCES to be
077: * able to execute this method.
078: */
079: public void delete(final ScarabUser user) throws TorqueException,
080: TurbineSecurityException {
081: boolean hasPermission = true;
082: if (getModule() != null) {
083: hasPermission = user.hasPermission(
084: ScarabSecurity.USER__EDIT_PREFERENCES, getModule());
085: } else //getModule() is null for X Project queries, so get the modules from the MIT List
086: {
087: final List moduleList = user.getCurrentMITList()
088: .getModules();
089: for (Iterator iter = moduleList.iterator(); iter.hasNext();) {
090: hasPermission = user.hasPermission(
091: ScarabSecurity.USER__EDIT_PREFERENCES,
092: (Module) iter.next());
093: if (!hasPermission) {
094: break;
095: }
096: }
097: }
098: if (hasPermission) {
099: final Criteria c = new Criteria().add(
100: RModuleUserAttributePeer.MODULE_ID, getModuleId())
101: .add(RModuleUserAttributePeer.USER_ID, getUserId())
102: .add(RModuleUserAttributePeer.ISSUE_TYPE_ID,
103: getIssueTypeId()).add(
104: RModuleUserAttributePeer.LIST_ID,
105: getListId()).add(
106: RModuleUserAttributePeer.ATTRIBUTE_ID,
107: getAttributeId());
108: RModuleUserAttributePeer.doDelete(c);
109: } else {
110: throw new TurbineSecurityException(
111: ScarabConstants.NO_PERMISSION_MESSAGE); //EXCEPTION
112: }
113: }
114:
115: public void setInternalAttribute(String v) {
116: try {
117: if (v != null) {
118: this .setAttributeId(new Integer(0));
119: }
120: } catch (TorqueException e) {
121: Log.get().error("setInternalAttribute(): " + e);
122: }
123: super .setInternalAttribute(v);
124: }
125:
126: public static Attribute MODIFIED_BY = new Attribute();
127: public static Attribute MODIFIED_DATE = new Attribute();
128: public static Attribute CREATED_BY = new Attribute();
129: public static Attribute CREATED_DATE = new Attribute();
130:
131: public static Set internalAttributes = new HashSet();
132: static {
133: MODIFIED_BY.setName(L10NKeySet.ModifiedBy.toString());
134: MODIFIED_DATE.setName(L10NKeySet.ModifiedDate.toString());
135: CREATED_BY.setName(L10NKeySet.CreatedBy.toString());
136: CREATED_DATE.setName(L10NKeySet.CreatedDate.toString());
137: internalAttributes.add(MODIFIED_BY);
138: internalAttributes.add(MODIFIED_DATE);
139: internalAttributes.add(CREATED_BY);
140: internalAttributes.add(CREATED_DATE);
141: }
142:
143: public boolean isInternal() {
144: boolean bInternal = false;
145: if (this .getAttributeId() == null
146: || this .getAttributeId().equals(new Integer(0))) {
147: for (Iterator it = internalAttributes.iterator(); it
148: .hasNext()
149: && !bInternal;) {
150: Attribute at = (Attribute) it.next();
151: bInternal = at.getName().equals(
152: this .getInternalAttribute());
153: }
154: }
155: return bInternal;
156: }
157:
158: /**
159: * Returns the proper name for the subyacent attribute, be it
160: * internal or a regular attribute.
161: *
162: * @return
163: */
164: public String getName() {
165: String name = this .getInternalAttribute();
166: try {
167: if (this .getAttributeId().intValue() != 0)
168: name = this .getAttribute().getName();
169: } catch (TorqueException e) {
170: getLog().error("getName(): " + e);
171: }
172:
173: return name;
174: }
175:
176: /**
177: * Returns a localized (if possible) version of the
178: * attribute name. It only applies to internal attributes, so
179: * any other attribute name will be returned unchanged.
180: *
181: * @param l10n
182: * @return
183: */
184: public String getName(ScarabLocalizationTool l10n) {
185: String attrName = this .getName();
186: if (this .isInternal()) {
187: // Internal attribute names are localizable
188: L10NKey key = new L10NKey(attrName);
189: attrName = (new L10NMessage(key)).getMessage(l10n);
190: }
191: return attrName;
192: }
193:
194: }
|