org.kievguide.controller.PlaceController.java Source code

Java tutorial

Introduction

Here is the source code for org.kievguide.controller.PlaceController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kievguide.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.kievguide.entity.Comment;
import org.kievguide.entity.Place;
import org.kievguide.entity.Station;
import org.kievguide.entity.User;
import org.kievguide.entity.Vote;
import org.kievguide.service.CommentService;
import org.kievguide.service.PlaceService;
import org.kievguide.service.StationService;
import org.kievguide.service.UserService;
import org.kievguide.service.VoteService;
import org.kievguide.util.Util;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author denis
 */
@Controller
public class PlaceController {
    @Inject
    @Qualifier("userService")
    private UserService userService;

    @Inject
    @Qualifier("placeService")
    private PlaceService placeService;

    @Inject
    @Qualifier("voteService")
    private VoteService voteService;

    @Inject
    @Qualifier("commentService")
    private CommentService commentService;

    @Inject
    @Qualifier("stationService")
    private StationService stationService;

    @RequestMapping(value = "/firstrequest", method = RequestMethod.GET)
    public ModelAndView firstRequest(@CookieValue(value = "userstatus", defaultValue = "guest") String cookie) {
        ModelAndView modelAndView = new ModelAndView();
        String userStatus;
        if (cookie.equals("guest")) {
            userStatus = Util.guestPanel();
        } else {
            userStatus = Util.userPanel(cookie);
        }

        List<Place> places = placeService.findAll();
        modelAndView.addObject("places", places);
        modelAndView.addObject("userstatus", userStatus);
        modelAndView.setViewName("index");
        return modelAndView;

    }

    @RequestMapping(value = "/station", method = RequestMethod.GET)
    public ModelAndView placesByMetro(@CookieValue(value = "userstatus", defaultValue = "guest") String cookie,
            @RequestParam("stationid") int stationid) {
        ModelAndView modelAndView = new ModelAndView();
        String userStatus;
        if (cookie.equals("guest")) {
            userStatus = Util.guestPanel();
        } else {
            userStatus = Util.userPanel(cookie);
        }
        Station metro = stationService.findById(stationid);
        List<Place> places = placeService.findByMetro(metro);

        modelAndView.addObject("places", places);
        modelAndView.addObject("station", metro);
        modelAndView.addObject("userstatus", userStatus);
        modelAndView.setViewName("indexall");
        return modelAndView;

    }

    @RequestMapping(value = "/place", method = RequestMethod.GET)
    public ModelAndView placeById(@CookieValue(value = "userstatus", defaultValue = "guest") String useremail,
            @RequestParam("placeid") int placeid) {

        ModelAndView modelAndView = new ModelAndView();
        Place place = placeService.searchPlaceById(placeid);
        place.setRatingvalue(new BigDecimal(place.getRatingvalue()).setScale(1, RoundingMode.UP).doubleValue());
        String userStatus = Util.userPanel(useremail);
        String voteStatus;
        User currentuser = userService.searchUser(useremail);
        Vote newvote = voteService.findByAuthorIdAndPlaceId(currentuser.getId().intValue(), placeid);
        if (newvote != null) {
            voteStatus = Util.userVote(newvote.getValue());
        } else {
            voteStatus = Util.guestVote(place.getId());
        }
        User author = userService.searchUserById(place.getAuthorid().getId());
        String authorname = author.getFirstname() + " " + author.getLastname();

        List<Comment> commentlist = commentService.findByPlaceid(place);
        for (Comment c : commentlist) {
            if (c.getAuthorid().getId() == currentuser.getId()) {
                c.setIsdelete("<a href=\"deletevote?commentid=" + c.getId() + "&placeid=" + c.getPlaceid().getId()
                        + "\" class=\"comment-delete\"> </a>");
            }
        }

        List<Place> anotherPlaces = anotherPlaces(place.getMetro().getId(), place.getId());

        modelAndView.addObject("userstatus", userStatus);
        modelAndView.addObject("author", authorname);
        modelAndView.addObject("currentuser", currentuser);
        modelAndView.addObject("votestatus", voteStatus);
        modelAndView.addObject("place", place);
        modelAndView.addObject("anotherplaces", anotherPlaces);
        modelAndView.addObject("comments", commentlist);
        modelAndView.setViewName("place");
        return modelAndView;
    }

    @RequestMapping(value = "/addnewplaceredirect", method = RequestMethod.GET)
    public ModelAndView addNewPlaceRedirect(
            @CookieValue(value = "userstatus", defaultValue = "guest") String useremail) {
        ModelAndView modelAndView = new ModelAndView();
        String userStatus = Util.userPanel(useremail);
        modelAndView.addObject("userstatus", userStatus);
        modelAndView.setViewName("addnewplace");
        return modelAndView;
    }

    @RequestMapping(value = "/addnewplace", method = RequestMethod.POST)
    public ModelAndView addNewPlace(HttpServletRequest request,
            @CookieValue(value = "userstatus", defaultValue = "guest") String useremail,
            @RequestParam("placename") String placename, @RequestParam("placedescription") String placedescription,
            @RequestParam("placeadress") String placeadress, @RequestParam("placemetro") Integer placemetro,
            @RequestParam("placephotosrc") MultipartFile file) throws IOException {
        ModelAndView modelAndView = new ModelAndView();
        SecureRandom random = new SecureRandom();
        String photoname = new BigInteger(130, random).toString(32);
        Place place = new Place();
        User user = userService.searchUser(useremail);
        Station metroStation = stationService.findById(placemetro);

        System.out.println("========================================== metro = " + metroStation.getName());

        place.setName(placename);
        place.setDescription(placedescription);
        place.setAdress(placeadress);
        place.setMetro(metroStation);
        place.setAuthorid(user);
        place.setPhotosrc("img/" + photoname + ".jpg");
        place.setRatingcount(0);
        place.setRatingvalue(0.0);
        String folder = request.getSession().getServletContext().getRealPath("");
        folder = folder.substring(0, 30);
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(folder + "/src/main/webapp/img/" + photoname + ".jpg")));
        FileCopyUtils.copy(file.getInputStream(), stream);
        stream.close();
        System.out.println(
                "========================================== ? ? = " + place.getName());
        placeService.addPlace(place);

        return new ModelAndView("redirect:" + "firstrequest");
    }

    private List anotherPlaces(Integer metroid, Integer placeId) {
        List<Place> allplacelist = placeService.findByMetro(stationService.findById(metroid));
        List<Place> anotherPlaces = new ArrayList<Place>();
        Set<Integer> anotherPlacesId = new HashSet<Integer>();
        Random r = new Random(System.currentTimeMillis());
        Integer temp;
        Integer quantity;
        if (allplacelist.size() < 5) {
            quantity = allplacelist.size() - 1;
        } else {
            quantity = 4;
        }

        while (anotherPlacesId.size() < quantity) {
            temp = r.nextInt(allplacelist.size());
            if (allplacelist.get(temp).getId() != placeId) {
                anotherPlacesId.add(temp);
            }
        }

        for (Integer a : anotherPlacesId) {
            anotherPlaces.add(allplacelist.get(a));
        }

        return anotherPlaces;
    }

}