/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void addObject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection c; PreparedStatement st; ResultSet set; String objName; int objTileSrc; int objXOff; int objYOff; int objWidth; int objHeight; String objDesc; int objId; int objAuth; Gson Gson; FormResponse resp; resp = null; c = null; st = null; set = null; Gson = new Gson(); try { // Verify we have authorization to do this! TODO: Set a special response code on authorization // failure. // Perform a quick, cursory validation resp = validateAddForm(request); if (!resp.isAccepted()) { resp.setResult("FAIL"); } else { // Apply parameters objName = request.getParameter("title"); objDesc = request.getParameter("desc"); objTileSrc = Integer.parseInt(request.getParameter("tileset")); objXOff = Integer.parseInt(request.getParameter("objXOff")); objYOff = Integer.parseInt(request.getParameter("objYOff")); objWidth = Integer.parseInt(request.getParameter("objWidth")); objHeight = Integer.parseInt(request.getParameter("objHeight")); // objAuth = (Integer) request.getSession().getAttribute("userid"); // TODO: Get the public flag System.out.println("Object name: '" + objName + "'"); // Generate the query c = DBResourceManager.getConnection(); st = c.prepareStatement(OBJTYPE_INSERT, PreparedStatement.RETURN_GENERATED_KEYS); st.setString(1, objName); st.setInt(2, objTileSrc); st.setInt(3, objXOff); st.setInt(4, objYOff); st.setInt(5, objWidth); st.setInt(6, objHeight); st.setString(7, objDesc); st.execute(); set = st.getGeneratedKeys(); // Grab the generated key if (set.next()) { objId = set.getInt(1); resp.setResult("OK"); resp.addParamResult("objId", "VALUEUP:" + objId); // Send the OK. Note that Dojo requires us to wrap the response // in an html doc's text area for max. compatibility. } else { System.out.println("Failure to create object."); resp.setResult("FAIL"); } } // Send the resposne object no matter what response .getWriter() .println("<html><body><textarea>" + Gson.toJson(resp) + "</textarea></html></body>"); } catch (Exception ex) { ex.printStackTrace(); if (resp == null) { resp = new FormResponse(); resp.setAcceptance(false); } resp.setResult("FAIL"); resp.addMessage(ex.toString()); response .getWriter() .println("<html><body><textarea>" + ex.toString() + "</textarea></html></body>"); } finally { DataTools.safeCleanUp(c, st, set); } }
/** * When browsing objects, we'd like to make it possible to interface the object type list with a * Dojo Data Source. To do so, we stream the objects out as a JSON object. * * @param request * @param response * @throws ServletException * @throws IOException */ private void doObjTypeRequestJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int fromIndex, maxResult; Connection c; PreparedStatement st; ResultSet set; JsonWriter jOut; int recCount; // Set the content up response.setContentType("application/json"); // Setup our database information c = null; st = null; set = null; // Setup the rest of our environment recCount = 0; jOut = new JsonWriter(response.getWriter()); try { // Parse request parameters fromIndex = Integer.parseInt(request.getParameter("start")); maxResult = Integer.parseInt(request.getParameter("count")); // Create connection to the database and prep the query c = DBResourceManager.getConnection(); st = c.prepareStatement(OBJTYPE_LIST_JSON); st.setInt(1, fromIndex); st.setInt(2, maxResult); // Execute the query set = st.executeQuery(); // Now, stream the results out as if they were an object jOut.beginObject(); jOut.name("items").beginArray(); while (set.next()) { // Write each element out as an object jOut.beginObject(); jOut.name("objid").value(set.getInt("id")); jOut.name("objname").value(set.getString("name")); jOut.name("objtilesrc").value(set.getString("tilesrc")); jOut.name("description").value(set.getString("description")); jOut.endObject(); // Increment the record count recCount++; } // Terminate the array jOut.endArray(); // Note the number of rows jOut.name("numRows").value(recCount); // End the overall object jOut.endObject(); // Flush the output jOut.flush(); } catch (Exception ex) { ex.printStackTrace(); } finally { DataTools.safeCleanUp(c, st, set); } }