uk.co.threeonefour.ifictionary.svc.GameServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.threeonefour.ifictionary.svc.GameServiceImpl.java

Source

/**
 * Copyright 2013 Paul Illingworth
 * 
 * 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 uk.co.threeonefour.ifictionary.svc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import uk.co.threeonefour.ifictionary.api.game.GameInfo;
import uk.co.threeonefour.ifictionary.svc.dao.GameFileDao;
import uk.co.threeonefour.ifictionary.svc.dao.GameInfoDao;

public class GameServiceImpl implements GameService {

    private final GameInfoDao gameInfoDao;
    private final GameFileDao gameFileDao;

    public GameServiceImpl(GameInfoDao gameInfoDao, GameFileDao gameFileDao) {
        this.gameInfoDao = gameInfoDao;
        this.gameFileDao = gameFileDao;
    }

    @Transactional
    public List<GameInfo> listGames() {
        List<GameInfo> games = gameInfoDao.list(0, Integer.MAX_VALUE);
        return games;
    }

    @Transactional
    public List<GameInfo> listFeaturedGames() {
        List<GameInfo> games = gameInfoDao.list(0, Integer.MAX_VALUE);
        return games;
    }

    @Override
    public GameInfo getGame(Long gameId) {
        return gameInfoDao.get(gameId);
    }

    @Override
    public InputStream getFile(Long gameId) throws IOException {

        GameInfo gameInfo = gameInfoDao.get(gameId);
        if (gameInfo != null) {
            return gameFileDao.read(gameInfo.getFileId());
        }

        throw new FileNotFoundException("" + gameId);
    }
}