<%-- Copyright (c) 2012-2018, 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="com.google.inject.Injector" %> <%@ page import="com.google.inject.Key" %> <%@ page import="com.google.inject.TypeLiteral" %> <%@ page import="java.util.ArrayList" %> <%@ page import="java.util.List" %> <%@ page import="java.util.Set" %> <%@ page import="net.socialgamer.cah.CahModule.Admins" %> <%@ page import="net.socialgamer.cah.HibernateUtil" %> <%@ page import="net.socialgamer.cah.StartupUtils" %> <%@ page import="net.socialgamer.cah.db.PyxBlackCard" %> <%@ page import="net.socialgamer.cah.db.PyxCardSet" %> <%@ page import="net.socialgamer.cah.db.PyxWhiteCard" %> <%@ 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); ServletContext servletContext = pageContext.getServletContext(); Injector injector = (Injector) servletContext.getAttribute(StartupUtils.INJECTOR); Set admins = injector.getInstance(Key.get(new TypeLiteral>(){}, Admins.class)); if (!admins.contains(wrapper.getRemoteAddr())) { 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"); PyxCardSet editCardSet = null; if (null != editParam) { try { editCardSet = (PyxCardSet)hibernateSession.load(PyxCardSet.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 = (PyxCardSet)hibernateSession.load(PyxCardSet.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 PyxCardSet(); } else { editCardSet = (PyxCardSet)hibernateSession.load(PyxCardSet.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 PyxBlackCard where id in (:ids)").setParameterList("ids", blackCardIds). setReadOnly(true).list(); @SuppressWarnings("unchecked") List realWhiteCards = hibernateSession.createQuery( "from PyxWhiteCard 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 PyxCardSet order by weight, name") .setReadOnly(true).list(); @SuppressWarnings("unchecked") List blackCards = hibernateSession.createQuery("from PyxBlackCard order by id") .setReadOnly(true).list(); @SuppressWarnings("unchecked") List whiteCards = hibernateSession.createQuery("from PyxWhiteCard order by id") .setReadOnly(true).list(); %> PYX - Edit Card Sets <% for (String message : messages) { %>

<%=message%>

<% } %>

Existing card sets

<% for (PyxCardSet cardSet : cardSets) { %> <% } %>
Name Delete Edit Weight Blacks Whites Active
<%=cardSet.getName()%> Delete Edit <%=cardSet.getWeight()%> <%=cardSet.getBlackCards().size()%> <%=cardSet.getWhiteCards().size()%> <%=cardSet.isActive()%>
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(); } %>