1
|
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
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.web.bind.annotation.*;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.List;
|
13
|
|
14
|
@RestController
|
15
|
@CrossOrigin(origins = "*")
|
16
|
public class CategoryController {
|
17
|
private final Logger log = Logger.getLogger(this.getClass());
|
18
|
|
19
|
@Autowired
|
20
|
private StakeholderDAO stakeholderDAO;
|
21
|
|
22
|
@Autowired
|
23
|
private TopicDAO topicDAO;
|
24
|
|
25
|
@Autowired
|
26
|
private CategoryDAO categoryDAO;
|
27
|
|
28
|
@Autowired
|
29
|
private SubCategoryDAO subCategoryDAO;
|
30
|
|
31
|
@Autowired
|
32
|
private IndicatorDAO indicatorDAO;
|
33
|
|
34
|
@Autowired
|
35
|
private SubCategoryController subCategoryController;
|
36
|
|
37
|
public Category<SubCategory> buildCategory(Category<SubCategory> categoryFull) {
|
38
|
Category<String> category = new Category<>(categoryFull);
|
39
|
|
40
|
List<String> subCategories = new ArrayList<>();
|
41
|
List<SubCategory> subCategoriesFull = new ArrayList<>();
|
42
|
for(SubCategory<Indicator> subCategory : categoryFull.getSubCategories()) {
|
43
|
SubCategory<Indicator> subcategoryFull = subCategoryController.buildSubCategory(subCategory);
|
44
|
subCategoriesFull.add(subcategoryFull);
|
45
|
subCategories.add(subcategoryFull.getId());
|
46
|
}
|
47
|
categoryFull.setSubCategories(subCategoriesFull);
|
48
|
category.setSubCategories(subCategories);
|
49
|
|
50
|
Category<String> categorySaved = categoryDAO.save(category);
|
51
|
|
52
|
categoryFull.setId(categorySaved.getId());
|
53
|
return categoryFull;
|
54
|
}
|
55
|
|
56
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/save", method = RequestMethod.POST)
|
57
|
public Category<SubCategory> saveCategory(@PathVariable("stakeholderId") String stakeholderId,
|
58
|
@PathVariable("topicId") String topicId,
|
59
|
@RequestBody Category<SubCategory> categoryFull) {
|
60
|
log.debug("save category");
|
61
|
|
62
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
63
|
|
64
|
if(stakeholder != null) {
|
65
|
|
66
|
Topic<String> topic = topicDAO.findById(topicId);
|
67
|
if(topic != null) {
|
68
|
if(stakeholder.getTopics().contains(topicId)) {
|
69
|
// if category not exists (no id), create a new default subcategory, identical to category
|
70
|
if(categoryFull.getId() == null) {
|
71
|
SubCategory<String> subCategory = new SubCategory<>();
|
72
|
subCategory.setName(categoryFull.getName());
|
73
|
subCategory.setAlias(categoryFull.getAlias());
|
74
|
subCategory.setIsActive(categoryFull.getIsActive());
|
75
|
subCategory.setIsPublic(categoryFull.getIsPublic());
|
76
|
subCategory.setIsDefault(categoryFull.getIsDefault());
|
77
|
subCategory.setCharts(new ArrayList<String>());
|
78
|
subCategory.setNumbers(new ArrayList<String>());
|
79
|
|
80
|
SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
|
81
|
List<SubCategory> subCategories = categoryFull.getSubCategories();
|
82
|
subCategories.add(subCategorySaved);
|
83
|
}
|
84
|
|
85
|
|
86
|
Category<String> category = new Category<>(categoryFull);
|
87
|
|
88
|
List<String> subCategories = new ArrayList<>();
|
89
|
for(SubCategory subCategory : categoryFull.getSubCategories()) {
|
90
|
subCategories.add(subCategory.getId());
|
91
|
}
|
92
|
category.setSubCategories(subCategories);
|
93
|
|
94
|
Category<String> categorySaved = categoryDAO.save(category);
|
95
|
|
96
|
List<String> categories = topic.getCategories();
|
97
|
int index = categories.indexOf(categorySaved.getId());
|
98
|
if(index == -1) {
|
99
|
categories.add(categorySaved.getId());
|
100
|
topicDAO.save(topic);
|
101
|
log.debug("Category saved!");
|
102
|
|
103
|
categoryFull.setId(categorySaved.getId());
|
104
|
}
|
105
|
|
106
|
subCategories = null;
|
107
|
category = null;
|
108
|
} else {
|
109
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
110
|
throw new PathNotValidException("Save category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
111
|
}
|
112
|
} else {
|
113
|
// EXCEPTION - Topic not found
|
114
|
throw new EntityNotFoundException("Save category: Topic with id: "+topicId+" not found");
|
115
|
}
|
116
|
} else {
|
117
|
// EXCEPTION - Stakeholder not found
|
118
|
throw new EntityNotFoundException("Save category: Stakeholder with id: "+stakeholderId+" not found");
|
119
|
}
|
120
|
return categoryFull;
|
121
|
}
|
122
|
|
123
|
@RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/delete", method = RequestMethod.DELETE)
|
124
|
public boolean deleteCategory(@PathVariable("stakeholderId") String stakeholderId,
|
125
|
@PathVariable("topicId") String topicId,
|
126
|
@PathVariable("categoryId") String categoryId) {
|
127
|
log.debug("delete category");
|
128
|
|
129
|
Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
|
130
|
|
131
|
if(stakeholder != null) {
|
132
|
|
133
|
Topic<String> topic = topicDAO.findById(topicId);
|
134
|
if(topic != null) {
|
135
|
if(stakeholder.getTopics().contains(topicId)) {
|
136
|
|
137
|
Category<String> category = categoryDAO.findById(categoryId);
|
138
|
if(category != null) {
|
139
|
|
140
|
List<String> categories = topic.getCategories();
|
141
|
int index = categories.indexOf(categoryId);
|
142
|
if(index != -1) {
|
143
|
for(String subCategoryId : category.getSubCategories()) {
|
144
|
SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
|
145
|
if(subcategory == null) {
|
146
|
// EXCEPTION - SubCategory not found
|
147
|
throw new EntityNotFoundException("Delete category: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
|
148
|
}
|
149
|
for(String chartId : subcategory.getCharts()) {
|
150
|
indicatorDAO.delete(chartId);
|
151
|
}
|
152
|
subcategory.setCharts(null);
|
153
|
|
154
|
for(String numberId : subcategory.getNumbers()) {
|
155
|
indicatorDAO.delete(numberId);
|
156
|
}
|
157
|
subcategory.setNumbers(null);
|
158
|
|
159
|
subCategoryDAO.delete(subCategoryId);
|
160
|
}
|
161
|
category.setSubCategories(null);
|
162
|
|
163
|
categories.remove(index);
|
164
|
topicDAO.save(topic);
|
165
|
|
166
|
categoryDAO.delete(categoryId);
|
167
|
log.debug("Category deleted!");
|
168
|
} else {
|
169
|
// EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
|
170
|
throw new PathNotValidException("Delete category: Category with id: "+categoryId+" not found in Topic: "+topicId);
|
171
|
}
|
172
|
|
173
|
} else {
|
174
|
// EXCEPTION - Category not found
|
175
|
throw new EntityNotFoundException("Delete category: Category with id: "+categoryId+" not found");
|
176
|
}
|
177
|
} else {
|
178
|
// EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
|
179
|
throw new PathNotValidException("Delete category: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
|
180
|
}
|
181
|
} else {
|
182
|
// EXCEPTION - Topic not found
|
183
|
throw new EntityNotFoundException("Delete category: Topic with id: "+topicId+" not found");
|
184
|
}
|
185
|
} else {
|
186
|
// EXCEPTION - Stakeholder not found
|
187
|
throw new EntityNotFoundException("Delete category: Stakeholder with id: "+stakeholderId+" not found");
|
188
|
}
|
189
|
return true;
|
190
|
}
|
191
|
}
|