source: trunk/www/admin/itemsubtypes/ajax.jsp @ 5687

Last change on this file since 5687 was 5687, checked in by Nicklas Nordborg, 12 years ago

References #1597: Subtypes of items

Smarter selection of default subtypes and related items (protocols, etc) have now been implemented in the other important edit dialogs.

Added support for also loading and displaying project default items dynamically based on the selected subtype.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 6.5 KB
Line 
1<%-- $Id: ajax.jsp 5687 2011-08-09 11:39:40Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2011 Nicklas Nordborg
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 3
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with BASE. If not, see <http://www.gnu.org/licenses/>.
20  ------------------------------------------------------------------
21
22  @author Nicklas
23--%>
24<%@ page pageEncoding="UTF-8" session="false" contentType="application/json"
25  import="net.sf.basedb.core.SessionControl"
26  import="net.sf.basedb.core.DbControl"
27  import="net.sf.basedb.core.Item"
28  import="net.sf.basedb.core.ItemContext"
29  import="net.sf.basedb.core.Project"
30  import="net.sf.basedb.core.ItemSubtype"
31  import="net.sf.basedb.core.Subtypable"
32  import="net.sf.basedb.core.BasicItem"
33  import="net.sf.basedb.core.Nameable"
34  import="net.sf.basedb.core.SystemItems"
35  import="net.sf.basedb.core.Protocol"
36  import="net.sf.basedb.core.Hardware"
37  import="net.sf.basedb.core.Software"
38  import="net.sf.basedb.core.InvalidDataException"
39  import="net.sf.basedb.util.Values"
40  import="net.sf.basedb.util.error.ThrowableUtil"
41  import="net.sf.basedb.clients.web.Base"
42  import="net.sf.basedb.clients.web.WebException"
43  import="org.json.simple.JSONObject"
44  import="org.json.simple.JSONArray"
45  import="java.util.List"
46%>
47<%
48final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
49final String ID = sc.getId();
50final String cmd = request.getParameter("cmd");
51final String root = request.getContextPath()+"/";
52final int itemId = Values.getInt(request.getParameter("item_id"));
53DbControl dc = null;
54out.clear();
55JSONObject json = new JSONObject();
56json.put("status", "ok");
57try
58{
59  if ("GetRelatedSubtype".equals(cmd))
60  {
61    Item relatedType = Item.valueOf(request.getParameter("relatedType"));
62    int defaultRelatedId = Values.getInt(request.getParameter("defaultRelatedId"));
63    dc = sc.newDbControl();
64    ItemSubtype related = null;
65    if (itemId > 0)
66    {
67      ItemSubtype subtype = ItemSubtype.getById(dc, itemId);
68      related = subtype.getRelatedSubtype(relatedType);
69    }
70    if (related == null && defaultRelatedId > 0)
71    {
72      related = ItemSubtype.getById(dc, defaultRelatedId);
73    }
74    if (related != null)
75    {
76      json.put("id", related.getId());
77      json.put("name", related.getName());
78    }
79    dc.close();
80  }
81  else if ("GetSubtypeForItem".equals(cmd))
82  {
83    Item itemType = Item.valueOf(request.getParameter("itemType"));
84    dc = sc.newDbControl();
85    Subtypable item = (Subtypable)itemType.getById(dc, itemId);
86    ItemSubtype subtype = item.getItemSubtype();
87    if (subtype != null)
88    {
89      json.put("id", subtype.getId());
90      json.put("name", subtype.getName());
91    }
92    dc.close();
93  }
94  else if ("GetRecentAndRelated".equals(cmd))
95  {
96    dc = sc.newDbControl();
97
98    // The main item's subtype
99    ItemSubtype subtype = itemId == 0 ? null : ItemSubtype.getById(dc, itemId);
100    Item mainItemType = null;
101    if (subtype != null)
102    {
103      mainItemType = subtype.getMainItemType();
104      json.put("id", subtype.getId());
105      json.put("name", subtype.getName());
106    }
107    else
108    {
109      mainItemType = Item.valueOf(request.getParameter("itemType"));
110    }
111
112    // Current context for the main item
113    ItemContext cc = sc.getCurrentContext(mainItemType);
114
115    // Active project
116    Project activeProject = sc.getActiveProjectId() > 0 ? Project.getById(dc, sc.getActiveProjectId()) : null;
117   
118    // Related item types
119    for (String relatedType : request.getParameterValues("relatedType"))
120    {
121      JSONObject jsonITEM = new JSONObject();
122      Item relatedItem = Item.valueOf(relatedType);
123     
124      // Load the related subtype
125      ItemSubtype relatedSubtype = null;
126      if (subtype != null)
127      {
128        relatedSubtype = subtype.getRelatedSubtype(relatedItem);
129        if (relatedSubtype != null)
130        {
131          JSONObject jsonRelated = new JSONObject();
132          jsonRelated.put("id", relatedSubtype.getId());
133          jsonRelated.put("name", relatedSubtype.getName());
134          jsonITEM.put("related", jsonRelated);
135        }
136      }
137     
138      if (relatedSubtype == null)
139      {
140        // Find a default related subtype
141        String systemId = null;
142        if (relatedItem == Item.PROTOCOL)
143        {
144          systemId = Protocol.getDefaultSystemId(mainItemType);
145        }
146        else if (relatedItem == Item.HARDWARE)
147        {
148          systemId = Hardware.getDefaultSystemId(mainItemType);
149        }
150        else if (relatedItem == Item.SOFTWARE)
151        {
152          systemId = Software.getDefaultSystemId(mainItemType);
153        }
154        if (systemId != null) 
155        {
156          relatedSubtype = ItemSubtype.getById(dc, SystemItems.getId(systemId));
157        }
158      }
159     
160      // Load the most recently used items
161      List<? extends BasicItem> recentItems = cc.getRecent(dc, relatedItem, subtype);
162      if (recentItems != null && recentItems.size() > 0)
163      {
164        JSONArray jsonRecent = new JSONArray();
165        for (BasicItem recent : recentItems)
166        {
167          JSONObject jsonRecentItem = new JSONObject();
168          jsonRecentItem.put("id", recent.getId());
169          jsonRecentItem.put("name", ((Nameable)recent).getName());
170          jsonRecent.add(jsonRecentItem);
171        }
172        jsonITEM.put("recent", jsonRecent);
173      }
174     
175      // Load project default items
176      if (activeProject != null)
177      {
178        List<? extends BasicItem> defaultItems = relatedSubtype != null ?
179            activeProject.findDefaultItems(dc, relatedSubtype, false) : activeProject.findDefaultItems(dc, relatedItem, true);
180           
181        JSONArray jsonDefault = new JSONArray();
182        for (BasicItem defaultItem : defaultItems)
183        {
184          JSONObject jsonDefaultItem = new JSONObject();
185          jsonDefaultItem.put("id", defaultItem.getId());
186          jsonDefaultItem.put("name", ((Nameable)defaultItem).getName());
187          jsonDefault.add(jsonDefaultItem);
188        }
189        jsonITEM.put("default", jsonDefault);
190      }
191     
192      json.put(relatedType, jsonITEM);
193    }
194    dc.close();
195  }
196  else
197  {
198    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
199  }
200}
201catch (Throwable t)
202{
203  t.printStackTrace();
204  json.clear();
205  json.put("status", "error");
206  json.put("message", t.getMessage());
207  json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
208}
209finally
210{
211  json.writeJSONString(out);
212  out.flush();
213  if (dc != null) dc.close();
214}
215%>
Note: See TracBrowser for help on using the repository browser.