Beispiel #1
0
  @POST
  @Path("/vote")
  public String vote(String json) {
    // Todo take the vote intention and make two things

    System.out.println("About to register vote  " + json);
    C2SVoteRequest o = g.fromJson(json, C2SVoteRequest.class);
    // get the poll see the type and deserliase based on that
    NQQuestion p;
    try {
      p = Services.vote.pollById(UUID.fromString(o.questionId));
      switch (p.type) {
        case Preferential:
          System.out.println("VOTE PREF " + json);
          try {
            // got to interpret a list of set as a rankedvote.
            System.out.println("o.choice : " + o.choice);
            ListOfSetOfString lss = CustomGson.getIt().fromJson(o.choice, ListOfSetOfString.class);
            System.out.println("LSS " + lss);
            RankedVote<String> rv = RankedVote.fromListOfSet(lss);
            System.out.println("rv " + rv);
            o.choice = CustomGson.getIt().toJson(rv);
            Services.vote.registerVote(o);
          } catch (AlreadyVoted | NoToken e1) {
            return "AlreadyVoted";
          }
          return "Preferential TODO";
        case SingleChoice:
          System.out.println("VOTE SINGLE " + json);
          NQAnswerReceipt res;
          try {
            res = Services.vote.registerVote(o);
            System.out.println("VOTE SINGLE DONE " + json);
            return g.toJson(res);
          } catch (AlreadyVoted e) {
            System.out.println("VOTE SINGLE Arleardy voted " + json);
            return "AlreadyVoted";
          } catch (NoToken e) {
            System.out.println("No Token voted " + json);
            return "NoToken";
          }
      }
      return "TODO";
    } catch (NoSuchPoll e1) {
      return e1.getClass().getSimpleName();
    }
  }
Beispiel #2
0
  @GET
  @Path("/results/{id}")
  public String results(@PathParam("id") String id) {

    NQQuestion p;
    try {
      p = Services.vote.pollById(UUID.fromString(id));
    } catch (NoSuchPoll e) {
      return e.getClass().getSimpleName();
    }
    switch (p.type) {
      case Preferential:
        {
          System.out.println("RESULTS PREF ");
          S2CPreferentialResult result = new S2CPreferentialResult();
          // build a BallotBox
          NQResult res = Services.vote.resultsForPoll(id);
          BallotBox<String> bb = new BallotBox<String>();
          for (String vote : res.result.keySet()) {
            RankedVote<String> ranked = CustomGson.getIt().fromJson(vote, RankedVote.class);
            bb.add(ranked, res.result.get(vote));
          }
          result.edges = bb.computePairwise().triplets();
          result.undisputedWinner =
              bb.undisputedWinners().size() > 0 ? bb.undisputedWinners().iterator().next() : null;
          // apply shulze
          result.shulze = RankedVote.fromListOfSet(new ShulzeOnBallotBox<String>(bb).results());
          result.tideman = new TidemanOnBallotBox(bb).results();
          result.instant = new InstantRunoffOnBallotBox(bb).results();
          // apply Shulze method and get the result
          System.out.println("Tideman condensed   :::   " + result.tideman.toCondense());
          System.out.println("Shulze  condensed   :::   " + result.shulze.toCondense());
          String json = g.toJson(result);
          // System.out.println("\n\n\n\n\n\n\n\n"+json);
          return json;
        }
      case SingleChoice:
        System.out.println("RESULTS SINGLE ");
        NQResult res = Services.vote.resultsForPoll(id);
        String json = g.toJson(res);
        System.out.println("   >> " + json);
        return json;
      default:
        return "TODO";
    }
  }