extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/SubtypeServlet.java

Code
Comments
Other
Rev Date Author Line
1543 28 Feb 12 nicklas 1 package net.sf.basedb.reggie.servlet;
1543 28 Feb 12 nicklas 2
1543 28 Feb 12 nicklas 3 import java.io.IOException;
1543 28 Feb 12 nicklas 4
1543 28 Feb 12 nicklas 5 import javax.servlet.ServletException;
1543 28 Feb 12 nicklas 6 import javax.servlet.http.HttpServlet;
1543 28 Feb 12 nicklas 7 import javax.servlet.http.HttpServletRequest;
1543 28 Feb 12 nicklas 8 import javax.servlet.http.HttpServletResponse;
1543 28 Feb 12 nicklas 9
1543 28 Feb 12 nicklas 10 import org.json.simple.JSONObject;
1543 28 Feb 12 nicklas 11
1543 28 Feb 12 nicklas 12 import net.sf.basedb.core.DbControl;
1544 29 Feb 12 nicklas 13 import net.sf.basedb.core.ItemNotFoundException;
1543 28 Feb 12 nicklas 14 import net.sf.basedb.core.SessionControl;
2598 22 Aug 14 nicklas 15 import net.sf.basedb.reggie.JsonUtil;
3975 26 May 16 nicklas 16 import net.sf.basedb.reggie.Reggie;
5435 17 May 19 nicklas 17 import net.sf.basedb.reggie.dao.Annotationtype;
5392 02 May 19 nicklas 18 import net.sf.basedb.reggie.dao.BioplateType;
4668 01 Feb 18 nicklas 19 import net.sf.basedb.reggie.dao.Datafiletype;
1543 28 Feb 12 nicklas 20 import net.sf.basedb.reggie.dao.Subtype;
1543 28 Feb 12 nicklas 21 import net.sf.basedb.util.error.ThrowableUtil;
1543 28 Feb 12 nicklas 22
1543 28 Feb 12 nicklas 23 /**
1543 28 Feb 12 nicklas 24   Get information about subtypes.
1543 28 Feb 12 nicklas 25   
1543 28 Feb 12 nicklas 26   @author nicklas
1543 28 Feb 12 nicklas 27    @since 2.4
1543 28 Feb 12 nicklas 28 */
1543 28 Feb 12 nicklas 29 public class SubtypeServlet 
1543 28 Feb 12 nicklas 30   extends HttpServlet 
1543 28 Feb 12 nicklas 31 {
1543 28 Feb 12 nicklas 32
1543 28 Feb 12 nicklas 33
1543 28 Feb 12 nicklas 34   private static final long serialVersionUID = -4113396134373808508L;
1543 28 Feb 12 nicklas 35
1543 28 Feb 12 nicklas 36   public SubtypeServlet()
1543 28 Feb 12 nicklas 37   {}
1543 28 Feb 12 nicklas 38
1543 28 Feb 12 nicklas 39   @Override
1543 28 Feb 12 nicklas 40   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1543 28 Feb 12 nicklas 41     throws ServletException, IOException 
1543 28 Feb 12 nicklas 42   {
1543 28 Feb 12 nicklas 43     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 44     JsonUtil.setJsonResponseHeaders(resp);
1543 28 Feb 12 nicklas 45     
1543 28 Feb 12 nicklas 46     JSONObject json = new JSONObject();
1543 28 Feb 12 nicklas 47     json.put("status", "ok");
1543 28 Feb 12 nicklas 48   
3975 26 May 16 nicklas 49     final SessionControl sc = Reggie.getSessionControl(req);
1543 28 Feb 12 nicklas 50     DbControl dc = null;
1543 28 Feb 12 nicklas 51     try
1543 28 Feb 12 nicklas 52     {
1543 28 Feb 12 nicklas 53       if ("GetSubtypeInfo".equals(cmd))
1543 28 Feb 12 nicklas 54       {
1543 28 Feb 12 nicklas 55         /*
1543 28 Feb 12 nicklas 56           Get information about the requested subtype.
1543 28 Feb 12 nicklas 57         */
1543 28 Feb 12 nicklas 58         String name = req.getParameter("name");
1544 29 Feb 12 nicklas 59         Subtype subtype = Subtype.getByCName(name);
1544 29 Feb 12 nicklas 60         if (subtype == null)
1544 29 Feb 12 nicklas 61         {
1544 29 Feb 12 nicklas 62           throw new ItemNotFoundException("Subtype." + name);
1544 29 Feb 12 nicklas 63         }
1543 28 Feb 12 nicklas 64         
6337 16 Jun 21 nicklas 65         dc = sc.newDbControl(":Get subtype information");
1543 28 Feb 12 nicklas 66         json.put("subtype", subtype.asJSONObject(dc));
1543 28 Feb 12 nicklas 67       }
4668 01 Feb 18 nicklas 68       else if ("GetDataFileTypeInfo".equals(cmd))
4668 01 Feb 18 nicklas 69       {
4668 01 Feb 18 nicklas 70         /*
4668 01 Feb 18 nicklas 71           Get information about the requested subtype.
4668 01 Feb 18 nicklas 72         */
4668 01 Feb 18 nicklas 73         String name = req.getParameter("name");
4668 01 Feb 18 nicklas 74         Datafiletype fileType = Datafiletype.getByCName(name);
4668 01 Feb 18 nicklas 75         if (fileType == null)
4668 01 Feb 18 nicklas 76         {
4668 01 Feb 18 nicklas 77           throw new ItemNotFoundException("DataFileType." + name);
4668 01 Feb 18 nicklas 78         }
4668 01 Feb 18 nicklas 79         
6337 16 Jun 21 nicklas 80         dc = sc.newDbControl(":Get file type information");
4668 01 Feb 18 nicklas 81         json.put("fileType", fileType.asJSONObject(dc));
4668 01 Feb 18 nicklas 82       }
5392 02 May 19 nicklas 83       else if ("GetPlateTypeInfo".equals(cmd))
5392 02 May 19 nicklas 84       {
5392 02 May 19 nicklas 85         /*
5392 02 May 19 nicklas 86           Get information about the requested subtype.
5392 02 May 19 nicklas 87         */
5392 02 May 19 nicklas 88         String name = req.getParameter("name");
5392 02 May 19 nicklas 89         BioplateType plateType = BioplateType.getByCName(name);
5392 02 May 19 nicklas 90         if (plateType == null)
5392 02 May 19 nicklas 91         {
5392 02 May 19 nicklas 92           throw new ItemNotFoundException("BioPlateType." + name);
5392 02 May 19 nicklas 93         }
5392 02 May 19 nicklas 94         
6337 16 Jun 21 nicklas 95         dc = sc.newDbControl(":Get plate type information");
5392 02 May 19 nicklas 96         json.put("plateType", plateType.asJSONObject(dc));
5392 02 May 19 nicklas 97       }
5435 17 May 19 nicklas 98       else if ("GetAnnotationTypeInfo".equals(cmd))
5435 17 May 19 nicklas 99       {
5435 17 May 19 nicklas 100         /*
5435 17 May 19 nicklas 101           Get information about the requested annotation type.
5435 17 May 19 nicklas 102         */
5435 17 May 19 nicklas 103         String name = req.getParameter("name");
5435 17 May 19 nicklas 104         Annotationtype annotationType = Annotationtype.getByCName(name);
5435 17 May 19 nicklas 105         if (annotationType == null)
5435 17 May 19 nicklas 106         {
5435 17 May 19 nicklas 107           throw new ItemNotFoundException("AnnotationType." + name);
5435 17 May 19 nicklas 108         }
5435 17 May 19 nicklas 109         
6337 16 Jun 21 nicklas 110         dc = sc.newDbControl(":Get annotation type information");
5435 17 May 19 nicklas 111         json.put("annotationType", annotationType.asJSONObject(dc));
5435 17 May 19 nicklas 112       }
4668 01 Feb 18 nicklas 113
1543 28 Feb 12 nicklas 114     }
1543 28 Feb 12 nicklas 115     catch (Throwable t)
1543 28 Feb 12 nicklas 116     {
1543 28 Feb 12 nicklas 117       t.printStackTrace();
1543 28 Feb 12 nicklas 118       json.clear();
1543 28 Feb 12 nicklas 119       json.put("status", "error");
1543 28 Feb 12 nicklas 120       json.put("message", t.getMessage());
1543 28 Feb 12 nicklas 121       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
1543 28 Feb 12 nicklas 122     }
1543 28 Feb 12 nicklas 123     finally
1543 28 Feb 12 nicklas 124     {
1543 28 Feb 12 nicklas 125       if (dc != null) dc.close();
1543 28 Feb 12 nicklas 126       json.writeJSONString(resp.getWriter());
1543 28 Feb 12 nicklas 127     }
1543 28 Feb 12 nicklas 128     
1543 28 Feb 12 nicklas 129   }
1543 28 Feb 12 nicklas 130
5392 02 May 19 nicklas 131
1543 28 Feb 12 nicklas 132 }