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

Code
Comments
Other
Rev Date Author Line
2057 10 Oct 13 nicklas 1 package net.sf.basedb.reggie.servlet;
2057 10 Oct 13 nicklas 2
2057 10 Oct 13 nicklas 3 import java.io.IOException;
2368 17 Apr 14 nicklas 4 import java.util.ArrayList;
2368 17 Apr 14 nicklas 5 import java.util.List;
2057 10 Oct 13 nicklas 6
2057 10 Oct 13 nicklas 7 import javax.servlet.ServletException;
2057 10 Oct 13 nicklas 8 import javax.servlet.http.HttpServlet;
2057 10 Oct 13 nicklas 9 import javax.servlet.http.HttpServletRequest;
2057 10 Oct 13 nicklas 10 import javax.servlet.http.HttpServletResponse;
2057 10 Oct 13 nicklas 11
2057 10 Oct 13 nicklas 12 import org.json.simple.JSONArray;
2057 10 Oct 13 nicklas 13 import org.json.simple.JSONObject;
2057 10 Oct 13 nicklas 14
5469 04 Jun 19 nicklas 15 import net.sf.basedb.core.AnnotationRestriction;
5469 04 Jun 19 nicklas 16 import net.sf.basedb.core.AnnotationSimpleRestriction;
2057 10 Oct 13 nicklas 17 import net.sf.basedb.core.DbControl;
2057 10 Oct 13 nicklas 18 import net.sf.basedb.core.Hardware;
2057 10 Oct 13 nicklas 19 import net.sf.basedb.core.ItemQuery;
5469 04 Jun 19 nicklas 20 import net.sf.basedb.core.Operator;
3603 16 Nov 15 nicklas 21 import net.sf.basedb.core.Permission;
2057 10 Oct 13 nicklas 22 import net.sf.basedb.core.Project;
2057 10 Oct 13 nicklas 23 import net.sf.basedb.core.SessionControl;
2057 10 Oct 13 nicklas 24 import net.sf.basedb.core.query.Hql;
2057 10 Oct 13 nicklas 25 import net.sf.basedb.core.query.Orders;
2598 22 Aug 14 nicklas 26 import net.sf.basedb.reggie.JsonUtil;
2057 10 Oct 13 nicklas 27 import net.sf.basedb.reggie.Reggie;
2368 17 Apr 14 nicklas 28 import net.sf.basedb.reggie.dao.Annotationtype;
2057 10 Oct 13 nicklas 29 import net.sf.basedb.reggie.dao.Subtype;
2057 10 Oct 13 nicklas 30 import net.sf.basedb.util.error.ThrowableUtil;
2057 10 Oct 13 nicklas 31
2057 10 Oct 13 nicklas 32 /**
2057 10 Oct 13 nicklas 33   Get information about hardware.
2057 10 Oct 13 nicklas 34   
2057 10 Oct 13 nicklas 35   @author nicklas
2057 10 Oct 13 nicklas 36    @since 2.13
2057 10 Oct 13 nicklas 37 */
2057 10 Oct 13 nicklas 38 public class HardwareServlet 
2057 10 Oct 13 nicklas 39   extends HttpServlet 
2057 10 Oct 13 nicklas 40 {
2057 10 Oct 13 nicklas 41
2057 10 Oct 13 nicklas 42
2057 10 Oct 13 nicklas 43   private static final long serialVersionUID = 928631805782071L;
2057 10 Oct 13 nicklas 44
2057 10 Oct 13 nicklas 45   public HardwareServlet()
2057 10 Oct 13 nicklas 46   {}
2057 10 Oct 13 nicklas 47
2057 10 Oct 13 nicklas 48   @Override
2057 10 Oct 13 nicklas 49   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
2057 10 Oct 13 nicklas 50     throws ServletException, IOException 
2057 10 Oct 13 nicklas 51   {
2057 10 Oct 13 nicklas 52
2057 10 Oct 13 nicklas 53     String ID = req.getParameter("ID");
2057 10 Oct 13 nicklas 54     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 55     JsonUtil.setJsonResponseHeaders(resp);
2057 10 Oct 13 nicklas 56     
2057 10 Oct 13 nicklas 57     JSONObject json = new JSONObject();
2057 10 Oct 13 nicklas 58     json.put("status", "ok");
2057 10 Oct 13 nicklas 59   
3975 26 May 16 nicklas 60     final SessionControl sc = Reggie.getSessionControl(req);
2057 10 Oct 13 nicklas 61     DbControl dc = null;
2057 10 Oct 13 nicklas 62     try
2057 10 Oct 13 nicklas 63     {
2057 10 Oct 13 nicklas 64       if ("GetHardware".equals(cmd))
2057 10 Oct 13 nicklas 65       {
2057 10 Oct 13 nicklas 66         /*
2057 10 Oct 13 nicklas 67           Get information about hardware. Add filter for optional 'subtype' parameter.
2057 10 Oct 13 nicklas 68           Sort by name.
2057 10 Oct 13 nicklas 69         */
6337 16 Jun 21 nicklas 70         dc = sc.newDbControl(":Get hardware information");
2057 10 Oct 13 nicklas 71         ItemQuery<Hardware> query = Hardware.getQuery();
2057 10 Oct 13 nicklas 72         query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3603 16 Nov 15 nicklas 73         query.setItemPermission(Permission.USE);
2057 10 Oct 13 nicklas 74         Subtype subtype = Subtype.getByCName(req.getParameter("subtype"));
2368 17 Apr 14 nicklas 75         
2057 10 Oct 13 nicklas 76         if (subtype != null)
2057 10 Oct 13 nicklas 77         {
2057 10 Oct 13 nicklas 78           subtype.addFilter(dc, query);
2057 10 Oct 13 nicklas 79         }
2057 10 Oct 13 nicklas 80         query.order(Orders.asc(Hql.property("name")));
2057 10 Oct 13 nicklas 81         query.order(Orders.asc(Hql.property("id")));
2057 10 Oct 13 nicklas 82         
2368 17 Apr 14 nicklas 83         String ann = req.getParameter("annotations");
5469 04 Jun 19 nicklas 84         String filter = req.getParameter("filter");
2368 17 Apr 14 nicklas 85         List<Annotationtype> annotations = new ArrayList<Annotationtype>();
2368 17 Apr 14 nicklas 86         if (ann != null)
2368 17 Apr 14 nicklas 87         {
5469 04 Jun 19 nicklas 88           String[] aTypes = ann.split(",");
5469 04 Jun 19 nicklas 89           String[] filters = filter != null ? filter.split(",") : null;
5469 04 Jun 19 nicklas 90           for (int index = 0; index < aTypes.length; index++)
2368 17 Apr 14 nicklas 91           {
5469 04 Jun 19 nicklas 92             Annotationtype a = Annotationtype.getByCName(aTypes[index]);
5469 04 Jun 19 nicklas 93             if (a != null) 
5469 04 Jun 19 nicklas 94             {
5469 04 Jun 19 nicklas 95               annotations.add(a);
5469 04 Jun 19 nicklas 96               if (filters != null && filters.length > index)
5469 04 Jun 19 nicklas 97               {
5469 04 Jun 19 nicklas 98                 query.restrict(new AnnotationSimpleRestriction((String)null, a.get(dc), Operator.EQ, filters[index], new AnnotationRestriction.Options()));
5469 04 Jun 19 nicklas 99               }
5469 04 Jun 19 nicklas 100             }
2368 17 Apr 14 nicklas 101           }
2368 17 Apr 14 nicklas 102         }
2368 17 Apr 14 nicklas 103         
2057 10 Oct 13 nicklas 104         Project project = sc.getActiveProjectId() == 0 ? null : Project.getById(dc, sc.getActiveProjectId());
2057 10 Oct 13 nicklas 105         
2057 10 Oct 13 nicklas 106         JSONArray jsonHardware = new JSONArray();
2057 10 Oct 13 nicklas 107         JSONObject jsonDefault = null;
2057 10 Oct 13 nicklas 108         int defaultHardwareId = -1;
2057 10 Oct 13 nicklas 109         for (Hardware h : query.list(dc))
2057 10 Oct 13 nicklas 110         {
2057 10 Oct 13 nicklas 111           JSONObject jsonItem = new JSONObject();
2057 10 Oct 13 nicklas 112           jsonItem.put("id", h.getId());
2057 10 Oct 13 nicklas 113           jsonItem.put("name", h.getName());
2368 17 Apr 14 nicklas 114           
2368 17 Apr 14 nicklas 115           for (Annotationtype a : annotations)
2368 17 Apr 14 nicklas 116           {
2368 17 Apr 14 nicklas 117             jsonItem.put(a.getName(), a.getAnnotationValue(dc, h));
2368 17 Apr 14 nicklas 118           }
2368 17 Apr 14 nicklas 119           
2057 10 Oct 13 nicklas 120           jsonHardware.add(jsonItem);
2057 10 Oct 13 nicklas 121           
2057 10 Oct 13 nicklas 122           // Check if the current hardware is the latest default hardware
2057 10 Oct 13 nicklas 123           if (project != null && project.isDefaultItem(h) && h.getId() > defaultHardwareId)
2057 10 Oct 13 nicklas 124           {
2057 10 Oct 13 nicklas 125             defaultHardwareId = h.getId();
2057 10 Oct 13 nicklas 126             jsonDefault = jsonItem;
2057 10 Oct 13 nicklas 127           }
2057 10 Oct 13 nicklas 128         }
2057 10 Oct 13 nicklas 129         if (jsonDefault != null)
2057 10 Oct 13 nicklas 130         {
2057 10 Oct 13 nicklas 131           jsonDefault.put("isDefault", true);
2057 10 Oct 13 nicklas 132         }
2057 10 Oct 13 nicklas 133         json.put("hardware", jsonHardware);
2057 10 Oct 13 nicklas 134       }
2057 10 Oct 13 nicklas 135       
2057 10 Oct 13 nicklas 136     }
2057 10 Oct 13 nicklas 137     catch (Throwable t)
2057 10 Oct 13 nicklas 138     {
2057 10 Oct 13 nicklas 139       t.printStackTrace();
2057 10 Oct 13 nicklas 140       json.clear();
2057 10 Oct 13 nicklas 141       json.put("status", "error");
2057 10 Oct 13 nicklas 142       json.put("message", t.getMessage());
2057 10 Oct 13 nicklas 143       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
2057 10 Oct 13 nicklas 144     }
2057 10 Oct 13 nicklas 145     finally
2057 10 Oct 13 nicklas 146     {
2057 10 Oct 13 nicklas 147       if (dc != null) dc.close();
2057 10 Oct 13 nicklas 148       json.writeJSONString(resp.getWriter());
2057 10 Oct 13 nicklas 149     }
2057 10 Oct 13 nicklas 150     
2057 10 Oct 13 nicklas 151   }
2057 10 Oct 13 nicklas 152
2057 10 Oct 13 nicklas 153   
2057 10 Oct 13 nicklas 154 }