KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > admin > setting > right > SettingsRightsImpl


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: SettingsRightsImpl.java,v $
31  * Revision 1.4 2005/04/29 02:47:52 colinmacleod
32  * Data bugfixes.
33  * Changed primary key back to Integer.
34  *
35  * Revision 1.3 2005/04/10 20:09:33 colinmacleod
36  * Added new themes.
37  * Changed id type to String.
38  * Changed i tag to em and b tag to strong.
39  * Improved PicoContainerFactory with NanoContainer scripts.
40  *
41  * Revision 1.2 2005/04/09 17:19:05 colinmacleod
42  * Changed copyright text to GPL v2 explicitly.
43  *
44  * Revision 1.1.1.1 2005/03/10 17:50:38 colinmacleod
45  * Restructured ivata op around Hibernate/PicoContainer.
46  * Renamed ivata groupware.
47  *
48  * Revision 1.4 2004/11/12 18:17:09 colinmacleod
49  * Ordered imports.
50  *
51  * Revision 1.3 2004/11/12 15:56:46 colinmacleod
52  * Removed dependencies on SSLEXT.
53  * Moved Persistence classes to ivata masks.
54  *
55  * Revision 1.2 2004/11/03 15:29:30 colinmacleod
56  * Added missing persistenceSession.close().
57  *
58  * Revision 1.1 2004/07/13 19:41:12 colinmacleod
59  * Moved project to POJOs from EJBs.
60  * Applied PicoContainer to services layer (replacing session EJBs).
61  * Applied Hibernate to persistence layer (replacing entity EJBs).
62  *
63  * Revision 1.3 2004/02/10 19:57:20 colinmacleod
64  * Changed email address.
65  *
66  * Revision 1.2 2004/02/01 22:00:32 colinmacleod
67  * Added full names to author tags
68  *
69  * Revision 1.1.1.1 2004/01/27 20:57:46 colinmacleod
70  * Moved open portal to sourceforge.
71  *
72  * Revision 1.5 2003/11/03 11:28:24 jano
73  * commiting addressbook,
74  * tryinjg to fix deploying problem
75  *
76  * Revision 1.4 2003/10/17 12:36:12 jano
77  * fixing problems with building
78  * converting intranet -> portal
79  * Eclipse building
80  *
81  * Revision 1.3 2003/10/15 13:08:06 jano
82  * converting to XDoclet
83  *
84  * Revision 1.2 2003/10/15 13:04:21 colin
85  * fixing for XDoclet
86  *
87  * Revision 1.2 2003/02/25 14:38:13 colin
88  * implemented setModified methods on entity beans thro IvataEntityBean superclass
89  * -----------------------------------------------------------------------------
90  */

91 package com.ivata.groupware.admin.setting.right;
92
93 import java.util.Collection JavaDoc;
94 import java.util.Iterator JavaDoc;
95
96 import com.ivata.groupware.admin.security.server.SecuritySession;
97 import com.ivata.groupware.admin.setting.SettingDO;
98 import com.ivata.groupware.business.BusinessLogic;
99 import com.ivata.groupware.business.addressbook.person.group.GroupConstants;
100 import com.ivata.groupware.business.addressbook.person.group.GroupDO;
101 import com.ivata.groupware.business.addressbook.person.group.right.RightConstants;
102 import com.ivata.groupware.business.addressbook.person.group.right.RightDO;
103 import com.ivata.groupware.business.addressbook.person.group.right.detail.RightDetailDO;
104 import com.ivata.groupware.container.persistence.QueryPersistenceManager;
105 import com.ivata.mask.persistence.PersistenceSession;
106 import com.ivata.mask.util.SystemException;
107
108
109 /**
110  * <p>This session bean manages rigths for changing settings.</p>
111  *
112  * @since 2003-02-11
113  * @author Peter Illes
114  * @version $Revision: 1.4 $
115  *
116  * @ejb.bean
117  * name="SettingsRights"
118  * display-name="SettingRights"
119  * type="Stateless"
120  * view-type="both"
121  * local-jndi-name="SettingsRightsLocal"
122  * jndi-name="SettingsRightsRemote"
123  *
124  *
125  * @ejb.home
126  * generate="false"
127  * remote-class="com.ivata.groupware.admin.setting.right.SettingsRightsRemoteHome"
128  *
129  * @ejb.interface
130  * remote-class="com.ivata.groupware.admin.setting.right.SettingsRightsRemote"
131  */

132 public class SettingsRightsImpl extends BusinessLogic implements SettingsRights {
133     private QueryPersistenceManager persistenceManager;
134     public SettingsRightsImpl(QueryPersistenceManager persistenceManager) {
135         this.persistenceManager = persistenceManager;
136     }
137
138     /**
139      * <p>this method sets a setting to be allowed for users to override the
140      * system value</p>, {see canAmendSetting(name)}
141      * @param name the name of the setting
142      *
143      * @ejb.interface-method
144      * view-type="both"
145      */

146     public void addAmendRightForSetting(final SecuritySession securitySession,
147             final String JavaDoc settingName)
148             throws SystemException {
149         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
150         try {
151             // get the system setting - we always refer to system setting, as
152
// they can't be removed
153
SettingDO setting = (SettingDO)
154                 persistenceManager.findInstance(persistenceSession,
155                 "adminSettingByName", new Object JavaDoc [] {settingName});
156             Integer JavaDoc settingId = setting.getId();
157
158             // detail with meaning amend setting - user level
159
RightDetailDO amendDetail = (RightDetailDO)
160                     persistenceManager.findByPrimaryKey(persistenceSession,
161                     RightDetailDO.class, RightConstants.DETAIL_SETTING_USER);
162
163             Collection JavaDoc amendRight =
164                 persistenceManager.find(persistenceSession,
165                             "rightByAccessDetailTargetId",
166                             new Object JavaDoc [] {RightConstants.ACCESS_AMEND,
167                                 RightConstants.DETAIL_SETTING_USER,
168                                 settingId});
169
170             // if there were no such rights set for this setting, do it now
171
if (amendRight.isEmpty()) {
172                 GroupDO userGroup = (GroupDO)
173                     persistenceManager.findByPrimaryKey(persistenceSession,
174                     GroupDO.class, GroupConstants.USER_GROUP);
175
176                 RightDO right = new RightDO();
177                 right.setAccess(RightConstants.ACCESS_AMEND);
178                 right.setDetail(amendDetail);
179
180                 right.setGroup(userGroup);
181                 right.setTargetId(settingId);
182
183                 persistenceManager.add(persistenceSession, right);
184             }
185         } catch (Exception JavaDoc e) {
186             persistenceSession.cancel();
187             throw new SystemException(e);
188         } finally {
189             persistenceSession.close();
190         }
191     }
192
193     /**
194      * <p>the method finds out whether a setting can be changed (overriden) by
195      * a user</p>
196      * @param name the name of the setting
197      * @return <code>true</code> when this setting can be overridden by user,
198      * <code>false</code> otherwise
199      *
200      * @ejb.interface-method
201      * view-type="both"
202      */

203     public boolean canAmendSetting(final SecuritySession securitySession,
204             final String JavaDoc settingName)
205             throws SystemException {
206         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
207         try {
208             // get the system setting - we always refer to system setting, as
209
// they can't be removed
210
SettingDO setting = (SettingDO)
211                 persistenceManager.findInstance(persistenceSession,
212                 "adminSettingByName", new Object JavaDoc [] {settingName});
213             Integer JavaDoc settingId = setting.getId();
214
215
216             Collection JavaDoc amendRight =
217                 persistenceManager.find(persistenceSession,
218                             "rightByAccessDetailTargetId",
219                             new Object JavaDoc [] {RightConstants.ACCESS_AMEND,
220                                 RightConstants.DETAIL_SETTING_USER,
221                                 settingId});
222
223             // if there were rights, they must be ours
224
return !amendRight.isEmpty();
225         } catch (Exception JavaDoc e) {
226             persistenceSession.cancel();
227             throw new SystemException(e);
228         } finally {
229             persistenceSession.close();
230         }
231     }
232
233
234     /**
235      * <p>the method tells whether a user can amend system settings</p>
236      * @param userName the name of the user
237      * @return <code>true</code> when this user can amend system settings or
238      * <code>false</code> when he can't
239      *
240      * @ejb.interface-method
241      * view-type="both"
242      */

243     public boolean canAmendSystemSettings(final SecuritySession securitySession)
244             throws SystemException {
245         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
246         try {
247             Collection JavaDoc targetIds = persistenceManager.find(persistenceSession,
248                 "rightByUserNameAccessDetail",
249                 new Object JavaDoc []
250                 {
251                     securitySession.getUser().getName(),
252                     RightConstants.ACCESS_AMEND,
253                     RightConstants.DETAIL_SETTING_SYSTEM
254                 });
255
256             // if the collection is nonEmpty, the user has right to change
257
// system settings
258
return !targetIds.isEmpty();
259         } catch (Exception JavaDoc e) {
260             persistenceSession.cancel();
261             throw new SystemException(e);
262         } finally {
263             persistenceSession.close();
264         }
265     }
266
267     /**
268      * <p>this method disables overriding the system value of one setting,
269      * {see canAmendSetting(name)}
270      * @param name the name of the setting
271      *
272      * @ejb.interface-method
273      * view-type="both"
274      */

275     public void removeAmendRightForSetting(final SecuritySession securitySession,
276             final String JavaDoc settingName)
277             throws SystemException {
278         PersistenceSession persistenceSession = persistenceManager.openSession(securitySession);
279         try {
280             // get the system setting - we always refer to system setting, as
281
// they can't be removed
282
SettingDO setting = (SettingDO)
283                 persistenceManager.findInstance(persistenceSession,
284                 "adminSettingByName", new Object JavaDoc [] {settingName});
285             Integer JavaDoc settingId = setting.getId();
286
287             // detail with meaning amend setting - user level
288
RightDetailDO amendDetail = (RightDetailDO)
289                     persistenceManager.findByPrimaryKey(persistenceSession,
290                     RightDetailDO.class, RightConstants.DETAIL_SETTING_USER);
291             Collection JavaDoc amendRight =
292                 persistenceManager.find(persistenceSession,
293                             "rightByAccessDetailTargetId",
294                             new Object JavaDoc [] {RightConstants.ACCESS_AMEND,
295                                 RightConstants.DETAIL_SETTING_USER,
296                                 settingId});
297
298             // remove all rights found, there should be only one though...
299
for (Iterator JavaDoc i = amendRight.iterator(); i.hasNext();) {
300                 RightDO currentRight = (RightDO) i.next();
301                 persistenceManager.remove(persistenceSession, currentRight);
302             }
303         } catch (Exception JavaDoc e) {
304             persistenceSession.cancel();
305             throw new SystemException(e);
306         } finally {
307             persistenceSession.close();
308         }
309     }
310 }
311
Popular Tags