AJWIP Examples
1.0 07/03/2015
MainPanel.html (NOTE: Please reference the ajwipweb-1.X.X.zip file for the latest code examples)
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link href="/html/template.css" rel="stylesheet" type="text/css"> <script src="jquery-2.1.4.min.js"></script> <!-- ajwip:title --> </head> <style type="text/css"> #table-wrapper { position:relative; } #table-scroll { height:260px; overflow:auto; margin-top:20px; } #table-wrapper table { width:100%; } td {border: 1px #DDD solid; padding: 5px; cursor: pointer;} .edit-span { color:black; border:0px solid gray; width:100px; display:inline-block; } .selected { background-color: lightblue; color: #000; } </style> <script type='text/javascript'> $(document).ready(function() { $("#table tr").click(function(){ $(this).addClass('selected').siblings().removeClass('selected'); var i=$(this).find('td:first').html(); var m=$(this).find('td:nth-child(2)').html(); var a=$(this).find('td:nth-child(3)').html(); var r=$(this).find('td:nth-child(4)').html(); var min=$(this).find('td:nth-child(5)').html(); $('#movieid').val(i); $('#movie').val(m); $('#actors').val(a); $('#rating').val(r); $('#minutes').val(min); // alert($('#movieid').val()); }); $("#add").on("click", function(e){ var n = $('#movie').val(); var a = $('#actors').val(); var r = $('#rating').val(); var m = $('#minutes').val(); if (n == undefined) { alert("Please enter a movie name."); return false; } if (a == undefined) { alert("Please enter a movie actor(s)."); return false; } if (r == undefined) { alert("Please enter a movie rating."); return false; } if (m == undefined) { alert("Please enter the movie minutes."); return false; } return true; }); $("#update").on("click", function(e){ var i = $("#table tr.selected td:first").html(); if (i == undefined) { alert("Please select a movie first."); return false; } else { return true; } }); $("#delete").on("click", function(e){ var i = $("#table tr.selected td:first").html(); var m = $("#table tr.selected td:nth-child(2)").html() if (i == undefined) return false; else return confirm("Delete movie ID: " + i + ", Name: " + m + "?"); }); }); </script> <body> <div style="margin: auto; width: 50%;"> <img src="/images/banner.png" width="500" height="100"> </div> <div id="table-wrapper" style="margin: auto; width: 80%;"> <div id="table-scroll"> <table id="table" style="border: 0px none;"> <thead> <tr> <th width="5%" align="left">ID</th> <th width="35%" align="left" nowrap>Movie Name</th> <th width="35%" align="left">Actors</th> <th width="10%" align="left">Rating</th> <th width="10%" align="left">Minutes</th> </tr> </thead> <tbody> <!-- ajwip:rows --> </tbody> </table> </div> </div> <br/> <form method="POST" action="/html/MainPanel.html"> <div style="margin: auto; width: 60%;"> <fieldset id="edit" class="center"> <legend >Add, Update and Delete editor</legend> <span class="edit-span">Movie name: </span><input type="text" name="movie" id="movie" size="60" /><br/> <span class="edit-span">Actors: </span><input type="text" name="actors" id="actors" size="60" /><br/> <!-- <span class="edit-span">Rating: </span><input type="text" name="rating" id="rating" /><br/> --> <span class="edit-span">Rating: </span> <!-- ajwip:ratings --> <br/> <span class="edit-span">Minutes: </span><input type="text" name="mins" id="minutes" /> <input type="hidden" id="movieid" name="movieid" /> </fieldset> <div style="margin: auto; width: 100%;"> <button type="submit" name="submit" value="add" id="add">Add Video</button> <button type="submit" name="submit" value="update" id="update">Update Video</button> <button type="submit" name="submit" value="delete" id="delete">Delete Video</button> </div> </div> </form> <form method="POST" action="/html/MainPanel.html"> <div style="margin: auto; width: 60%;" align="right"><button type="submit" name="submit" value="logout" id="delete">Log Out</button></div> </form> <div style="margin: auto; width: 90%;"> <!-- ajwip:status --> </div> </body> </html>
MainPanel.java
package com; import htmltags.AjwipLabel; import htmltags.AjwipOption; import htmltags.AjwipTd; import htmltags.AjwipTitle; import htmltags.AjwipTr; import java.io.InputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Map; import utils.AjwipSql; import formelem.AjwipSelect; public class MainPanel implements AjwipInterface { private Ajwip ajwip = null; private static AjwipWebGlobal twg = AjwipWebGlobal.getInstance(); public MainPanel() { } /* The submit method for form POST actions. * * (non-Javadoc) * @see com.jwipInterface#submit(java.io.InputStream) */ @Override public RetStatus submit(InputStream in) { RetStatus rs = null; ajwip.msgCon(this, "Submitting..."); Map<String, String> map = Ajwip.readInputRequest(in); for (String key : map.keySet()) { ajwip.msgCon(this, "Post: " + key + " " + map.get(key)); } String value = map.get("submit"); rs = new RetStatus(Ajwip.AJWIP_FAILED); // jwip.msgCon(this, "value: " + value); if ("add".equals(value)) { // User clicked Add button. try { Timestamp ts = new Timestamp(System.currentTimeMillis()); Statement st = twg.db.createStatement(); String sql = "insert into videos (" + "movie_name, " + "movie_actors, " + "movie_rating, " + "movie_mins, " + "tstamp) values(" + "'" + map.get("movie").replaceAll("\\+", " ") + "', " + "'" + map.get("actors").replaceAll("\\+", " ") + "', " + "'" + map.get("rating") + "', " + "'" + map.get("mins").replaceAll("\\+", " ") + "', " + "'" + ts + "')"; ajwip.msgCon(this, sql); st.executeUpdate(sql); rs.setRetValue(Ajwip.AJWIP_OK); rs.setMsg("Add successful."); st.close(); } catch (SQLException e) { // e.printStackTrace(); String emsg = new AjwipSql().sqlErrorFormatter(e.getMessage(), e.getStackTrace()); ajwip.msgCon(this, emsg); rs.setMsg("Add Failed."); } } else if ("delete".equals(value)) { // User clicked Delete button. try { Statement st = twg.db.createStatement(); String sql = "delete from videos where " + "movie_id = " + map.get("movieid"); ajwip.msgCon(this, sql); st.execute(sql); rs.setRetValue(Ajwip.AJWIP_OK); rs.setMsg("Delete successful."); st.close(); } catch (SQLException e) { // e.printStackTrace(); String emsg = new AjwipSql().sqlErrorFormatter(e.getMessage(), e.getStackTrace()); ajwip.msgCon(this, emsg); rs.setMsg("Delete Failed."); } } else if ("update".equals(value)) { // User clicked Update button. try { Statement st = twg.db.createStatement(); String sql = "update videos SET " + "movie_name='" + map.get("movie").replaceAll("\\+", " ") + "', " + "movie_actors='" + map.get("actors").replaceAll("\\+", " ") + "', " + "movie_rating='" + map.get("rating") + "', " + "movie_mins='" + map.get("mins").replaceAll("\\+", " ") + "' " + "where movie_id = " + map.get("movieid"); ajwip.msgCon(this, sql); st.executeUpdate(sql); rs.setRetValue(Ajwip.AJWIP_OK); rs.setMsg("Update successful."); st.close(); } catch (SQLException e) { // e.printStackTrace(); String emsg = new AjwipSql().sqlErrorFormatter(e.getMessage(), e.getStackTrace()); ajwip.msgCon(this, emsg); rs.setMsg("Update Failed."); } } else if ("logout".equals(value)) { // User clicked Logout button. rs.setRetValue(Ajwip.AJWIP_OK); rs.setUri("/html/LoginPanel.html"); rs.setLoggedin(false); } return rs; } /* The methods to populate the web page from a GET action. * * (non-Javadoc) * @see com.jwipInterface#populate(java.lang.String) */ @Override public String populate(String html) { return populate(html, null); } @Override public String populate(String html, String msg) { String txt = html; ArrayList<String> optText = new ArrayList<String>(Arrays.asList("G","PG","PG-13","R","NC-17","NR")); ArrayList<String> names = new ArrayList<String>(); // output some debug messages. if (msg == null) ajwip.msgCon(this, "Populating..."); else ajwip.msgCon(this, "Populating with msg..."); ajwip.msgCon(this, "Count of 'ajwip:' keywords = " + ajwip.ajwipCount(html)); ajwip.msgCon(this, "Does 'ajwip:rows' exist in HTML: " + ajwip.exists(html, "rows")); // returns an Object array of the ajwip:xxx strings in the html text. Object[] a = ajwip.arrayOf(html); String list = null; for (Object o : a) { if (list == null) list = (String)o; else list += ", " + (String)o; } ajwip.msgCon(this, "List all 'ajwip:': " + list); // do page title. AjwipTitle at = new AjwipTitle(null, "MainPanel"); txt = ajwip.txtReplace(txt, "ajwip:title", at); // do ratings type select field. AjwipSelect as = new AjwipSelect("rating"); as.setName("rating"); for (String s : optText) { AjwipOption ao = new AjwipOption(null); ao.setOptionText(s); ao.setValue(s); as.addOption(ao); } txt = ajwip.txtReplace(txt, "ajwip:ratings", as); // Retrieve all rows from database and place into ArrayList try { Statement st = twg.db.createStatement(); ResultSet rs = st.executeQuery("select * from videos order by movie_name"); while(rs.next()) { int movie_id = rs.getInt("movie_id"); String movie_name = rs.getString("movie_name").replaceAll("%27", "'"); String movie_actors = rs.getString("movie_actors"); String movie_rating = rs.getString("movie_rating"); String movie_mins = rs.getString("movie_mins"); // build a comma separated string of the database columns. String csv = movie_id + "," + movie_name + "," + movie_actors + "," + movie_rating + "," + movie_mins; names.add(csv); } rs.close(); st.close(); } catch (SQLException e) { // e.printStackTrace(); String emsg = new AjwipSql().sqlErrorFormatter(e.getMessage(), e.getStackTrace()); ajwip.msgCon(this, emsg); } // Create table rows ArrayList ArrayList<AjwipTr> trs = new ArrayList<AjwipTr>(); for (String row : names) { String[] colData = row.split(","); AjwipTr tr = new AjwipTr(null); for (String data : colData) { AjwipTd td = new AjwipTd(null, data.replaceAll("%2C", ",")); tr.addTd(td); } trs.add(tr); } // concatenate the table rows together. String rows = ""; for (AjwipTr tr : trs) rows += tr; txt = ajwip.txtReplace(txt, "ajwip:rows", rows); // If status message, add that to HTML. if (msg != null) { // create label for status message. AjwipLabel al = new AjwipLabel(); al.setLabelText(msg); txt = ajwip.txtReplace(txt, "ajwip:status", al); } return txt; } @Override public void setAjwip(Ajwip ajwip) { this.ajwip = ajwip; } }