Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.controllers;
2

    
3
//import com.fasterxml.jackson.core.type.TypeReference;
4
//import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.uoamonitorservice.dao.*;
6
import eu.dnetlib.uoamonitorservice.entities.*;
7
import eu.dnetlib.uoamonitorservice.handlers.EntityNotFoundException;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.web.bind.annotation.*;
11

    
12
import java.util.ArrayList;
13
import java.util.Date;
14
import java.util.List;
15

    
16
@RestController
17
@CrossOrigin(origins = "*")
18
public class StakeholderController {
19
    private final Logger log = Logger.getLogger(this.getClass());
20

    
21
    @Autowired
22
    private StakeholderDAO stakeholderDAO;
23

    
24
    @Autowired
25
    private TopicDAO topicDAO;
26

    
27
    @Autowired
28
    private CategoryDAO categoryDAO;
29

    
30
    @Autowired
31
    private SubCategoryDAO subCategoryDAO;
32

    
33
    @Autowired
34
    private IndicatorDAO indicatorDAO;
35

    
36
    @Autowired
37
    private TopicController topicController;
38

    
39
    @RequestMapping(value = "/build-stakeholder", method = RequestMethod.POST)
40
    public Stakeholder<Topic<Category<SubCategory<Indicator>>>> buildFullStakeholder(@RequestBody Stakeholder<Topic<Category<SubCategory<Indicator>>>> stakeholderFull) {
41
        log.debug("build stakeholder");
42
        log.debug("Alias: "+stakeholderFull.getAlias());
43

    
44
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
45

    
46
        List<String> topics = new ArrayList<>();
47
        List<Topic<Category<SubCategory<Indicator>>>> topicsFull = new ArrayList<>();
48
        for(Topic topic : stakeholderFull.getTopics()) {
49
            Topic<Category<SubCategory<Indicator>>> topicFull = topicController.buildTopic(topic);
50
            topicsFull.add(topicFull);
51
            topics.add(topicFull.getId());
52
        }
53
        stakeholderFull.setTopics(topicsFull);
54
        stakeholder.setTopics(topics);
55

    
56
        Date date = new Date();
57
        stakeholder.setCreationDate(date);
58
        stakeholder.setUpdateDate(date);
59

    
60
        stakeholderFull.setCreationDate(date);
61
        stakeholderFull.setUpdateDate(date);
62

    
63
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
64
        stakeholderFull.setId(stakeholderSaved.getId());
65
        return stakeholderFull;
66
        //return null;
67
    }
68

    
69
    public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
70
        Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
71

    
72
        List<Topic> topics = new ArrayList<>();
73

    
74
        for (String topicId: (List<String>)stakeholder.getTopics()) {
75
            Topic<String> topic = topicDAO.findById(topicId);
76
            if(topic == null) {
77
                // EXCEPTION - Topic not found
78
                throw new EntityNotFoundException("Get stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholder.getId()+")");
79
            }
80
            Topic<Category> topicFull = new Topic<Category>(topic);
81

    
82
            List<Category> categories = new ArrayList<>();
83

    
84
            for(String categoryId : topic.getCategories()) {
85
                Category<String> category = categoryDAO.findById(categoryId);
86
                if(category == null) {
87
                    // EXCEPTION - Category not found
88
                    throw new EntityNotFoundException("Get stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
89
                }
90
                Category<SubCategory> categoryFull = new Category<SubCategory>(category);
91

    
92
                List<SubCategory> subCategories = new ArrayList<>();
93

    
94
                for(String subCategoryId : category.getSubCategories()) {
95
                    SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
96
                    if(subCategory == null) {
97
                        // EXCEPTION - SubCategory not found
98
                        throw new EntityNotFoundException("Get stakeholder: SubCategory with id: "+subCategoryId+" not found (subCategory exists in category: "+categoryId+")");
99
                    }
100
                    SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
101

    
102
                    List<Indicator> charts = new ArrayList<>();
103
                    for(String indicatorId : subCategory.getCharts()) {
104
                        Indicator indicator = indicatorDAO.findById(indicatorId);
105
                        if(indicator == null) {
106
                            // EXCEPTION - Indicator not found
107
                            throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
108
                        }
109
                        charts.add(indicator);
110
                    }
111
                    subCategoryFull.setCharts(charts);
112

    
113
                    List<Indicator> numbers = new ArrayList<>();
114
                    for(String indicatorId : subCategory.getNumbers()) {
115
                        Indicator indicator = indicatorDAO.findById(indicatorId);
116
                        if(indicator == null) {
117
                            // EXCEPTION - Indicator not found
118
                            throw new EntityNotFoundException("Get stakeholder: Indicator with id: "+indicatorId+" not found (indicator exists in subCategory: "+subCategoryId+")");
119
                        }
120
                        numbers.add(indicator);
121
                    }
122
                    subCategoryFull.setNumbers(numbers);
123

    
124
                    subCategories.add(subCategoryFull);
125
                }
126

    
127
                categoryFull.setSubCategories(subCategories);
128
                categories.add(categoryFull);
129
            }
130

    
131
            topicFull.setCategories(categories);
132
            topics.add(topicFull);
133
        }
134

    
135
        stakeholderFull.setTopics(topics);
136
        return stakeholderFull;
137
    }
138

    
139
    @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
140
    public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
141
        log.debug("get all stakeholders" + (type != null ? " with type: "+type : ""));
142

    
143
        List<Stakeholder> stakeholders;
144
        if(type == null) {
145
            stakeholders = stakeholderDAO.findAll();
146
        } else {
147
            stakeholders = stakeholderDAO.findByType(type);
148
        }
149

    
150
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
151
        for(Stakeholder stakeholder : stakeholders) {
152
            stakeholdersFull.add(this.setFullEntities(stakeholder));
153
        }
154

    
155
        return stakeholdersFull;
156
    }
157

    
158
    @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
159
    public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
160
        log.debug("get all default stakeholders" + (type != null ? " with type: "+type : ""));
161

    
162
        List<Stakeholder> stakeholders;
163
        if(type == null) {
164
            stakeholders = stakeholderDAO.findByDefaultId(null);
165
        } else {
166
            stakeholders = stakeholderDAO.findByDefaultIdAndType(null, type);
167
        }
168

    
169
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
170
        for(Stakeholder stakeholder : stakeholders) {
171
            stakeholdersFull.add(this.setFullEntities(stakeholder));
172
        }
173
        return stakeholdersFull;
174
    }
175

    
176
    @RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
177
    public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
178
        log.debug("get all NOT default stakeholders" + (type != null ? " with type: "+type : ""));
179

    
180
        List<Stakeholder> stakeholders;
181
        if(type == null) {
182
            stakeholders = stakeholderDAO.findByDefaultIdNot(null);
183
        } else {
184
            stakeholders = stakeholderDAO.findByDefaultIdNotAndType(null, type);
185
        }
186

    
187
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
188
        for(Stakeholder stakeholder : stakeholders) {
189
            stakeholdersFull.add(this.setFullEntities(stakeholder));
190
        }
191
        log.debug(new Date());
192

    
193
        return stakeholdersFull;
194
    }
195

    
196
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
197
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
198
        log.debug("get stakeholder: "+alias);
199

    
200
        Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
201
        if(stakeholder == null) {
202
            // EXCEPTION - Stakeholder not found
203
            throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
204
        }
205
        return this.setFullEntities(stakeholder);
206
    }
207

    
208
    @RequestMapping(value = "/save", method = RequestMethod.POST)
209
    public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
210
        log.debug("save stakeholder");
211
        log.debug("Alias: "+stakeholderFull.getAlias() + " - Id: "+stakeholderFull.getId());
212

    
213
//        if(stakeholderFull == null) {
214
//            log.debug("stakeholder null");
215
//            // EXCEPTION - Parameter for Stakeholder is not accepted
216
//        }
217

    
218
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
219

    
220
        Date date = new Date();
221
        stakeholder.setUpdateDate(date);
222

    
223
        // stakeholder does not exist in DB
224
        if(stakeholderFull.getId() == null) {
225
            stakeholder.setCreationDate(date);
226
        }
227

    
228
        List<String> topics = new ArrayList<>();
229
        for(Topic topic : stakeholderFull.getTopics()) {
230
            topics.add(topic.getId());
231
        }
232
        stakeholder.setTopics(topics);
233

    
234
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
235
        stakeholderFull.setId(stakeholderSaved.getId());
236
        stakeholderFull.setCreationDate(stakeholderSaved.getCreationDate());
237
        stakeholderFull.setUpdateDate(stakeholderSaved.getUpdateDate());
238

    
239
        topics = null;
240
        stakeholder = null;
241
        stakeholderSaved = null;
242

    
243
        return stakeholderFull;
244
    }
245

    
246

    
247
    @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
248
    public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
249
        log.debug("delete stakeholder");
250
        log.debug("Id: "+stakeholderId);
251

    
252
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
253

    
254
        if(stakeholder != null) {
255

    
256
            for(String topicId : stakeholder.getTopics()) {
257
                Topic<String> topic = topicDAO.findById(topicId);
258
                if (topic == null) {
259
                    // EXCEPTION - Topic not found
260
                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
261
                }
262

    
263
                for (String categoryId : topic.getCategories()) {
264
                    Category<String> category = categoryDAO.findById(categoryId);
265
                    if (category == null) {
266
                        // EXCEPTION - Category not found
267
                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
268
                    }
269

    
270
                    for (String subCategoryId : category.getSubCategories()) {
271
                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
272
                        if (subcategory == null) {
273
                            // EXCEPTION - SubCategory not found
274
                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
275
                        }
276

    
277
                        for (String chartId : subcategory.getCharts()) {
278
                            indicatorDAO.delete(chartId);
279
                        }
280
                        subcategory.setCharts(null);
281

    
282
                        for (String numberId : subcategory.getNumbers()) {
283
                            indicatorDAO.delete(numberId);
284
                        }
285
                        subcategory.setNumbers(null);
286

    
287
                        subCategoryDAO.delete(subCategoryId);
288
                    }
289
                    category.setSubCategories(null);
290
                    categoryDAO.delete(categoryId);
291
                }
292
                topic.setCategories(null);
293
                topicDAO.delete(topicId);
294
            }
295
            stakeholder.setTopics(null);
296
            stakeholderDAO.delete(stakeholderId);
297
            log.debug("Stakeholder deleted!");
298
        } else {
299
            // EXCEPTION - Stakeholder not found
300
            throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
301
        }
302
        return true;
303
    }
304

    
305

    
306
    // The following are not supposed to be used
307
//    @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
308
//    public List<Date> getAllStakeholderDates() {
309
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
310
//        List<Date> profileDates = new ArrayList<>();
311
//
312
//        int i=0;
313
//        for(Stakeholder profile : profiles) {
314
//            log.debug(profile.getCreationDate());
315
//            profileDates.add(profile.getCreationDate());
316
//            log.debug(profileDates.get(i));
317
//            i++;
318
//        }
319
//        return profileDates;
320
//    }
321
//
322
//    @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
323
//    public List<String> getAllStakeholderDates1() {
324
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
325
//        List<String> profileDates = new ArrayList<>();
326
//
327
//        for(Stakeholder profile : profiles) {
328
//            log.debug(profile.getCreationDate().toString());
329
//            profileDates.add(profile.getCreationDate().toString());
330
//        }
331
//        return profileDates;
332
//    }
333
//
334
//    @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
335
//    public List<String> getAllStakeholderDates2() {
336
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
337
//        List<String> profileDates = new ArrayList<>();
338
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
339
//
340
//        for(Stakeholder profile : profiles) {
341
//            log.debug(format.format(profile.getCreationDate()));
342
//            profileDates.add(format.format(profile.getCreationDate()));
343
//        }
344
//        return profileDates;
345
//    }
346
}
(4-4/7)