<%-- Copyright (c) 2012, Andy Janata All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --%> <%-- Administration tools. @author Andy Janata (ajanata@socialgamer.net) --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page import="net.socialgamer.cah.HibernateUtil" %> <%@ page import="net.socialgamer.cah.db.BlackCard" %> <%@ page import="net.socialgamer.cah.db.CardSet" %> <%@ page import="net.socialgamer.cah.db.WhiteCard" %> <%@ page import="net.socialgamer.cah.RequestWrapper" %> <%@ page import="org.apache.commons.lang3.StringEscapeUtils" %> <%@ page import="org.hibernate.Session" %> <%@ page import="org.hibernate.Transaction" %> <% RequestWrapper wrapper = new RequestWrapper(request); String remoteAddr = wrapper.getRemoteAddr(); //TODO better access control than hard-coding IP addresses. if (!(remoteAddr.equals("0:0:0:0:0:0:0:1") || remoteAddr.equals("127.0.0.1") || remoteAddr.equals("98.248.33.90") || remoteAddr.equals("207.161.39.198"))) { response.sendError(403, "Access is restricted to known hosts"); return; } List messages = new ArrayList(); Session hibernateSession = HibernateUtil.instance.sessionFactory.openSession(); // cheap way to make sure we can close the hibernate session at the end of the page try { String editParam = request.getParameter("edit"); CardSet editCardSet = null; if (null != editParam) { try { editCardSet = (CardSet)hibernateSession.load(CardSet.class, Integer.parseInt(editParam)); } catch (NumberFormatException nfe) { messages.add("Unable to parse or locate requested card set to edit."); } } String deleteParam = request.getParameter("delete"); if (null != deleteParam) { try { editCardSet = (CardSet)hibernateSession.load(CardSet.class, Integer.parseInt(deleteParam)); Transaction t = hibernateSession.beginTransaction(); hibernateSession.delete(editCardSet); t.commit(); response.sendRedirect("cardsets.jsp"); return; } catch (NumberFormatException nfe) { messages.add("Invalid id."); } } String actionParam = request.getParameter("action"); if ("edit".equals(actionParam)) { String idParam = request.getParameter("cardSetId"); int id = 0; try { id = Integer.parseInt(idParam); if (-1 == id) { editCardSet = new CardSet(); } else { editCardSet = (CardSet)hibernateSession.load(CardSet.class, id); } if (null != editCardSet) { String nameParam = request.getParameter("cardSetName"); String descriptionParam = request.getParameter("cardSetDescription"); String weightParam = request.getParameter("cardSetWeight"); String activeParam = request.getParameter("active"); String baseDeckParam = request.getParameter("baseDeck"); String[] selectedBlackCardsParam = request.getParameterValues("selectedBlackCards"); String[] selectedWhiteCardsParam = request.getParameterValues("selectedWhiteCards"); int weight = -1; try { weight = Integer.valueOf(weightParam); } catch (Exception e) { // pass } if (weight <= 0 || weight > 9999) { messages.add("Weight must be a positive integer less than 10000."); } else if (null == nameParam || nameParam.isEmpty() || null == selectedBlackCardsParam || null == selectedWhiteCardsParam) { messages.add("You didn't specify something."); if (-1 == id) { editCardSet = null; } } else { editCardSet.setName(nameParam); editCardSet.setDescription(descriptionParam); editCardSet.setWeight(weight); editCardSet.setActive("on".equals(activeParam)); editCardSet.setBaseDeck("on".equals(baseDeckParam)); List blackCardIds = new ArrayList(selectedBlackCardsParam.length); for (String bc : selectedBlackCardsParam) { blackCardIds.add(Integer.parseInt(bc)); } List whiteCardIds = new ArrayList(selectedWhiteCardsParam.length); for (String wc : selectedWhiteCardsParam) { whiteCardIds.add(Integer.parseInt(wc)); } @SuppressWarnings("unchecked") List realBlackCards = hibernateSession.createQuery( "from BlackCard where id in (:ids)").setParameterList("ids", blackCardIds). setReadOnly(true).list(); @SuppressWarnings("unchecked") List realWhiteCards = hibernateSession.createQuery( "from WhiteCard where id in (:ids)").setParameterList("ids", whiteCardIds). setReadOnly(true).list(); editCardSet.getBlackCards().clear(); editCardSet.getBlackCards().addAll(realBlackCards); editCardSet.getWhiteCards().clear(); editCardSet.getWhiteCards().addAll(realWhiteCards); Transaction t = hibernateSession.beginTransaction(); hibernateSession.saveOrUpdate(editCardSet); t.commit(); hibernateSession.flush(); response.sendRedirect("cardsets.jsp"); return; } } else { messages.add("Unable to find card set with id " + id + "."); } } catch (Exception e) { messages.add("Something went wrong. " + e.toString()); } } @SuppressWarnings("unchecked") List cardSets = hibernateSession.createQuery("from CardSet order by weight, id") .setReadOnly(true).list(); @SuppressWarnings("unchecked") List blackCards = hibernateSession.createQuery("from BlackCard order by id") .setReadOnly(true).list(); @SuppressWarnings("unchecked") List whiteCards = hibernateSession.createQuery("from WhiteCard order by id") .setReadOnly(true).list(); %> PYX - Edit Card Sets <% for (String message : messages) { %>

<%= message %>

<% } %>

Existing card sets

<% for (CardSet cardSet : cardSets) { %> <% } %>
Name Delete Edit Weight
<%= cardSet.getName() %> Delete Edit <%= cardSet.getWeight() %>
Create New

<% if (editCardSet != null) { %> Editing <%= editCardSet.getName() %> <% } else { %> Creating new card set <% } %>

" />
" />
" />
/>
/>
Available Black Cards:


Black Cards in Card Set:

Available White Cards:


White Cards in Card Set:

<% } finally { hibernateSession.close(); } %>