/**
   * The method deletes a given campaign based on campaign id
   *
   * @return struts return value
   */
  public String deleteSerpsCampaign() {

    // 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) {
      Integer campaignId = 0;
      campaignId = Integer.parseInt(jString);
      System.out.println("Site id is:::" + campaignId);

      // reading the 'customerID' from session and type casting it to integer
      Integer customerID = (Integer) objSession.getAttribute("customerID");

      // now invoking the deleteCampaign() method of CampaignsServiceImpl
      String campaignName = objCampaignsService.deleteCampaign(campaignId, customerID);
      message = "'" + campaignName + "' - Campaign has been Deleted";
      objSession.setAttribute("message", message);
      return SUCCESS;
    }

    // if session attribute 'customerID' is null then return result parameter as 'LOGIN'
    // this result parameter is mapped in 'struts.xml'
    return LOGIN;
  }
  /**
   * The method adds a new campaign to database
   *
   * @return struts return value
   */
  public String addSerpsCampaign() {

    // 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) {
      String campaignName = "";
      campaignName = jString;

      // checking for validation, campaign name must be alphanumeric with space
      boolean b = checkValidation(campaignName);
      if (!b) {

        // if validation failed then add action message
        message = "Campaign Name should be alphanumeric";
        objSession.setAttribute("message", message);
        addActionError(message);
        return SUCCESS;
      }
      if (!campaignName.equals("")) {

        // reading the 'userID' from session and type casting it to integer
        Integer userID = Integer.parseInt(objSession.getAttribute("userID").toString());
        System.out.println("UserID : " + userID);
        System.out.println("campaignName:::" + campaignName);
        // int Quota=objCampaignsService.checkCampaignQuota(customerID);
        // int campaignId = 1;

        // now invoking the addCampaign() method of CampaignsServiceImpl
        int campaignId = objCampaignsService.addCampaign(campaignName, userID);
        if (campaignId == -1) {
          message = "Sorry This Campaign value already exist in Database";
        } else if (campaignId == -10) {
          message = "Your Assigned Quota for Number of Campaigns is Full.";
          quotaCompleted = 1;
          setQuotaCompleted(quotaCompleted);
        } else {
          message = "Campaign has been created. Please Click on Campaign Name to Add Keywords";
        }
      } else {
        message = "Campaign Value required";
      }
    } else {

      // if session attribute 'customerID' is null then return result parameter as 'LOGIN'
      // this result parameter is mapped in 'struts.xml'
      return LOGIN;
    }
    objSession.setAttribute("message", message);
    return SUCCESS;
  }
  /**
   * Refresh Serps Campaign
   *
   * @return
   */
  public String refreshSerpsCampaign() {

    // 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) {
      String[] data = jString.split(":");
      Integer campaignId = Integer.parseInt(data[0]);
      System.out.println("campaignId ------------------= " + campaignId);
      String campaignName = data[1];
      System.out.println("campaignName --------------= " + campaignName);

      Integer customerID = Integer.parseInt(objSession.getAttribute("customerID").toString());
      System.out.println("customerID = " + customerID);

      // now invoking the refreshCampaign() method of CampaignsServiceImpl
      int updated = objCampaignsService.refreshCampaign(campaignId, campaignName, customerID);
      if (updated == 1) {
        message = "'" + campaignName + "' - Campaign has been Refreshed";
        System.out.println("message = " + message);
      } else {
        message =
            "Sorry Campaign Name "
                + campaignName
                + " cannot be refreshed!! You can refresh a campaign once a day!!  Try it tomorrow";
        System.out.println("message = " + message);
        addActionError(message);
      }
      objSession.setAttribute("message", message);
      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 edits/updates a given campaign name
   *
   * @return struts return value
   */
  public String editSerpsCampaign() {

    // 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) {
      String[] data = jString.split(":");
      Integer campaignId = Integer.parseInt(data[0]);
      // System.out.println("campaignId ------------------= " + campaignId);
      String campaignName = data[1];
      // System.out.println("campaignName --------------= " + campaignName);
      Integer customerID = Integer.parseInt(objSession.getAttribute("customerID").toString());
      //  System.out.println("customerID = " + customerID);
      // reading the 'userID' from session and type casting it to integer
      Integer userID = Integer.parseInt(objSession.getAttribute("userID").toString());
      System.out.println("userID = " + userID);
      // System.out.println("Site id is:::" + campaignId);

      // now invoking the editCampaign() method of CampaignsServiceImpl
      int updated = objCampaignsService.editCampaign(campaignId, campaignName, customerID);
      if (updated == 1) {
        message = "Campaign has been Edited";
        System.out.println("message = " + message);
      } else {
        message = "Sorry Campaign Name '" + campaignName + "' already Exist";
        System.out.println("message = " + message);
        addActionError(message);
      }
      objSession.setAttribute("message", message);
      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 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 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;
    }
  }