jp.co.nemuzuka.controller.IndexController.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.nemuzuka.controller.IndexController.java

Source

/*
 * Copyright 2012 Kazumune Katagiri. (http://d.hatena.ne.jp/nemuzuka)
 *
 * 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 jp.co.nemuzuka.controller;

import jp.co.nemuzuka.common.Authority;
import jp.co.nemuzuka.core.annotation.NoSessionCheck;
import jp.co.nemuzuka.core.controller.HtmlController;
import jp.co.nemuzuka.service.MemberService;
import jp.co.nemuzuka.service.impl.MemberServiceImpl;

import org.apache.commons.lang.StringUtils;
import org.slim3.controller.Navigation;

import com.google.appengine.api.users.User;

/**
 * Controller.
 * ????MemberModel????????
 * ???????Modelput???
 * @author kazumune
 */
public class IndexController extends HtmlController {

    private MemberService memberService = MemberServiceImpl.getInstance();

    /* (? Javadoc)
     * @see jp.co.nemuzuka.core.controller.HtmlController#execute()
     */
    @NoSessionCheck
    @Override
    protected Navigation execute() throws Exception {

        if (userService.isUserAdmin()) {
            //????MemberModel??????
            User currentUser = userService.getCurrentUser();
            memberService.checkAndCreateMember(currentUser.getEmail(), currentUser.getNickname(), Authority.admin);
        } else if (StringUtils.isNotEmpty((String) sessionScope(USE_TRIAL_USER))) {
            //??????trial???MemberModel??????
            User currentUser = userService.getCurrentUser();
            memberService.checkAndCreateMember(currentUser.getEmail(), currentUser.getEmail(), Authority.normal);
        }

        if (isSmartPhone(request.getHeader("User-Agent")) == true) {
            return forward("/mobile/bts/");
        }
        return forward("/bts/");
    }

    /**
     * ?????.
     * @param header User-Agent
     * @return ?????true
     */
    private boolean isSmartPhone(String header) {

        if (StringUtils.isEmpty(header)) {
            return false;
        }

        boolean ret = false;
        if (header.indexOf("Android") != -1) {
            ret = true;
        } else if (header.indexOf("iPhone") != -1) {
            ret = true;
        } else if (header.indexOf("iPad") != -1) {
            ret = true;
        } else if (header.indexOf("iPod") != -1) {
            ret = true;
        } else if (header.indexOf("Windows Phone") != -1) {
            ret = true;
        }
        return ret;
    }
}