Пример #1
0
 public Boolean identify() throws IOException, SQLException {
   Boolean result = false;
   try {
     resultSet =
         hBaseSQLManager.executeSqlGetString(
             "SELECT MD5_ID FROM VISITOR WHERE MD5_ID ='" + getMd5Id() + "'");
     if (resultSet.next()) result = true;
   } catch (SQLException | ClassNotFoundException e) {
     e.printStackTrace();
   } finally {
     if (hBaseSQLManager.statement != null)
       try {
         hBaseSQLManager.statement.close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
   }
   return result;
 }
Пример #2
0
  /**
   * Method which saves visitor. There are listed 4 cases: 1. Visitor's cookiesId & cookiesData are
   * equal to visitor's MD5_ID & MD5_DATA into HBase. 2. Visitor's cookiesId are equal to MD5_ID
   * from HBase, but DATA is not equal. The we update visitor. 3. Visitor's cookies does not have
   * cookiesId or cookiesData. If visitor's md5Data is found into HBase, then we assign visitor to
   * existing one. 4. Visitor's cookies does not have cookiesId or cookiesData. Create new visitor
   * if nothing similar found into Hbase.
   */
  @Override
  public void save() {
    // JSONObject response = new JSONObject();
    String cookiesId = getMd5Id();
    String cookiesData = getMd5Data();
    String md5IdSearch = "";
    String md5DataSearch = "";
    int visitorId = 0;

    // CASE: WHEN THERE ARE COOKIES BUT THERE ARE NO USER IN DB!!!

    /** This if checks if visitor has cookies. */
    if (!cookiesId.equals("")) {
      try {
        resultSet =
            hBaseSQLManager.executeSqlGetString(
                "SELECT * FROM VISITOR WHERE MD5_ID ='" + cookiesId + "'");
        if (resultSet.next()) {
          md5IdSearch = resultSet.getString("MD5_ID");
          md5DataSearch = resultSet.getString("MD5_DATA");
          visitorId = resultSet.getInt("ID");
        }
      } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
      } finally {
        if (hBaseSQLManager.statement != null)
          try {
            hBaseSQLManager.statement.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
      }
      if (cookiesId.equals(md5IdSearch)) {
        if (cookiesData.equals(md5DataSearch)) {
        } else {
          /* Seems user changed something. We'll update visitor. OK */
          try {
            hBaseSQLManager.executeSqlGetIdOnUpdate(
                "UPSERT INTO VISITOR(ID, USER_AGENT, HEADERS, "
                    + "TIMEZONE, SCREEN, ENABLED_COOKIES, PLUGINS, FONTS, MD5_ID, MD5_DATA) "
                    + "VALUES("
                    + visitorId
                    + ", '"
                    + getUserAgent()
                    + "', '"
                    + getHeaders()
                    + "', "
                    + getTimezone()
                    + ", '"
                    + getScreen()
                    + "', '"
                    + isEnabledCookies()
                    + "'"
                    + ", '"
                    + getPlugins()
                    + "', '"
                    + getFonts()
                    + "', '"
                    + getMd5Id()
                    + "', '"
                    + getMd5Data()
                    + "')");
          } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
          } finally {
            if (hBaseSQLManager.statement != null)
              try {
                hBaseSQLManager.statement.close();
              } catch (SQLException e) {
                e.printStackTrace();
              }
          }
        }
      }
    }

    /** This if checks if visitor does not have cookies. */
    if (cookiesId.equals("")) {
      String md5id = "";
      try {
        resultSet =
            hBaseSQLManager.executeSqlGetString(
                "SELECT MD5_DATA FROM VISITOR WHERE MD5_DATA = '" + getMd5Data() + "'");
        if (resultSet.next()) md5DataSearch = resultSet.getString("MD5_DATA");
      } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
      } finally {
        if (hBaseSQLManager.statement != null)
          try {
            hBaseSQLManager.statement.close();
          } catch (SQLException e) {
            e.printStackTrace();
          }
      }
      if (md5DataSearch.equals(getMd5Data())) {
        try {
          resultSet =
              hBaseSQLManager.executeSqlGetString(
                  "SELECT MD5_ID FROM VISITOR WHERE MD5_DATA = '" + md5DataSearch + "'");
          if (resultSet.next()) md5id = resultSet.getString("MD5_ID");
        } catch (SQLException | ClassNotFoundException e) {
          e.printStackTrace();
        } finally {
          if (hBaseSQLManager.statement != null)
            try {
              hBaseSQLManager.statement.close();
            } catch (SQLException e) {
              e.printStackTrace();
            }
        }
        setMd5Id(md5id);

      } else {
        try {
          hBaseSQLManager.executeSqlGetIdOnUpdate(
              "UPSERT INTO VISITOR(ID, USER_AGENT, TIMEZONE, HEADERS, SCREEN, ENABLED_COOKIES, PLUGINS,"
                  + " FONTS, MD5_DATA) "
                  + "VALUES(NEXT VALUE FOR VISITOR.VISITOR_SEQUENCE, '"
                  + getUserAgent()
                  + "', "
                  + getTimezone()
                  + ", '"
                  + getHeaders()
                  + "', '"
                  + getScreen()
                  + "', '"
                  + isEnabledCookies()
                  + "'"
                  + ", '"
                  + getPlugins()
                  + "', '"
                  + getFonts()
                  + "', '"
                  + getMd5Data()
                  + "')");
          try {
            resultSet =
                hBaseSQLManager.executeSqlGetString(
                    "SELECT ID FROM VISITOR WHERE MD5_DATA = '" + getMd5Data() + "'");
            if (resultSet.next()) visitorId = resultSet.getInt("ID");
          } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
          } finally {
            if (hBaseSQLManager.statement != null) hBaseSQLManager.statement.close();
          }
          md5id = createMD5(Integer.toString(visitorId));
          setMd5Id(md5id);
          hBaseSQLManager.executeSqlGetIdOnUpdate(
              "UPSERT INTO VISITOR(ID, MD5_ID) VALUES(" + visitorId + ", '" + getMd5Id() + "')");
        } catch (SQLException | ClassNotFoundException e) {
          e.printStackTrace();
        } finally {
          if (hBaseSQLManager.statement != null)
            try {
              hBaseSQLManager.statement.close();
            } catch (SQLException e) {
              e.printStackTrace();
            }
        }
      }
    }

    try {
      try {
        hBaseSQLManager.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }