source: trunk/www/admin/groups/edit_group.jsp @ 4889

Last change on this file since 4889 was 4889, checked in by Nicklas Nordborg, 14 years ago

References #1290: Change source files to UTF-8

Changed 'Hakkinen' to 'Häkkinen'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.8 KB
Line 
1<%-- $Id: edit_group.jsp 4889 2009-04-06 12:52: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 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="java.util.Arrays"
53%>
54<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
55<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
56<%
57final Item itemType = Item.GROUP;
58final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
59final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null);
60final int itemId = cc.getId();
61final String ID = sc.getId();
62final float scale = Base.getScale(sc);
63final DbControl dc = sc.newDbControl();
64try
65{
66  String title = null;
67  Group group = null;
68
69  boolean isEveryone = false;
70  boolean isDefault = false;
71  final QuotaType total = QuotaType.getById(dc, SystemItems.getId(QuotaType.TOTAL));
72  Quota currentQuota = null;
73 
74  // Query to retrieve child groups
75  ItemQuery<Group> groupQuery = null;
76  // Query to retrieve user members
77  ItemQuery<User> userQuery = null;
78
79  if (itemId == 0)
80  {
81    title = "Create group";
82    cc.removeObject("item");
83    if (cc.getPropertyFilter("quota.name") != null)
84    {
85      currentQuota = Base.getFirstMatching(dc, Quota.getQuery(), "name", cc.getPropertyFilter("quota.name"));
86    }
87    String[] selected = request.getParameterValues("user_id");
88    if (selected != null)
89    {
90      userQuery = User.getQuery();
91      userQuery.include(Include.ALL);
92      userQuery.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected")));
93      userQuery.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT);
94    }
95    isDefault = Values.getBoolean(cc.getPropertyValue("default"), false);
96  }
97  else
98  {
99    group = Group.getById(dc, itemId);
100    cc.setObject("item", group);
101    title = "Edit group -- " + HTML.encodeTags(group.getName());
102    isEveryone = Group.EVERYONE.equals(group.getSystemId());
103    isDefault = group.isDefault();
104
105    try
106    {
107      currentQuota = group.getQuota();
108    }
109    catch (PermissionDeniedException ex)
110    {}
111   
112    groupQuery = group.getGroups();
113    groupQuery.include(Include.ALL);
114    groupQuery.order(Orders.asc(Hql.property("name")));
115    userQuery = group.getUsers();
116    userQuery.include(Include.ALL);
117    userQuery.order(Orders.asc(Hql.property("name")));
118  }
119  if (group != null && !group.hasPermission(Permission.WRITE))
120  {
121    throw new PermissionDeniedException(Permission.WRITE, itemType.toString());
122  }
123 
124  final boolean readQuota = sc.hasPermission(Permission.READ, Item.QUOTA);
125  final boolean useQuota = sc.hasPermission(Permission.USE, Item.QUOTA);
126  final boolean useUsers = sc.hasPermission(Permission.WRITE, Item.USER);
127  final boolean writeGroups = sc.hasPermission(Permission.WRITE, Item.GROUP);
128  final boolean writeMembers = useUsers && writeGroups;
129 
130  // Query to retrieve quota
131  final ItemQuery<Quota> quotaQuery = Quota.getQuery();
132  quotaQuery.include(Include.ALL);
133  quotaQuery.order(Orders.asc(Hql.property("name")));
134  quotaQuery.setCacheResult(true);
135
136  final String clazz = "class=\"text\"";
137  final String requiredClazz = "class=\"text required\"";
138  %>
139  <base:page type="popup" title="<%=title%>">
140  <base:head scripts="tabcontrol.js,linkitems.js" styles="tabcontrol.css">
141    <script language="JavaScript">
142    // Validate the "Group" tab
143    function validateGroup()
144    {
145      var frm = document.forms['group'];
146      if (Main.trimString(frm.name.value) == '')
147      {
148        alert("You must enter a name");
149        frm.name.focus();
150        return false;
151      }
152      return true;
153    }
154    // Validate the "Members" tab
155    function validateMembers()
156    {
157      return true;
158    }
159
160    // Submit the form
161    function saveSettings()
162    {
163      var frm = document.forms['group'];
164      if (TabControl.validateActiveTab('settings'))
165      {
166        frm.addUsers.value = Link.getActionIds(1, 'U').join(',');
167        frm.removeUsers.value = Link.getActionIds(-1, 'U').join(',');
168        frm.addGroups.value = Link.getActionIds(1, 'G').join(',');
169        frm.removeGroups.value = Link.getActionIds(-1, 'G').join(',');
170        frm.submit();
171      }
172    }
173   
174    function init()
175    {
176      <%
177      if (group == null)
178      {
179        %>
180        var frm = document.forms['group'];
181        frm.name.focus();
182        frm.name.select();
183        <%
184      }
185      %>
186      initMembers();
187    }
188    function initMembers()
189    {
190      var members = document.forms['group'].members;
191      Link.addNewSection(members, new Section('U', 'Users'));
192      <%
193      if (userQuery != null)
194      {
195        ItemResultList<User> users = userQuery.list(dc);
196        String linkMethod = group == null ? "addItem" : "addNewItem";
197        for (User user : users)
198        {
199          %>
200          Link.<%=linkMethod%>(members, new Item('U', <%=user.getId()%>, '<%=HTML.javaScriptEncode(user.getName())%>'));
201          <%
202        }
203      }
204      %>
205      Link.addNewSection(members, new Section('G', 'Groups'));
206      <%
207      if (groupQuery != null)
208      {
209        ItemResultList<Group> children = groupQuery.list(dc);
210        for (Group child : children)
211        {
212          %>
213          Link.addNewItem(members, new Item('G', <%=child.getId()%>, '<%=HTML.javaScriptEncode(child.getName())%>'));
214          <%
215        }
216      }
217      %>
218    }
219    function addUsersOnClick()
220    {
221      var users = Link.getListIds(document.forms['group'].members, 'U');
222      var url = '../users/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addUserCallback';
223      url += '&exclude='+users.join(',');
224      Main.openPopup(url, 'AddUsers', 1000, 700);
225    }
226    function addUserCallback(userId, name)
227    {
228      var item = Link.getItem('U', userId);
229      if (!item) item = new Item('U', userId, name);
230      Link.addItem(document.forms['group'].members, item);
231    }
232    function addGroupsOnClick()
233    {
234      var groups = Link.getListIds(document.forms['group'].members, 'G');
235      var url = '../groups/index.jsp?ID=<%=ID%>&cmd=UpdateContext&mode=selectmultiple&callback=addGroupCallback';
236      url += '&exclude='+groups.join(',');
237      Main.openPopup(url, 'AddGroups', 1000, 700);
238    }
239    function addGroupCallback(groupId, name)
240    {
241      var item = Link.getItem('G', groupId);
242      if (!item) item = new Item('G', groupId, name);
243      Link.addItem(document.forms['group'].members, item);
244    }
245    function removeOnClick()
246    {
247      Link.removeSelected(document.forms['group'].members);
248    }
249 
250    </script>
251  </base:head>
252  <base:body onload="init()">
253    <p>
254    <form action="index.jsp?ID=<%=ID%>" method="post" name="group" onsubmit="return false;">
255    <input type="hidden" name="cmd" value="UpdateItem">
256
257    <h3 class="docked"><%=title%> <base:help tabcontrol="settings" /></h3>
258    <t:tabcontrol id="settings" contentstyle="<%="height: "+(int)(scale*280)+"px;"%>" 
259      position="bottom" remember="<%=group != null%>">
260    <t:tab id="info" title="Group" validate="validateGroup()" helpid="group.edit">
261      <table class="form" cellspacing=0>
262      <tr>
263        <td class="prompt">Name</td>
264        <td><input <%=requiredClazz%> type="text" name="name" 
265          value="<%=HTML.encodeTags(group == null ? Values.getString(cc.getPropertyValue("name"), "New group") : group.getName())%>" 
266          size="40" maxlength="<%=Group.MAX_NAME_LENGTH%>"></td>
267      </tr>
268      <tr>
269        <td class="prompt">Default</td>
270        <td>
271          <input type="radio" name="is_default" value="0" <%=!isDefault ? "checked" : ""%>>no
272          <input type="radio" name="is_default" value="1" <%=isDefault ? "checked" : ""%>>yes
273        </td>
274      </tr> 
275      <tr valign="top">
276        <td class="prompt">Quota</td>
277        <td>
278          <select name="quota_id" <%=isEveryone || !useQuota ? "disabled readonly class=\"disabled\"" : ""%>>
279          <%
280          if (!readQuota)
281          {
282            %>
283            <option value="-1">- denied -
284            <%
285          }
286          else
287          {
288            %>
289            <option value="0">- none -
290            <%
291            ItemResultList<Quota> quotas = quotaQuery.list(dc);
292            for (Quota quota : quotas)
293            {
294              boolean current = quota.equals(currentQuota);
295              if (!current && quota.isRemoved()) continue;
296              int id = quota.getId();
297              long totalBytes = quota.getQuotaValue(total, Location.PRIMARY);
298              String fTotal = totalBytes == Quota.UNLIMITED ? "unlimited" : Values.formatBytes(totalBytes);
299              %>
300              <option 
301                value="<%=current && group != null ? -id : id%>" 
302                <%=current ? "selected" : ""%>
303                ><%=HTML.encodeTags(quota.getName())%> (<%=fTotal%> total)
304              <%
305            }
306          }
307          %>
308        </td>
309      </tr>
310      <tr valign=top>
311        <td class="prompt">Description</td>
312        <td nowrap>
313          <textarea <%=clazz%> rows="4" cols="40" name="description" wrap="virtual"
314            ><%=HTML.encodeTags(group == null ? cc.getPropertyValue("description") : group.getDescription())%></textarea>
315          <a href="javascript:Main.zoom('Description', 'group', 'description')"
316            title="Edit in larger window"><base:icon image="zoom.gif" /></a>
317        </td>
318      </tr>
319      </table>
320      <div align=right>&nbsp;<i><base:icon image="required.gif" /> = required information</i></div>
321    </t:tab>
322   
323    <t:tab id="members" title="Members" tooltip="Add/remove members of this group" 
324      validate="validateMembers()" helpid="group.edit.membership">
325    <table >
326    <tr valign="top">
327    <td>
328      <b>Members</b><br>
329      <select name="members" size="14" multiple <%=isEveryone || !writeMembers ? "disabled readonly class=\"disabled\"" : ""%> style="width: 15em;">
330      </select>
331      <input type="hidden" name="removeUsers" value="">
332      <input type="hidden" name="addUsers" value="">
333      <input type="hidden" name="removeGroups" value="">
334      <input type="hidden" name="addGroups" value="">
335    </td>
336    <td>
337      <br>
338      <table width="150">
339      <tr><td><base:button 
340        onclick="addUsersOnClick()" 
341        title="Add&nbsp;users&hellip;" 
342        tooltip="Add users to this group"
343        disabled="<%=isEveryone || !writeMembers %>" 
344        /></td></tr>
345      <tr><td><base:button 
346        onclick="addGroupsOnClick()" 
347        title="Add&nbsp;groups&hellip;" 
348        tooltip="Add child groups to this group"
349        disabled="<%=isEveryone || !writeMembers %>" 
350      /></td></tr>
351      <tr><td><base:button 
352        onclick="removeOnClick()" 
353        title="Remove" 
354        tooltip="Remove the selected items from this group"
355        disabled="<%=isEveryone || !writeMembers%>" 
356      /></td></tr>
357      </table>
358    </td>
359    </tr>
360    </table>
361    </t:tab>
362    </t:tabcontrol>
363
364    <table align="center">
365    <tr>
366      <td width="50%"><base:button onclick="saveSettings()" title="Save" /></td>
367      <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td>
368    </tr>
369    </table>
370    </form>
371  </base:body>
372  </base:page>
373  <%
374}
375finally
376{
377  if (dc != null) dc.close();
378}
379%>
Note: See TracBrowser for help on using the repository browser.