From 8d3a393de397bcf4612b32766131b7cdedc97fac Mon Sep 17 00:00:00 2001 From: Andy Janata Date: Sun, 1 Jul 2012 23:55:12 -0700 Subject: [PATCH] card sets, and a way to edit them --- WebContent/cardsets.jsp | 306 ++++++++++++++++++++++ src/hibernate.cfg.xml | 1 + src/net/socialgamer/cah/db/BlackCard.java | 32 ++- src/net/socialgamer/cah/db/CardSet.java | 73 ++++++ src/net/socialgamer/cah/db/WhiteCard.java | 25 +- 5 files changed, 429 insertions(+), 8 deletions(-) create mode 100644 WebContent/cardsets.jsp create mode 100644 src/net/socialgamer/cah/db/CardSet.java diff --git a/WebContent/cardsets.jsp b/WebContent/cardsets.jsp new file mode 100644 index 0000000..2a8e351 --- /dev/null +++ b/WebContent/cardsets.jsp @@ -0,0 +1,306 @@ + +<%-- +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="org.apache.commons.lang3.StringEscapeUtils" %> +<%@ page import="org.hibernate.Session" %> +<%@ page import="org.hibernate.Transaction" %> +<% +String remoteAddr = request.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.125.132"))) { + response.sendError(403, "Access is restricted to known hosts"); + return; +} + +List messages = new ArrayList(); + +Session hibernateSession = HibernateUtil.instance.sessionFactory.openSession(); + +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 activeParam = request.getParameter("active"); + String[] selectedBlackCardsParam = request.getParameterValues("selectedBlackCards"); + String[] selectedWhiteCardsParam = request.getParameterValues("selectedWhiteCards"); + 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.setActive("on".equals(activeParam)); + 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 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) { %> + + + + + + <% } %> + +
NameDeleteEdit
<%= StringEscapeUtils.escapeXml(cardSet.getName()) %>DeleteEdit
+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: +
+ +
+ +
+ + diff --git a/src/hibernate.cfg.xml b/src/hibernate.cfg.xml index 8989a09..2b7dd22 100644 --- a/src/hibernate.cfg.xml +++ b/src/hibernate.cfg.xml @@ -16,5 +16,6 @@ + diff --git a/src/net/socialgamer/cah/db/BlackCard.java b/src/net/socialgamer/cah/db/BlackCard.java index 0b20625..bf20c7c 100644 --- a/src/net/socialgamer/cah/db/BlackCard.java +++ b/src/net/socialgamer/cah/db/BlackCard.java @@ -42,12 +42,20 @@ import net.socialgamer.cah.Constants.BlackCardData; @Entity @Table(name = "black_cards") public class BlackCard { + @Id @GeneratedValue - int id; - String text; - int draw; - int pick; + private int id; + + private String text; + + private int draw; + + private int pick; + + private boolean in_v1; + + private boolean in_v2; public int getId() { return id; @@ -83,7 +91,21 @@ public class BlackCard { @Override public String toString() { - return text + " (id:" + id + ", draw:" + draw + ", pick:" + pick + ")"; + return text + " (id:" + id + ", draw:" + draw + ", pick:" + pick + ", v1:" + in_v1 + " v2:" + + in_v2 + ")"; + } + + @Override + public boolean equals(final Object other) { + if (!(other instanceof BlackCard)) { + return false; + } + return ((BlackCard) other).getId() == id; + } + + @Override + public int hashCode() { + return id; } /** diff --git a/src/net/socialgamer/cah/db/CardSet.java b/src/net/socialgamer/cah/db/CardSet.java new file mode 100644 index 0000000..ed60477 --- /dev/null +++ b/src/net/socialgamer/cah/db/CardSet.java @@ -0,0 +1,73 @@ +package net.socialgamer.cah.db; + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; + + +@Entity +@Table(name = "card_set") +public class CardSet { + + @Id + @GeneratedValue + private int id; + + private String name; + + private boolean active; + + @ManyToMany + @JoinTable( + name = "card_set_black_card", + joinColumns = { @JoinColumn(name = "card_set_id") }, + inverseJoinColumns = { @JoinColumn(name = "black_card_id") }) + private final Set blackCards; + + @ManyToMany + @JoinTable( + name = "card_set_white_card", + joinColumns = { @JoinColumn(name = "card_set_id") }, + inverseJoinColumns = { @JoinColumn(name = "white_card_id") }) + private final Set whiteCards; + + public CardSet() { + blackCards = new HashSet(); + whiteCards = new HashSet(); + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public boolean isActive() { + return active; + } + + public void setActive(final boolean active) { + this.active = active; + } + + public int getId() { + return id; + } + + public Set getBlackCards() { + return blackCards; + } + + public Set getWhiteCards() { + return whiteCards; + } +} diff --git a/src/net/socialgamer/cah/db/WhiteCard.java b/src/net/socialgamer/cah/db/WhiteCard.java index 9b1bf23..90f2a0e 100644 --- a/src/net/socialgamer/cah/db/WhiteCard.java +++ b/src/net/socialgamer/cah/db/WhiteCard.java @@ -42,10 +42,16 @@ import net.socialgamer.cah.Constants.WhiteCardData; @Entity @Table(name = "white_cards") public class WhiteCard { + @Id @GeneratedValue - int id; - String text; + private int id; + + private String text; + + private boolean in_v1; + + private boolean in_v2; public int getId() { return id; @@ -65,7 +71,20 @@ public class WhiteCard { @Override public String toString() { - return text + " (id:" + id + ")"; + return text + " (id:" + id + ", v1:" + in_v1 + ", v2:" + in_v2 + ")"; + } + + @Override + public boolean equals(final Object other) { + if (!(other instanceof WhiteCard)) { + return false; + } + return ((WhiteCard) other).getId() == id; + } + + @Override + public int hashCode() { + return id; } /**