1 |
57671
|
konstantin
|
package eu.dnetlib.uoamonitorservice.controllers;
|
2 |
|
|
|
3 |
|
|
import eu.dnetlib.uoamonitorservice.dao.*;
|
4 |
|
|
import eu.dnetlib.uoamonitorservice.entities.*;
|
5 |
|
|
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
|
6 |
|
|
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
|
7 |
59814
|
konstantin
|
import eu.dnetlib.uoamonitorservice.handlers.utils.RolesUtils;
|
8 |
57671
|
konstantin
|
import org.apache.log4j.Logger;
|
9 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
10 |
59814
|
konstantin
|
import org.springframework.security.access.AccessDeniedException;
|
11 |
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
12 |
57671
|
konstantin
|
import org.springframework.web.bind.annotation.*;
|
13 |
|
|
|
14 |
|
|
import java.util.ArrayList;
|
15 |
59814
|
konstantin
|
import java.util.Date;
|
16 |
58978
|
konstantin
|
import java.util.Iterator;
|
17 |
57671
|
konstantin
|
import java.util.List;
|
18 |
|
|
|
19 |
|
|
@RestController
|
20 |
|
|
@CrossOrigin(origins = "*")
|
21 |
|
|
public class SubCategoryController {
|
22 |
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
23 |
|
|
|
24 |
|
|
@Autowired
|
25 |
59814
|
konstantin
|
private RolesUtils rolesUtils;
|
26 |
|
|
|
27 |
|
|
@Autowired
|
28 |
57671
|
konstantin
|
private StakeholderDAO stakeholderDAO;
|
29 |
|
|
|
30 |
|
|
@Autowired
|
31 |
|
|
private TopicDAO topicDAO;
|
32 |
|
|
|
33 |
|
|
@Autowired
|
34 |
|
|
private CategoryDAO categoryDAO;
|
35 |
|
|
|
36 |
|
|
@Autowired
|
37 |
|
|
private SubCategoryDAO subCategoryDAO;
|
38 |
|
|
|
39 |
|
|
@Autowired
|
40 |
57964
|
konstantin
|
private SectionDAO sectionDAO;
|
41 |
|
|
|
42 |
|
|
@Autowired
|
43 |
57671
|
konstantin
|
private IndicatorDAO indicatorDAO;
|
44 |
|
|
|
45 |
57964
|
konstantin
|
@Autowired
|
46 |
|
|
private SectionController sectionController;
|
47 |
|
|
|
48 |
|
|
public SubCategory<Section<Indicator>> buildSubCategory(SubCategory<Section<Indicator>> subcategoryFull) {
|
49 |
57671
|
konstantin
|
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
|
50 |
|
|
|
51 |
57964
|
konstantin
|
List<String> sectionCharts = new ArrayList<>();
|
52 |
|
|
List<Section<Indicator>> sectionChartsFull = new ArrayList<>();
|
53 |
|
|
|
54 |
|
|
for(Section section : subcategoryFull.getCharts()) {
|
55 |
|
|
Section<Indicator> sectionFull = sectionController.buildSection(section);
|
56 |
|
|
sectionChartsFull.add(sectionFull);
|
57 |
|
|
sectionCharts.add(sectionFull.getId());
|
58 |
57671
|
konstantin
|
}
|
59 |
57964
|
konstantin
|
subcategoryFull.setCharts(sectionChartsFull);
|
60 |
|
|
subCategory.setCharts(sectionCharts);
|
61 |
57671
|
konstantin
|
|
62 |
57964
|
konstantin
|
List<String> sectionNumbers = new ArrayList<>();
|
63 |
|
|
List<Section<Indicator>> sectionNumbersFull = new ArrayList<>();
|
64 |
|
|
|
65 |
|
|
for(Section section : subcategoryFull.getNumbers()) {
|
66 |
|
|
Section<Indicator> sectionFull = sectionController.buildSection(section);
|
67 |
|
|
sectionNumbersFull.add(sectionFull);
|
68 |
|
|
sectionNumbers.add(sectionFull.getId());
|
69 |
57671
|
konstantin
|
}
|
70 |
57964
|
konstantin
|
subcategoryFull.setNumbers(sectionNumbersFull);
|
71 |
|
|
subCategory.setNumbers(sectionNumbers);
|
72 |
57671
|
konstantin
|
|
73 |
59814
|
konstantin
|
Date date = new Date();
|
74 |
|
|
subCategory.setCreationDate(date);
|
75 |
|
|
subCategory.setUpdateDate(date);
|
76 |
|
|
|
77 |
|
|
subcategoryFull.setCreationDate(date);
|
78 |
|
|
subcategoryFull.setUpdateDate(date);
|
79 |
|
|
|
80 |
|
|
|
81 |
57923
|
konstantin
|
subCategoryDAO.save(subCategory);
|
82 |
57671
|
konstantin
|
|
83 |
57923
|
konstantin
|
subcategoryFull.setId(subCategory.getId());
|
84 |
57671
|
konstantin
|
return subcategoryFull;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
59814
|
konstantin
|
@PreAuthorize("isAuthenticated()")
|
88 |
57671
|
konstantin
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
|
89 |
57964
|
konstantin
|
public SubCategory<Section<Indicator>> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
|
90 |
|
|
@PathVariable("topicId") String topicId,
|
91 |
|
|
@PathVariable("categoryId") String categoryId,
|
92 |
|
|
@RequestBody SubCategory<Section<Indicator>> subcategoryFull) {
|
93 |
57671
|
konstantin
|
log.debug("save subcategory");
|
94 |
57923
|
konstantin
|
log.debug("Alias: "+subcategoryFull.getAlias() + " - Id: "+subcategoryFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
|
95 |
57671
|
konstantin
|
|
96 |
57964
|
konstantin
|
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
|
97 |
58708
|
konstantin
|
|
98 |
59814
|
konstantin
|
SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
|
99 |
|
|
|
100 |
|
|
Date date = new Date();
|
101 |
|
|
subCategory.setUpdateDate(date);
|
102 |
|
|
subcategoryFull.setUpdateDate(date);
|
103 |
|
|
|
104 |
58708
|
konstantin
|
SubCategory<String> oldSubcategory = null;
|
105 |
|
|
if(subcategoryFull.getId() != null) {
|
106 |
|
|
oldSubcategory = subCategoryDAO.findById(subcategoryFull.getId());
|
107 |
59814
|
konstantin
|
} else { // subcategory does not exist in DB
|
108 |
|
|
subCategory.setCreationDate(date);
|
109 |
|
|
subcategoryFull.setCreationDate(date);
|
110 |
58708
|
konstantin
|
}
|
111 |
|
|
|
112 |
57964
|
konstantin
|
// List<String> charts = new ArrayList<>();
|
113 |
|
|
// for(Indicator chart : subcategoryFull.getCharts()) {
|
114 |
|
|
// charts.add(chart.getId());
|
115 |
|
|
// }
|
116 |
|
|
// subCategory.setCharts(charts);
|
117 |
|
|
//
|
118 |
|
|
// List<String> numbers = new ArrayList<>();
|
119 |
|
|
// for(Indicator numbr : subcategoryFull.getNumbers()) {
|
120 |
|
|
// numbers.add(numbr.getId());
|
121 |
|
|
// }
|
122 |
|
|
// subCategory.setNumbers(numbers);
|
123 |
57671
|
konstantin
|
|
124 |
57964
|
konstantin
|
List<String> chartSections = new ArrayList<>();
|
125 |
|
|
for(Section chartSection : subcategoryFull.getCharts()) {
|
126 |
|
|
chartSections.add(chartSection.getId());
|
127 |
|
|
}
|
128 |
|
|
subCategory.setCharts(chartSections);
|
129 |
57671
|
konstantin
|
|
130 |
57964
|
konstantin
|
List<String> numberSections = new ArrayList<>();
|
131 |
|
|
for(Section numberSection : subcategoryFull.getNumbers()) {
|
132 |
|
|
numberSections.add(numberSection.getId());
|
133 |
|
|
}
|
134 |
|
|
subCategory.setNumbers(numberSections);
|
135 |
57671
|
konstantin
|
|
136 |
57964
|
konstantin
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
137 |
|
|
if(stakeholder.getDefaultId() == null) {
|
138 |
|
|
if(subcategoryFull.getId() == null) {
|
139 |
59814
|
konstantin
|
subCategoryDAO.save(subCategory);
|
140 |
57964
|
konstantin
|
onSaveDefaultSubCategory(subCategory, categoryId);
|
141 |
|
|
} else {
|
142 |
58708
|
konstantin
|
onUpdateDefaultSubCategory(subCategory, oldSubcategory);
|
143 |
59814
|
konstantin
|
subCategoryDAO.save(subCategory);
|
144 |
57964
|
konstantin
|
}
|
145 |
59814
|
konstantin
|
} else {
|
146 |
|
|
subCategoryDAO.save(subCategory);
|
147 |
57964
|
konstantin
|
}
|
148 |
57671
|
konstantin
|
|
149 |
57964
|
konstantin
|
List<String> subcategories = category.getSubCategories();
|
150 |
|
|
int index = subcategories.indexOf(subCategory.getId());
|
151 |
|
|
if(index == -1) {
|
152 |
|
|
subcategories.add(subCategory.getId());
|
153 |
|
|
categoryDAO.save(category);
|
154 |
|
|
log.debug("Subcategory saved!");
|
155 |
57671
|
konstantin
|
|
156 |
57964
|
konstantin
|
subcategoryFull.setId(subCategory.getId());
|
157 |
|
|
}
|
158 |
57923
|
konstantin
|
|
159 |
57964
|
konstantin
|
chartSections = null;
|
160 |
|
|
numberSections = null;
|
161 |
|
|
subCategory = null;
|
162 |
57671
|
konstantin
|
|
163 |
|
|
return subcategoryFull;
|
164 |
|
|
}
|
165 |
|
|
|
166 |
57923
|
konstantin
|
public void onSaveDefaultSubCategory(SubCategory subCategory, String categoryId) {
|
167 |
|
|
log.debug("On save default subCategory");
|
168 |
|
|
|
169 |
|
|
List<Category> categories = categoryDAO.findByDefaultId(categoryId);
|
170 |
|
|
for(Category category : categories) {
|
171 |
|
|
SubCategory subCategoryNew = new SubCategory();
|
172 |
|
|
subCategoryNew.copyFromDefault(subCategory);
|
173 |
|
|
|
174 |
|
|
subCategoryDAO.save(subCategoryNew);
|
175 |
|
|
|
176 |
|
|
List<String> subCategories = category.getSubCategories();
|
177 |
|
|
subCategories.add(subCategoryNew.getId());
|
178 |
|
|
|
179 |
|
|
categoryDAO.save(category);
|
180 |
|
|
}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
58708
|
konstantin
|
public void onUpdateDefaultSubCategory(SubCategory subCategory, SubCategory oldSubcategory) {
|
184 |
57923
|
konstantin
|
log.debug("On update default subCategory");
|
185 |
|
|
|
186 |
|
|
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(subCategory.getId());
|
187 |
|
|
boolean changed = false;
|
188 |
|
|
for(SubCategory subCategoryBasedOnDefault : subCategories) {
|
189 |
58708
|
konstantin
|
if(subCategory.getName() != null && !subCategory.getName().equals(subCategoryBasedOnDefault.getName())
|
190 |
|
|
&& (oldSubcategory.getName() == null || oldSubcategory.getName().equals(subCategoryBasedOnDefault.getName()))) {
|
191 |
|
|
|
192 |
|
|
subCategoryBasedOnDefault.setName(subCategory.getName());
|
193 |
59483
|
konstantin
|
subCategoryBasedOnDefault.setAlias(subCategory.getAlias());
|
194 |
57923
|
konstantin
|
changed = true;
|
195 |
|
|
}
|
196 |
58708
|
konstantin
|
if(subCategory.getDescription() != null && !subCategory.getDescription().equals(subCategoryBasedOnDefault.getDescription())
|
197 |
|
|
&& (oldSubcategory.getDescription() == null || oldSubcategory.getDescription().equals(subCategoryBasedOnDefault.getDescription()))) {
|
198 |
|
|
|
199 |
|
|
subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
|
200 |
57923
|
konstantin
|
changed = true;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
if(!changed) {
|
204 |
58708
|
konstantin
|
// break;
|
205 |
|
|
continue;
|
206 |
57923
|
konstantin
|
}
|
207 |
58708
|
konstantin
|
|
208 |
|
|
// subCategoryBasedOnDefault.setName(subCategory.getName());
|
209 |
|
|
// subCategoryBasedOnDefault.setDescription(subCategory.getDescription());
|
210 |
59814
|
konstantin
|
subCategoryBasedOnDefault.setUpdateDate(subCategory.getUpdateDate());
|
211 |
57923
|
konstantin
|
subCategoryDAO.save(subCategoryBasedOnDefault);
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
|
215 |
59814
|
konstantin
|
@PreAuthorize("isAuthenticated()")
|
216 |
57671
|
konstantin
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
|
217 |
|
|
public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
|
218 |
|
|
@PathVariable("topicId") String topicId,
|
219 |
|
|
@PathVariable("categoryId") String categoryId,
|
220 |
58978
|
konstantin
|
@PathVariable("subcategoryId") String subcategoryId,
|
221 |
|
|
@RequestParam(required = false) String children) {
|
222 |
57671
|
konstantin
|
log.debug("delete subcategory");
|
223 |
57923
|
konstantin
|
log.debug("Id: "+subcategoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
|
224 |
57671
|
konstantin
|
|
225 |
57964
|
konstantin
|
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
|
226 |
57671
|
konstantin
|
|
227 |
57964
|
konstantin
|
SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
228 |
|
|
if(subcategory != null) {
|
229 |
59814
|
konstantin
|
|
230 |
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
231 |
|
|
List<String> roles = rolesUtils.getRoles();
|
232 |
|
|
if(subcategory.getDefaultId() != null && !rolesUtils.hasCreateAndDeleteAuthority(roles, stakeholder.getType())) {
|
233 |
|
|
// EXCEPTION - Access denied
|
234 |
|
|
throw new AccessDeniedException("Delete subcategory: You are not authorized to delete a default SubCategory in stakeholder with id: "+stakeholderId);
|
235 |
|
|
}
|
236 |
|
|
|
237 |
57964
|
konstantin
|
List<String> subcategories = category.getSubCategories();
|
238 |
|
|
int index = subcategories.indexOf(subcategoryId);
|
239 |
|
|
if(index != -1) {
|
240 |
58978
|
konstantin
|
// this subCategory belongs in default profile
|
241 |
|
|
if(category.getDefaultId() == null && children != null) {
|
242 |
|
|
onDeleteDefaultSubCategory(subcategoryId, categoryId, children);
|
243 |
57964
|
konstantin
|
}
|
244 |
57671
|
konstantin
|
|
245 |
58978
|
konstantin
|
// for(String chartSectionId : subcategory.getCharts()) {
|
246 |
|
|
// Section<String> chartSection = sectionDAO.findById(chartSectionId);
|
247 |
|
|
// if (chartSection == null) {
|
248 |
|
|
// // EXCEPTION - Section not found
|
249 |
|
|
// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
|
250 |
|
|
// }
|
251 |
|
|
//
|
252 |
|
|
// for (String chartId : chartSection.getIndicators()) {
|
253 |
|
|
// indicatorDAO.delete(chartId);
|
254 |
|
|
// }
|
255 |
|
|
// subcategory.setCharts(null);
|
256 |
|
|
// sectionDAO.delete(chartSectionId);
|
257 |
|
|
// }
|
258 |
|
|
//
|
259 |
|
|
// for(String numberSectionId : subcategory.getNumbers()) {
|
260 |
|
|
// Section<String> numberSection = sectionDAO.findById(numberSectionId);
|
261 |
|
|
// if (numberSection == null) {
|
262 |
|
|
// // EXCEPTION - Section not found
|
263 |
|
|
// throw new EntityNotFoundException("Delete SubCategory: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subcategoryId+")");
|
264 |
|
|
// }
|
265 |
|
|
//
|
266 |
|
|
// for (String numberId : numberSection.getIndicators()) {
|
267 |
|
|
// indicatorDAO.delete(numberId);
|
268 |
|
|
// }
|
269 |
|
|
// subcategory.setNumbers(null);
|
270 |
|
|
// sectionDAO.delete(numberSectionId);
|
271 |
|
|
// }
|
272 |
57671
|
konstantin
|
|
273 |
58978
|
konstantin
|
sectionController.deleteTree(subcategory);
|
274 |
57671
|
konstantin
|
|
275 |
58978
|
konstantin
|
subcategory.setCharts(null);
|
276 |
|
|
subcategory.setNumbers(null);
|
277 |
|
|
|
278 |
57964
|
konstantin
|
subcategories.remove(index);
|
279 |
|
|
categoryDAO.save(category);
|
280 |
57671
|
konstantin
|
|
281 |
57964
|
konstantin
|
subCategoryDAO.delete(subcategoryId);
|
282 |
|
|
log.debug("Subcategory deleted!");
|
283 |
57671
|
konstantin
|
} else {
|
284 |
57964
|
konstantin
|
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
285 |
|
|
throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
286 |
57671
|
konstantin
|
}
|
287 |
57964
|
konstantin
|
|
288 |
57671
|
konstantin
|
} else {
|
289 |
57964
|
konstantin
|
// EXCEPTION - SubCategory not found
|
290 |
|
|
throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
|
291 |
57671
|
konstantin
|
}
|
292 |
|
|
return true;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
58978
|
konstantin
|
public boolean onDeleteDefaultSubCategory(String defaultSubCategoryId, String defaultCategoryId, String children) {
|
296 |
|
|
if(children.equals("delete")) {
|
297 |
|
|
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId);
|
298 |
|
|
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
|
299 |
|
|
|
300 |
|
|
for(Category category : categories) {
|
301 |
|
|
Iterator<SubCategory> subCategoriesIterator = subCategories.iterator();
|
302 |
|
|
while(subCategoriesIterator.hasNext()) {
|
303 |
|
|
SubCategory subCategory = subCategoriesIterator.next();
|
304 |
|
|
|
305 |
|
|
String subCategoryId = subCategory.getId();
|
306 |
|
|
|
307 |
|
|
if(category.getSubCategories() != null && category.getSubCategories().contains(subCategoryId)) {
|
308 |
|
|
subCategoriesIterator.remove();
|
309 |
|
|
|
310 |
|
|
category.getSubCategories().remove(subCategoryId);
|
311 |
|
|
categoryDAO.save(category);
|
312 |
|
|
|
313 |
|
|
sectionController.deleteTree(subCategory);
|
314 |
|
|
|
315 |
|
|
subCategoryDAO.delete(subCategoryId);
|
316 |
|
|
log.debug("SubCategory with id: "+subCategoryId+" deleted!");
|
317 |
|
|
|
318 |
|
|
break;
|
319 |
|
|
}
|
320 |
|
|
}
|
321 |
|
|
}
|
322 |
|
|
} else if(children.equals("disconnect")) {
|
323 |
|
|
List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubCategoryId);
|
324 |
|
|
for(SubCategory subCategory : subCategories) {
|
325 |
|
|
sectionController.disConnectTree(subCategory);
|
326 |
|
|
|
327 |
|
|
subCategory.setDefaultId(null);
|
328 |
|
|
subCategoryDAO.save(subCategory);
|
329 |
|
|
|
330 |
|
|
log.debug("DefaultId for SubCategory with id: "+subCategory.getId()+" empty!");
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
return true;
|
334 |
|
|
}
|
335 |
|
|
|
336 |
59814
|
konstantin
|
@PreAuthorize("isAuthenticated()")
|
337 |
58992
|
konstantin
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/reorder", method = RequestMethod.POST)
|
338 |
|
|
public List<SubCategory> reorderSubCategories(@PathVariable("stakeholderId") String stakeholderId,
|
339 |
|
|
@PathVariable("topicId") String topicId,
|
340 |
|
|
@PathVariable("categoryId") String categoryId,
|
341 |
|
|
@RequestBody List<String> subCategories) {
|
342 |
|
|
log.debug("reorder subCategories");
|
343 |
|
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId);
|
344 |
|
|
|
345 |
|
|
Category<String> category = checkForExceptions(stakeholderId, topicId, categoryId);
|
346 |
|
|
|
347 |
|
|
category.setSubCategories(subCategories);
|
348 |
|
|
|
349 |
|
|
categoryDAO.save(category);
|
350 |
|
|
log.debug("SubCategories reordered!");
|
351 |
|
|
|
352 |
|
|
List<SubCategory> subCategoriesFull = new ArrayList<>();
|
353 |
|
|
for(String subCategoryId : subCategories) {
|
354 |
|
|
subCategoriesFull.add(subCategoryDAO.findById(subCategoryId));
|
355 |
|
|
}
|
356 |
|
|
return subCategoriesFull;
|
357 |
|
|
}
|
358 |
|
|
|
359 |
59814
|
konstantin
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-status", method = RequestMethod.POST)
|
360 |
|
|
// public Boolean toggleSubCategoryStatus(@PathVariable("stakeholderId") String stakeholderId,
|
361 |
|
|
// @PathVariable("topicId") String topicId,
|
362 |
|
|
// @PathVariable("categoryId") String categoryId,
|
363 |
|
|
// @PathVariable("subcategoryId") String subcategoryId) {
|
364 |
|
|
// log.debug("toggle subCategory status (isActive)");
|
365 |
|
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
366 |
|
|
//
|
367 |
|
|
// SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
|
368 |
|
|
// if (subCategory == null) {
|
369 |
|
|
// // EXCEPTION - SubCategory not found
|
370 |
|
|
// throw new EntityNotFoundException("Toggle subCategory status: SubCategory with id: "+subcategoryId+" not found");
|
371 |
|
|
// }
|
372 |
|
|
// subCategory.setIsActive(!subCategory.getIsActive());
|
373 |
|
|
//
|
374 |
|
|
// this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
|
375 |
|
|
//
|
376 |
|
|
// return subCategory.getIsActive();
|
377 |
|
|
// }
|
378 |
|
|
//
|
379 |
|
|
// @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/toggle-access", method = RequestMethod.POST)
|
380 |
|
|
// public Boolean toggleSubCategoryAccess(@PathVariable("stakeholderId") String stakeholderId,
|
381 |
|
|
// @PathVariable("topicId") String topicId,
|
382 |
|
|
// @PathVariable("categoryId") String categoryId,
|
383 |
|
|
// @PathVariable("subcategoryId") String subcategoryId) {
|
384 |
|
|
// log.debug("toggle subCategory access (isPublic)");
|
385 |
|
|
// log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
386 |
|
|
//
|
387 |
|
|
// SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
|
388 |
|
|
// if (subCategory == null) {
|
389 |
|
|
// // EXCEPTION - SubCategory not found
|
390 |
|
|
// throw new EntityNotFoundException("Toggle subCategory access: SubCategory with id: "+subcategoryId+" not found");
|
391 |
|
|
// }
|
392 |
|
|
// subCategory.setIsPublic(!subCategory.getIsPublic());
|
393 |
|
|
//
|
394 |
|
|
// this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
|
395 |
|
|
//
|
396 |
|
|
// return subCategory.getIsPublic();
|
397 |
|
|
// }
|
398 |
57934
|
konstantin
|
|
399 |
59814
|
konstantin
|
@PreAuthorize("isAuthenticated()")
|
400 |
|
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/change-visibility", method = RequestMethod.POST)
|
401 |
|
|
public Visibility changeTopicVisibility(@PathVariable("stakeholderId") String stakeholderId,
|
402 |
|
|
@PathVariable("topicId") String topicId,
|
403 |
|
|
@PathVariable("categoryId") String categoryId,
|
404 |
|
|
@PathVariable("subcategoryId") String subcategoryId,
|
405 |
|
|
@RequestParam("visibility") Visibility visibility) {
|
406 |
|
|
log.debug("change subCategory visibility: "+visibility);
|
407 |
57934
|
konstantin
|
log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
|
408 |
|
|
|
409 |
|
|
SubCategory subCategory = subCategoryDAO.findById(subcategoryId);
|
410 |
|
|
if (subCategory == null) {
|
411 |
|
|
// EXCEPTION - SubCategory not found
|
412 |
59814
|
konstantin
|
throw new EntityNotFoundException("Change subCategory visibility: SubCategory with id: "+subcategoryId+" not found");
|
413 |
57934
|
konstantin
|
}
|
414 |
59814
|
konstantin
|
subCategory.setVisibility(visibility);
|
415 |
57934
|
konstantin
|
|
416 |
|
|
this.toggleSubCategory(stakeholderId, topicId, categoryId, subCategory);
|
417 |
|
|
|
418 |
59814
|
konstantin
|
return subCategory.getVisibility();
|
419 |
57934
|
konstantin
|
}
|
420 |
|
|
|
421 |
|
|
public void toggleSubCategory(String stakeholderId, String topicId, String categoryId, SubCategory subcategory) {
|
422 |
57964
|
konstantin
|
Category category = checkForExceptions(stakeholderId, topicId, categoryId);
|
423 |
57934
|
konstantin
|
|
424 |
57964
|
konstantin
|
if (category.getSubCategories().contains(subcategory.getId())) {
|
425 |
|
|
subCategoryDAO.save(subcategory);
|
426 |
|
|
log.debug("SubCategory toggled!");
|
427 |
|
|
} else {
|
428 |
|
|
// EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
429 |
|
|
throw new PathNotValidException("Toggle subCategory: SubCategory with id: "+subcategory.getId()+" not found in Category: "+categoryId);
|
430 |
|
|
}
|
431 |
|
|
}
|
432 |
57934
|
konstantin
|
|
433 |
|
|
|
434 |
57964
|
konstantin
|
private Category checkForExceptions(String stakeholderId, String topicId, String categoryId) {
|
435 |
|
|
|
436 |
|
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
437 |
|
|
|
438 |
|
|
if(stakeholder == null) {
|
439 |
57934
|
konstantin
|
// EXCEPTION - Stakeholder not found
|
440 |
57964
|
konstantin
|
throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
|
441 |
57934
|
konstantin
|
}
|
442 |
57964
|
konstantin
|
|
443 |
59814
|
konstantin
|
List<String> roles = rolesUtils.getRoles();
|
444 |
|
|
if(!rolesUtils.hasUpdateAuthority(roles, stakeholder.getType(), stakeholder.getAlias())) {
|
445 |
|
|
// EXCEPTION - Access denied
|
446 |
|
|
throw new AccessDeniedException("CheckForExceptions SubCategory: You are not authorized to update stakeholder with id: "+stakeholderId);
|
447 |
|
|
}
|
448 |
|
|
|
449 |
57964
|
konstantin
|
Topic<String> topic = topicDAO.findById(topicId);
|
450 |
|
|
if(topic == null) {
|
451 |
|
|
// EXCEPTION - Topic not found
|
452 |
|
|
throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
if(!stakeholder.getTopics().contains(topicId)) {
|
456 |
|
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
457 |
|
|
throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
Category<String> category = categoryDAO.findById(categoryId);
|
461 |
|
|
if(category == null) {
|
462 |
|
|
// EXCEPTION - Category not found
|
463 |
|
|
throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
|
464 |
|
|
}
|
465 |
|
|
|
466 |
|
|
if(!topic.getCategories().contains(categoryId)) {
|
467 |
|
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
468 |
|
|
throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
469 |
|
|
}
|
470 |
|
|
|
471 |
|
|
// SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
|
472 |
|
|
// if(subcategory == null) {
|
473 |
|
|
// // EXCEPTION - SubCategory not found
|
474 |
|
|
// throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
|
475 |
|
|
// }
|
476 |
|
|
//
|
477 |
|
|
// if (!category.getSubCategories().contains(subcategoryId)) {
|
478 |
|
|
// // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
|
479 |
|
|
// throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
|
480 |
|
|
// }
|
481 |
|
|
|
482 |
|
|
return category;
|
483 |
57934
|
konstantin
|
}
|
484 |
|
|
|
485 |
58978
|
konstantin
|
public void deleteTree(Category category) {
|
486 |
|
|
List<String> subCategories = category.getSubCategories();
|
487 |
|
|
for(String subCategoryId : subCategories) {
|
488 |
|
|
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
|
489 |
|
|
if (subCategory == null) {
|
490 |
|
|
// EXCEPTION - SubCategory not found
|
491 |
|
|
throw new EntityNotFoundException("SubCategory delete tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
|
492 |
|
|
}
|
493 |
|
|
|
494 |
|
|
sectionController.deleteTree(subCategory);
|
495 |
|
|
|
496 |
|
|
subCategoryDAO.delete(subCategoryId);
|
497 |
|
|
}
|
498 |
|
|
}
|
499 |
|
|
|
500 |
|
|
public void disConnectTree(Category category) {
|
501 |
|
|
List<String> subCategories = category.getSubCategories();
|
502 |
|
|
for(String subCategoryId : subCategories) {
|
503 |
|
|
SubCategory subCategory = subCategoryDAO.findById(subCategoryId);
|
504 |
|
|
if (subCategory == null) {
|
505 |
|
|
// EXCEPTION - SubCategory not found
|
506 |
|
|
throw new EntityNotFoundException("SubCategory disconnect tree: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+category.getId()+")");
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
sectionController.disConnectTree(subCategory);
|
510 |
|
|
|
511 |
|
|
subCategory.setDefaultId(null);
|
512 |
|
|
subCategoryDAO.save(subCategory);
|
513 |
|
|
}
|
514 |
|
|
}
|
515 |
57671
|
konstantin
|
}
|