Revision 58978
Added by Konstantina Galouni almost 4 years ago
CategoryController.java | ||
---|---|---|
9 | 9 |
import org.springframework.web.bind.annotation.*; |
10 | 10 |
|
11 | 11 |
import java.util.ArrayList; |
12 |
import java.util.Iterator; |
|
12 | 13 |
import java.util.List; |
13 | 14 |
|
14 | 15 |
@RestController |
... | ... | |
183 | 184 |
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE) |
184 | 185 |
public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId, |
185 | 186 |
@PathVariable("topicId") String topicId, |
186 |
@PathVariable("categoryId") String categoryId) { |
|
187 |
@PathVariable("categoryId") String categoryId, |
|
188 |
@RequestParam(required = false) String children) { |
|
187 | 189 |
log.debug("delete category"); |
188 | 190 |
log.debug("Id: "+categoryId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId); |
189 | 191 |
|
... | ... | |
201 | 203 |
List<String> categories = topic.getCategories(); |
202 | 204 |
int index = categories.indexOf(categoryId); |
203 | 205 |
if(index != -1) { |
204 |
for(String subCategoryId : category.getSubCategories()) { |
|
205 |
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId); |
|
206 |
if(subcategory == null) { |
|
207 |
// EXCEPTION - SubCategory not found |
|
208 |
throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); |
|
209 |
} |
|
206 |
// this category belongs in default profile |
|
207 |
if(topic.getDefaultId() == null && children != null) { |
|
208 |
onDeleteDefaultCategory(categoryId, topicId, children); |
|
209 |
} |
|
210 | 210 |
|
211 |
for(String chartSectionId : subcategory.getCharts()) { |
|
212 |
Section<String> chartSection = sectionDAO.findById(chartSectionId); |
|
213 |
if (chartSection == null) { |
|
214 |
// EXCEPTION - Section not found |
|
215 |
throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); |
|
216 |
} |
|
211 |
// for(String subCategoryId : category.getSubCategories()) { |
|
212 |
// SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId); |
|
213 |
// if(subcategory == null) { |
|
214 |
// // EXCEPTION - SubCategory not found |
|
215 |
// throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")"); |
|
216 |
// } |
|
217 |
// |
|
218 |
// for(String chartSectionId : subcategory.getCharts()) { |
|
219 |
// Section<String> chartSection = sectionDAO.findById(chartSectionId); |
|
220 |
// if (chartSection == null) { |
|
221 |
// // EXCEPTION - Section not found |
|
222 |
// throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); |
|
223 |
// } |
|
224 |
// |
|
225 |
// for (String chartId : chartSection.getIndicators()) { |
|
226 |
// indicatorDAO.delete(chartId); |
|
227 |
// } |
|
228 |
// subcategory.setCharts(null); |
|
229 |
// sectionDAO.delete(chartSectionId); |
|
230 |
// } |
|
231 |
// |
|
232 |
// for(String numberSectionId : subcategory.getNumbers()) { |
|
233 |
// Section<String> numberSection = sectionDAO.findById(numberSectionId); |
|
234 |
// if (numberSection == null) { |
|
235 |
// // EXCEPTION - Section not found |
|
236 |
// throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); |
|
237 |
// } |
|
238 |
// |
|
239 |
// for (String numberId : numberSection.getIndicators()) { |
|
240 |
// indicatorDAO.delete(numberId); |
|
241 |
// } |
|
242 |
// subcategory.setNumbers(null); |
|
243 |
// sectionDAO.delete(numberSectionId); |
|
244 |
// } |
|
245 |
// |
|
246 |
// subCategoryDAO.delete(subCategoryId); |
|
247 |
// } |
|
248 |
subCategoryController.deleteTree(category); |
|
217 | 249 |
|
218 |
for (String chartId : chartSection.getIndicators()) { |
|
219 |
indicatorDAO.delete(chartId); |
|
220 |
} |
|
221 |
subcategory.setCharts(null); |
|
222 |
sectionDAO.delete(chartSectionId); |
|
223 |
} |
|
224 |
|
|
225 |
for(String numberSectionId : subcategory.getNumbers()) { |
|
226 |
Section<String> numberSection = sectionDAO.findById(numberSectionId); |
|
227 |
if (numberSection == null) { |
|
228 |
// EXCEPTION - Section not found |
|
229 |
throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")"); |
|
230 |
} |
|
231 |
|
|
232 |
for (String numberId : numberSection.getIndicators()) { |
|
233 |
indicatorDAO.delete(numberId); |
|
234 |
} |
|
235 |
subcategory.setNumbers(null); |
|
236 |
sectionDAO.delete(numberSectionId); |
|
237 |
} |
|
238 |
|
|
239 |
subCategoryDAO.delete(subCategoryId); |
|
240 |
} |
|
241 | 250 |
category.setSubCategories(null); |
242 | 251 |
|
243 | 252 |
categories.remove(index); |
... | ... | |
269 | 278 |
return true; |
270 | 279 |
} |
271 | 280 |
|
281 |
|
|
282 |
public boolean onDeleteDefaultCategory(String defaultCategoryId, String defaultTopicId, String children) { |
|
283 |
if(children.equals("delete")) { |
|
284 |
List<Topic> topics = topicDAO.findByDefaultId(defaultTopicId); |
|
285 |
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId); |
|
286 |
|
|
287 |
for(Topic topic : topics) { |
|
288 |
Iterator<Category> categoriesIterator = categories.iterator(); |
|
289 |
while(categoriesIterator.hasNext()) { |
|
290 |
Category category = categoriesIterator.next(); |
|
291 |
|
|
292 |
String categoryId = category.getId(); |
|
293 |
|
|
294 |
if(topic.getCategories() != null && topic.getCategories().contains(categoryId)) { |
|
295 |
categoriesIterator.remove(); |
|
296 |
|
|
297 |
topic.getCategories().remove(categoryId); |
|
298 |
topicDAO.save(topic); |
|
299 |
|
|
300 |
subCategoryController.deleteTree(category); |
|
301 |
|
|
302 |
categoryDAO.delete(categoryId); |
|
303 |
log.debug("Category with id: "+categoryId+" deleted!"); |
|
304 |
|
|
305 |
break; |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
} else if(children.equals("disconnect")) { |
|
310 |
List<Category> categories = categoryDAO.findByDefaultId(defaultCategoryId); |
|
311 |
for(Category category : categories) { |
|
312 |
subCategoryController.disConnectTree(category); |
|
313 |
|
|
314 |
category.setDefaultId(null); |
|
315 |
categoryDAO.save(category); |
|
316 |
|
|
317 |
log.debug("DefaultId for Category with id: "+category.getId()+" empty!"); |
|
318 |
} |
|
319 |
} |
|
320 |
return true; |
|
321 |
} |
|
322 |
|
|
272 | 323 |
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/toggle-status", method = RequestMethod.POST) |
273 | 324 |
public Boolean toggleCategoryStatus(@PathVariable("stakeholderId") String stakeholderId, |
274 | 325 |
@PathVariable("topicId") String topicId, |
... | ... | |
335 | 386 |
throw new EntityNotFoundException("Toggle category: Stakeholder with id: "+stakeholderId+" not found"); |
336 | 387 |
} |
337 | 388 |
} |
389 |
|
|
390 |
public void deleteTree(Topic topic) { |
|
391 |
List<String> categories = topic.getCategories(); |
|
392 |
for(String categoryId : categories) { |
|
393 |
Category category = categoryDAO.findById(categoryId); |
|
394 |
if (category == null) { |
|
395 |
// EXCEPTION - Category not found |
|
396 |
throw new EntityNotFoundException("Category delete tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); |
|
397 |
} |
|
398 |
|
|
399 |
subCategoryController.deleteTree(category); |
|
400 |
|
|
401 |
categoryDAO.delete(categoryId); |
|
402 |
} |
|
403 |
} |
|
404 |
|
|
405 |
public void disConnectTree(Topic topic) { |
|
406 |
List<String> categories = topic.getCategories(); |
|
407 |
for(String categoryId : categories) { |
|
408 |
Category category = categoryDAO.findById(categoryId); |
|
409 |
if (category == null) { |
|
410 |
// EXCEPTION - Category not found |
|
411 |
throw new EntityNotFoundException("Category disconnect tree: Category with id: "+categoryId+" not found (category exists in topic: "+topic.getId()+")"); |
|
412 |
} |
|
413 |
|
|
414 |
subCategoryController.disConnectTree(category); |
|
415 |
|
|
416 |
category.setDefaultId(null); |
|
417 |
categoryDAO.save(category); |
|
418 |
} |
|
419 |
} |
|
338 | 420 |
} |
Also available in: Unified diff
[Trunk | Monitor Service]:
1. Adding parameter in delete methods to delete or disconnect all profiles based on the default one that is being deleted (not for stakeholder).
2. In delete methods remove iterations (for deleting inner elements) and call "deleteTree()" method.