/*
*
* Copyright 2011, Ibrahim Arief
*
* This file is part of Bambu Game Backend.
*
* Bambu Game Backend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bambu Game Backend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bambu Game Backend. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.appspot.bambugame.server.rest;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.appspot.bambugame.server.data.HangmanQuestion;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public class HangmanSentencesServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = -8957362320144318364L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
try
{
writeInputForm( req, resp.getWriter() );
String tCommandString = req.getParameter( "cmd" );
if (tCommandString != null && tCommandString.equalsIgnoreCase( "add" ))
{
String tSentences = req.getParameter( "sentence" );
String tHint = req.getParameter( "hint" );
String tExtras = req.getParameter( "extras" );
if (tSentences != null && tHint != null)
{
tSentences = tSentences.trim().toLowerCase();
tHint = tHint.trim().toLowerCase();
if (tExtras != null && tExtras.trim().isEmpty()) tExtras = null;
String[] tSentenceArray = tSentences.split( "\\|" );
System.out.println("Adding sentence array " + Arrays.toString( tSentenceArray ) );
for (String tSentence : tSentenceArray)
{
if (tSentence.length() > 0) addNewSentence(tSentence, tHint, tExtras);
resp.getWriter().write( "<br>Thanks! Sentence " + tSentence + " with hint " + tHint + " and extras " + tExtras + " have been added to the system!" );
}
MemcacheServiceFactory.getMemcacheService().put( HangmanGameValidatorServlet.cHangmanForceReloadKey, Boolean.TRUE );
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
protected void writeInputForm(HttpServletRequest req, PrintWriter pPrintWriter )
{
String tWordValue = req.getParameter( "sentence" ) == null ? "" : "";//req.getParameter( "word" );
String tHintValue = req.getParameter( "hint" ) == null ? "" : req.getParameter( "hint" );
pPrintWriter.write("\n<form action=\"hangmansentences\" method=\"get\">\n<br>Sentence : <input type=\"text\" name=\"sentence\" value=\"" + tWordValue + "\" autofocus />\n<br>Hint : <input type=\"text\" name=\"hint\" value=\"" + tHintValue + "\"/>\n<br>Extras : <input type=\"text\" name=\"extras\" />\n<br><input type=\"hidden\" name=\"cmd\" value=\"add\" />\n<br><input type=\"submit\" value=\"Submit\" /></form>");
}
protected void addNewSentence(String pHangmanSentence, String pHint, String pExtras)
{
pHangmanSentence = pHangmanSentence.trim().toLowerCase();
HangmanQuestion tQuestion = new HangmanQuestion( pHangmanSentence, pHint, pExtras );
Objectify tObjectify = ObjectifyService.begin();
tObjectify.put( tQuestion );
}
}