org.mage.server.rest.GameService.java Source code

Java tutorial

Introduction

Here is the source code for org.mage.server.rest.GameService.java

Source

/**
 *  Copyright 2008 - 2011
 *            Matthew L. Maurer maurer.it@gmail.com
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *       http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.mage.server.rest;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import javax.xml.bind.JAXBException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.mage.server.dao.GameDao;
import org.mage.server.dao.GameStateDao;
import org.mage.shared.xml.Action;
import org.mage.shared.xml.Game;
import org.mage.shared.xml.GameCreateResponse;
import org.mage.shared.xml.GameState;
import org.mage.shared.xml.GameStateCreateResponse;
import org.mage.shared.xml.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 * TODO: Javadoc Me.
 *
 * @author Matthew L. Maurer maurer.it@gmail.com
 */
@Controller
public class GameService {
    private static final String GAME_VIEW_NAME = "game";
    private static final String STATE_VIEW_NAME = "gameState";
    private static final String ACTIONS_VIEW_NAME = "actions";

    private static final Logger logger = LoggerFactory.getLogger(GameService.class);

    private GameDao gameDao = new GameDaoTmp();
    private GameStateDao gameStateDao = new GameDaoTmp();

    @Autowired
    private Unmarshaller jaxb2Unmarshaller;

    @Autowired
    private Marshaller jaxb2Marshaller;

    public void setJaxb2Unmarshaller(Unmarshaller jaxb2Marshaller) {
        this.jaxb2Unmarshaller = jaxb2Marshaller;
    }

    public void setJaxb2Marshaller(Marshaller jaxb2Marshaller) {
        this.jaxb2Marshaller = jaxb2Marshaller;
    }

    public void setGameDao(GameDao dao) {
        this.gameDao = dao;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/game/{id}")
    public ModelAndView newGame(@RequestBody String body, @PathVariable String id)
            throws XmlMappingException, IOException, JAXBException {
        Source source = new StreamSource(new StringReader(body));
        Game game = (Game) jaxb2Unmarshaller.unmarshal(source);

        gameDao.save(game);

        GameCreateResponse response = new GameCreateResponse();
        response.setValue(true);
        Response responseOuter = new Response();
        responseOuter.setGameCreated(response);

        return new ModelAndView(GAME_VIEW_NAME, "object", responseOuter);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/game/{id}/state")
    public ModelAndView newGameState(@RequestBody String body, @PathVariable String id)
            throws XmlMappingException, IOException, JAXBException {
        Source source = new StreamSource(new StringReader(body));
        GameState state = (GameState) jaxb2Unmarshaller.unmarshal(source);

        gameStateDao.save(state);

        GameStateCreateResponse response = new GameStateCreateResponse();
        response.setValue(true);
        Response responseOuter = new Response();
        responseOuter.setStateCreated(response);

        return new ModelAndView(STATE_VIEW_NAME, "object", responseOuter);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/advice")
    public ModelAndView getMoveAdvice(@RequestBody String body) throws XmlMappingException, IOException {
        Source source = new StreamSource(new StringReader(body));
        GameState state = (GameState) jaxb2Unmarshaller.unmarshal(source);

        Response actionsResponse = new Response();
        actionsResponse.getActions().addAll(gameStateDao.findBestMoves(state));
        return new ModelAndView(ACTIONS_VIEW_NAME, "object", actionsResponse);
    }

    /**
     * Not really a dao at all, just here for ease of swapping in and out while
     * running locally to see whats being reported to the service.  This is also
     * the default 'dao' that the outer class will declare so that all that will
     * be required to use this functionality will be to remove the bean from the
     * spring context later.
     *
     * @author Matthew L. Maurer maurer.it@gmail.com
     */
    class GameDaoTmp implements GameDao, GameStateDao {

        public void save(Game game) {
            StringWriter writer = new StringWriter();
            Result result = new StreamResult(writer);
            try {
                jaxb2Marshaller.marshal(game, result);
            } catch (Exception e) {
                /* Don't really care */ }
            logger.info(writer.toString());
        }

        public void save(GameState state) {
            StringWriter writer = new StringWriter();
            Result result = new StreamResult(writer);
            try {
                jaxb2Marshaller.marshal(state, result);
            } catch (Exception e) {
                /* Don't really care */ }
            logger.info(writer.toString());
        }

        public List<Action> findBestMoves(GameState state) {
            return null;
        }
    }
}