| 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-- |
|
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 | 6 |
import org.iri_research.renkan.Constants; |
7 |
import org.iri_research.renkan.Constants.EditMode; |
|
| 51 | 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 | 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 | 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 | 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 | 23 |
import org.springframework.web.bind.annotation.RequestParam; |
| 86 | 24 |
import org.springframework.web.bind.annotation.ResponseBody; |
| 51 | 25 |
import org.springframework.web.client.HttpClientErrorException; |
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 | 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 | 35 |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
@Autowired |
| 77 | 37 |
private ProjectsRepository projectsRepository; |
38 |
|
|
39 |
@Autowired |
|
40 |
private SpacesRepository spacesRepository; |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
|
| 80 | 42 |
private void checkCowebkey(String cowebkey, Project project, Constants.EditMode editMode) { |
| 51 | 43 |
if(cowebkey == null || cowebkey.isEmpty()) { |
44 |
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Cowebkey missing"); |
|
45 |
} |
|
46 |
try { |
|
| 80 | 47 |
if(!project.checkKey(cowebkey, editMode)) { |
| 51 | 48 |
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Bad cowebkey"); |
49 |
} |
|
50 |
} catch (RenkanException e) { |
|
51 |
throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
| 86 | 55 |
@RequestMapping(value="/copy", method = RequestMethod.POST, produces={"application/json;charset=UTF-8"}) |
56 |
public @ResponseBody Project copyProject(@RequestParam(value="project_id") String projectId ) { |
|
57 |
|
|
58 |
if(projectId == null || projectId.length() == 0) { |
|
59 |
throw new IllegalArgumentException("RenkanContoller.renkanProject.copyProject: Project id is null or empty."); |
|
60 |
} |
|
61 |
Project project = this.projectsRepository.findOne(projectId); |
|
62 |
if(project == null) { |
|
63 |
throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + projectId + " not found for copyProject."); |
|
64 |
} |
|
65 |
||
66 |
Project newProject = this.projectsRepository.copy(project, project.getTitle() + " (copy)"); |
|
67 |
|
|
68 |
return newProject; |
|
69 |
} |
|
70 |
||
71 |
|
|
| 51 | 72 |
@RequestMapping(value="/{project_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8", "!image/*"}) |
73 |
public ModelAndView renkanProject( |
|
74 |
@PathVariable(value="project_id") String project_id, |
|
75 |
@RequestHeader(value="Accept") String accept_header, |
|
76 |
@RequestParam(value="cowebkey") String cowebkey |
|
| 80 | 77 |
) throws HttpMediaTypeNotSupportedException |
78 |
{ |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
|
| 51 | 80 |
this.logger.debug("renkanProject : " + project_id + " Accept : " + accept_header!=null?accept_header:"" + ", cowebkey: "+ cowebkey!=null?cowebkey:""); |
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 | 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 | 90 |
Project project = this.projectsRepository.findOne(project_id); |
| 51 | 91 |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
if(project == null) { |
| 51 | 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 | 96 |
this.checkCowebkey(cowebkey, project, EditMode.EDITION); |
| 51 | 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 | 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 | 104 |
|
105 |
@RequestMapping(value="/pub/{project_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8", "!image/*"}) |
|
106 |
public String renkanPublishProject( |
|
107 |
Model model, |
|
108 |
@PathVariable(value="project_id") String project_id, |
|
109 |
@RequestParam(value="cowebkey") String cowebkey |
|
110 |
) |
|
111 |
{ |
|
112 |
if(project_id == null || project_id.length() == 0) { |
|
113 |
throw new IllegalArgumentException("RenkanContoller.renkanProject: Project id is null or empty."); |
|
114 |
} |
|
| 51 | 115 |
|
| 80 | 116 |
Project project = this.projectsRepository.findOne(project_id); |
| 84 | 117 |
if(project == null) { |
118 |
throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + project_id + " not found."); |
|
119 |
} |
|
| 80 | 120 |
|
121 |
this.checkCowebkey(cowebkey, project, EditMode.READ_ONLY); |
|
122 |
|
|
123 |
model.addAttribute("project", project); |
|
124 |
model.addAttribute("space", spacesRepository.findOne(project.getSpaceId())); |
|
125 |
|
|
126 |
return "renkanProjectPublish"; |
|
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 |
} |