View Javadoc

1   package org.cateproject.controller.common;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.UUID;
6   
7   import org.cateproject.controller.Cacheable;
8   import org.cateproject.controller.GenericController;
9   import org.cateproject.controller.editor.PrintableDefinedTermPropertyEditor;
10  import org.cateproject.service.event.CdmObjectAnnotated;
11  import org.cateproject.service.event.CdmObjectEvent;
12  import org.springframework.beans.BeansException;
13  import org.springframework.beans.factory.annotation.Autowired;
14  import org.springframework.context.ApplicationContext;
15  import org.springframework.context.ApplicationContextAware;
16  import org.springframework.security.core.context.SecurityContextHolder;
17  import org.springframework.validation.BindingResult;
18  import org.springframework.validation.FieldError;
19  import org.springframework.web.bind.WebDataBinder;
20  import org.springframework.web.bind.annotation.InitBinder;
21  import org.springframework.web.bind.annotation.ModelAttribute;
22  import org.springframework.web.bind.annotation.PathVariable;
23  import org.springframework.web.bind.annotation.RequestMapping;
24  import org.springframework.web.bind.annotation.RequestMethod;
25  import org.springframework.web.bind.annotation.RequestParam;
26  import org.springframework.web.servlet.ModelAndView;
27  
28  import eu.etaxonomy.cdm.api.service.IAnnotatableService;
29  import eu.etaxonomy.cdm.api.service.IAnnotationService;
30  import eu.etaxonomy.cdm.api.service.IMarkerService;
31  import eu.etaxonomy.cdm.api.service.ITermService;
32  import eu.etaxonomy.cdm.api.service.IUserService;
33  import eu.etaxonomy.cdm.api.service.pager.Pager;
34  import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
35  import eu.etaxonomy.cdm.model.common.Annotation;
36  import eu.etaxonomy.cdm.model.common.Marker;
37  import eu.etaxonomy.cdm.model.common.MarkerType;
38  import eu.etaxonomy.cdm.model.common.User;
39  import eu.etaxonomy.cdm.persistence.query.OrderHint;
40  import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
41  
42  public abstract class AnnotatableController<T extends AnnotatableEntity,SERVICE extends IAnnotatableService<T>> extends VersionableController<T,SERVICE> {
43  	
44  	public AnnotatableController(Class<T> type) {
45  		super(type);
46  	}
47  
48      private IUserService userService; 
49      
50      protected ITermService termService;
51  	
52  	private AnnotationValidator annotationValidator;
53  	
54  
55  	
56  	@InitBinder
57      public void initBinder(WebDataBinder binder) {
58  		super.initBinder(binder);
59  		binder.registerCustomEditor(MarkerType.class, new PrintableDefinedTermPropertyEditor(MarkerType.class,termService));
60  	}
61  	
62  	@Autowired
63      public void setUserService(IUserService userService) {
64          this.userService = userService;
65      }
66  	
67  	@Autowired
68  	public void setAnnotationValidator(AnnotationValidator annotationValidator) {
69  		this.annotationValidator = annotationValidator;
70  	}
71  	
72  	@Autowired
73  	public void setTermService(ITermService termService) {
74  		this.termService = termService;
75  	}
76  
77  	@RequestMapping(value = "/annotations/edit", method = RequestMethod.GET)
78  	@Cacheable(value="common/annotations/edit")
79  	public ModelAndView editAnnotations(@PathVariable(value = "uuid") UUID uuid) {
80  		ModelAndView modelAndView = new ModelAndView("common/annotations/edit");
81  		List<String> propertyPaths = new ArrayList<String>();
82  		propertyPaths.add("createdBy");
83  		propertyPaths.add("updatedBy");
84  		propertyPaths.add("title");
85  		modelAndView.addObject(GenericController.RESOURCE_KEY,handleLoad(uuid,propertyPaths));
86  		modelAndView.addObject(Annotation.NewDefaultLanguageInstance(""));
87  		return modelAndView;
88  	}	
89  	
90  	@RequestMapping(value = "/annotations", method = RequestMethod.POST)
91  	public ModelAndView postAnnotations(@PathVariable(value = "uuid") UUID uuid,
92  			                 @ModelAttribute("annotation") Annotation annotation, BindingResult result) {
93  		annotationValidator.validate(annotation, result);
94          if (result.hasErrors()) {
95          	ModelAndView modelAndView = new ModelAndView("common/annotations/edit");
96          	List<String> propertyPaths = new ArrayList<String>();
97      		propertyPaths.add("title");
98      		propertyPaths.add("createdBy");
99      		propertyPaths.add("updatedBy");
100     		modelAndView.addObject(GenericController.RESOURCE_KEY,handleLoad(uuid,propertyPaths));
101             return modelAndView;
102         } else {
103         	User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
104             List<String> propertyPaths = new ArrayList<String>();
105             
106             user = userService.load(user.getId(),propertyPaths);
107         	
108         	propertyPaths = new ArrayList<String>();
109         	propertyPaths.add("name");
110         	propertyPaths.add("taxon.name");
111         	propertyPaths.add("taxonName");
112         	propertyPaths.add("updatedBy");
113         	propertyPaths.add("createdBy");
114     		propertyPaths.add("annotations");
115     		
116         	T annotatableEntity = handleLoad(uuid,propertyPaths);
117 			annotation.setAnnotatedObj(annotatableEntity);
118 			annotatableEntity.addAnnotation(annotation);
119 			
120 			service.saveOrUpdate(annotatableEntity);
121 			applicationContext.publishEvent(new CdmObjectAnnotated<T>(this, annotatableEntity,annotation));
122 			List<OrderHint> orderHints = new ArrayList<OrderHint>();
123 			orderHints.add(new OrderHint("created",SortOrder.DESCENDING));
124 			return getAnnotations(uuid,0,20,orderHints);
125         }
126 	}
127 	
128 	@RequestMapping(value = "/annotations", method = RequestMethod.GET)
129 	@Cacheable(value="common/annotations")
130 	public ModelAndView getAnnotations(@PathVariable(value = "uuid") UUID uuid,
131 			                @RequestParam(value = "page", required = false, defaultValue = GenericController.DEFAULT_PAGE) Integer page, 
132 			                @RequestParam(value = "limit", required = false, defaultValue = GenericController.DEFAULT_LIMIT) Integer limit, 
133 			                @RequestParam(value = "sort", required = false, defaultValue = GenericController.DEFAULT_SORT) List<OrderHint> orderHints) {	
134 		ModelAndView modelAndView = new ModelAndView("common/annotations");
135 		List<String> propertyPaths = new ArrayList<String>();
136 		propertyPaths.add("title");
137 		propertyPaths.add("representations");
138 		propertyPaths.add("representations.parts");
139 		propertyPaths.add("createdBy");
140 		propertyPaths.add("updatedBy");
141 		T annotatableEntity = handleLoad(uuid,propertyPaths);
142 		modelAndView.addObject(GenericController.RESOURCE_KEY, annotatableEntity);
143 		modelAndView.addObject(GenericController.ORDER_HINTS_KEY,orderHints);
144 		
145 		propertyPaths = new ArrayList<String>();
146 		propertyPaths.add("annotatedObj");
147 		propertyPaths.add("annotatedObj.title");
148 		propertyPaths.add("annotatedObj.representations");
149 		propertyPaths.add("annotatedObj.representations.parts");
150 		propertyPaths.add("createdBy");	
151 		propertyPaths.add("annotations");
152 		Pager<Annotation> annotations = service.getAnnotations(annotatableEntity, null,limit, page, orderHints,propertyPaths);
153 		
154 		modelAndView.addObject(GenericController.LIST_KEY, annotations);
155 				
156 		return modelAndView;
157 	}
158 	
159 	@RequestMapping(value = "/markers/edit", method = RequestMethod.GET)
160 	@Cacheable(value="common/markers/edit")
161 	public ModelAndView editMarkers(@PathVariable(value = "uuid") UUID uuid) {
162 		ModelAndView modelAndView = new ModelAndView("common/markers/edit");
163 		List<String> propertyPaths = new ArrayList<String>();
164 		propertyPaths.add("createdBy");
165 		propertyPaths.add("updatedBy");
166 		propertyPaths.add("title");
167 		modelAndView.addObject(GenericController.RESOURCE_KEY,handleLoad(uuid,propertyPaths));
168 		modelAndView.addObject(Marker.NewInstance(null, true));
169 		modelAndView.addObject("create",Boolean.FALSE);
170 		return modelAndView;
171 	}	
172 	
173 	@RequestMapping(value = "/markers", method = RequestMethod.POST)
174 	public ModelAndView postMarkers(@PathVariable(value = "uuid") UUID uuid,
175 			                        @ModelAttribute("marker") Marker marker, BindingResult result, @RequestParam(value = "create", required = false, defaultValue = "false") Boolean create) {
176 		
177 		if(create == true && result.getFieldError("markerType") != null) {
178 			FieldError fieldError = result.getFieldError("markerType");
179 			String tag = (String)fieldError.getRejectedValue();
180 			MarkerType markerType = MarkerType.NewInstance(tag, tag,tag);
181 			markerType.setTitleCache(tag,true);
182 			markerType.setVocabulary(MarkerType.TO_BE_CHECKED().getVocabulary());
183 			termService.saveOrUpdate(markerType);
184 			marker.setMarkerType(markerType);
185 		} else if (result.hasErrors()) {
186         	ModelAndView modelAndView = new ModelAndView("common/markers/edit");
187         	List<String> propertyPaths = new ArrayList<String>();
188     		propertyPaths.add("title");
189     		propertyPaths.add("createdBy");
190     		propertyPaths.add("updatedBy");
191     		modelAndView.addObject("create",create);
192     		modelAndView.addObject(GenericController.RESOURCE_KEY,handleLoad(uuid,propertyPaths));
193             return modelAndView;
194         } 
195 
196         List<String> propertyPaths = new ArrayList<String>();
197         propertyPaths = new ArrayList<String>();
198         propertyPaths.add("name");
199     	propertyPaths.add("taxon.name");
200     	propertyPaths.add("taxonName");
201     	propertyPaths.add("markers");
202     	propertyPaths.add("updatedBy");
203     	propertyPaths.add("createdBy");
204     	
205         T annotatableEntity = handleLoad(uuid,propertyPaths);
206 		marker.setMarkedObj(annotatableEntity);
207 		annotatableEntity.addMarker(marker);
208 		//markerService.save(marker);
209 		service.saveOrUpdate(annotatableEntity);
210 		applicationContext.publishEvent(new CdmObjectEvent<T>(this, annotatableEntity));
211 		List<OrderHint> orderHints = new ArrayList<OrderHint>();
212 		orderHints.add(new OrderHint("created",SortOrder.DESCENDING));
213 		return getMarkers(uuid,0,20,orderHints);
214 	}
215 	
216 	@RequestMapping(value = "/markers", method = RequestMethod.GET)
217 	@Cacheable(value="common/markers")
218 	public ModelAndView getMarkers(@PathVariable(value = "uuid") UUID uuid,
219 			                @RequestParam(value = "page", required = false, defaultValue = GenericController.DEFAULT_PAGE) Integer page, 
220 			                @RequestParam(value = "limit", required = false, defaultValue = GenericController.DEFAULT_LIMIT) Integer limit, 
221 			                @RequestParam(value = "sort", required = false, defaultValue = GenericController.DEFAULT_SORT) List<OrderHint> orderHints) {	
222 		ModelAndView modelAndView = new ModelAndView("common/markers");
223 		List<String> propertyPaths = new ArrayList<String>();
224 		propertyPaths.add("title");
225 		propertyPaths.add("createdBy");
226 		propertyPaths.add("updatedBy");
227 		propertyPaths.add("representations");
228 		propertyPaths.add("representations.parts");
229 		T annotatableEntity = handleLoad(uuid,propertyPaths);
230 		modelAndView.addObject(GenericController.RESOURCE_KEY, annotatableEntity);
231 		modelAndView.addObject(GenericController.ORDER_HINTS_KEY,orderHints);
232 		
233 		propertyPaths = new ArrayList<String>();
234 		propertyPaths.add("markedObj");
235 		propertyPaths.add("markedObj.title");
236 		propertyPaths.add("markedObj.representations");
237 		propertyPaths.add("markedObj.representations.parts");
238 		propertyPaths.add("createdBy");	
239 		propertyPaths.add("markers");
240 		propertyPaths.add("markerType");
241 		Pager<Marker> markers = service.getMarkers(annotatableEntity, null,limit, page, orderHints,propertyPaths);
242 		
243 		modelAndView.addObject(GenericController.LIST_KEY, markers);
244 				
245 		return modelAndView;
246 	}
247 }