source: branches/3.13-stable/www/views/itemlists/syncfilter/ajax.jsp @ 7546

Last change on this file since 7546 was 7546, checked in by Nicklas Nordborg, 3 years ago

Fixes #2133: Formatting of sync filter

File size: 6.1 KB
Line 
1<%-- $Id$
2  ------------------------------------------------------------------
3  Copyright (C) 2015 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.SyncFilter"
30  import="net.sf.basedb.core.SyncFilter.SourceItemTransform"
31  import="net.sf.basedb.core.PropertyFilter"
32  import="net.sf.basedb.clients.web.Base"
33  import="net.sf.basedb.util.Values"
34  import="net.sf.basedb.util.error.ThrowableUtil"
35  import="net.sf.basedb.util.formatter.Formatter"
36  import="net.sf.basedb.util.formatter.PropertyFilterFormatter"
37  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
38  import="net.sf.basedb.clients.web.WebException"
39  import="net.sf.basedb.clients.web.util.HTML"
40  import="org.json.simple.JSONObject"
41  import="java.util.Date"
42  import="java.util.List"
43%>
44<%!
45  String formatFilter(DbControl dc, SyncFilter syncFilter)
46  {
47    StringBuilder sb = new StringBuilder();
48    Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(dc.getSessionControl());
49    Formatter<PropertyFilter> filterFormatter = new PropertyFilterFormatter(dc, dateFormatter, true);
50    for (int filterRow = 0; filterRow < syncFilter.getFilterRows(); filterRow++)
51    {
52      sb.append(filterRow > 0 ? "\n<operator>OR</operator> " : "");
53      List<String> filters = SyncFilter.getFormattedAndSortedFilters(syncFilter.getPropertyFilters(filterRow), filterFormatter);
54      sb.append(Values.getString(filters, "\n   <operator>AND</operator> ", true));
55    }
56    return sb.toString();
57  }
58%>
59<%
60response.setHeader("Cache-Control", "no-cache, max-age=0");
61final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
62final String ID = sc.getId();
63final String cmd = request.getParameter("cmd");
64final String root = request.getContextPath()+"/";
65out.clear();
66JSONObject json = new JSONObject();
67json.put("status", "ok");
68DbControl dc = null;
69try
70{
71  if ("GetCurrentFilter".equals(cmd))
72  {
73    dc = sc.newDbControl();
74   
75    // Get the current sync filter item we are editing
76    ItemContext cc = sc.getCurrentContext(Item.SYNCFILTER);
77    SyncFilter syncFilter = (SyncFilter)cc.getObject("item");
78   
79    // Get the requested source item type in the edit dialog
80    Item sourceItemType = Item.valueOf(request.getParameter("sourceItemType"));
81    SourceItemTransform sourceItemTransform = SourceItemTransform.valueOf(request.getParameter("sourceItemTransform"));
82   
83    if (sourceItemTransform != syncFilter.getSourceItemTransformation())
84    {
85      syncFilter.setSyncContext(null, sourceItemTransform);
86    }
87
88    // If they match we return the formatted filter, otherwise not
89    if (sourceItemType == syncFilter.getSourceItemType())
90    {
91      json.put("filter", HTML.encodeTags(formatFilter(dc, syncFilter), "value|name|operator|function"));
92    }
93  }
94  else if ("CopySyncFilterToItemContext".equals(cmd))
95  {
96    dc = sc.newDbControl();
97    int syncFilterId = Values.getInt(request.getParameter("syncfilter_id"));
98    String srcType = Values.getStringOrNull(request.getParameter("sourceItemType"));
99   
100    // Get the current sync filter item we are editing
101    SyncFilter syncFilter;
102    Item sourceItemType;
103    if (syncFilterId == 0)
104    {
105      ItemContext cc = sc.getCurrentContext(Item.SYNCFILTER);
106      syncFilter = (SyncFilter)cc.getObject("item");
107    }
108    else
109    {
110      syncFilter = SyncFilter.getById(dc, syncFilterId);
111    }
112   
113    if (srcType == null)
114    {
115      sourceItemType = syncFilter.getSourceItemType();
116    }
117    else
118    {
119      // Get the requested source item type in the edit dialog
120      sourceItemType = Item.valueOf(srcType);
121    }
122   
123    // If they match we copy the filter to the requested item context
124    if (sourceItemType == syncFilter.getSourceItemType())
125    {
126      syncFilter.copyToContext(sc.getCurrentContext(sourceItemType));
127    }
128  }
129  else if ("CopyItemContextToSyncFilter".equals(cmd))
130  {
131    dc = sc.newDbControl();
132    int syncFilterId = Values.getInt(request.getParameter("syncfilter_id"));
133
134    SyncFilter syncFilter;
135    Item sourceItemType;
136    SourceItemTransform transform;
137    if (syncFilterId == 0)
138    {
139      // Get the current sync filter item we are editing
140      ItemContext cc = sc.getCurrentContext(Item.SYNCFILTER);
141      syncFilter = (SyncFilter)cc.getObject("item");
142
143      // Get the requested source item type in the edit dialog
144      sourceItemType = Item.valueOf(request.getParameter("sourceItemType"));
145      transform = SourceItemTransform.valueOf(request.getParameter("sourceItemTransform"));
146    }
147    else
148    {
149      syncFilter = SyncFilter.getById(dc, syncFilterId);
150      sourceItemType = syncFilter.getSourceItemType();
151      transform = syncFilter.getSourceItemTransformation();
152    }
153   
154    // Copy back to the current filter
155    syncFilter.setSyncContext(sc.getCurrentContext(sourceItemType), transform);
156   
157    json.put("filter", HTML.encodeTags(formatFilter(dc, syncFilter)));
158    dc.commit();
159  }
160  else
161  {
162    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
163  }
164}
165catch (Throwable t)
166{
167  t.printStackTrace();
168  json.clear();
169  json.put("status", "error");
170  json.put("message", t.getMessage());
171  json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
172}
173finally
174{
175  json.writeJSONString(out);
176  out.flush();
177  if (dc != null) dc.close();
178}
179%>
Note: See TracBrowser for help on using the repository browser.