/**
   * This method is used in a web service(limitedserpskeywords.action) that returns 100 seokeywords
   * details, 100 serpkeywords details & social signal details in the JSON format by each time it is
   * invoked
   *
   * @author rinkesh jha
   * @throws Exception
   */
  public void getLimitedSerps() throws Exception {

    objResponse = ServletActionContext.getResponse(); // getting response object
    PrintWriter out = objResponse.getWriter();

    JSONObject result = new JSONObject(); // declaring & creating the JSONObject

    // initializing http request object
    objRequest = ServletActionContext.getRequest(); // getting request object
    // initializing http session object
    objSession = objRequest.getSession(); // getting session object

    try {

      System.out.println("CUSTOMER ID : " + objSession.getAttribute("customerID"));

      // checking for initial value(It is used for the starting row number)
      if (objRequest.getParameter("initial") != null) {
        if (objSession.getAttribute("customerID") != null) { // checking for customer id
          if (objRequest.getParameter("campaignId") != null) { // checking for campaignId

            campaignId = Integer.parseInt(objRequest.getParameter("campaignId"));
            customerId = Integer.parseInt(objSession.getAttribute("customerID").toString());
            int initial = Integer.parseInt(objRequest.getParameter("initial"));

            // retrieving the list of keywords object from getData method of KeywordsServiceImpl for
            // <campaignId>
            Object[] dataObject =
                objKeywordsService.getSerpDataLimited(
                    campaignId, initial); // getting all the serps & seo data
            lstUpdatedKeywords = (List<Serpkeywords>) dataObject[0]; // assigning serpskeywords data
            lstSeoDetails = (List<Seokeyworddetails>) dataObject[1]; // assigning seokeywords data
            lstDisplaysettings =
                objKeywordsService.getCustomerSettings(
                    customerId); // getting display settings of that particular customerId

            // if the lstDisplaysettings object will become null the below code add one
            // Displaysettings object to the lstDisplaysettings object
            if (lstDisplaysettings.isEmpty()) {
              Displaysettings d = new Displaysettings(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
              lstDisplaysettings.add(d);
            }

            // FETCHING OF DISPLAY SETTINGS & MAKING JSON
            // making display settings json
            JSONObject jsonDisplaySettings = new JSONObject();

            // putting all the display settings into the jsonDisplaySettings object
            jsonDisplaySettings.put("Googletab", lstDisplaysettings.get(0).getGoogletab());
            jsonDisplaySettings.put("Yahootab", lstDisplaysettings.get(0).getYahootab());
            jsonDisplaySettings.put("Bingtab", lstDisplaysettings.get(0).getBingtab());
            jsonDisplaySettings.put("Daychangetab", lstDisplaysettings.get(0).getDaychangetab());
            jsonDisplaySettings.put("Weekchangetab", lstDisplaysettings.get(0).getWeekchangetab());
            jsonDisplaySettings.put(
                "Monthchangetab", lstDisplaysettings.get(0).getMonthchangetab());
            jsonDisplaySettings.put("Alexatab", lstDisplaysettings.get(0).getAlexatab());
            jsonDisplaySettings.put("Backlinkstab", lstDisplaysettings.get(0).getBacklinkstab());
            jsonDisplaySettings.put(
                "Monthlysearchstab", lstDisplaysettings.get(0).getMonthlysearchstab());
            jsonDisplaySettings.put(
                "Serpcampaigntab", lstDisplaysettings.get(0).getSerpcampaigntab());
            jsonDisplaySettings.put(
                "Videocampaigntab", lstDisplaysettings.get(0).getVideocampaigntab());

            result.put(
                "displaySettings",
                jsonDisplaySettings); // adding the displaySettings JSON to main result JSON

            // FETCHING SERPS KEYWORDS & MAKING JSON
            int i = 0;
            try {
              // creating JSON array of serps keywords that contain collection of different
              // serpkeywords
              JSONArray serpkeywordArray = new JSONArray();
              for (Serpkeywords updatedKeywords : lstUpdatedKeywords) {

                if (i >= initial && i <= (initial + 49)) { // cheking for initial values
                  JSONObject keyword =
                      new JSONObject(); // making single row JSON object for a serp keyword

                  // putting all the attributes in keyword object
                  keyword.put("KeywordID", updatedKeywords.getKeywordID());
                  // keyword.put("CampaignID", updatedKeywords.getCampaignID());
                  keyword.put("Url", updatedKeywords.getUrl());
                  keyword.put("Keyword", updatedKeywords.getKeyword());
                  keyword.put("LinkGoogle", updatedKeywords.getLinkGoogle());
                  keyword.put("Region", updatedKeywords.getRegion());
                  keyword.put("RankGoogle", updatedKeywords.getRankGoogle());
                  keyword.put("BestMatchRankGoogle", updatedKeywords.getBestMatchRankGoogle());
                  keyword.put("BestMatchLinkGoogle", updatedKeywords.getBestMatchLinkGoogle());
                  keyword.put("GooglePageRank", updatedKeywords.getGooglePageRank());
                  keyword.put("RankBing", updatedKeywords.getRankBing());
                  keyword.put("BestMatchRankBing", updatedKeywords.getBestMatchRankBing());
                  keyword.put("RankYahoo", updatedKeywords.getRankYahoo());
                  keyword.put("BestMatchRankYahoo", updatedKeywords.getBestMatchRankYahoo());
                  keyword.put("BestMatchLinkYahoo", updatedKeywords.getBestMatchLinkYahoo());
                  keyword.put("RankGoogleDayChange", updatedKeywords.getRankGoogleDayChange());
                  keyword.put("RankGoogleWeekChange", updatedKeywords.getRankGoogleWeekChange());
                  keyword.put("RankGoogleMonthChange", updatedKeywords.getRankGoogleMonthChange());
                  keyword.put("RankBingDayChange", updatedKeywords.getRankBingDayChange());
                  keyword.put("RankBingWeekChange", updatedKeywords.getRankBingWeekChange());
                  keyword.put("RankBingMonthChange", updatedKeywords.getRankBingMonthChange());
                  keyword.put("RankYahooDayChange", updatedKeywords.getRankYahooDayChange());
                  keyword.put("RankYahooWeekChange", updatedKeywords.getRankGoogleWeekChange());
                  keyword.put("RankYahooMonthChange", updatedKeywords.getRankYahooMonthChange());
                  keyword.put("GoogleUpdatedDate", updatedKeywords.getGoogleUpdatedDate());
                  keyword.put("YahooUpdateDate", updatedKeywords.getYahooUpdateDate());
                  keyword.put("BingUpdateDate", updatedKeywords.getBingUpdateDate());
                  keyword.put("Visibility", updatedKeywords.getVisibility());

                  // adding this single JSON object to main JSON array of serp keywords
                  serpkeywordArray.put(keyword);
                }
                i++;
              }

              // adding the serpkeywordArray to the main result JSON object
              result.put("serpskeywords", serpkeywordArray);

            } catch (Exception ea) {
            }

            // FETCHING SEO KEYWORDS & MAKING JSON
            i = 0;
            try {
              // creating JSON array of seo keywords that contain collection of different
              // seokeywords
              JSONArray seokeywordArray = new JSONArray();
              for (Seokeyworddetails seoKeyword : lstSeoDetails) {

                if (i >= initial && i <= (initial + 49)) { // cheking for initial values
                  // making single row JSON object for a seo keyword
                  JSONObject keyword = new JSONObject();

                  // putting all the attributes in keyword object
                  keyword.put("SEOKeywordId", seoKeyword.getSEOKeywordId());
                  // keyword.put("CampaignID",seoKeyword.getCampaignID());
                  // keyword.put("KeywordID", seoKeyword.getKeywordID());
                  keyword.put("Url", seoKeyword.getUrl());
                  keyword.put("Keyword", seoKeyword.getKeyword());
                  keyword.put("SearchVolume", seoKeyword.getSearchVolume());
                  keyword.put("GoogleCPC", seoKeyword.getGoogleCPC());
                  keyword.put("KeywordCompetition", seoKeyword.getKeywordCompetition());
                  keyword.put("NumberofResult", seoKeyword.getNumberofResult());
                  keyword.put("GooglePA", seoKeyword.getGooglePA());
                  keyword.put("GoogleDA", seoKeyword.getGoogleDA());
                  keyword.put("SiteIndexing", seoKeyword.getSiteIndexing());
                  keyword.put("AddedDate", seoKeyword.getAddedDate());
                  keyword.put("RankPage", seoKeyword.getRankPage());
                  keyword.put("RankAlexa", seoKeyword.getRankAlexa());
                  keyword.put("CountBackLinks", seoKeyword.getCountBackLinks());
                  keyword.put("CountMonthlySearches", seoKeyword.getCountMonthlySearches());
                  keyword.put("Visibility", seoKeyword.getVisibility());

                  // adding this single JSON object to main JSON array of seo keywords
                  seokeywordArray.put(keyword);
                }
                i++;
              }

              // adding the seokeywordArray to the main result JSON object
              result.put("seokeywords", seokeywordArray);

            } catch (Exception ea) {
            }

          } else {
            result.put("result", "No campaigns id");
          }
        } else {
          result.put("result", "No customer id");
        }
      } else {
        result.put("result", "No initial value");
      }

    } catch (Exception e) {
      e.printStackTrace();
      result.put("result", "invalid");
    }
    objResponse.setContentType("text");
    objResponse.setHeader("Cache-Control", "no-cache");
    out.write(result.toString());
  }
  /**
   * The method retrieves campaigns based on customer id
   *
   * @return struts return value
   * @throws Exception
   */
  @Override
  public String execute() throws Exception {

    // initializing http session object
    objRequest = ServletActionContext.getRequest();

    // initializing http session object
    objSession = objRequest.getSession();

    // checking for 'customerID' attribute in session
    if (objSession.getAttribute("customerID") != null) {

      if (objSession.getAttribute("activationPeriod").toString().equals("0")) {
        return "renewal";
      }
      // reading the 'customerID' from session and type casting it to integer
      String sCustomerID = objSession.getAttribute("customerID").toString();
      Integer customerID = Integer.parseInt(sCustomerID);

      // invoking the getData() method of CampaignsServiceImpl
      Object[] dataObject = objCampaignsService.getSerpData(customerID);

      // retrieving theb list of keywords data from dataObject
      lstKeywordData = (List<Integer>) dataObject[0];

      // now retrieving the active keyword count and allowed keyword count
      activeKeywordsCount = lstKeywordData.get(0);
      allowedKeywordsCount = lstKeywordData.get(1);

      // retrieving the list of campaigns
      lstCampaigns = (List<Campaigns>) dataObject[1];
      lstCampaignsSearch = (List<Campaigns>) dataObject[1];

      lstKeywords = objCampaignsService.getRankData(customerID);
      try {
        int count5 = 0;
        int count10 = 0;
        int count20 = 0;
        int count30 = 0;
        int count100 = 0;
        int keywordcount = lstKeywords.size();
        int keyrank;
        rankComparision = new RankComparision();
        if (!lstKeywords.isEmpty()) {
          Iterator itr = lstKeywords.iterator();
          while (itr.hasNext()) {
            Serpkeywords keys = (Serpkeywords) itr.next();
            keyrank = keys.getRankGoogle();
            if (keyrank <= 5 & keyrank != 0) {
              count5++;
            }
            if (keyrank <= 10 & keyrank != 0) {
              count10++;
            }
            if (keyrank <= 20 & keyrank != 0) {
              count20++;
            }
            if (keyrank <= 30 & keyrank != 0) {
              count30++;
            }
            if (keyrank <= 100 & keyrank != 0) {
              count100++;
            }
          }
          rankComparision.setKeywordsRankBelow5(count5);
          rankComparision.setKeywordsRankBelow10(count10);
          rankComparision.setKeywordsRankBelow20(count20);
          rankComparision.setKeywordsRankBelow30(count30);
          rankComparision.setKeywordsRankBelow100(count100);
          rankComparision.setTotalkeywords(keywordcount);
        }
        objRequest.setAttribute("highlight", "CAMPAIGN");
        if (objSession.getAttribute("message") != null) {
          addActionMessage(objSession.getAttribute("message").toString());
          objSession.removeAttribute("message");
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
      return SUCCESS;
    } else {

      // if session attribute 'customerID' is null then return result parameter as 'LOGIN'
      // this result parameter is mapped in 'struts.xml'
      return LOGIN;
    }
  }
  /**
   * The method retrieves tempKeywords based on campaign id
   *
   * @return struts return value
   * @throws Exception
   */
  @Override
  public String execute() throws Exception {
    setkeywordslastupdate = new HashSet<>();

    // initializing http request object
    objRequest = ServletActionContext.getRequest();
    List lst = null;
    // initializing http session object
    objSession = objRequest.getSession();
    String alerts = (String) objSession.getAttribute("alerts");
    if ("on".equals(alerts)) {
      lstkeywords = (List<Serpkeywords>) objSession.getAttribute("lstkeywords");
      objSession.removeAttribute("lstkeywords");
      objSession.removeAttribute("alerts");
      return SUCCESS;
    }

    try {
      // checking for 'customerID' attribute in session
      if (objSession.getAttribute("customerID") != null) {
        if (objRequest.getParameter("campaignId") != null) {
          campaignId = Integer.valueOf(objRequest.getParameter("campaignId"));
        } else {

          // reading the 'campaignID' from session and type casting it to integer
          campaignId = Integer.valueOf(objSession.getAttribute("campaignId").toString());
        }
        objSession.setAttribute("campaignId", campaignId);
        customerId = Integer.parseInt(objSession.getAttribute("customerID").toString());
        // retrieving the list of keywords object from getData method of KeywordsServiceImpl for
        // <campaignId>
        Object[] dataObject = objKeywordsService.getSerpData(campaignId, customerId);
        lstUpdatedKeywords = (List<Serpkeywords>) dataObject[0];
        lstsocialsignalupdate = (List<KeywordsLastUpdatedForm>) dataObject[1];
        lstSeoDetails = (List<Seokeyworddetails>) dataObject[3];
        int count5 = 0;
        int count10 = 0;
        int count20 = 0;
        int count30 = 0;
        int count100 = 0;
        int keywordcount = lstUpdatedKeywords.size();
        int keyrank;
        rankComparision = new RankComparision();
        if (!lstUpdatedKeywords.isEmpty()) {
          Iterator itr = lstUpdatedKeywords.iterator();
          while (itr.hasNext()) {
            Serpkeywords keys = (Serpkeywords) itr.next();
            keyrank = keys.getRankGoogle();
            if (keyrank <= 5 & keyrank != 0) {
              count5++;
            }
            if (keyrank <= 10 & keyrank != 0) {
              count10++;
            }
            if (keyrank <= 20 & keyrank != 0) {
              count20++;
            }
            if (keyrank <= 30 & keyrank != 0) {
              count30++;
            }
            if (keyrank <= 100 & keyrank != 0) {
              count100++;
            }
          }
          rankComparision.setKeywordsRankBelow5(count5);
          rankComparision.setKeywordsRankBelow10(count10);
          rankComparision.setKeywordsRankBelow20(count20);
          rankComparision.setKeywordsRankBelow30(count30);
          rankComparision.setKeywordsRankBelow100(count100);
          rankComparision.setTotalkeywords(keywordcount);
        }

        campaignName = dataObject[2].toString();
        if (objSession.getAttribute("message") != null) {
          addActionMessage(objSession.getAttribute("message").toString());
          objSession.removeAttribute("message");
        }

        lstDisplaysettings = objKeywordsService.getCustomerSettings(customerId);
        // if the lstDisplaysettings object will become null the below code add one Displaysettings
        // object to the lstDisplaysettings object

        if (lstDisplaysettings.isEmpty()) {
          lstDisplaysettings = new ArrayList<>();
          Displaysettings d =
              new Displaysettings(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1); // making all tab active
          lstDisplaysettings.add(d); // adding to array list
        }

        objRequest.setAttribute("highlight", "CAMPAIGNS");
        return SUCCESS;
      }
    } catch (Exception e) {
      e.printStackTrace();
      return LOGIN;
    }
    return LOGIN;
  }
  /**
   * The method retrieves campaigns based on given search key
   *
   * @return struts return value
   */
  public String searchSerpsCampaign() {

    // initializing http request object
    objRequest = ServletActionContext.getRequest();

    // initializing http session object
    objSession = objRequest.getSession();

    // checking for 'customerID' attribute in session
    if (objSession.getAttribute("customerID") != null) {

      // reading the 'customerID' from session and type casting it to integer
      Integer customerID = (Integer) objSession.getAttribute("customerID");
      String campaign = objRequest.getParameter("searchSerpsCampaign");
      System.out.println(campaign);
      if (campaign.equals("Search Campaign")) {
        message = "Campaign name is Required";
        addActionError(message);
        return INPUT;
      }

      // now invoking the searchCampaign() method of CampaignsServiceImpl
      lstCampaigns = objCampaignsService.searchCampaign(campaign, customerID);
      Object[] dataObject = objCampaignsService.getSerpData(customerID);
      lstCampaignsSearch = (List<Campaigns>) dataObject[1];

      // retrieving the list of keywords
      lstKeywordData = (List<Integer>) dataObject[0];

      // retrieving active keyword count and allowed keyword count
      activeKeywordsCount = lstKeywordData.get(0);
      allowedKeywordsCount = lstKeywordData.get(1);
      lstKeywords = objCampaignsService.getRankData(customerID);
      int count5 = 0;
      int count10 = 0;
      int count20 = 0;
      int count30 = 0;
      int count100 = 0;
      int keywordcount = lstKeywords.size();
      int keyrank;
      rankComparision = new RankComparision();
      if (!lstKeywords.isEmpty()) {
        Iterator itr = lstKeywords.iterator();
        while (itr.hasNext()) {
          Serpkeywords keys = (Serpkeywords) itr.next();
          keyrank = keys.getRankGoogle();
          if (keyrank <= 5 & keyrank != 0) {
            count5++;
          }
          if (keyrank <= 10 & keyrank != 0) {
            count10++;
          }
          if (keyrank <= 20 & keyrank != 0) {
            count20++;
          }
          if (keyrank <= 30 & keyrank != 0) {
            count30++;
          }
          if (keyrank <= 100 & keyrank != 0) {
            count100++;
          }
        }
        rankComparision.setKeywordsRankBelow5(count5);
        rankComparision.setKeywordsRankBelow10(count10);
        rankComparision.setKeywordsRankBelow20(count20);
        rankComparision.setKeywordsRankBelow30(count30);
        rankComparision.setKeywordsRankBelow100(count100);
        rankComparision.setTotalkeywords(keywordcount);
      }
      if (lstCampaigns.isEmpty()) {
        message = "Sorry! Your Search Campaign not found!";
        addActionMessage(message);
        //                setMessage(message);
        //                seacrhCampaign = campaign;
        //                setSeacrhCampaign(seacrhCampaign);
        //                objRequest.setAttribute("notfound", campaign);
      }
      return SUCCESS;
    } else {

      // if session attribute 'customerID' is null then return result parameter as 'LOGIN'
      // this result parameter is mapped in 'struts.xml'
      return LOGIN;
    }
  }