Project

General

Profile

1 57503 konstantin
package eu.dnetlib.uoamonitorservice.controllers;
2
3 57671 konstantin
//import com.fasterxml.jackson.core.type.TypeReference;
4
//import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.uoamonitorservice.dao.*;
6 57578 konstantin
import eu.dnetlib.uoamonitorservice.entities.*;
7 57671 konstantin
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
8 57934 konstantin
import eu.dnetlib.uoamonitorservice.handlers.PathNotValidException;
9 57503 konstantin
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.web.bind.annotation.*;
12
13
import java.util.ArrayList;
14
import java.util.Date;
15
import java.util.List;
16
17
@RestController
18
@CrossOrigin(origins = "*")
19
public class StakeholderController {
20
    private final Logger log = Logger.getLogger(this.getClass());
21
22
    @Autowired
23
    private StakeholderDAO stakeholderDAO;
24
25 57578 konstantin
    @Autowired
26 57671 konstantin
    private TopicDAO topicDAO;
27
28
    @Autowired
29
    private CategoryDAO categoryDAO;
30
31
    @Autowired
32
    private SubCategoryDAO subCategoryDAO;
33
34
    @Autowired
35 57964 konstantin
    private SectionDAO sectionDAO;
36
37
    @Autowired
38 57578 konstantin
    private IndicatorDAO indicatorDAO;
39
40 57671 konstantin
    @Autowired
41
    private TopicController topicController;
42
43
    @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
44 57964 konstantin
    public Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Section<Indicator>>>>> stakeholderFull) {
45 57671 konstantin
        log.debug("build stakeholder");
46 57923 konstantin
        log.debug("Alias: "+stakeholderFull.getAlias());
47 57671 konstantin
48
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
49
50
        List<String> topics = new ArrayList<>();
51 57964 konstantin
        List<Topic<Category<SubCategory<Section<Indicator>>>>> topicsFull = new ArrayList<>();
52 57671 konstantin
        for(Topic topic : stakeholderFull.getTopics()) {
53 57964 konstantin
            Topic<Category<SubCategory<Section<Indicator>>>> topicFull = topicController.buildTopic(topic);
54 57671 konstantin
            topicsFull.add(topicFull);
55
            topics.add(topicFull.getId());
56
        }
57
        stakeholderFull.setTopics(topicsFull);
58
        stakeholder.setTopics(topics);
59
60 57923 konstantin
        Date date = new Date();
61
        stakeholder.setCreationDate(date);
62
        stakeholder.setUpdateDate(date);
63
64
        stakeholderFull.setCreationDate(date);
65
        stakeholderFull.setUpdateDate(date);
66
67 57671 konstantin
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
68
        stakeholderFull.setId(stakeholderSaved.getId());
69
        return stakeholderFull;
70
        //return null;
71
    }
72
73
    public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
74
        Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
75
76
        List<Topic> topics = new ArrayList<>();
77
78
        for (String topicId: (List<String>)stakeholder.getTopics()) {
79
            Topic<String> topic = topicDAO.findById(topicId);
80 57923 konstantin
            if(topic == null) {
81
                // EXCEPTION - Topic not found
82
                throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
83
            }
84 57671 konstantin
            Topic<Category> topicFull = new Topic<Category>(topic);
85
86
            List<Category> categories = new ArrayList<>();
87
88
            for(String categoryId : topic.getCategories()) {
89
                Category<String> category = categoryDAO.findById(categoryId);
90 57923 konstantin
                if(category == null) {
91
                    // EXCEPTION - Category not found
92
                    throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
93
                }
94 57671 konstantin
                Category<SubCategory> categoryFull = new Category<SubCategory>(category);
95
96 57578 konstantin
                List<SubCategory> subCategories = new ArrayList<>();
97
98 57671 konstantin
                for(String subCategoryId : category.getSubCategories()) {
99
                    SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
100 57923 konstantin
                    if(subCategory == null) {
101
                        // EXCEPTION - SubCategory not found
102
                        throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
103
                    }
104 57964 konstantin
                    SubCategory subCategoryFull = new SubCategory<Section<Indicator>>(subCategory);
105 57578 konstantin
106 57964 konstantin
                    List<Section> sectionsCharts = new ArrayList<>();
107
108
                    for(String sectionId : subCategory.getCharts()) {
109
                        sectionsCharts.add(getSectionFull(sectionId, subCategoryId));
110 57578 konstantin
                    }
111 57964 konstantin
                    subCategoryFull.setCharts(sectionsCharts);
112 57578 konstantin
113 57964 konstantin
                    List<Section> sectionsNumbers = new ArrayList<>();
114
115
                    for(String sectionId : subCategory.getNumbers()) {
116
                        sectionsNumbers.add(getSectionFull(sectionId, subCategoryId));
117 57578 konstantin
                    }
118 57964 konstantin
                    subCategoryFull.setNumbers(sectionsNumbers);
119 57578 konstantin
120 57964 konstantin
//                    List<Indicator> charts = new ArrayList<>();
121
//                    for(String indicatorId : subCategory.getCharts()) {
122
//                        Indicator indicator = indicatorDAO.findById(indicatorId);
123
//                        if(indicator == null) {
124
//                            // EXCEPTION - Indicator not found
125
//                            throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
126
//                        }
127
//                        charts.add(indicator);
128
//                    }
129
//                    subCategoryFull.setCharts(charts);
130
//
131
//                    List<Indicator> numbers = new ArrayList<>();
132
//                    for (String indicatorId : subCategory.getNumbers()) {
133
//                        Indicator indicator = indicatorDAO.findById(indicatorId);
134
//                        if (indicator == null) {
135
//                            // EXCEPTION - Indicator not found
136
//                            throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in subCategory: " + subCategoryId + ")");
137
//                        }
138
//                        numbers.add(indicator);
139
//                    }
140
//                    subCategoryFull.setNumbers(numbers);
141
142 57578 konstantin
                    subCategories.add(subCategoryFull);
143
                }
144
145 57671 konstantin
                categoryFull.setSubCategories(subCategories);
146
                categories.add(categoryFull);
147 57578 konstantin
            }
148
149 57671 konstantin
            topicFull.setCategories(categories);
150
            topics.add(topicFull);
151 57578 konstantin
        }
152 57671 konstantin
153
        stakeholderFull.setTopics(topics);
154
        return stakeholderFull;
155 57578 konstantin
    }
156
157 57964 konstantin
    private Section getSectionFull(String sectionId, String subCategoryId) {
158
        Section<String> section = sectionDAO.findById(sectionId);
159
        if (section == null) {
160
            // EXCEPTION - Section not found
161
            throw new EntityNotFoundException("Get stakeholder: Section with id: " + sectionId + " not found (section exists in subCategory: " + subCategoryId + ")");
162
        }
163
        Section sectionFull = new Section<Indicator>(section);
164
165
        List<Indicator> indicators = new ArrayList<>();
166
        for (String indicatorId : section.getIndicators()) {
167
            Indicator indicator = indicatorDAO.findById(indicatorId);
168
            if (indicator == null) {
169
                // EXCEPTION - Indicator not found
170
                throw new EntityNotFoundException("Get stakeholder: Indicator with id: " + indicatorId + " not found (indicator exists in section: " + sectionId + ")");
171
            }
172
            indicators.add(indicator);
173
        }
174
        sectionFull.setIndicators(indicators);
175
176
        return sectionFull;
177
    }
178
179 57503 konstantin
    @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
180
    public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
181 57923 konstantin
        log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
182
183 57503 konstantin
        List<Stakeholder> stakeholders;
184
        if(type == null) {
185
            stakeholders = stakeholderDAO.findAll();
186
        } else {
187
            stakeholders = stakeholderDAO.findByType(type);
188
        }
189 57578 konstantin
190 57671 konstantin
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
191 57578 konstantin
        for(Stakeholder stakeholder : stakeholders) {
192 57671 konstantin
            stakeholdersFull.add(this.setFullEntities(stakeholder));
193 57578 konstantin
        }
194
195 57671 konstantin
        return stakeholdersFull;
196 57503 konstantin
    }
197
198
    @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
199
    public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
200 57923 konstantin
        log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
201
202 57503 konstantin
        List<Stakeholder> stakeholders;
203
        if(type == null) {
204 57923 konstantin
            stakeholders = stakeholderDAO.findByDefaultId(null);
205 57503 konstantin
        } else {
206 57923 konstantin
            stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
207 57503 konstantin
        }
208 57578 konstantin
209 57671 konstantin
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
210 57578 konstantin
        for(Stakeholder stakeholder : stakeholders) {
211 57671 konstantin
            stakeholdersFull.add(this.setFullEntities(stakeholder));
212 57578 konstantin
        }
213 57671 konstantin
        return stakeholdersFull;
214 57503 konstantin
    }
215
216
    @RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
217
    public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
218 57923 konstantin
        log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
219
220 57503 konstantin
        List<Stakeholder> stakeholders;
221
        if(type == null) {
222 57923 konstantin
            stakeholders = stakeholderDAO.findByDefaultIdNot(null);
223 57503 konstantin
        } else {
224 57923 konstantin
            stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
225 57503 konstantin
        }
226 57578 konstantin
227 57671 konstantin
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
228 57578 konstantin
        for(Stakeholder stakeholder : stakeholders) {
229 57671 konstantin
            stakeholdersFull.add(this.setFullEntities(stakeholder));
230 57578 konstantin
        }
231 57503 konstantin
        log.debug(new Date());
232
233 57671 konstantin
        return stakeholdersFull;
234 57503 konstantin
    }
235
236 57578 konstantin
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
237
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
238 57923 konstantin
        log.debug("get stakeholder: "+alias);
239
240 57671 konstantin
        Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
241
        if(stakeholder == null) {
242
            // EXCEPTION - Stakeholder not found
243
            throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
244
        }
245
        return this.setFullEntities(stakeholder);
246 57578 konstantin
    }
247
248 57671 konstantin
    @RequestMapping(value = "/save", method = RequestMethod.POST)
249
    public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
250 57578 konstantin
        log.debug("save stakeholder");
251 57923 konstantin
        log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId());
252 57578 konstantin
253 57671 konstantin
//        if(stakeholderFull == null) {
254
//            log.debug("stakeholder null");
255
//            // EXCEPTION - Parameter for Stakeholder is not accepted
256
//        }
257 57578 konstantin
258 57671 konstantin
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
259 57578 konstantin
260 57923 konstantin
        Date date = new Date();
261
        stakeholder.setUpdateDate(date);
262
263
        // stakeholder does not exist in DB
264
        if(stakeholderFull.getId() == null) {
265
            stakeholder.setCreationDate(date);
266
        }
267
268 57671 konstantin
        List<String> topics = new ArrayList<>();
269
        for(Topic topic : stakeholderFull.getTopics()) {
270
            topics.add(topic.getId());
271 57578 konstantin
        }
272 57671 konstantin
        stakeholder.setTopics(topics);
273 57578 konstantin
274 57671 konstantin
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
275 57578 konstantin
        stakeholderFull.setId(stakeholderSaved.getId());
276 57923 konstantin
        stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate());
277
        stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate());
278 57578 konstantin
279 57671 konstantin
        topics = null;
280
        stakeholder = null;
281
        stakeholderSaved = null;
282 57637 konstantin
283 57671 konstantin
        return stakeholderFull;
284 57637 konstantin
    }
285
286
287 57671 konstantin
    @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
288
    public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
289
        log.debug("delete stakeholder");
290 57923 konstantin
        log.debug("Id: "+stakeholderId);
291 57637 konstantin
292 57671 konstantin
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
293 57637 konstantin
294 57671 konstantin
        if(stakeholder != null) {
295 57637 konstantin
296 57671 konstantin
            for(String topicId : stakeholder.getTopics()) {
297
                Topic<String> topic = topicDAO.findById(topicId);
298
                if (topic == null) {
299
                    // EXCEPTION - Topic not found
300
                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
301 57637 konstantin
                }
302
303 57671 konstantin
                for (String categoryId : topic.getCategories()) {
304
                    Category<String> category = categoryDAO.findById(categoryId);
305
                    if (category == null) {
306
                        // EXCEPTION - Category not found
307
                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
308 57637 konstantin
                    }
309
310 57671 konstantin
                    for (String subCategoryId : category.getSubCategories()) {
311
                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
312
                        if (subcategory == null) {
313
                            // EXCEPTION - SubCategory not found
314
                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
315 57637 konstantin
                        }
316
317 57964 konstantin
                        for(String chartSectionId : subcategory.getCharts()) {
318
                            Section<String> chartSection = sectionDAO.findById(chartSectionId);
319
                            if (chartSection == null) {
320
                                // EXCEPTION - Section not found
321
                                throw new EntityNotFoundException("Delete topic: Section with id: "+chartSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
322
                            }
323
324
                            for (String chartId : chartSection.getIndicators()) {
325
                                indicatorDAO.delete(chartId);
326
                            }
327
                            subcategory.setCharts(null);
328
                            sectionDAO.delete(chartSectionId);
329 57578 konstantin
                        }
330
331 57964 konstantin
                        for(String numberSectionId : subcategory.getNumbers()) {
332
                            Section<String> numberSection = sectionDAO.findById(numberSectionId);
333
                            if (numberSection == null) {
334
                                // EXCEPTION - Section not found
335
                                throw new EntityNotFoundException("Delete topic: Section with id: "+numberSectionId+" not found (section exists in subcategory: "+subCategoryId+")");
336
                            }
337
338
                            for (String numberId : numberSection.getIndicators()) {
339
                                indicatorDAO.delete(numberId);
340
                            }
341
                            subcategory.setNumbers(null);
342
                            sectionDAO.delete(numberSectionId);
343 57578 konstantin
                        }
344
345 57671 konstantin
                        subCategoryDAO.delete(subCategoryId);
346 57578 konstantin
                    }
347 57671 konstantin
                    category.setSubCategories(null);
348
                    categoryDAO.delete(categoryId);
349 57578 konstantin
                }
350 57671 konstantin
                topic.setCategories(null);
351
                topicDAO.delete(topicId);
352 57578 konstantin
            }
353 57671 konstantin
            stakeholder.setTopics(null);
354
            stakeholderDAO.delete(stakeholderId);
355
            log.debug("Stakeholder deleted!");
356 57578 konstantin
        } else {
357
            // EXCEPTION - Stakeholder not found
358 57671 konstantin
            throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
359 57578 konstantin
        }
360
        return true;
361
    }
362
363
364 57934 konstantin
    @RequestMapping(value = "/{stakeholderId}/toggle-status", method = RequestMethod.POST)
365
    public Boolean toggleStakeholderStatus(@PathVariable("stakeholderId") String stakeholderId) {
366 57964 konstantin
        log.debug("toggle stakeholder status (isActive)");
367 57934 konstantin
        log.debug("Stakeholder: "+stakeholderId);
368
369
        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
370
        if (stakeholder == null) {
371
            // EXCEPTION - Stakeholder not found
372
            throw new EntityNotFoundException("Toggle stakeholder status: Stakeholder with id: "+stakeholderId+" not found");
373
        }
374
        stakeholder.setIsActive(!stakeholder.getIsActive());
375
376
        stakeholderDAO.save(stakeholder);
377
        log.debug("Stakeholder toggled!");
378
379
        return stakeholder.getIsActive();
380
    }
381
382
    @RequestMapping(value = "/{stakeholderId}/toggle-access", method = RequestMethod.POST)
383
    public Boolean toggleStakeholderAccess(@PathVariable("stakeholderId") String stakeholderId) {
384 57964 konstantin
        log.debug("toggle stakeholder access (isPublic)");
385 57934 konstantin
        log.debug("Stakeholder: "+stakeholderId);
386
387
        Stakeholder stakeholder = stakeholderDAO.findById(stakeholderId);
388
        if (stakeholder == null) {
389
            // EXCEPTION - Stakeholder not found
390
            throw new EntityNotFoundException("Toggle stakeholder access: Stakeholder with id: "+stakeholderId+" not found");
391
        }
392
        stakeholder.setIsPublic(!stakeholder.getIsPublic());
393
394
        stakeholderDAO.save(stakeholder);
395
        log.debug("Stakeholder toggled!");
396
397
        return stakeholder.getIsPublic();
398
    }
399
400
401 57578 konstantin
    // The following are not supposed to be used
402 57671 konstantin
//    @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
403
//    public List<Date> getAllStakeholderDates() {
404
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
405
//        List<Date> profileDates = new ArrayList<>();
406
//
407
//        int i=0;
408
//        for(Stakeholder profile : profiles) {
409
//            log.debug(profile.getCreationDate());
410
//            profileDates.add(profile.getCreationDate());
411
//            log.debug(profileDates.get(i));
412
//            i++;
413
//        }
414
//        return profileDates;
415
//    }
416
//
417
//    @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
418
//    public List<String> getAllStakeholderDates1() {
419
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
420
//        List<String> profileDates = new ArrayList<>();
421
//
422
//        for(Stakeholder profile : profiles) {
423
//            log.debug(profile.getCreationDate().toString());
424
//            profileDates.add(profile.getCreationDate().toString());
425
//        }
426
//        return profileDates;
427
//    }
428
//
429
//    @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
430
//    public List<String> getAllStakeholderDates2() {
431
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
432
//        List<String> profileDates = new ArrayList<>();
433
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
434
//
435
//        for(Stakeholder profile : profiles) {
436
//            log.debug(format.format(profile.getCreationDate()));
437
//            profileDates.add(format.format(profile.getCreationDate()));
438
//        }
439
//        return profileDates;
440
//    }
441 57503 konstantin
}