br.com.arduinoweb.controller.AndroidController.java Source code

Java tutorial

Introduction

Here is the source code for br.com.arduinoweb.controller.AndroidController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.arduinoweb.controller;

import br.com.arduinoweb.model.Command;
import br.com.arduinoweb.model.Log;
import br.com.arduinoweb.model.Login;
import br.com.arduinoweb.model.Page;
import br.com.arduinoweb.model.Profile;
import br.com.arduinoweb.model.User;
import br.com.arduinoweb.service.ArduinoService;
import br.com.arduinoweb.service.CommandService;
import br.com.arduinoweb.service.LogService;
import br.com.arduinoweb.service.OutputService;
import br.com.arduinoweb.service.PageService;
import br.com.arduinoweb.service.ProfileService;
import br.com.arduinoweb.service.StatusService;
import br.com.arduinoweb.service.UserService;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author rodolfo.pereira
 */
@Controller
public class AndroidController {

    Gson gson = new Gson();

    @RequestMapping("Android/Login")
    public @ResponseBody String Authentic(User user) throws IOException {
        User logado = new UserService().Authentic(user);

        if (logado != null) {
            for (Page page : logado.getProfile().getPage()) {
                page.setProfile(null);
            }

            Login login;
            if (new ArduinoService().TestaConexao()) {
                login = new Login(logado, true);
            } else {
                login = new Login(logado, false);
            }
            return gson.toJson(login);
        }
        return null;
    }

    @RequestMapping("Android/Home")
    public @ResponseBody String Home(Login login, HttpSession session) throws IOException {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        setSession(session, login);
        new OutputService().ChangeOutputByList(session);
        List<Command> commandShow = new CommandService().getCommandShow();
        return gson.toJson(commandShow);
    }

    @RequestMapping("Android/Play")
    public @ResponseBody String Play(Login login, HttpSession session, Command command) throws IOException {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        if (login.isArduino()) {
            new ArduinoService().ArduinoExecuta(command.getCommand());
            User user = login.getUser();
            new LogService().SaveLog(new Log(command, user));
        }
        return Home(login, session);
    }

    @RequestMapping("Android/User")
    public @ResponseBody String User(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        List<User> listUser = new UserService().getUsers();
        for (User user : listUser) {
            for (Page page : user.getProfile().getPage()) {
                page.setProfile(null);
            }
        }
        return gson.toJson(listUser);
    }

    @RequestMapping("Android/SaveUser")
    public @ResponseBody void SaveUser(Login login, User user, Profile profile) {
        if (login.getUser() != null) {
            new UserService().SaveUser(user, profile.getProfileId());
        }
    }

    @RequestMapping("Android/DeleteUser")
    public @ResponseBody void DeleteUser(Login login, User user) {
        if (login.getUser() != null) {
            new UserService().DeleteUser(user);
        }
    }

    @RequestMapping("Android/Profile")
    public @ResponseBody String Profile(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        List<Profile> listProfile = new ProfileService().getProfiles();
        for (Profile profile : listProfile) {
            for (Page page : profile.getPage()) {
                page.setProfile(null);
            }
        }
        return gson.toJson(listProfile);
    }

    @RequestMapping("Android/SaveProfile")
    public @ResponseBody void SaveProfile(Login login, Profile profile) {
        if (login.getUser() != null) {
            new ProfileService().SaveProfile(profile);
        }
    }

    @RequestMapping("Android/DeleteProfile")
    public @ResponseBody void DeleteProfile(Login login, Profile profile) {
        if (login.getUser() != null) {
            new ProfileService().DeleteProfile(profile);
        }
    }

    @RequestMapping("Android/Command")
    public @ResponseBody String Command(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        return gson.toJson(new CommandService().getCommands());
    }

    @RequestMapping("Android/SaveCommand")
    public @ResponseBody void SaveCommand(Login login, Command command) {
        if (login.getUser() != null) {
            new CommandService().SaveCommand(command);
        }
    }

    @RequestMapping("Android/DeleteCommand")
    public @ResponseBody void DeleteCommand(Login login, Command command) {
        if (login.getUser() != null) {
            new CommandService().DeleteCommand(command);
        }
    }

    @RequestMapping("Android/Output")
    public @ResponseBody String Output(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        return gson.toJson(new OutputService().getOutputs());
    }

    @RequestMapping("Android/Status")
    public @ResponseBody String Status(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        return gson.toJson(new StatusService().getStatus());
    }

    @RequestMapping("Android/Page")
    public @ResponseBody String Page(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        List<Page> listPage = new PageService().getPages();
        for (Page page : listPage) {
            page.setProfile(null);
        }
        return gson.toJson(listPage);
    }

    @RequestMapping("Android/SaveProfilePage")
    public @ResponseBody void SaveProfilePage(Login login, Profile profile, List<Page> listPage) {
        if (login.getUser() != null) {
            String[] listId = new String[listPage.size()];
            for (int i = 0; i < listPage.size(); i++) {
                listId[i] = Integer.toString(listPage.get(i).getPageId());
            }
            new ProfileService().ChangeProfile(profile, listId);
        }
    }

    @RequestMapping("Android/DeleteProfilePage")
    public @ResponseBody void DeleteProfilePage(Login login, Profile profile, Page page) {
        if (login.getUser() != null) {
            new ProfileService().DeleteProfilePage(profile, page);
        }
    }

    @RequestMapping("Android/Log")
    public @ResponseBody String Log(Login login) {
        if (login.getUser() == null) {
            return "Unauthorized";
        }
        return gson.toJson(new LogService().getLogs());
    }

    private void setSession(HttpSession session, Login login) {
        if (login.isArduino()) {
            session.setAttribute("conexao", "btn-success");
        } else {
            session.setAttribute("conexao", "btn-danger");
        }
    }

}