server/src/main/java/org/iri_research/renkan/controller/RenkanController.java
author ymh <ymh.work@gmail.com>
Fri, 15 Mar 2013 17:11:44 +0100
changeset 86 0fe9045d25b7
parent 84 b4b83e9b808d
child 109 b6b20e8c4a1a
permissions -rw-r--r--
Add translation on delete message Add copy method on controler.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
package org.iri_research.renkan.controller;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import java.util.HashMap;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import java.util.Map;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
     6
import org.iri_research.renkan.Constants;
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
     7
import org.iri_research.renkan.Constants.EditMode;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
     8
import org.iri_research.renkan.RenkanException;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import org.iri_research.renkan.models.Project;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import org.iri_research.renkan.repositories.ProjectsRepository;
77
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    11
import org.iri_research.renkan.repositories.SpacesRepository;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
import org.slf4j.Logger;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import org.slf4j.LoggerFactory;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
import org.springframework.beans.factory.annotation.Autowired;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    15
import org.springframework.http.HttpStatus;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import org.springframework.stereotype.Controller;
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    17
import org.springframework.ui.Model;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import org.springframework.web.HttpMediaTypeNotSupportedException;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
import org.springframework.web.bind.annotation.PathVariable;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
import org.springframework.web.bind.annotation.RequestHeader;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
import org.springframework.web.bind.annotation.RequestMapping;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
import org.springframework.web.bind.annotation.RequestMethod;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    23
import org.springframework.web.bind.annotation.RequestParam;
86
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    24
import org.springframework.web.bind.annotation.ResponseBody;
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    25
import org.springframework.web.client.HttpClientErrorException;
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    26
import org.springframework.web.client.HttpServerErrorException;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
import org.springframework.web.servlet.ModelAndView;
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
@Controller
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    31
@RequestMapping("/p")
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
public class RenkanController {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	private final Logger logger = LoggerFactory.getLogger(RenkanController.class);
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    35
		
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	@Autowired
77
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    37
	private ProjectsRepository projectsRepository;
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    38
	
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    39
	@Autowired
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    40
	private SpacesRepository spacesRepository;
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
	
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    42
	private void checkCowebkey(String cowebkey, Project project, Constants.EditMode editMode) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    43
		if(cowebkey == null || cowebkey.isEmpty()) {
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    44
			throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Cowebkey missing");
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    45
		}
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    46
		try {
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    47
			if(!project.checkKey(cowebkey, editMode)) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    48
				throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Bad cowebkey");
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    49
			}
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    50
		} catch (RenkanException e) {
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    51
			throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    52
		}
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    53
	}
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    54
	
86
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    55
	@RequestMapping(value="/copy", method = RequestMethod.POST, produces={"application/json;charset=UTF-8"})
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    56
	public @ResponseBody Project copyProject(@RequestParam(value="project_id") String projectId ) {
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    57
				
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    58
		if(projectId == null || projectId.length() == 0) {
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    59
			throw new IllegalArgumentException("RenkanContoller.renkanProject.copyProject: Project id is null or empty.");
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    60
		}
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    61
		Project project = this.projectsRepository.findOne(projectId);
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    62
		if(project == null) {
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    63
			throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + projectId + " not found for copyProject.");
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    64
		}
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    65
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    66
		Project newProject = this.projectsRepository.copy(project, project.getTitle() + " (copy)");
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    67
		
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    68
		return newProject;
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    69
	}
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    70
0fe9045d25b7 Add translation on delete message
ymh <ymh.work@gmail.com>
parents: 84
diff changeset
    71
	 	
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    72
	@RequestMapping(value="/{project_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8", "!image/*"})
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    73
	public ModelAndView renkanProject(
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    74
			@PathVariable(value="project_id") String project_id,
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    75
			@RequestHeader(value="Accept") String accept_header,
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    76
			@RequestParam(value="cowebkey") String cowebkey
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    77
			) throws HttpMediaTypeNotSupportedException
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    78
	{
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    80
		this.logger.debug("renkanProject : " + project_id + " Accept : " + accept_header!=null?accept_header:"" + ", cowebkey: "+ cowebkey!=null?cowebkey:"");
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    81
				
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		if(accept_header != null && accept_header.matches(".*image/.*")) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    83
			throw new HttpMediaTypeNotSupportedException("Do not produce image");
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		if(project_id == null || project_id.length() == 0) {
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			throw new IllegalArgumentException("RenkanContoller.renkanProject: Project id is null or empty.");
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		
77
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
    90
		Project project = this.projectsRepository.findOne(project_id);
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    91
		
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		if(project == null) {
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    93
			throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + project_id + " not found.");
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		}
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
    96
		this.checkCowebkey(cowebkey, project, EditMode.EDITION);
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
    97
		
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		Map<String, Object> model = new HashMap<String, Object>();
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		model.put("project", project);
77
4f2511a70880 add bin config from spaces.
ymh <ymh.work@gmail.com>
parents: 51
diff changeset
   100
		model.put("space", spacesRepository.findOne(project.getSpaceId()));
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		return new ModelAndView("renkanProjectEdit", model);
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	}
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   104
	
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   105
	@RequestMapping(value="/pub/{project_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8", "!image/*"})
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   106
	public String renkanPublishProject(
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   107
			Model model,
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   108
			@PathVariable(value="project_id") String project_id,
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   109
			@RequestParam(value="cowebkey") String cowebkey
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   110
			)
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   111
	{
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   112
		if(project_id == null || project_id.length() == 0) {
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   113
			throw new IllegalArgumentException("RenkanContoller.renkanProject: Project id is null or empty.");
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   114
		}
51
3247fccfbd3f update on renkan
ymh <ymh.work@gmail.com>
parents: 47
diff changeset
   115
		
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   116
		Project project = this.projectsRepository.findOne(project_id);
84
b4b83e9b808d Add copy for projects
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   117
		if(project == null) {
b4b83e9b808d Add copy for projects
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   118
			throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + project_id + " not found.");
b4b83e9b808d Add copy for projects
ymh <ymh.work@gmail.com>
parents: 80
diff changeset
   119
		}
80
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   120
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   121
		this.checkCowebkey(cowebkey, project, EditMode.READ_ONLY);
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   122
		
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   123
		model.addAttribute("project", project);
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   124
		model.addAttribute("space", spacesRepository.findOne(project.getSpaceId()));
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   125
		
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   126
		return "renkanProjectPublish";
5295e118320b add link for publish
ymh <ymh.work@gmail.com>
parents: 77
diff changeset
   127
	}
47
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	
267d67791e05 first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
}