1 | <%-- $Id: edit_group.jsp 5767 2011-09-28 07:31:39Z nicklas $ |
---|
2 | ------------------------------------------------------------------ |
---|
3 | Copyright (C) 2005 Nicklas Nordborg |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson |
---|
5 | Copyright (C) 2007 Nicklas Nordborg |
---|
6 | |
---|
7 | This file is part of BASE - BioArray Software Environment. |
---|
8 | Available at http://base.thep.lu.se/ |
---|
9 | |
---|
10 | BASE is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License |
---|
12 | as published by the Free Software Foundation; either version 3 |
---|
13 | of the License, or (at your option) any later version. |
---|
14 | |
---|
15 | BASE is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with BASE. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | ------------------------------------------------------------------ |
---|
23 | |
---|
24 | |
---|
25 | @author Nicklas |
---|
26 | @version 2.0 |
---|
27 | --%> |
---|
28 | <%@ page pageEncoding="UTF-8" session="false" |
---|
29 | import="net.sf.basedb.core.SessionControl" |
---|
30 | import="net.sf.basedb.core.DbControl" |
---|
31 | import="net.sf.basedb.core.SystemItems" |
---|
32 | import="net.sf.basedb.core.Item" |
---|
33 | import="net.sf.basedb.core.Type" |
---|
34 | import="net.sf.basedb.core.ItemContext" |
---|
35 | import="net.sf.basedb.core.Include" |
---|
36 | import="net.sf.basedb.core.Permission" |
---|
37 | import="net.sf.basedb.core.Group" |
---|
38 | import="net.sf.basedb.core.User" |
---|
39 | import="net.sf.basedb.core.Quota" |
---|
40 | import="net.sf.basedb.core.QuotaType" |
---|
41 | import="net.sf.basedb.core.Location" |
---|
42 | import="net.sf.basedb.core.ItemQuery" |
---|
43 | import="net.sf.basedb.core.ItemResultList" |
---|
44 | import="net.sf.basedb.core.PermissionDeniedException" |
---|
45 | import="net.sf.basedb.core.query.Orders" |
---|
46 | import="net.sf.basedb.core.query.Hql" |
---|
47 | import="net.sf.basedb.core.query.Restrictions" |
---|
48 | import="net.sf.basedb.core.query.Expressions" |
---|
49 | import="net.sf.basedb.clients.web.Base" |
---|
50 | import="net.sf.basedb.clients.web.util.HTML" |
---|
51 | import="net.sf.basedb.util.Values" |
---|
52 | import="net.sf.basedb.core.plugin.GuiContext" |
---|
53 | import="net.sf.basedb.clients.web.extensions.ExtensionsControl" |
---|
54 | import="net.sf.basedb.clients.web.extensions.JspContext" |
---|
55 | import="net.sf.basedb.clients.web.extensions.edit.EditUtil" |
---|
56 | import="net.sf.basedb.util.extensions.ExtensionsInvoker" |
---|
57 | import="java.util.Arrays" |
---|
58 | %> |
---|
59 | <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %> |
---|
60 | <%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %> |
---|
61 | <% |
---|
62 | final Item itemType = Item.GROUP; |
---|
63 | final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
---|
64 | final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null); |
---|
65 | final int itemId = cc.getId(); |
---|
66 | final String ID = sc.getId(); |
---|
67 | final float scale = Base.getScale(sc); |
---|
68 | final DbControl dc = sc.newDbControl(); |
---|
69 | try |
---|
70 | { |
---|
71 | String title = null; |
---|
72 | Group group = null; |
---|
73 | |
---|
74 | boolean isEveryone = false; |
---|
75 | boolean isDefault = false; |
---|
76 | boolean hiddenMembers = false; |
---|
77 | final QuotaType total = QuotaType.getById(dc, SystemItems.getId(QuotaType.TOTAL)); |
---|
78 | Quota currentQuota = null; |
---|
79 | |
---|
80 | // Query to retrieve child groups |
---|
81 | ItemQuery<Group> groupQuery = null; |
---|
82 | // Query to retrieve user members |
---|
83 | ItemQuery<User> userQuery = null; |
---|
84 | |
---|
85 | if (itemId == 0) |
---|
86 | { |
---|
87 | title = "Create group"; |
---|
88 | cc.removeObject("item"); |
---|
89 | if (cc.getPropertyFilter("quota.name") != null) |
---|
90 | { |
---|
91 | currentQuota = Base.getFirstMatching(dc, Quota.getQuery(), "name", cc.getPropertyFilter("quota.name")); |
---|
92 | } |
---|
93 | String[] selected = request.getParameterValues("user_id"); |
---|
94 | if (selected != null) |
---|
95 | { |
---|
96 | userQuery = User.getQuery(); |
---|
97 | userQuery.include(Include.ALL); |
---|
98 | userQuery.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected"))); |
---|
99 | userQuery.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT); |
---|
100 | } |
---|
101 | isDefault = Values.getBoolean(cc.getPropertyValue("default"), false); |
---|
102 | hiddenMembers = Values.getBoolean(cc.getPropertyValue("hiddenMembers"), false); |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | group = Group.getById(dc, itemId); |
---|
107 | cc.setObject("item", group); |
---|
108 | title = "Edit group -- " + HTML.encodeTags(group.getName()); |
---|
109 | isEveryone = Group.EVERYONE.equals(group.getSystemId()); |
---|
110 | isDefault = group.isDefault(); |
---|
111 | hiddenMembers = group.hasHiddenMembers(); |
---|
112 | |
---|
113 | try |
---|
114 | { |
---|
115 | currentQuota = group.getQuota(); |
---|
116 | } |
---|
117 | catch (PermissionDeniedException ex) |
---|
118 | {} |
---|
119 | |
---|
120 | groupQuery = group.getGroups(); |
---|
121 | groupQuery.include(Include.ALL); |
---|
122 | groupQuery.order(Orders.asc(Hql.property("name"))); |
---|
123 | userQuery = group.getUsers(); |
---|
124 | userQuery.include(Include.ALL); |
---|
125 | userQuery.order(Orders.asc(Hql.property("name"))); |
---|
126 | } |
---|
127 | if (group != null && !group.hasPermission(Permission.WRITE)) |
---|
128 | { |
---|
129 | throw new PermissionDeniedException(Permission.WRITE, itemType.toString()); |
---|
130 | } |
---|
131 | |
---|
132 | final boolean readQuota = sc.hasPermission(Permission.READ, Item.QUOTA); |
---|
133 | final boolean useQuota = sc.hasPermission(Permission.USE, Item.QUOTA); |
---|
134 | final boolean useUsers = sc.hasPermission(Permission.WRITE, Item.USER); |
---|
135 | final boolean writeGroups = sc.hasPermission(Permission.WRITE, Item.GROUP); |
---|
136 | final boolean writeMembers = useUsers && writeGroups; |
---|
137 | |
---|
138 | // Query to retrieve quota |
---|
139 | final ItemQuery<Quota> quotaQuery = Quota.getQuery(); |
---|
140 | quotaQuery.include(Include.ALL); |
---|
141 | quotaQuery.order(Orders.asc(Hql.property("name"))); |
---|
142 | quotaQuery.setCacheResult(true); |
---|
143 | |
---|
144 | final String clazz = "class=\"text\""; |
---|
145 | final String requiredClazz = "class=\"text required\""; |
---|
146 | JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(itemType), group); |
---|
147 | ExtensionsInvoker invoker = EditUtil.useEditExtensions(jspContext); |
---|
148 | %> |
---|
149 | <base:page type="popup" title="<%=title%>"> |
---|
150 | <base:head scripts="tabcontrol.js,linkitems.js" styles="tabcontrol.css"> |
---|
151 | <ext:scripts context="<%=jspContext%>" /> |
---|
152 | <ext:stylesheets context="<%=jspContext%>" /> |
---|
153 | <script language="JavaScript"> |
---|
154 | // Validate the "Group" tab |
---|
155 | function validateGroup() |
---|
156 | { |
---|
157 | var frm = document.forms['group']; |
---|
158 | if (Main.trimString(frm.name.value) == '') |
---|
159 | { |
---|
160 | alert("You must enter a name"); |
---|
161 | frm.name.focus(); |
---|
162 | return false; |
---|
163 | } |
---|
164 | return true; |
---|
165 | } |
---|
166 | // Validate the "Members" tab |
---|
167 | function validateMembers() |
---|
168 | { |
---|
169 | return true; |
---|
170 | } |
---|
171 | |
---|
172 | // Submit the form |
---|
173 | function saveSettings() |
---|
174 | { |
---|
175 | var frm = document.forms['group']; |
---|
176 | if (TabControl.validateActiveTab('settings')) |
---|
177 | { |
---|
178 | frm.addUsers.value = Link.getActionIds(1, 'U').join(','); |
---|
179 | frm.removeUsers.value = Link.getActionIds(-1, 'U').join(','); |
---|
180 | frm.addGroups.value = Link.getActionIds(1, 'G').join(','); |
---|
181 | frm.removeGroups.value = Link.getActionIds(-1, 'G').join(','); |
---|
182 | frm.submit(); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | function init() |
---|
187 | { |
---|
188 | <% |
---|
189 | if (group == null) |
---|
190 | { |
---|
191 | %> |
---|
192 | var frm = document.forms['group']; |
---|
193 | frm.name.focus(); |
---|
194 | frm.name.select(); |
---|
195 | <% |
---|
196 | } |
---|
197 | %> |
---|
198 | initMembers(); |
---|
199 | } |
---|
200 | function initMembers() |
---|
201 | { |
---|
202 | var members = document.forms['group'].members; |
---|
203 | Link.addNewSection(members, new Section('U', 'Users')); |
---|
204 | <% |
---|
205 | if (userQuery != null) |
---|
206 | { |
---|
207 | ItemResultList<User> users = userQuery.list(dc); |
---|
208 | String linkMethod = group == null ? "addItem" : "addNewItem"; |
---|
209 | for (User user : users) |
---|
210 | { |
---|
211 | %> |
---|
212 | Link.<%=linkMethod%>(members, new Item('U', <%=user.getId()%>, '<%=HTML.javaScriptEncode(user.getName())%>')); |
---|
213 | <% |
---|
214 | } |
---|
215 | } |
---|
216 | %> |
---|
217 | Link.addNewSection(members, new Section('G', 'Groups')); |
---|
218 | <% |
---|
219 | if (groupQuery != null) |
---|
220 | { |
---|
221 | ItemResultList<Group> children = groupQuery.list(dc); |
---|
222 | for (Group child : children) |
---|
223 | { |
---|
224 | %> |
---|
225 | Link.addNewItem(members, new Item('G', <%=child.getId()%>, '<%=HTML.javaScriptEncode(child.getName())%>')); |
---|
226 | <% |
---|
227 | } |
---|
228 | } |
---|
229 | %> |
---|
230 | } |
---|
231 | function addUsersOnClick() |
---|
232 | { |
---|
233 | var users = Link.getListIds(document.forms['group'].members, 'U'); |
---|
234 | var url = '../users/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addUserCallback'; |
---|
235 | url += '&exclude='+users.join(','); |
---|
236 | Main.openPopup(url, 'AddUsers', 1000, 700); |
---|
237 | } |
---|
238 | function addUserCallback(userId, name) |
---|
239 | { |
---|
240 | var item = Link.getItem('U', userId); |
---|
241 | if (!item) item = new Item('U', userId, name); |
---|
242 | Link.addItem(document.forms['group'].members, item); |
---|
243 | } |
---|
244 | function addGroupsOnClick() |
---|
245 | { |
---|
246 | var groups = Link.getListIds(document.forms['group'].members, 'G'); |
---|
247 | var url = '../groups/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addGroupCallback'; |
---|
248 | url += '&exclude='+groups.join(','); |
---|
249 | Main.openPopup(url, 'AddGroups', 1000, 700); |
---|
250 | } |
---|
251 | function addGroupCallback(groupId, name) |
---|
252 | { |
---|
253 | var item = Link.getItem('G', groupId); |
---|
254 | if (!item) item = new Item('G', groupId, name); |
---|
255 | Link.addItem(document.forms['group'].members, item); |
---|
256 | } |
---|
257 | function removeOnClick() |
---|
258 | { |
---|
259 | Link.removeSelected(document.forms['group'].members); |
---|
260 | } |
---|
261 | |
---|
262 | </script> |
---|
263 | </base:head> |
---|
264 | <base:body onload="init()"> |
---|
265 | <p> |
---|
266 | <form action="index.jsp?ID=<%=ID%>" method="post" name="group" onsubmit="return false;"> |
---|
267 | <input type="hidden" name="cmd" value="UpdateItem"> |
---|
268 | |
---|
269 | <h3 class="docked"><%=title%> <base:help tabcontrol="settings" /></h3> |
---|
270 | <t:tabcontrol id="settings" contentstyle="<%="height: "+(int)(scale*280)+"px;"%>" |
---|
271 | position="bottom" remember="<%=group != null%>" |
---|
272 | extensions="<%=invoker%>"> |
---|
273 | <t:tab id="info" title="Group" validate="validateGroup()" helpid="group.edit"> |
---|
274 | <table class="form" cellspacing=0> |
---|
275 | <tr> |
---|
276 | <td class="prompt">Name</td> |
---|
277 | <td><input <%=requiredClazz%> type="text" name="name" |
---|
278 | value="<%=HTML.encodeTags(group == null ? Values.getString(cc.getPropertyValue("name"), "New group") : group.getName())%>" |
---|
279 | size="40" maxlength="<%=Group.MAX_NAME_LENGTH%>"></td> |
---|
280 | </tr> |
---|
281 | <tr> |
---|
282 | <td class="prompt">Default</td> |
---|
283 | <td> |
---|
284 | <input type="radio" name="is_default" value="0" <%=!isDefault ? "checked" : ""%>>no |
---|
285 | <input type="radio" name="is_default" value="1" <%=isDefault ? "checked" : ""%>>yes |
---|
286 | </td> |
---|
287 | </tr> |
---|
288 | <tr> |
---|
289 | <td class="prompt">Hidden members</td> |
---|
290 | <td> |
---|
291 | <input type="radio" name="hidden_members" value="0" <%=!hiddenMembers ? "checked" : ""%>>no |
---|
292 | <input type="radio" name="hidden_members" value="1" <%=hiddenMembers ? "checked" : ""%>>yes |
---|
293 | </td> |
---|
294 | </tr> |
---|
295 | <tr valign="top"> |
---|
296 | <td class="prompt">Quota</td> |
---|
297 | <td> |
---|
298 | <select name="quota_id" <%=isEveryone || !useQuota ? "disabled readonly class=\"disabled\"" : ""%>> |
---|
299 | <% |
---|
300 | if (!readQuota) |
---|
301 | { |
---|
302 | %> |
---|
303 | <option value="-1">- denied - |
---|
304 | <% |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | %> |
---|
309 | <option value="0">- none - |
---|
310 | <% |
---|
311 | ItemResultList<Quota> quotas = quotaQuery.list(dc); |
---|
312 | for (Quota quota : quotas) |
---|
313 | { |
---|
314 | boolean current = quota.equals(currentQuota); |
---|
315 | if (!current && quota.isRemoved()) continue; |
---|
316 | int id = quota.getId(); |
---|
317 | long totalBytes = quota.getQuotaValue(total, Location.PRIMARY); |
---|
318 | String fTotal = totalBytes == Quota.UNLIMITED ? "unlimited" : Values.formatBytes(totalBytes); |
---|
319 | %> |
---|
320 | <option |
---|
321 | value="<%=current && group != null ? -id : id%>" |
---|
322 | <%=current ? "selected" : ""%> |
---|
323 | ><%=HTML.encodeTags(quota.getName())%> (<%=fTotal%> total) |
---|
324 | <% |
---|
325 | } |
---|
326 | } |
---|
327 | %> |
---|
328 | </td> |
---|
329 | </tr> |
---|
330 | <tr valign=top> |
---|
331 | <td class="prompt">Description</td> |
---|
332 | <td nowrap> |
---|
333 | <textarea <%=clazz%> rows="4" cols="40" name="description" wrap="virtual" |
---|
334 | ><%=HTML.encodeTags(group == null ? cc.getPropertyValue("description") : group.getDescription())%></textarea> |
---|
335 | <a href="javascript:Main.zoom('Description', 'group', 'description')" |
---|
336 | title="Edit in larger window"><base:icon image="zoom.gif" /></a> |
---|
337 | </td> |
---|
338 | </tr> |
---|
339 | </table> |
---|
340 | <div align=right> <i><base:icon image="required.gif" /> = required information</i></div> |
---|
341 | </t:tab> |
---|
342 | |
---|
343 | <t:tab id="members" title="Members" tooltip="Add/remove members of this group" |
---|
344 | validate="validateMembers()" helpid="group.edit.membership"> |
---|
345 | <table > |
---|
346 | <tr valign="top"> |
---|
347 | <td> |
---|
348 | <b>Members</b><br> |
---|
349 | <select name="members" size="14" multiple <%=isEveryone || !writeMembers ? "disabled readonly class=\"disabled\"" : ""%> style="width: 15em;"> |
---|
350 | </select> |
---|
351 | <input type="hidden" name="removeUsers" value=""> |
---|
352 | <input type="hidden" name="addUsers" value=""> |
---|
353 | <input type="hidden" name="removeGroups" value=""> |
---|
354 | <input type="hidden" name="addGroups" value=""> |
---|
355 | </td> |
---|
356 | <td> |
---|
357 | <br> |
---|
358 | <table width="150"> |
---|
359 | <tr><td><base:button |
---|
360 | clazz="leftaligned buttonclass" |
---|
361 | onclick="addUsersOnClick()" |
---|
362 | title="Add users…" |
---|
363 | tooltip="Add users to this group" |
---|
364 | disabled="<%=isEveryone || !writeMembers %>" |
---|
365 | /></td></tr> |
---|
366 | <tr><td><base:button |
---|
367 | clazz="leftaligned buttonclass" |
---|
368 | onclick="addGroupsOnClick()" |
---|
369 | title="Add groups…" |
---|
370 | tooltip="Add child groups to this group" |
---|
371 | disabled="<%=isEveryone || !writeMembers %>" |
---|
372 | /></td></tr> |
---|
373 | <tr><td><base:button |
---|
374 | clazz="leftaligned buttonclass" |
---|
375 | onclick="removeOnClick()" |
---|
376 | title="Remove" |
---|
377 | tooltip="Remove the selected items from this group" |
---|
378 | disabled="<%=isEveryone || !writeMembers%>" |
---|
379 | /></td></tr> |
---|
380 | </table> |
---|
381 | </td> |
---|
382 | </tr> |
---|
383 | </table> |
---|
384 | </t:tab> |
---|
385 | </t:tabcontrol> |
---|
386 | |
---|
387 | <table align="center"> |
---|
388 | <tr> |
---|
389 | <td width="50%"><base:button onclick="saveSettings()" title="Save" /></td> |
---|
390 | <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td> |
---|
391 | </tr> |
---|
392 | </table> |
---|
393 | </form> |
---|
394 | </base:body> |
---|
395 | </base:page> |
---|
396 | <% |
---|
397 | } |
---|
398 | finally |
---|
399 | { |
---|
400 | if (dc != null) dc.close(); |
---|
401 | } |
---|
402 | %> |
---|