server/src/main/java/org/iri_research/renkan/controller/RenkanRootController.java
author ymh <ymh.work@gmail.com>
Mon, 21 Oct 2013 17:55:12 +0200
branchuser_management
changeset 224 0167b777ad15
parent 223 5ad314cb2337
permissions -rw-r--r--
remove deprecated warnings + fix date picker default langauge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
package org.iri_research.renkan.controller;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import java.util.HashMap;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import java.util.Map;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
76
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
     6
import javax.servlet.http.HttpServletRequest;
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
     7
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
     8
import org.iri_research.renkan.Constants;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import org.iri_research.renkan.models.Project;
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    10
import org.iri_research.renkan.models.Space;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import org.iri_research.renkan.repositories.ProjectsRepository;
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    12
import org.iri_research.renkan.repositories.SpacesRepository;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import org.slf4j.Logger;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import org.slf4j.LoggerFactory;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import org.springframework.beans.factory.annotation.Autowired;
76
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
    16
import org.springframework.data.domain.Page;
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
    17
import org.springframework.data.domain.Pageable;
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
    18
import org.springframework.data.domain.Sort.Direction;
224
0167b777ad15 remove deprecated warnings + fix date picker default langauge
ymh <ymh.work@gmail.com>
parents: 223
diff changeset
    19
import org.springframework.data.web.PageableDefault;
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    20
import org.springframework.http.HttpStatus;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
import org.springframework.stereotype.Controller;
76
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
    22
import org.springframework.ui.Model;
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    23
import org.springframework.web.bind.annotation.PathVariable;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
import org.springframework.web.bind.annotation.RequestMapping;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
import org.springframework.web.bind.annotation.RequestMethod;
137
efc0fce30814 Add filter on projects
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    26
import org.springframework.web.bind.annotation.RequestParam;
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    27
import org.springframework.web.client.HttpClientErrorException;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
import org.springframework.web.servlet.ModelAndView;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
@Controller
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
@RequestMapping("/")
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
public class RenkanRootController {
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    34
    private final Logger logger = LoggerFactory
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    35
            .getLogger(RenkanRootController.class);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    36
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    37
    @Autowired
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    38
    private ProjectsRepository projectsRepository;
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    39
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    40
    @Autowired
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    41
    private SpacesRepository spacesRepository;
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    42
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    43
    @RequestMapping(value = "", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    44
    public String renkanIndex(
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    45
            Model model,
224
0167b777ad15 remove deprecated warnings + fix date picker default langauge
ymh <ymh.work@gmail.com>
parents: 223
diff changeset
    46
            @PageableDefault(sort = { "created" }, direction = Direction.DESC, page = 0, value = Constants.PAGINATION_SIZE) Pageable p,
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    47
            HttpServletRequest request) {
76
523f0647513e add the count of project by spaces, add pagination, update libraries and add some more unit tests.
ymh <ymh.work@gmail.com>
parents: 71
diff changeset
    48
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    49
        Page<Space> page = this.spacesRepository.findAll(p);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    50
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    51
        model.addAttribute("page", page);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    52
        model.addAttribute("baseUrl", Utils.buildBaseUrl(request));
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    53
        model.addAttribute("projectsCount",
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    54
                this.projectsRepository.getCountBySpace());
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    55
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    56
        return "renkanIndex";
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    57
    }
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    58
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    59
    @RequestMapping(value = "/s/{space_id}", method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" })
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    60
    public ModelAndView spaceIndex(
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    61
            @PathVariable("space_id") String spaceId,
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    62
            @RequestParam(required = false) String filter,
224
0167b777ad15 remove deprecated warnings + fix date picker default langauge
ymh <ymh.work@gmail.com>
parents: 223
diff changeset
    63
            @PageableDefault(sort = { "updated", "created" }, direction = Direction.DESC, page = 0, value = Constants.PAGINATION_SIZE) Pageable p,
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    64
            HttpServletRequest request) {
71
9af0874ce43f First version of space level.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    65
223
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    66
        logger.debug("SpaceId : " + (spaceId == null ? "null" : spaceId));
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    67
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    68
        Map<String, Object> model = new HashMap<String, Object>();
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    69
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    70
        if ("_".equals(spaceId)) {
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    71
            spaceId = null;
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    72
        }
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    73
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    74
        Space space = this.spacesRepository.findOne(spaceId);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    75
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    76
        if (null == space) {
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    77
            throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Space "
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    78
                    + spaceId + " not found.");
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    79
        }
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    80
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    81
        model.put("space", space);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    82
        Page<Project> page;
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    83
        if (filter != null && !filter.isEmpty()) {
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    84
            page = this.projectsRepository.findBySpaceIdAndTitleRegex(spaceId,
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    85
                    filter, p);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    86
        } else {
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    87
            page = this.projectsRepository.findBySpaceId(spaceId, p);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    88
        }
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    89
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    90
        model.put("page", page);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    91
        model.put("baseUrl", Utils.buildBaseUrl(request));
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    92
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    93
        return new ModelAndView("projectIndex", model);
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    94
    }
5ad314cb2337 Upgrade libs + format sources
ymh <ymh.work@gmail.com>
parents: 137
diff changeset
    95
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
}