extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/JsonUtil.java

Code
Comments
Other
Rev Date Author Line
1326 29 Mar 11 nicklas 1 package net.sf.basedb.reggie;
1326 29 Mar 11 nicklas 2
3752 17 Feb 16 nicklas 3 import java.io.BufferedReader;
3752 17 Feb 16 nicklas 4 import java.io.IOException;
3752 17 Feb 16 nicklas 5 import java.io.InputStreamReader;
3752 17 Feb 16 nicklas 6 import java.io.Reader;
3752 17 Feb 16 nicklas 7 import java.nio.charset.Charset;
1326 29 Mar 11 nicklas 8 import java.util.Collection;
1326 29 Mar 11 nicklas 9
3752 17 Feb 16 nicklas 10 import javax.servlet.http.HttpServletRequest;
2598 22 Aug 14 nicklas 11 import javax.servlet.http.HttpServletResponse;
2598 22 Aug 14 nicklas 12
2918 11 Nov 14 nicklas 13 import net.sf.basedb.core.AnyToAny;
2918 11 Nov 14 nicklas 14 import net.sf.basedb.core.BasicItem;
1826 07 Feb 13 nicklas 15 import net.sf.basedb.core.BioPlate;
1826 07 Feb 13 nicklas 16 import net.sf.basedb.core.BioWell;
2918 11 Nov 14 nicklas 17 import net.sf.basedb.core.DbControl;
5730 18 Nov 19 nicklas 18 import net.sf.basedb.core.File;
1826 07 Feb 13 nicklas 19 import net.sf.basedb.core.Hardware;
3782 11 Mar 16 nicklas 20 import net.sf.basedb.core.Item;
2364 15 Apr 14 nicklas 21 import net.sf.basedb.core.Job;
2918 11 Nov 14 nicklas 22 import net.sf.basedb.core.Nameable;
1826 07 Feb 13 nicklas 23 import net.sf.basedb.core.Permission;
1826 07 Feb 13 nicklas 24 import net.sf.basedb.core.Protocol;
4603 02 Oct 17 nicklas 25 import net.sf.basedb.core.Software;
3787 17 Mar 16 nicklas 26 import net.sf.basedb.core.SystemItems;
3787 17 Mar 16 nicklas 27 import net.sf.basedb.core.User;
1333 05 Apr 11 nicklas 28 import net.sf.basedb.reggie.converter.ValueConverter;
3787 17 Mar 16 nicklas 29 import net.sf.basedb.reggie.dao.ReggieRole;
3810 23 Mar 16 nicklas 30 import net.sf.basedb.reggie.json.LoadMoreJson;
3743 12 Feb 16 nicklas 31 import net.sf.basedb.util.Values;
1333 05 Apr 11 nicklas 32
3743 12 Feb 16 nicklas 33 import org.jdom2.Element;
1326 29 Mar 11 nicklas 34 import org.json.simple.JSONArray;
1826 07 Feb 13 nicklas 35 import org.json.simple.JSONObject;
3752 17 Feb 16 nicklas 36 import org.json.simple.parser.JSONParser;
3752 17 Feb 16 nicklas 37 import org.json.simple.parser.ParseException;
1326 29 Mar 11 nicklas 38
1326 29 Mar 11 nicklas 39 /**
1326 29 Mar 11 nicklas 40   Utility functions for JSON.
1326 29 Mar 11 nicklas 41   @since 1.0
1326 29 Mar 11 nicklas 42 */
1326 29 Mar 11 nicklas 43 public class JsonUtil 
1326 29 Mar 11 nicklas 44 {
2598 22 Aug 14 nicklas 45   /**
2598 22 Aug 14 nicklas 46     Set proper response headers for returning a JSON response.
2598 22 Aug 14 nicklas 47     This sets the content type to: application/json;charset=UTF8
2598 22 Aug 14 nicklas 48     and disable caching of the responses.
2598 22 Aug 14 nicklas 49   */
2598 22 Aug 14 nicklas 50   public static void setJsonResponseHeaders(HttpServletResponse resp)
2598 22 Aug 14 nicklas 51   {
2598 22 Aug 14 nicklas 52     resp.setContentType("application/json");
2598 22 Aug 14 nicklas 53     resp.setCharacterEncoding("UTF-8");
2598 22 Aug 14 nicklas 54     resp.setHeader("Cache-Control", "no-cache, max-age=0");
2598 22 Aug 14 nicklas 55   }
1326 29 Mar 11 nicklas 56
1326 29 Mar 11 nicklas 57   /**
3752 17 Feb 16 nicklas 58     Parse the request as JSON data.
3752 17 Feb 16 nicklas 59     @since 4.1.1
3752 17 Feb 16 nicklas 60   */
3752 17 Feb 16 nicklas 61   public static JSONObject parseRequest(HttpServletRequest request)
3752 17 Feb 16 nicklas 62     throws IOException, ParseException
3752 17 Feb 16 nicklas 63   {
3752 17 Feb 16 nicklas 64     Reader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), Charset.forName("UTF-8")));
3752 17 Feb 16 nicklas 65     JSONObject json = (JSONObject)new JSONParser().parse(reader);
3752 17 Feb 16 nicklas 66     return json;
3752 17 Feb 16 nicklas 67   }
3752 17 Feb 16 nicklas 68   
3752 17 Feb 16 nicklas 69   /**
1326 29 Mar 11 nicklas 70     Create a JSON array from a collection.
1326 29 Mar 11 nicklas 71   */
1333 05 Apr 11 nicklas 72   public static <F> JSONArray toArray(Collection<F> values, ValueConverter<F, ?> converter)
1326 29 Mar 11 nicklas 73   {
1326 29 Mar 11 nicklas 74     JSONArray array = new JSONArray();
1333 05 Apr 11 nicklas 75     if (values != null)
1326 29 Mar 11 nicklas 76     {
1333 05 Apr 11 nicklas 77       for (F value : values)
1333 05 Apr 11 nicklas 78       {
1333 05 Apr 11 nicklas 79         array.add(converter.convert(value));
1333 05 Apr 11 nicklas 80       }
1326 29 Mar 11 nicklas 81     }
1326 29 Mar 11 nicklas 82     return array;
1326 29 Mar 11 nicklas 83   }
1333 05 Apr 11 nicklas 84
1940 18 Apr 13 nicklas 85   /**
5091 14 Nov 18 nicklas 86     Creates an JSON array of a number of JSON objects. 
5091 14 Nov 18 nicklas 87     'null' items are not included.
5091 14 Nov 18 nicklas 88     @since 4.21
5091 14 Nov 18 nicklas 89   */
5091 14 Nov 18 nicklas 90   public static JSONArray asArray(JSONObject... items)
5091 14 Nov 18 nicklas 91   {
5091 14 Nov 18 nicklas 92     JSONArray json = new JSONArray();
5091 14 Nov 18 nicklas 93     for (JSONObject o : items)
5091 14 Nov 18 nicklas 94     {
5091 14 Nov 18 nicklas 95       if (o != null) json.add(o);
5091 14 Nov 18 nicklas 96     }
5091 14 Nov 18 nicklas 97     return json;
5091 14 Nov 18 nicklas 98   }
5091 14 Nov 18 nicklas 99
5091 14 Nov 18 nicklas 100   /**
1940 18 Apr 13 nicklas 101     Get a value from a JSON object as a Float value, or null
1940 18 Apr 13 nicklas 102     if no value exists.
1940 18 Apr 13 nicklas 103     @since 2.12
1940 18 Apr 13 nicklas 104   */
1940 18 Apr 13 nicklas 105   public static Float getAsFloat(JSONObject json, String key)
1940 18 Apr 13 nicklas 106   {
1940 18 Apr 13 nicklas 107     Number n = (Number)json.get(key);
1940 18 Apr 13 nicklas 108     return n == null ? null : n.floatValue();
1940 18 Apr 13 nicklas 109   }
1826 07 Feb 13 nicklas 110   
3743 12 Feb 16 nicklas 111   /**
3743 12 Feb 16 nicklas 112     Get the <experimental-features> from reggie-config.xml as a
3743 12 Feb 16 nicklas 113     JSONObject. Child tag names are used as keys and the value
3743 12 Feb 16 nicklas 114     is expected to be 0 or 1.
3743 12 Feb 16 nicklas 115     @since 4.1
3743 12 Feb 16 nicklas 116   */
3743 12 Feb 16 nicklas 117   public static JSONObject getExperimentalFeatures()
3743 12 Feb 16 nicklas 118   {
3743 12 Feb 16 nicklas 119     JSONObject json = new JSONObject();
3743 12 Feb 16 nicklas 120     Element root = Reggie.getConfig().getRoot();
3743 12 Feb 16 nicklas 121     Element experimentalFeatures = root.getChild("experimental-features");
3743 12 Feb 16 nicklas 122     if (experimentalFeatures != null)
3743 12 Feb 16 nicklas 123     {
3743 12 Feb 16 nicklas 124       for (Element feature : experimentalFeatures.getChildren())
3743 12 Feb 16 nicklas 125       {
3743 12 Feb 16 nicklas 126         json.put(feature.getName(), Values.getBoolean(feature.getTextTrim()) ? 1 : 0);
3743 12 Feb 16 nicklas 127       }
3743 12 Feb 16 nicklas 128     }
3743 12 Feb 16 nicklas 129     return json;
3743 12 Feb 16 nicklas 130   }
3743 12 Feb 16 nicklas 131   
3787 17 Mar 16 nicklas 132   /**
3787 17 Mar 16 nicklas 133     Get the permissions of the logged in user as a JSON object.
3787 17 Mar 16 nicklas 134     @since 4.3
3787 17 Mar 16 nicklas 135   */
3787 17 Mar 16 nicklas 136   public static JSONObject getPermissions(DbControl dc)
3787 17 Mar 16 nicklas 137   {
3787 17 Mar 16 nicklas 138     boolean isRoot = dc.getSessionControl().getLoggedInUserId() == SystemItems.getId(User.ROOT);
3787 17 Mar 16 nicklas 139     JSONObject jsonPermissions = new JSONObject();
3787 17 Mar 16 nicklas 140     jsonPermissions.put("Administrator", isRoot|| ReggieRole.ADMINISTRATOR.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 141     jsonPermissions.put("PatientCurator", ReggieRole.PATIENT_CURATOR.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 142     jsonPermissions.put("SamplePrep", ReggieRole.SAMPLE_PREP.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 143     jsonPermissions.put("PrepCurator", ReggieRole.PREP_CURATOR.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 144     jsonPermissions.put("Histology", ReggieRole.HISTOLOGY.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 145     jsonPermissions.put("LibraryPrep", ReggieRole.LIBRARY_PREP.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 146     jsonPermissions.put("LibraryPlateDesigner", ReggieRole.LIBRARY_PLATE_DESIGNER.isMember(dc) ? 1 : 0);
5405 08 May 19 nicklas 147     jsonPermissions.put("MIPsLibraryPrep", ReggieRole.MIPS_LIBRARY_PREP.isMember(dc) ? 1 : 0);
5405 08 May 19 nicklas 148     jsonPermissions.put("MIPsPlateDesigner", ReggieRole.MIPS_PLATE_DESIGNER.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 149     jsonPermissions.put("SecondaryAnalysis", ReggieRole.SECONDARY_ANALYSIS.isMember(dc) ? 1 : 0);
5485 12 Jun 19 nicklas 150     jsonPermissions.put("MIPsSecondaryAnalysis", ReggieRole.MIPS_SECONDARY_ANALYSIS.isMember(dc) ? 1 : 0);
3787 17 Mar 16 nicklas 151     return jsonPermissions;
3787 17 Mar 16 nicklas 152   }
3787 17 Mar 16 nicklas 153   
1826 07 Feb 13 nicklas 154   public static JSONObject getProtocolAsJSON(Protocol p)
1826 07 Feb 13 nicklas 155   {
1826 07 Feb 13 nicklas 156     if (p == null) return null;
1826 07 Feb 13 nicklas 157     JSONObject json = new JSONObject();
1826 07 Feb 13 nicklas 158     json.put("id", p.getId());
1826 07 Feb 13 nicklas 159     json.put("name", p.getName());
1826 07 Feb 13 nicklas 160     json.put("editable", p.hasPermission(Permission.WRITE));
1826 07 Feb 13 nicklas 161     return json;
1826 07 Feb 13 nicklas 162   }
1826 07 Feb 13 nicklas 163
4603 02 Oct 17 nicklas 164   public static JSONObject getSoftwareAsJSON(Software s)
4603 02 Oct 17 nicklas 165   {
4603 02 Oct 17 nicklas 166     if (s == null) return null;
4603 02 Oct 17 nicklas 167     JSONObject json = new JSONObject();
4603 02 Oct 17 nicklas 168     json.put("id", s.getId());
4603 02 Oct 17 nicklas 169     json.put("name", s.getName());
4603 02 Oct 17 nicklas 170     json.put("editable", s.hasPermission(Permission.WRITE));
4603 02 Oct 17 nicklas 171     return json;
4603 02 Oct 17 nicklas 172   }
4603 02 Oct 17 nicklas 173
2134 11 Nov 13 nicklas 174   public static JSONObject getBioWellAsJSON(BioWell well, boolean includePlateInfo)
1826 07 Feb 13 nicklas 175   {
1826 07 Feb 13 nicklas 176     if (well == null) return null;
1826 07 Feb 13 nicklas 177     JSONObject json = new JSONObject();
1826 07 Feb 13 nicklas 178
1826 07 Feb 13 nicklas 179     json.put("id", well.getId());
1826 07 Feb 13 nicklas 180     json.put("row", well.getRow());
1826 07 Feb 13 nicklas 181     json.put("column", well.getColumn());
1826 07 Feb 13 nicklas 182     json.put("location", well.getCoordinate());
1826 07 Feb 13 nicklas 183     json.put("canAdd", well.canAddBioMaterial());
1826 07 Feb 13 nicklas 184     json.put("canRemove", well.canClearBioMaterial());
2134 11 Nov 13 nicklas 185     
2134 11 Nov 13 nicklas 186     if (includePlateInfo)
2134 11 Nov 13 nicklas 187     {        
2134 11 Nov 13 nicklas 188       BioPlate plate = well.getPlate();
2134 11 Nov 13 nicklas 189       JSONObject jsonPlate = new JSONObject();
2134 11 Nov 13 nicklas 190       jsonPlate.put("id", plate.getId());
2134 11 Nov 13 nicklas 191       jsonPlate.put("name", plate.getName());
2134 11 Nov 13 nicklas 192       jsonPlate.put("barcode", plate.getBarcode());
2134 11 Nov 13 nicklas 193       jsonPlate.put("editable", plate.hasPermission(Permission.WRITE));
1826 07 Feb 13 nicklas 194       
2134 11 Nov 13 nicklas 195       Hardware storage = plate.getFreezer();
2134 11 Nov 13 nicklas 196       if (storage != null || plate.getSection() != null || plate.getTray() != null || plate.getPosition() != null)
1826 07 Feb 13 nicklas 197       {
2134 11 Nov 13 nicklas 198         JSONObject jsonStorage = new JSONObject();
2134 11 Nov 13 nicklas 199         if (storage != null)
2134 11 Nov 13 nicklas 200         {
2134 11 Nov 13 nicklas 201           jsonStorage.put("id", storage.getId());
2134 11 Nov 13 nicklas 202           jsonStorage.put("name", storage.getName());
2134 11 Nov 13 nicklas 203         }
2134 11 Nov 13 nicklas 204         jsonStorage.put("section", plate.getSection());
2134 11 Nov 13 nicklas 205         jsonStorage.put("tray", plate.getTray());
2134 11 Nov 13 nicklas 206         jsonStorage.put("position", plate.getPosition());
2134 11 Nov 13 nicklas 207         jsonPlate.put("storage", jsonStorage);
1826 07 Feb 13 nicklas 208       }
2134 11 Nov 13 nicklas 209       
2134 11 Nov 13 nicklas 210       json.put("bioPlate", jsonPlate);
1826 07 Feb 13 nicklas 211     }
1826 07 Feb 13 nicklas 212     return json;
1826 07 Feb 13 nicklas 213   }
1826 07 Feb 13 nicklas 214
2364 15 Apr 14 nicklas 215   /**
2364 15 Apr 14 nicklas 216     Get job information as a JSON object.
2364 15 Apr 14 nicklas 217     @since 2.16
2364 15 Apr 14 nicklas 218   */
2364 15 Apr 14 nicklas 219   public static JSONObject getJobAsJSON(Job job)
2364 15 Apr 14 nicklas 220   {
2364 15 Apr 14 nicklas 221     if (job == null) return null;
2364 15 Apr 14 nicklas 222     JSONObject json = new JSONObject();
2364 15 Apr 14 nicklas 223     json.put("id", job.getId());
2364 15 Apr 14 nicklas 224     json.put("name", job.getName());
2364 15 Apr 14 nicklas 225     json.put("editable", job.hasPermission(Permission.WRITE));
2364 15 Apr 14 nicklas 226     json.put("status", job.getStatus().name());
2364 15 Apr 14 nicklas 227     json.put("statusMessage", job.getStatusMessage());
2914 11 Nov 14 nicklas 228     json.put("percentComplete", job.getPercentComplete());
2364 15 Apr 14 nicklas 229     json.put("scheduled", Reggie.CONVERTER_DATETIME_TO_STRING.convert(job.getScheduled()));
2364 15 Apr 14 nicklas 230     json.put("started", Reggie.CONVERTER_DATETIME_TO_STRING.convert(job.getStarted()));
2364 15 Apr 14 nicklas 231     json.put("ended", Reggie.CONVERTER_DATETIME_TO_STRING.convert(job.getEnded()));
2364 15 Apr 14 nicklas 232     json.put("externalId", job.getExternalId());
2364 15 Apr 14 nicklas 233     json.put("server", job.getServer());
6453 22 Oct 21 nicklas 234     json.put("serverNode", job.getServerAndNode());
2364 15 Apr 14 nicklas 235     return json;
2364 15 Apr 14 nicklas 236     
2364 15 Apr 14 nicklas 237   }
2918 11 Nov 14 nicklas 238   
2918 11 Nov 14 nicklas 239   /**
2918 11 Nov 14 nicklas 240     Load an any-to-any item and return id and name as an json object.
3782 11 Mar 16 nicklas 241     @since 2.18, 4.3
2918 11 Nov 14 nicklas 242   */
3810 23 Mar 16 nicklas 243   @SuppressWarnings({ "unchecked", "rawtypes" })
3810 23 Mar 16 nicklas 244   public static JSONObject loadLinkedItem(DbControl dc, BasicItem item, String linkName, Item linkedType, LoadMoreJson moreJson)
2918 11 Nov 14 nicklas 245   {
2918 11 Nov 14 nicklas 246     JSONObject json = null;
2918 11 Nov 14 nicklas 247     try
2918 11 Nov 14 nicklas 248     {
2918 11 Nov 14 nicklas 249       AnyToAny link = AnyToAny.getByName(dc, item, linkName);
3782 11 Mar 16 nicklas 250       if (linkedType == null || linkedType == link.getToType())
3782 11 Mar 16 nicklas 251       {
3782 11 Mar 16 nicklas 252         Nameable linkedItem = (Nameable)link.getTo();
3782 11 Mar 16 nicklas 253         json = new JSONObject();
3782 11 Mar 16 nicklas 254         json.put("id", linkedItem.getId());
3782 11 Mar 16 nicklas 255         json.put("name", linkedItem.getName());
3782 11 Mar 16 nicklas 256         json.put("type", linkedItem.getType().name());
3810 23 Mar 16 nicklas 257         if (moreJson != null) moreJson.addMore(dc, json, linkedItem);
3782 11 Mar 16 nicklas 258       }
2918 11 Nov 14 nicklas 259     }
2918 11 Nov 14 nicklas 260     catch (RuntimeException ex)
2918 11 Nov 14 nicklas 261     {} // Typically, ItemNotFound if no PDF exists
2918 11 Nov 14 nicklas 262     return json;
2918 11 Nov 14 nicklas 263   }
5730 18 Nov 19 nicklas 264   
5730 18 Nov 19 nicklas 265   /**
5730 18 Nov 19 nicklas 266     @since 4.24
5730 18 Nov 19 nicklas 267   */
5730 18 Nov 19 nicklas 268   public static JSONObject getFileAsJSON(File file)
5730 18 Nov 19 nicklas 269   {
5730 18 Nov 19 nicklas 270     JSONObject jsonFile = new JSONObject();
5730 18 Nov 19 nicklas 271     jsonFile.put("id", file.getId());
5730 18 Nov 19 nicklas 272     jsonFile.put("name", file.getName());
5730 18 Nov 19 nicklas 273     jsonFile.put("size", file.getSize());
5730 18 Nov 19 nicklas 274     jsonFile.put("path", file.getPath().toString());
5730 18 Nov 19 nicklas 275     return jsonFile;
5730 18 Nov 19 nicklas 276   }
2918 11 Nov 14 nicklas 277
1326 29 Mar 11 nicklas 278 }