Пример #1
0
  public static Result start() {
    java.util.Map<String, String[]> map = request().body().asFormUrlEncoded();

    List<String> terms = new ArrayList<>(map.size());
    for (int i = 0; i < map.size(); i++) {
      String key = "terms[" + i + "]";
      if (map.containsKey(key)) {
        String[] values = map.get(key);
        if ((values != null) && (values.length >= 1)) {
          terms.add(values[0]);
        }
      }
    }

    StreamConfig config = getConfig();
    config.putTerms(terms);
    config.update();

    StringBuilder sb = new StringBuilder();
    for (String t : terms) {
      sb.append(t);
      sb.append(", ");
    }
    sb.delete(sb.length() - 2, sb.length());

    try {
      startStream(terms);
      flash("success", "Twitter stream started (" + sb.toString() + ")");
    } catch (TwitterException e) {
      Logger.info("Error starting twitter stream", e);
      flash("error", "Error starting Twitter stream" + e.getMessage());
    }
    return redirect(routes.Streams.listAll());
  }
Пример #2
0
  private List<Medication> searchRegnr(String lang, String regnr) {
    List<Medication> med_auth = new ArrayList<>();

    try {
      Connection conn = lang.equals("de") ? german_db.getConnection() : french_db.getConnection();
      Statement stat = conn.createStatement();
      String query =
          "select "
              + SHORT_TABLE
              + " from "
              + DATABASE_TABLE
              + " where "
              + KEY_REGNRS
              + " like "
              + "'%, "
              + regnr
              + "%' or "
              + KEY_REGNRS
              + " like "
              + "'"
              + regnr
              + "%'";
      ResultSet rs = stat.executeQuery(query);
      if (rs != null) {
        while (rs.next()) {
          med_auth.add(cursorToShortMedi(rs));
        }
      }
      conn.close();
    } catch (SQLException e) {
      System.err.println(">> SqlDatabase: SQLException in searchRegnr!");
    }

    return med_auth;
  }
Пример #3
0
  private List<Medication> searchName(String lang, String name) {
    List<Medication> med_titles = new ArrayList<>();

    try {
      Connection conn = lang.equals("de") ? german_db.getConnection() : french_db.getConnection();
      Statement stat = conn.createStatement();
      ResultSet rs;
      // Allow for search to start inside a word...
      if (name.length() > 2) {
        String query =
            "select "
                + SHORT_TABLE
                + " from "
                + DATABASE_TABLE
                + " where "
                + KEY_TITLE
                + " like "
                + "'"
                + name
                + "%' or "
                + KEY_TITLE
                + " like "
                + "'%"
                + name
                + "%'";
        rs = stat.executeQuery(query);
      } else {
        String query =
            "select "
                + SHORT_TABLE
                + " from "
                + DATABASE_TABLE
                + " where "
                + KEY_TITLE
                + " like "
                + "'"
                + name
                + "%'";
        rs = stat.executeQuery(query);
      }
      if (rs != null) {
        while (rs.next()) {
          med_titles.add(cursorToShortMedi(rs));
        }
      }
      conn.close();
    } catch (SQLException e) {
      System.err.println(">> SqlDatabase: SQLException in searchName for " + name);
    }

    return med_titles;
  }
Пример #4
0
 public TweetListener(List<String> terms, Client esClient, String esIndex, String esType) {
   this.terms = terms;
   this.esClient = esClient;
   StringBuilder query = new StringBuilder();
   for (int i = 0; i < terms.size(); i++) {
     if (i > 0) query.append("|");
     // query.append("(").append(Pattern.quote(terms.get(i))).append(")");
     // query.append("(").append(terms.get(i)).append(")");
     query.append(terms.get(i));
   }
   Logger.info("Query match pattern: " + query.toString());
   this.matchPattern = Pattern.compile(query.toString(), Pattern.CASE_INSENSITIVE);
   Logger.info("Query match pattern: " + matchPattern.toString());
 }
Пример #5
0
  private static void startStream(List<String> terms) throws TwitterException {
    if (twitter != null) {
      twitter.cleanUp();
    }
    if (esClient != null) {
      esClient.close();
      esClient = null;
    }

    play.Configuration pconf = Play.application().configuration();
    String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name");
    if (elasticSearchCluster != null) {
      Logger.info("Configuring ElasticSearch...");
      Settings settings =
          ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build();

      esClient =
          new TransportClient(settings)
              .addTransportAddress(
                  new InetSocketTransportAddress(
                      pconf.getString("tweet.elasticsearch.transport.host"),
                      pconf.getInt("tweet.elasticsearch.transport.port")));
    } else {
      esClient = null;
    }

    twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration();
    TwitterStreamFactory tf = new TwitterStreamFactory(tconf);
    twitter = tf.getInstance();
    StatusListener l =
        new TweetListener(
            terms,
            esClient,
            pconf.getString("tweet.elasticsearch.index"),
            pconf.getString("tweet.elasticsearch.type"));
    twitter.addListener(l);

    String[] tracks = new String[terms.size()];
    StringBuffer termsString = new StringBuffer();
    for (int i = 0; i < terms.size(); i++) {
      tracks[i] = terms.get(i);
      if (i != 0) termsString.append(",");
      termsString.append(terms.get(i));
    }
    FilterQuery q = new FilterQuery().track(tracks);
    twitter.filter(q);
    Logger.info("Starting listening for tweets using terms " + termsString.toString() + "...");
  }
Пример #6
0
 private static StreamConfig getConfig() {
   List<StreamConfig> configs = StreamConfig.find.findList();
   if (configs.size() > 1) {
     Logger.error("Multiple stream configurations present!");
   }
   StreamConfig config;
   if (configs.isEmpty()) {
     String[] tracks = new String[3];
     tracks[0] = "nl-alert";
     tracks[1] = "nlalert";
     tracks[2] = "\"nl alert\"";
     config = new StreamConfig(tracks);
     config.save();
   } else {
     config = configs.get(0);
   }
   return config;
 }
Пример #7
0
  /**
   * Utility method loads all of the workflow form definitions from yaml files contained in the
   * workflow directory.
   *
   * @return
   */
  public static List<FormDefinition> loadWorkflowFormDefinitions() {
    //        List<FormDefinition> formDefs = new ArrayList<>();
    //
    //        URL path = Play.application().classloader().getResource(WORKFLOWS_PATH);
    //        try {
    //            File dir = new File(path.toURI());
    //
    //            File[] workflows = dir.listFiles(new FilenameFilter() {
    //                public boolean accept(File dir, String name) {
    //                    return name.toLowerCase().endsWith(".yaml");
    //                }
    //            });
    //
    //            for (File file : workflows) {
    //                formDefs.add(loadFormDefinition(file.getAbsolutePath()));
    //            }
    //        } catch (URISyntaxException e) { /* Should not occur */ }

    List<FormDefinition> formDefs = new ArrayList<>();

    Collection<config.WorkflowConfig> workflows = ConfigManager.getInstance().getWorkflowConfigs();

    for (config.WorkflowConfig workflow : workflows) {
      FormDefinition formDef = new FormDefinition();
      formDef.name = workflow.getName();
      formDef.title = workflow.getTitle();
      formDef.yamlFile = workflow.getYaml();
      formDef.documentation = workflow.getDocumentation();
      formDef.instructions = workflow.getInstructions();
      formDef.summary = workflow.getSummary();

      for (ParameterConfig parameter : workflow.getParameters()) {
        if (parameter.isTyped()) {
          String type = parameter.getType();

          switch (type) {
            case "text":
              TextField textField = new TextField();
              textField.name = parameter.getName();
              textField.label = parameter.getLabel();
              textField.tooltip = parameter.getDescription();
              formDef.addField(textField);
              break;
            case "select":
              SelectField selectField = new SelectField();
              selectField.name = parameter.getName();
              selectField.label = parameter.getLabel();
              selectField.options = parameter.getOptions();
              selectField.tooltip = parameter.getDescription();
              formDef.addField(selectField);
              break;
            case "upload":
              FileInput fileInput = new FileInput();
              fileInput.name = parameter.getName();
              fileInput.label = parameter.getLabel();
              fileInput.tooltip = parameter.getDescription();
              formDef.addField(fileInput);
              break;
          }
        }
      }

      formDefs.add(formDef);
    }
    return formDefs;
  }