Java tutorial
/* * Copyright 2014 Algodefu * * 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 com.algodefu.yeti.web; import com.algodefu.yeti.data.*; import com.algodefu.yeti.data.hdf5.TaskStorage; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; public class ShowTradeInPassServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); PrintWriter out = response.getWriter(); Gson gson = new Gson(); Hashtable<String, Object> hashtable = new Hashtable<>(); String srvMsg = ""; List<Long> tradesList = new ArrayList<>(); String passId = request.getParameter("passId"); if (passId.trim().isEmpty()) { srvMsg = "ERROR! Please enter passId."; //out.println(gson.toJson("ERROR! Please enter passId. ")); } else { try { TaskStorage taskStorage = TaskStorage.getInstance(); Trade[] trades = taskStorage.getTradesByPassId(passId); if (trades.length > 0) { for (Trade trade : trades) { if (!trade.isWasCanceled()) { tradesList.add(trade.getTradeID()); } } } } catch (Exception e) { srvMsg = e.toString(); e.printStackTrace(); } } hashtable.put("srvMsg", srvMsg); hashtable.put("tradesList", tradesList.toArray()); out.println(gson.toJson(hashtable)); out.flush(); out.close(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String reqTradeId = request.getParameter("tradeId"); String reqPassId = request.getParameter("passId"); String reqShowAllTrades = request.getParameter("showAllTrades"); String reqStartTradeId = request.getParameter("startTradeId"); String reqEndTradeId = request.getParameter("endTradeId"); // for JSON String errMsg = ""; String symbol = ""; String clusterTitle = ""; List<Cluster> clusterData = new ArrayList<>(); boolean forwardReq = false; if (reqPassId == null || reqPassId.trim().isEmpty()) forwardReq = true; if (forwardReq) { // show input passId form RequestDispatcher view = request.getRequestDispatcher("/formpassid.html"); view.forward(request, response); } else { response.setHeader("Cache-Control", "nocache"); response.setCharacterEncoding("utf-8"); response.setContentType("application/json"); PrintWriter out = response.getWriter(); long singleTradeId = 0; long startTradeId = 0; long endTradeId = 0; int showAllTrades = 0; try { if (!reqTradeId.equals("undefined") && reqTradeId != null && !reqTradeId.trim().isEmpty()) singleTradeId = Long.parseLong(reqTradeId); if (!reqStartTradeId.equals("undefined") && reqStartTradeId != null && !reqStartTradeId.trim().isEmpty()) startTradeId = Long.parseLong(reqStartTradeId); if (!reqEndTradeId.equals("undefined") && reqEndTradeId != null && !reqEndTradeId.trim().isEmpty()) endTradeId = Long.parseLong(reqEndTradeId); if (!reqShowAllTrades.equals("undefined") && reqShowAllTrades != null && !reqShowAllTrades.trim().isEmpty()) showAllTrades = Integer.parseInt(reqShowAllTrades); TaskStorage taskStorage = TaskStorage.getInstance(); // find the required trade / trades Trade[] trades = taskStorage.getTradesByPassId(reqPassId); if (showAllTrades != 1) { List<Trade> tradeList = new ArrayList<>(); for (Trade t : trades) { if (singleTradeId > 0 && t.getTradeID() == singleTradeId) { tradeList.add(t); break; } else if (startTradeId > 0 && endTradeId > 0 && t.getTradeID() >= startTradeId && t.getTradeID() <= endTradeId) { tradeList.add(t); } } trades = tradeList.toArray(new Trade[0]); } // get deals for each trade List<Deal> deals = new ArrayList<>(); List<Deal> tempDeals = new ArrayList<>(); for (Trade t : trades) { tempDeals = t.getDeals(); for (int i = 0; i < tempDeals.size(); i++) { if (tempDeals.get(i).getOrder() != OrderType.CANCELLED) deals.add(tempDeals.get(i)); } } ClusterFormat clusterFormat = taskStorage.getStrategyParamSetByPassId(reqPassId).getClusterFormat(); symbol = clusterFormat.getSymbol(); ClusterSource clusterSource = new ClusterSource(clusterFormat); List<Cluster> clusterList = clusterSource.getClusters(); int startClusterIndex = 0; int endClusterIndex = 0; int tempClusterIndex = 0; for (Deal deal : deals) { Cluster cluster = null; for (int i = 0; i < clusterList.size(); i++) { cluster = clusterList.get(i); if (deal.getDateTime().compareTo(cluster.getOpenDateTime()) >= 0 && deal.getDateTime().compareTo(cluster.getCloseDateTime()) <= 0) { if (deal.getEntry() == DealEntry.IN) { cluster.addDeal(deal); tempClusterIndex = i - 20; if (tempClusterIndex < 0) tempClusterIndex = 0; if (startClusterIndex == 0) startClusterIndex = tempClusterIndex; if (tempClusterIndex < startClusterIndex) startClusterIndex = tempClusterIndex; // if(tempClusterIndex > 0 && (startClusterIndex > 0 && tempClusterIndex < startClusterIndex)) // startClusterIndex = tempClusterIndex; break; } if (deal.getEntry() == DealEntry.OUT && deal.getPrice() >= cluster.getLow() && deal.getPrice() <= cluster.getHigh()) { cluster.addDeal(deal); tempClusterIndex = i + 1; if (tempClusterIndex > clusterList.size() - 1) tempClusterIndex = clusterList.size() - 1; if (endClusterIndex == 0) endClusterIndex = tempClusterIndex; if (tempClusterIndex > endClusterIndex) endClusterIndex = tempClusterIndex; // if(tempClusterIndex > 0 && tempClusterIndex < clusterList.size() - 1 && // (endClusterIndex >= 0 && tempClusterIndex > endClusterIndex)) // endClusterIndex = tempClusterIndex; break; } } } // end for clusterList } // end for - deal // add clusterType as title if (clusterFormat.getType() != null) clusterTitle = clusterFormat.getType().toString() + " CLUSTERS"; // add clusterData for (int i = startClusterIndex; i <= endClusterIndex; i++) { clusterData.add(clusterList.get(i)); } } catch (Exception e) { errMsg = e.toString(); e.printStackTrace(); } Hashtable<String, Object> hashtable = new Hashtable<>(); hashtable.put("errMsg", errMsg); hashtable.put("symbol", symbol); hashtable.put("clusterTitle", clusterTitle); hashtable.put("clusterData", clusterData.toArray()); GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Cluster.class, new ClusterTypeAdapter()); Gson gson = gsonBuilder.create(); out.println(gson.toJson(hashtable)); out.flush(); out.close(); } } }