@DELETE
  @Path("/{name}")
  @RolesAllowed({"user", "admin"})
  public Response deleteClustering(@PathParam("name") String clusteringName) {

    EntityManager em = Application.createEntityManager();
    Clustering clustering = em.find(Clustering.class, clusteringName);

    if (clustering == null)
      return Response.status(404).entity("Clustering not found for this name.").build();

    try {
      em.getTransaction().begin();
      Query q = em.createQuery("delete from News n where n.clustering.name = :name");
      q.setParameter("name", clusteringName);
      q.executeUpdate();
      em.remove(clustering);
      em.getTransaction().commit();
    } catch (Exception e) {
      em.getTransaction().rollback();
      return Response.status(500).entity(e.getMessage()).build();
    }

    return Response.status(200).build();
  }
 static {
   EntityManager em = Application.createEntityManager();
   stopWordsLists =
       em.createQuery("select l from NoiseWordsList l", NoiseWordsList.class)
           .getResultList()
           .stream()
           .collect(Collectors.toMap(NoiseWordsList::getLanguage, NoiseWordsList::getWords));
   em.close();
   if (stopWordsLists.size() == 0)
     throw new IllegalArgumentException(
         "No noise words lists found. Please provide at least one list.");
 }
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  @RolesAllowed({"user", "admin", "guest"})
  public List<Clustering> getClusterings() {

    EntityManager em = Application.createEntityManager();
    List<Clustering> clusterings;

    try {
      clusterings = em.createQuery("select c from Clustering c", Clustering.class).getResultList();
    } catch (NoResultException e) {
      clusterings = null;
    }

    em.close();

    return clusterings;
  }
Esempio n. 4
0
  public static void main(String[] args) {

    String title =
        "\n"
            + "      _   __                     ___                \n"
            + "     / | / /__ _      _______   /   |  ____  ____   \n"
            + "    /  |/ / _ \\ | /| / / ___/  / /| | / __ \\/ __ \\  \n"
            + "   / /|  /  __/ |/ |/ (__  )  / ___ |/ /_/ / /_/ /  \n"
            + "  /_/ |_/\\___/|__/|__/____/  /_/  |_/ .___/ .___/   \n"
            + "                                   /_/   /_/        \n";

    System.out.println(title);
    System.out.flush();

    if (args.length != 0 && (args[0].equalsIgnoreCase("help") || args[0].equalsIgnoreCase("h"))) {
      InputStream stream = Application.class.getResourceAsStream("/help.txt");
      BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
      reader.lines().forEach(System.out::println);
      System.out.flush();
      return;
    }

    System.out.println("Starting Up...");

    try {
      props = new PropertyConfig();
    } catch (Exception e) {
      System.err.println(e.getMessage());
      e.printStackTrace();
      System.exit(-1);
    }

    Logging.setUp();
    log = Logging.registerLogger(Application.class.getName());
    Logging.registerLogger("");
    Logging.registerLogger("org.hibernate", Level.WARNING);

    log.info("Initializing Entity Manager factory...");
    log.info("Persistence Unit: " + props.getProp("PERSISTENCE_UNIT"));
    persistenceManager = new PersistenceManager(props.getProp("PERSISTENCE_UNIT"));

    if (args.length != 0 && args[0].equals("multilevel-clustering")) {

      String clusteringName = args[1];
      double metaNewsThreshold = Double.parseDouble(args[2]);
      double newsThreshold = Double.parseDouble(args[3]);
      String tfDictionaryName = args[4];
      String languageString = args[5];

      try {
        MultilevelIncrementalClusteringTask.execTask(
            clusteringName, metaNewsThreshold, newsThreshold, tfDictionaryName, languageString);
      } catch (Exception e) {
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      Application.tearDown();
      System.exit(0);
    } else if (args.length != 0 && args[0].equals("multilevel-performance")) {

      double metaNewsThresholdSeed = Double.parseDouble(args[1]);
      int metaNewsThresholdLimit = Integer.parseInt(args[2]);
      double newsThresholdSeed = Double.parseDouble(args[3]);
      int newsThresholdLimit = Integer.parseInt(args[4]);
      String tfDictionaryName = args[5];
      String languageString = args[6];

      try {
        MultilevelClusteringPerformanceTask.execTask(
            metaNewsThresholdSeed,
            metaNewsThresholdLimit,
            newsThresholdSeed,
            newsThresholdLimit,
            tfDictionaryName,
            languageString);
      } catch (Exception e) {
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      Application.tearDown();
      System.exit(0);

    } else if (args.length != 0 && args[0].equals("generate-tf-dictionary")) {

      String dictionaryName = args[1];
      String languageString = args[2];

      if (dictionaryName == null || dictionaryName.length() == 0) {
        System.err.println("Please provide a valid name for TF Dictionary");
        Application.tearDown();
        System.exit(-1);
      }

      int returnCode = -1;

      try {
        TFDictionaryGenerationTask.generateDictionary(dictionaryName, languageString);
        returnCode = 0;
      } catch (Exception e) {
        e.printStackTrace();
        returnCode = -1;
      } finally {
        Application.tearDown();
        System.exit(returnCode);
      }

    } else if (args.length != 0 && args[0].equals("load-words-list")) {

      String filePath = args[1];
      String languageString = args[2];

      if (!Files.exists(Paths.get(filePath))) {
        System.err.println("File does not exists.");
        Application.tearDown();
        System.exit(-1);
      }

      Language language = null;
      try {
        language = Language.valueOf(languageString);
      } catch (Exception e) {
        System.err.println(e.getMessage());
        Application.tearDown();
        System.exit(-1);
      }

      EntityManager em = Application.createEntityManager();

      em.getTransaction().begin();

      NoiseWordsList old =
          em.createQuery(
                  "select l from NoiseWordsList l where l.language=:lang", NoiseWordsList.class)
              .setParameter("lang", language)
              .getSingleResult();
      em.remove(old);

      NoiseWordsList list = new NoiseWordsList();
      list.setDescription(languageString);
      list.setLanguage(language);

      try {
        Files.lines(Paths.get(filePath))
            .forEach(
                w -> {
                  list.getWords().add(w.trim());
                });
      } catch (IOException e) {
        System.err.println("Cannot open file.");
        e.printStackTrace();
        Application.tearDown();
        System.exit(-1);
      }

      em.persist(list);

      em.getTransaction().commit();
      em.close();

      System.out.println("Task completed successfully.");
      Application.tearDown();
      System.exit(0);

    } else if (args.length != 0 && args[0].equals("add-user")) {
      if (args.length < 4) {
        System.err.println("Wrong command syntax, missing arguments.");
        Application.tearDown();
        System.exit(-1);
      }

      String username = args[1];
      String password = args[2];
      String role = args[3];

      if (!role.equalsIgnoreCase("admin") && !role.equalsIgnoreCase("guest")) {
        System.err.println("Role can be one of [admin, guest]");
        Application.tearDown();
        System.exit(-1);
      }

      EntityManager em = Application.createEntityManager();

      User user = null;
      try {
        user =
            em.createQuery("select u from User u where u.username=:uname", User.class)
                .setParameter("uname", username)
                .getSingleResult();
      } catch (NoResultException e) {
        user = new User();
        user.setEmail("");
      }

      user.setUsername(username);
      user.setPassword(password);
      user.setRole(role.toLowerCase());

      em.getTransaction().begin();
      em.persist(user);
      em.getTransaction().commit();

      System.out.println("User successfully created.");
      Application.tearDown();
      System.exit(0);
    }

    log.info("Setting up Scheduler");
    Task.setLogger(Logging.registerLogger("it.fcambi.news.Tasks"));
    configureScheduler();

    log.info("Starting up Web Services...");
    log.info("Bind url >> " + props.getProp("BIND_URI"));
    httpServer = new Server(props);
    httpServer.startServer();

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                Application.tearDown();
              }
            });

    configureArticleDownloaderTask();

    log.info("Startup Completed - All OK");

    System.out.println(
        "\nApplication Ready\n\t- Server @ "
            + props.getProp("BIND_URI")
            + "\n\t- GUI @ "
            + props.getProp("BIND_URI")
            + "/gui/app/\n");
    System.out.println("Press CTRL+C to stop...");

    try {
      Thread.currentThread().join();
    } catch (Exception e) {
      log.log(Level.SEVERE, "Error joining current thread.", e);
      System.exit(-1);
    }
  }