Package include.dbcommunication

Source Code of include.dbcommunication.Executor

package include.dbcommunication;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.ArrayList;
import java.util.Enumeration;

import village.webservice.room.RequestParameter;
import utils.Utils;
import village.webservice.VillageServiceInterfaceProxy;
import include.net.Server;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import village.database.portlets.*;


/* bad; change ASAP */
/* ASAP has come ;) it's rollin' */


public class Executor {
 
  public String execute(String sid, HttpServletRequest request) {

    String result = null;
    Server serv = new Server();
    String rid = null;
    String server = null;
    include.auth.Validation val = new include.auth.Validation();
    RequestParameter params[] = httpServlet2Parameters(request);

//    System.out.println("[Executor] Executing function 'execute'. Parameters:");
//    System.out.println("[Executor] sid: '" + sid + "'");
       
    rid = getParameter("rid", params);
    /* rid is in JS format (w/ "_") */
    if (rid.indexOf('_') != -1)
      rid = rid.replace('_', '@');
   
    server = rid.substring(rid.indexOf('@') + 1);
     
    if (serv.isMyName(server)) {
       
//      System.out.println("[Executor] Local execution.");
     
      if (val.validate(sid) != null) {
//        System.out.println("[Executor] Session is valid.");
       
        result = doLocal(sid, rid, params);
      }
      else;
//        System.out.println("[Executor] Session is valid.");
    } else {
       
//      System.out.println("[Executor] Remote execution.");
      VillageServiceInterfaceProxy prox = new VillageServiceInterfaceProxy();
     
      village.webservice.VillageServiceInterface iface =
        prox.setEndpoint("http://"+server+prox.villagePort+prox.villageSuffix);

      try {
        result = iface.doRequest(sid, rid, params);       
      } catch(Exception cex){
        cex.printStackTrace();
      }
    }
       
//    System.out.println("[Executor] Function 'execute' returned value: '" + result + "'");
   
    return result;
  }


  private RequestParameter[] httpServlet2Parameters(HttpServletRequest request) {
    System.out.println("[Executor] httpServlet2Parameters start.");   
    ArrayList<RequestParameter> list = new ArrayList<RequestParameter>();
    String parName = null;
    RequestParameter result[] = new RequestParameter[0];
    RequestParameter tmpParameter = null;
     
       for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
         parName = (String) e.nextElement();
           tmpParameter = new RequestParameter();
           tmpParameter.setName(parName);
           tmpParameter.setValue((String) request.getParameter(parName));

           list.add(tmpParameter);
       }

    return list.toArray(result);
  }
 
  private String getParameter(String name, RequestParameter[] params) {
   
    Integer i = 0;
    String result = null;
   
    while ((i < params.length) && (result == null)) {
      if (name.equals(params[i].getName()))
        result = params[i].getValue();
      i++;
    }
   
    return result;
   
  }
 
 
  public String doLocal(String sid, String rid, RequestParameter[] params) {
   
    String result = null;
    String actionName = getParameter("action", params);
    String portletName = getParameter("rid", params);
    include.auth.Validation val = new include.auth.Validation();
   
    result = ("<portlet-action>" + actionName + "</portlet-action>");

    if (portletName.startsWith("clock")) {
      Date currentDate = new Date();
      SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      result = result + ("<date-time>" + f.format(currentDate)
          + "</date-time>");
    } else if (portletName.startsWith("forex")){
      Utils u = new Utils();
      String s = u.getForexDataFromNBPWebsite();
      result += s;
     
    } else if (portletName.startsWith("weather")){
      Utils u = new Utils();
      String s = u.getWeatherDataFromWPWebsite();
      result += s;
    } else if (portletName.startsWith("warcaby")) {
      System.out.println("[Warcaby]");
            CheckerController ch = null;
            try {
                InitialContext ctx = new InitialContext();
                ch = (CheckerController) ctx
                        .lookup("ear3/CMPCheckerController/local");
            } catch (NamingException e) {
                e.printStackTrace();
            }
            if (actionName.equals("getState")) {
                Collection<CheckerGame> games = ch.getGames();
                /*
                 * if (!games.isEmpty()) ch.removeGame("n"); games =
                 * ch.getGames();
                 */

                if (games.isEmpty()) {
                    System.out.println("PIERWSZY GRACZ");

                    ch.addGame("n", "p");
                    String login = val.validate(sid);
                    /* tworzymy stan gry */
                    String board = "<checkers><players_n>1</players_n>"
                            + "<player_1>"
                            + login
                            + "</player_1>"
                            + "<player_2>null</player_2>"
                            + "<who_play>0</who_play>"
                            + "<p00>1</p00><p01>0</p01><p02>1</p02><p03>0</p03><p04>0</p04><p05>0</p05><p06>-1</p06><p07>0</p07>"
                            + "<p10>0</p10><p11>1</p11><p12>0</p12><p13>0</p13><p14>0</p14><p15>-1</p15><p16>0</p16><p17>-1</p17>"
                            + "<p20>1</p20><p21>0</p21><p22>1</p22><p23>0</p23><p24>0</p24><p25>0</p25><p26>-1</p26><p27>0</p27>"
                            + "<p30>0</p30><p31>1</p31><p32>0</p32><p33>0</p33><p34>0</p34><p35>-1</p35><p36>0</p36><p37>-1</p37>"
                            + "<p40>1</p40><p41>0</p41><p42>1</p42><p43>0</p43><p44>0</p44><p45>0</p45><p46>-1</p46><p47>0</p47>"
                            + "<p50>0</p50><p51>1</p51><p52>0</p52><p53>0</p53><p54>0</p54><p55>-1</p55><p56>0</p56><p57>-1</p57>"
                            + "<p60>1</p60><p61>0</p61><p62>1</p62><p63>0</p63><p64>0</p64><p65>0</p65><p66>-1</p66><p67>0</p67>"
                            + "<p70>0</p70><p71>1</p71><p72>0</p72><p73>0</p73><p74>0</p74><p75>-1</p75><p76>0</p76><p77>-1</p77>"
                            + "</checkers>";
                    ch.updateGame("n", board);
                    result += board;
                } else { // jest gra
                    String board = games.iterator().next().getBoard();
                    String players_n = board.substring(board
                            .indexOf("<players_n>") + "<players_n>".length(), board
                            .indexOf("</players_n>"));
                    String player_1 = board.substring(board
                            .indexOf("<player_1>") + "<player_1>".length(), board
                            .indexOf("</player_1>"));
                    String login = val.validate(sid);
                    System.out.println("[]"+login);
                    if (players_n.equals("1")) {
                         System.out.println("PROBUJEMY DOLACZYC SIE DO GRY");

                        if (ch.joinGame("n", "p")) {
                            board = "<checkers><players_n>2</players_n>"
                                    + "<player_1>"
                                    + player_1
                                    + "</player_1>"
                                    + "<player_2>"
                                    + login
                                    + "</player_2>"
                                    + "<who_play>1</who_play>"
                                    + "<p00>1</p00><p01>0</p01><p02>1</p02><p03>0</p03><p04>0</p04><p05>0</p05><p06>-1</p06><p07>0</p07>"
                                    + "<p10>0</p10><p11>1</p11><p12>0</p12><p13>0</p13><p14>0</p14><p15>-1</p15><p16>0</p16><p17>-1</p17>"
                                    + "<p20>1</p20><p21>0</p21><p22>1</p22><p23>0</p23><p24>0</p24><p25>0</p25><p26>-1</p26><p27>0</p27>"
                                    + "<p30>0</p30><p31>1</p31><p32>0</p32><p33>0</p33><p34>0</p34><p35>-1</p35><p36>0</p36><p37>-1</p37>"
                                    + "<p40>1</p40><p41>0</p41><p42>1</p42><p43>0</p43><p44>0</p44><p45>0</p45><p46>-1</p46><p47>0</p47>"
                                    + "<p50>0</p50><p51>1</p51><p52>0</p52><p53>0</p53><p54>0</p54><p55>-1</p55><p56>0</p56><p57>-1</p57>"
                                    + "<p60>1</p60><p61>0</p61><p62>1</p62><p63>0</p63><p64>0</p64><p65>0</p65><p66>-1</p66><p67>0</p67>"
                                    + "<p70>0</p70><p71>1</p71><p72>0</p72><p73>0</p73><p74>0</p74><p75>-1</p75><p76>0</p76><p77>-1</p77>"
                                    + "</checkers>";
                            ch.updateGame("n", board);
                            result += board;
                        }
                    } else {
                      String board_suf = board.substring(board.indexOf("<player_1>"));
                     
                      result += "<checkers><players_n>3</players_n>"+board_suf;
                        System.out.println("JESTESMY TYLKO OBSERWATOREM");
                    }
                    System.out.println("NIE");
                }
            } else if (actionName.equals("waitForOpp")) {
                Collection<CheckerGame> games = ch.getGames();
                String board = games.iterator().next().getBoard();
                System.out.println("[CHUJ!!!!!]"+board);
                String players_n = board.substring(
                        board.indexOf("<players_n>") + "<players_n>".length(), board
                                .indexOf("</players_n>"));
                if (players_n.equals("1"))
                    result += "<who_play>0</who_play>";
                else {
                    String player_2 = board.substring(board
                            .indexOf("<player_2>") + "<player_2>".length(), board
                            .indexOf("</player_2>"));
                    System.out.println("[CHUJ!!!!!]"+player_2);
                   
                    result += "<who_play>1</who_play><player_2>" + player_2
                            + "</player_2>";
                }

            } else if (actionName.equals("waitForOppMove")) {
                Collection<CheckerGame> games = ch.getGames();
                String board = games.iterator ().next().getBoard();
                result += board;
            } else if (actionName.equals("change_player")) {
                String board = ch.getGame("n").getBoard();
                String players_n = board.substring(
                        board.indexOf("<players_n>") + "<players_n>".length(), board
                                .indexOf("</players_n>"));
                String player_1 = board.substring (
                        board.indexOf("<player_1>") + "<player_1>".length(), board
                                .indexOf("</player_1>"));
                String player_2 = board.substring(
                         board.indexOf("<player_2>") + "<player_2>".length(), board
                                .indexOf("</player_2>"));
                String who_play = board.substring(
                        board.indexOf ("<who_play>") + "<who_play>".length(), board
                                .indexOf("</who_play>"));
                if (who_play.equals("1"))
                    who_play = "-1";
                else
                    who_play = "1";
                String newboard = "<checkers><players_n>2</players_n>"
                        + "<player_1>" + player_1 + "</player_1>"
                        + "<player_2>" + player_2 + "</player_2>"
                        + "<who_play>" + who_play + "</who_play>";
                for (int i = 0; i < 8; i++)
                    for (int j = 0; j < 8; j++)
                        newboard += "<p" + i + j + ">"
                                + getParameter(("p" + i + j), params) + "</p" + i
                                + j + ">";
                newboard += "</checkers>";
                ch.updateGame("n", newboard);
            }
        };
   
/* registration portlet temporarily omitted */
   
    /*else if (portletName.startsWith("registration")) {
      if (actionName.equals("checkLogin")) {
        String strLogin = getParameter("login", params);
        if (strLogin != null) {
          if (auth.getAccount(strLogin) != null)
            result.concat("<login-exists>1</login-exists>");
          else
            result.concat("<login-exists>0</login-exists>");
        }
      } else if (actionName.equals("registerUser")) {
        String strLogin = getParameter("login",params);
        String strPassword = getParameter("pass",params);

        if (strLogin == null || strPassword == null)
          return result;

//        if (um.UserExistsByLogin(strLogin)) {
          result.concat("<reg-status>exists</reg-status>");
//        } else {
          try {
//            Account acc = new Account(strLogin, strPassword);
//            auth.add(acc);
            result.concat("<reg-status>ok</reg-status>");
          } catch (Exception e) {
            result.concat("<reg-status>error</reg-status>");
          }
//        }
       
      }
    }
*/   
    return result;
  }
}
TOP

Related Classes of include.dbcommunication.Executor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.