| author | ymh <ymh.work@gmail.com> |
| Wed, 23 Oct 2013 03:01:44 +0200 | |
| changeset 201 | c8b3700eaed6 |
| parent 109 | b6b20e8c4a1a |
| child 222 | 6ac00231ee34 |
| 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; |
| 109 | 9 |
import org.iri_research.renkan.RenkanProperties; |
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
import org.iri_research.renkan.models.Project; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
import org.iri_research.renkan.repositories.ProjectsRepository; |
| 77 | 12 |
import org.iri_research.renkan.repositories.SpacesRepository; |
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
import org.slf4j.Logger; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
import org.slf4j.LoggerFactory; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
import org.springframework.beans.factory.annotation.Autowired; |
| 51 | 16 |
import org.springframework.http.HttpStatus; |
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
import org.springframework.stereotype.Controller; |
| 80 | 18 |
import org.springframework.ui.Model; |
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
import org.springframework.web.HttpMediaTypeNotSupportedException; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
import org.springframework.web.bind.annotation.PathVariable; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
import org.springframework.web.bind.annotation.RequestHeader; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
import org.springframework.web.bind.annotation.RequestMethod; |
| 51 | 24 |
import org.springframework.web.bind.annotation.RequestParam; |
| 86 | 25 |
import org.springframework.web.bind.annotation.ResponseBody; |
| 51 | 26 |
import org.springframework.web.client.HttpClientErrorException; |
27 |
import org.springframework.web.client.HttpServerErrorException; |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
import org.springframework.web.servlet.ModelAndView; |
|
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 |
|
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
@Controller |
| 51 | 32 |
@RequestMapping("/p") |
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
public class RenkanController { |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
|
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
private final Logger logger = LoggerFactory.getLogger(RenkanController.class); |
| 51 | 36 |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
@Autowired |
| 77 | 38 |
private ProjectsRepository projectsRepository; |
39 |
|
|
40 |
@Autowired |
|
41 |
private SpacesRepository spacesRepository; |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
|
| 80 | 43 |
private void checkCowebkey(String cowebkey, Project project, Constants.EditMode editMode) { |
| 51 | 44 |
if(cowebkey == null || cowebkey.isEmpty()) { |
45 |
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Cowebkey missing"); |
|
46 |
} |
|
47 |
try { |
|
| 80 | 48 |
if(!project.checkKey(cowebkey, editMode)) { |
| 51 | 49 |
throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Bad cowebkey"); |
50 |
} |
|
51 |
} catch (RenkanException e) { |
|
52 |
throw new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
| 86 | 56 |
@RequestMapping(value="/copy", method = RequestMethod.POST, produces={"application/json;charset=UTF-8"}) |
57 |
public @ResponseBody Project copyProject(@RequestParam(value="project_id") String projectId ) { |
|
58 |
|
|
59 |
if(projectId == null || projectId.length() == 0) { |
|
60 |
throw new IllegalArgumentException("RenkanContoller.renkanProject.copyProject: Project id is null or empty."); |
|
61 |
} |
|
62 |
Project project = this.projectsRepository.findOne(projectId); |
|
63 |
if(project == null) { |
|
64 |
throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Project " + projectId + " not found for copyProject."); |
|
65 |
} |
|
66 |
||
67 |
Project newProject = this.projectsRepository.copy(project, project.getTitle() + " (copy)"); |
|
68 |
|
|
69 |
return newProject; |
|
70 |
} |
|
71 |
||
72 |
|
|
| 51 | 73 |
@RequestMapping(value="/{project_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8", "!image/*"}) |
74 |
public ModelAndView renkanProject( |
|
75 |
@PathVariable(value="project_id") String project_id, |
|
76 |
@RequestHeader(value="Accept") String accept_header, |
|
77 |
@RequestParam(value="cowebkey") String cowebkey |
|
| 80 | 78 |
) throws HttpMediaTypeNotSupportedException |
79 |
{ |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
|
| 51 | 81 |
this.logger.debug("renkanProject : " + project_id + " Accept : " + accept_header!=null?accept_header:"" + ", cowebkey: "+ cowebkey!=null?cowebkey:""); |
82 |
|
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
|
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
if(project_id == null || project_id.length() == 0) { |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
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
|
86 |
} |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
|
| 77 | 88 |
Project project = this.projectsRepository.findOne(project_id); |
| 51 | 89 |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
if(project == null) { |
| 51 | 91 |
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
|
92 |
} |
|
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
|
| 80 | 94 |
this.checkCowebkey(cowebkey, project, EditMode.EDITION); |
| 51 | 95 |
|
|
47
267d67791e05
first stabilization of editing a renkan.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
Map<String, Object> model = new HashMap<String, Object>(); |
| 109 | 97 |
model.put("coweb_debug", Boolean.parseBoolean(RenkanProperties.getInstance().getProperty("renkan.coweb.debug","false"))); |
98 |
model.put("coweb_websockets", Boolean.parseBoolean(RenkanProperties.getInstance().getProperty("renkan.coweb.websocket", "true"))); |
|
|
47
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 |
} |