source: trunk/www/admin/quota/edit_quota.jsp @ 5908

Last change on this file since 5908 was 5908, checked in by Nicklas Nordborg, 10 years ago

References #1655: GUI improvements

  • File manager dialogs
  • File server edit dialog
  • Changed zoom.gif to png
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1<%-- $Id: edit_quota.jsp 5908 2011-12-13 13:48:46Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23
24  @author Nicklas
25  @version 2.0
26--%>
27<%@ page pageEncoding="UTF-8" session="false"
28  import="net.sf.basedb.core.SessionControl"
29  import="net.sf.basedb.core.DbControl"
30  import="net.sf.basedb.core.Item"
31  import="net.sf.basedb.core.SystemItems"
32  import="net.sf.basedb.core.ItemContext"
33  import="net.sf.basedb.core.Permission"
34  import="net.sf.basedb.core.Quota"
35  import="net.sf.basedb.core.QuotaType"
36  import="net.sf.basedb.core.Location"
37  import="net.sf.basedb.core.ItemQuery"
38  import="net.sf.basedb.core.ItemResultList"
39  import="net.sf.basedb.core.query.Restrictions"
40  import="net.sf.basedb.core.query.Expressions"
41  import="net.sf.basedb.core.query.Orders"
42  import="net.sf.basedb.core.query.Hql"
43  import="net.sf.basedb.core.PermissionDeniedException"
44  import="net.sf.basedb.clients.web.Base"
45  import="net.sf.basedb.clients.web.util.HTML"
46  import="net.sf.basedb.util.Values"
47  import="net.sf.basedb.core.plugin.GuiContext"
48  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
49  import="net.sf.basedb.clients.web.extensions.JspContext"
50  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
51  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
52%>
53<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
54<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
55<%!
56private String getQuotaInput(Quota quota, QuotaType type, Location location)
57{
58  if (location == Location.SECONDARY && !type.hasSecondaryLocation()) return "N/A";
59  long bytes = quota == null ? Quota.UNDEFINED : quota.getQuotaValue(type, location);
60  String fBytes = "";
61  String clazz = "text";
62  if (bytes == Quota.UNLIMITED)
63  {
64    fBytes = "unlimited";
65    clazz += " disabled";
66  }
67  else if (bytes >= 0)
68  {
69    fBytes = Values.formatBytes(bytes);
70  }
71  boolean required = type.getId() == SystemItems.getId(QuotaType.TOTAL) && location == Location.PRIMARY;
72  if (required) clazz += " required";
73
74  StringBuilder sb = new StringBuilder();
75  sb.append("<input type=\"text\" class=\"").append(clazz).append("\"");
76  sb.append(" size=\"16\" maxlength=\"16\"");
77  sb.append(" name=\"").append(location.name()).append(type.getId()).append("\"");
78  sb.append(" value=\"").append(fBytes).append("\"");
79  sb.append(">");
80  sb.append("<input type=\"checkbox\" value=\"1\"");
81  sb.append(" name=\"unlimited_").append(location.name()).append(type.getId()).append("\"");
82  sb.append(bytes==Quota.UNLIMITED ? " checked" : "");
83  sb.append(" onclick=\"setQuotaValue('").append(location.name()).append(type.getId()).append("', this.checked)\"");
84  sb.append(" title=\"Unlimited quota\">\n");
85  return sb.toString();
86}
87%>
88<%
89final Item itemType = Item.QUOTA;
90final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
91final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null);
92final int itemId = cc.getId();
93final String ID = sc.getId();
94final float scale = Base.getScale(sc);
95final DbControl dc = sc.newDbControl();
96try
97{
98  String title = null;
99  Quota quota = null;
100
101  if (itemId == 0)
102  {
103    title = "Create quota";
104    cc.removeObject("item");
105  }
106  else
107  {
108    quota = Quota.getById(dc, itemId);
109    cc.setObject("item", quota);
110    title = "Edit quota -- " + HTML.encodeTags(quota.getName());
111  }
112  if (quota != null && !quota.hasPermission(Permission.WRITE))
113  {
114    throw new PermissionDeniedException(Permission.WRITE, itemType.toString());
115  }
116 
117  final QuotaType total = QuotaType.getById(dc, SystemItems.getId(QuotaType.TOTAL));
118
119  // Query to retrieve quota types
120  final ItemQuery<QuotaType> quotaTypeQuery = QuotaType.getQuery();
121  quotaTypeQuery.order(Orders.asc(Hql.property("id")));
122  quotaTypeQuery.restrict(
123    Restrictions.neq(
124      Hql.property("id"), 
125      Hql.entity(total)
126    )
127  );
128  quotaTypeQuery.setCacheResult(true);
129
130 
131  final String clazz = "class=\"text\"";
132  final String requiredClazz = "class=\"text required\"";
133  JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(itemType), quota);
134  ExtensionsInvoker invoker = EditUtil.useEditExtensions(jspContext);
135  %>
136  <base:page type="popup" title="<%=title%>">
137  <base:head scripts="tabcontrol.js" styles="tabcontrol.css">
138    <ext:scripts context="<%=jspContext%>" />
139    <ext:stylesheets context="<%=jspContext%>" />
140    <script language="JavaScript">
141    // Validate the "Quota" tab
142    function validateQuota()
143    {
144      var frm = document.forms['quota'];
145      if (Main.trimString(frm.name.value) == '')
146      {
147        alert("You must enter a name for the quota.");
148        frm.name.focus();
149        return false;
150      }
151      return true;
152    }
153    // Validate the "Quota values" tab
154    function validateValues()
155    {
156      var frm = document.forms['quota'];
157      if (Main.trimString(frm.<%=Location.PRIMARY.name()+total.getId()%>.value) == '')
158      {
159        TabControl.setActiveTab('settings', 'values');
160        alert('You must enter a total primary quota');
161        frm.<%=Location.PRIMARY.name()+total.getId()%>.focus();
162        return false;
163      }
164      return true;
165    }
166   
167    // Submit the form
168    function saveSettings()
169    {
170      var frm = document.forms['quota'];
171      if (TabControl.validateActiveTab('settings'))
172      {
173        if (!validateValues()) return;
174        frm.submit();
175      }
176    }
177   
178    function setQuotaValue(name, unlimitedChecked)
179    {
180      var frm = document.forms['quota'];;
181      var quota = frm[name];
182      if (unlimitedChecked)
183      {
184        Main.addClass(quota, 'disabled');
185        quota.disabled = true;
186        quota.oldValue = quota.value;
187        quota.value = 'unlimited';
188      }
189      else
190      {
191        Main.removeClass(quota, 'disabled');
192        quota.disabled = false;
193        quota.value = quota.oldValue ? quota.oldValue : '';
194      }
195    }
196
197    function init()
198    {
199      <%
200      if (quota == null)
201      {
202        %>
203        var frm = document.forms['quota'];
204        frm.name.focus();
205        frm.name.select();
206        <%
207      }
208      %>
209    }
210    </script>
211  </base:head>
212  <base:body onload="init()">
213    <p>
214    <form action="index.jsp?ID=<%=ID%>" method="post" name="quota" onsubmit="return false;">
215    <input type="hidden" name="cmd" value="UpdateItem">
216
217    <h3 class="docked"><%=title%> <base:help tabcontrol="settings" /></h3>
218    <t:tabcontrol id="settings" contentstyle="<%="height: "+(int)(scale*320)+"px;"%>" 
219      position="bottom" remember="<%=quota != null%>"
220      extensions="<%=invoker%>">
221    <t:tab id="info" title="Quota" validate="validateQuota()" helpid="quota.edit">
222      <table class="form" cellspacing=0>
223      <tr>
224        <td class="prompt">Name</td>
225        <td><input <%=requiredClazz%> type="text" name="name" 
226          value="<%=HTML.encodeTags(quota == null ? Values.getString(cc.getPropertyValue("name"), "New quota") : quota.getName())%>" 
227          size="40" maxlength="<%=Quota.MAX_NAME_LENGTH%>"></td>
228      </tr>
229      <tr >
230        <td class="prompt">Description</td>
231        <td nowrap>
232          <textarea <%=clazz%> rows="4" cols="40" name="description" 
233            ><%=HTML.encodeTags(quota == null ? cc.getPropertyValue("description") : quota.getDescription())%></textarea>
234          <a href="javascript:Main.zoom('Description', 'quota', 'description')"
235            title="Edit in larger window"><base:icon image="zoom.png" /></a>
236        </td>
237      </tr>
238      </table>
239      <div align=right>&nbsp;<i><base:icon image="required.gif" /> = required information</i></div>
240    </t:tab>
241   
242    <t:tab id="values" title="Quota values" tooltip="Specify maximum disk usage values" 
243      validate="validateValues()" helpid="quota.edit.values">
244      <table class="form" width="100%">
245      <tr>
246        <td></td>
247        <td><b>Primary location</b></td>
248        <td><b>Secondary location</b></td>
249      </tr>
250      <tr>
251        <td class="prompt">Total quota</td>
252        <td><%=getQuotaInput(quota, total, Location.PRIMARY)%></td>
253        <td><%=getQuotaInput(quota, total, Location.SECONDARY)%></td>
254      </tr>
255      <tr>
256        <td colspan="3" style="border-top: 1px solid #cccccc"></td>
257      </tr>
258      <%
259      ItemResultList<QuotaType> quotaTypes = quotaTypeQuery.list(dc);
260      for (QuotaType current : quotaTypes)
261      {
262        %>
263        <tr>
264        <td><%=HTML.encodeTags(current.getName())%></td>
265        <td><%=getQuotaInput(quota, current, Location.PRIMARY)%></td>
266        <td><%=getQuotaInput(quota, current, Location.SECONDARY)%></td>
267        </tr>
268        <%
269      }
270      %>
271      <tr>
272        <td colspan="3">
273          <div align="right">&nbsp;<i><base:icon image="required.gif" /> = required information</i></div>
274            <br>
275            The individual quota values doesn't have to sum up to the total value.
276            The most restrictive value is always used.
277            You can use the following units (case-insensitive):
278            <ul>
279            <li><b>Gb</b> or <b>G</b> for gigabyte values (<code>1.5 Gb</code>)
280            <li><b>Mb</b> or <b>M</b> for megabyte values (<code>10M</code>)
281            <li><b>Kb</b> or <b>K</b> for kilobyte values (<code>800k</code>)
282            <li><b>bytes</b> or <b>b</b> for byte values (<code>50 bytes</code>)
283            </ul>
284        </td>
285      </tr>
286      </table>
287     
288    </t:tab>
289   
290    </t:tabcontrol>
291
292    <table align="center">
293    <tr>
294      <td width="50%"><base:button onclick="saveSettings()" title="Save" /></td>
295      <td width="50%"><base:button onclick="window.close()" title="Cancel" /></td>
296    </tr>
297    </table>
298    </form>
299  </base:body>
300  </base:page>
301  <%
302}
303finally
304{
305  if (dc != null) dc.close();
306}
307%>
Note: See TracBrowser for help on using the repository browser.