Example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest

List of usage examples for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest.

Prototype

public static ServletUriComponentsBuilder fromCurrentRequest() 

Source Link

Document

Same as #fromRequest(HttpServletRequest) except the request is obtained through RequestContextHolder .

Usage

From source file:eu.supersede.gr.rest.GameRest.java

@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<?> createGame(Authentication auth, @RequestBody HAHPGame game,
        @RequestParam(required = true) String criteriaValues, @RequestParam Long processId)
        throws JsonParseException, JsonMappingException, IOException {
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
    };// w w w.  ja v  a 2  s  .c  om

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Map<String, Integer>> cvs = mapper.readValue(criteriaValues, typeRef);
    game.setStartTime(new Date());
    // re-attach detached requirements
    List<Requirement> rs = game.getRequirements();

    //        if( System.currentTimeMillis() > 0 )
    //           throw new RuntimeException("");

    for (int i = 0; i < rs.size(); i++) {
        rs.set(i, requirements.findOne(rs.get(i).getRequirementId()));
    }
    // re-attach detached users
    List<User> us = game.getPlayers();

    for (int i = 0; i < us.size(); i++) {
        us.set(i, users.findOne(us.get(i).getUserId()));
    }
    // re-attach detached criterias
    List<ValutationCriteria> cs = game.getCriterias();

    for (int i = 0; i < cs.size(); i++) {
        cs.set(i, criterias.findOne(cs.get(i).getCriteriaId()));
    }

    Object user = auth.getPrincipal();

    if (user instanceof DatabaseUser) {
        DatabaseUser dbUser = (DatabaseUser) user;
        User u = users.getOne(dbUser.getUserId());
        game.setCreator(u);

        // add points for the creation of a game
        pointsLogic.addPoint(u, -3l, -1l);
    }

    if (cs.size() < 2) {
        throw new RuntimeException("Not enough criteria");
    }
    if (us.size() < 1) {
        throw new RuntimeException("Not enough players");
    }
    if (rs.size() < 2) {
        throw new RuntimeException("Not enough requirements");
    }

    game.setFinished(false);
    game = games.save(game);

    ProcessManager mgr = DMGame.get().getProcessManager(processId);
    {
        HActivity a = mgr.createActivity(AHPPlayerMethod.NAME,
                ((DatabaseUser) auth.getPrincipal()).getUserId());
        a.setUserId(null); // do this to avoid showing it in the supervisor pending activities list
        PropertyBag bag = mgr.getProperties(a);
        bag.set("gameId", "" + game.getGameId());
    }

    for (int i = 0; i < cs.size() - 1; i++) {
        for (int j = i + 1; j < cs.size(); j++) {
            HAHPCriteriasMatrixData cmd = new HAHPCriteriasMatrixData();
            cmd.setGame(game);
            cmd.setRowCriteria(cs.get(j));
            cmd.setColumnCriteria(cs.get(i));
            String c1Id = cs.get(j).getCriteriaId().toString();
            String c2Id = cs.get(i).getCriteriaId().toString();

            if (cvs.containsKey(c1Id) && cvs.get(c1Id).containsKey(c2Id)) {
                cmd.setValue(new Long(cvs.get(c1Id).get(c2Id)));
            } else {
                cmd.setValue(new Long(cvs.get(c2Id).get(c1Id)));
            }

            criteriasMatricesData.save(cmd);
        }
    }

    for (int c = 0; c < cs.size(); c++) {
        for (int i = 0; i < rs.size() - 1; i++) {
            for (int j = i + 1; j < rs.size(); j++) {
                HAHPRequirementsMatrixData rmd = new HAHPRequirementsMatrixData();
                rmd.setGame(game);
                rmd.setRowRequirement(rs.get(j));
                rmd.setColumnRequirement(rs.get(i));
                rmd.setCriteria(cs.get(c));
                rmd.setValue(-1l);
                requirementsMatricesData.save(rmd);

                for (int p = 0; p < us.size(); p++) {
                    HAHPPlayerMove pm = new HAHPPlayerMove();
                    pm.setPlayer(us.get(p));
                    pm.setRequirementsMatrixData(rmd);
                    pm.setPlayed(false);
                    playerMoves.save(pm);
                }

                HAHPJudgeAct ja = new HAHPJudgeAct();
                ja.setVoted(false);
                ja.setRequirementsMatrixData(rmd);
                judgeActs.save(ja);
            }
        }
    }

    DatabaseUser currentUser = (DatabaseUser) auth.getPrincipal();

    for (User u : us) {
        eu.supersede.integration.api.datastore.fe.types.User proxyUser = proxy.getFEDataStoreProxy()
                .getUser(currentUser.getTenantId(), u.getUserId().intValue(), true, currentUser.getToken());
        // creation of email for the players when a game is started
        supersedeMailSender.sendEmail("New Decision Making Process",
                "Hi " + proxyUser.getFirst_name() + " " + proxyUser.getLast_name()
                        + ", this is an automatically generated mail. You have just been invited to "
                        + "participate in a prioritization process.",
                proxyUser.getEmail());

        notificationUtil.createNotificationForUser(proxyUser.getEmail(),
                "A new decision making process has been created, are you ready to vote?",
                "supersede-dm-app/ahprp/player_games");

        // create a GamePlayerPoint for this game and for all the players in the game
        HAHPGamePlayerPoint gpp = new HAHPGamePlayerPoint();
        gpp.setGame(game);
        gpp.setUser(u);
        gpp.setPoints(0l);
        gamesPlayersPoints.save(gpp);

        HActivity a = mgr.createActivity(AHPPlayerMethod.NAME, u.getUserId());
        PropertyBag bag = mgr.getProperties(a);
        bag.set("gameId", "" + game.getGameId());
    }

    // FIXME
    //        notificationUtil.createNotificationsForProfile("OPINION_NEGOTIATOR",
    //                "A new decision making process has been created, you are in charge to take decisions",
    //                "supersede-dm-app/ahprp/judge_games");

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(game.getGameId()).toUri());
    return new ResponseEntity<>(game.getGameId(), httpHeaders, HttpStatus.CREATED);
}