Esempio n. 1
0
    protected void insert(EidType eid) {
      List<EventMapId> mapIdList = eid.getEventMapId();
      String encounterId = eid.getEventId().getValue();
      String encounterIdSource = eid.getEventId().getSource();
      String encounterPatientId = eid.getEventId().getPatientId();
      String encounterPatientIdSource = eid.getEventId().getPatientIdSource();

      for (EventMapId mapId : mapIdList) {

        Object[] objs =
            new Object[] {
              mapId.getValue(),
              mapId.getSource(),
              mapId.getPatientId(),
              mapId.getPatientIdSource(),
              encounterId,
              encounterIdSource,
              mapId.getStatus(),
              (mapId.getUpdateDate() != null)
                  ? mapId.getUpdateDate().toGregorianCalendar().getTime()
                  : null,
              (mapId.getDownloadDate() != null)
                  ? mapId.getDownloadDate().toGregorianCalendar().getTime()
                  : null,
              mapId.getSourcesystemCd()
            };
        super.update(objs);
      }
      // do self insert
      Object[] objs =
          new Object[] {
            encounterId,
            encounterIdSource,
            encounterPatientId,
            encounterPatientIdSource,
            encounterId,
            encounterIdSource,
            eid.getEventId().getStatus(),
            (eid.getEventId().getUpdateDate() != null)
                ? eid.getEventId().getUpdateDate().toGregorianCalendar().getTime()
                : null,
            (eid.getEventId().getDownloadDate() != null)
                ? eid.getEventId().getDownloadDate().toGregorianCalendar().getTime()
                : null,
            eid.getEventId().getSourcesystemCd()
          };
      super.update(objs);
    }
  /* (non-Javadoc)
   * @see java.lang.Thread#run()
   */
  @Override
  public void run() {
    ResultSet tweets = null;

    tweets = JDBCUtility.executeQuerySingleConnection(query);
    try {
      String content = null;
      Long tweetId = null;

      CalaisClient client = new CalaisRestClient(API_KEY);
      CalaisResponse response = null;

      BatchSqlUpdate bsu =
          new BatchSqlUpdate(
              JDBCUtility.ds,
              "INSERT IGNORE INTO semanticsTweetsEntity (tweetId, type, typeURI, name, uri, relevance) "
                  + " values (?,?,?,?,?,?)");
      bsu.declareParameter(new SqlParameter("tweetId", Types.BIGINT)); // $NON-NLS-1$
      bsu.declareParameter(new SqlParameter("type", Types.VARCHAR)); // $NON-NLS-1$
      bsu.declareParameter(new SqlParameter("typeURI", Types.VARCHAR)); // $NON-NLS-1$
      bsu.declareParameter(new SqlParameter("name", Types.VARCHAR)); // $NON-NLS-1$
      bsu.declareParameter(new SqlParameter("uri", Types.VARCHAR)); // $NON-NLS-1$
      bsu.declareParameter(new SqlParameter("relevance", Types.DOUBLE)); // $NON-NLS-1$
      bsu.compile();

      BatchSqlUpdate bsu_topic =
          new BatchSqlUpdate(
              JDBCUtility.ds,
              "INSERT IGNORE INTO semanticsTweetsTopic (tweetId, topic, uri, relevance) "
                  + " values (?,?,?,?)");
      bsu_topic.declareParameter(new SqlParameter("tweetId", Types.BIGINT)); // $NON-NLS-1$
      bsu_topic.declareParameter(new SqlParameter("topic", Types.VARCHAR)); // $NON-NLS-1$
      bsu_topic.declareParameter(new SqlParameter("uri", Types.VARCHAR)); // $NON-NLS-1$
      bsu_topic.declareParameter(new SqlParameter("relevance", Types.DOUBLE)); // $NON-NLS-1$
      bsu_topic.compile();

      // iterate over all news to make OpenCalais Web Service Call and store the entities, etc.
      int count = 0;
      int storeAfter = 50;
      while (tweets.next()) {
        try {
          count++;
          content = TweetUtility.removeURLs(tweets.getString("content"));
          tweetId = tweets.getLong("id");

          System.out.println("Processing Tweet " + tweetId);

          // make OpenCalais Web service call:
          response = client.analyze(content);

          // store entities:
          try {
            if (response.getEntities() != null) {
              for (CalaisObject entity : response.getEntities()) {
                // store news entity assignment:
                Object[] toadd = {
                  tweetId,
                  entity.getField("_type"),
                  entity.getField("_typeReference"),
                  entity.getField("name"),
                  entity.getField("_uri"),
                  (entity.getField("relevance") != null ? entity.getField("relevance") : 0.0),
                };
                bsu.update(toadd);
              }
            }
          } catch (Exception e) {
            System.err.println(
                "Problems while storing tweet entity assignments: " + e.getMessage());
          }

          // store topics:
          try {
            if (response.getTopics() != null) {
              for (CalaisObject topic : response.getTopics()) {
                Object[] toadd = {
                  tweetId,
                  topic.getField("categoryName"),
                  topic.getField("category"),
                  (topic.getField("score") != null ? topic.getField("score") : 0.0),
                };
                bsu_topic.update(toadd);
              }
            }
          } catch (Exception e) {
            System.err.println("Problems while stroing tweet topic assignments: " + e.getMessage());
          }

          if (count % storeAfter == 0) {
            bsu.flush();
            bsu_topic.flush();
            System.out.println(
                "\n*********************\n* Processed "
                    + count
                    + " tweets.\n**********************\n");
          }
        } catch (Exception e) {
          e.printStackTrace();
          if (e.getMessage() != null
              && e.getMessage().contains("returned a response status of 403")
              && e.getMessage().toLowerCase().contains("response")) {
            API_KEY = OpencalaisCrawler.switchAPIKey(API_KEY);
            client = new CalaisRestClient(API_KEY);
            System.out.println("####\n#### New API key: " + API_KEY);
          }
        }
      }

      bsu.flush();
      bsu_topic.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }