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

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

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

    
55
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
56
        stakeholderFull.setId(stakeholderSaved.getId());
57
        return stakeholderFull;
58
        //return null;
59
    }
60

    
61
    public Stakeholder setFullEntities(Stakeholder<String> stakeholder) {
62
        Stakeholder<Topic> stakeholderFull = new Stakeholder<>(stakeholder);
63

    
64
        List<Topic> topics = new ArrayList<>();
65

    
66
        for (String topicId: (List<String>)stakeholder.getTopics()) {
67
            Topic<String> topic = topicDAO.findById(topicId);
68
            Topic<Category> topicFull = new Topic<Category>(topic);
69

    
70
            List<Category> categories = new ArrayList<>();
71

    
72
            for(String categoryId : topic.getCategories()) {
73
                Category<String> category = categoryDAO.findById(categoryId);
74
                Category<SubCategory> categoryFull = new Category<SubCategory>(category);
75

    
76
                List<SubCategory> subCategories = new ArrayList<>();
77

    
78
                for(String subCategoryId : category.getSubCategories()) {
79
                    SubCategory<String> subCategory = subCategoryDAO.findById(subCategoryId);
80
                    SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
81

    
82
                    List<Indicator> charts = new ArrayList<>();
83
                    for(String indicatorId : subCategory.getCharts()) {
84
                        charts.add(indicatorDAO.findById(indicatorId));
85
                    }
86
                    subCategoryFull.setCharts(charts);
87

    
88
                    List<Indicator> numbers = new ArrayList<>();
89
                    for(String indicatorId : subCategory.getNumbers()) {
90
                        numbers.add(indicatorDAO.findById(indicatorId));
91
                    }
92
                    subCategoryFull.setNumbers(numbers);
93

    
94
                    subCategories.add(subCategoryFull);
95
                }
96

    
97
                categoryFull.setSubCategories(subCategories);
98
                categories.add(categoryFull);
99
            }
100

    
101
            topicFull.setCategories(categories);
102
            topics.add(topicFull);
103
        }
104

    
105
        stakeholderFull.setTopics(topics);
106
        return stakeholderFull;
107
    }
108

    
109
    @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
110
    public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
111
        List<Stakeholder> stakeholders;
112
        if(type == null) {
113
            stakeholders = stakeholderDAO.findAll();
114
        } else {
115
            stakeholders = stakeholderDAO.findByType(type);
116
        }
117

    
118
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
119
        for(Stakeholder stakeholder : stakeholders) {
120
            stakeholdersFull.add(this.setFullEntities(stakeholder));
121
        }
122

    
123
        return stakeholdersFull;
124
    }
125

    
126
    @RequestMapping(value = "/stakeholder/default", method = RequestMethod.GET)
127
    public List<Stakeholder> getAllDefaultStakeholders(@RequestParam(required = false) String type) {
128
        List<Stakeholder> stakeholders;
129
        if(type == null) {
130
            stakeholders = stakeholderDAO.findByIsDefaultProfile(true);
131
        } else {
132
            stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type);
133
        }
134

    
135
        List<Stakeholder> stakeholdersFull = new ArrayList<>();
136
        for(Stakeholder stakeholder : stakeholders) {
137
            stakeholdersFull.add(this.setFullEntities(stakeholder));
138
        }
139
        return stakeholdersFull;
140
    }
141

    
142
    @RequestMapping(value = "/stakeholder", method = RequestMethod.GET)
143
    public List<Stakeholder> getAllRealStakeholders(@RequestParam(required = false) String type) {
144
        List<Stakeholder> stakeholders;
145
        if(type == null) {
146
            stakeholders = stakeholderDAO.findByIsDefaultProfile(false);
147
        } else {
148
            stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type);
149
        }
150

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

    
157
        return stakeholdersFull;
158
    }
159

    
160
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
161
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
162
        Stakeholder<String> stakeholder = stakeholderDAO.findByAlias(alias);
163
        if(stakeholder == null) {
164
            // EXCEPTION - Stakeholder not found
165
            throw new EntityNotFoundException("Get stakeholder: Stakeholder with alias: "+alias+" not found");
166
        }
167
        return this.setFullEntities(stakeholder);
168
    }
169

    
170
    @RequestMapping(value = "/save", method = RequestMethod.POST)
171
    public Stakeholder<Topic> saveStakeholder(@RequestBody Stakeholder<Topic> stakeholderFull) {
172
        log.debug("save stakeholder");
173

    
174
//        if(stakeholderFull == null) {
175
//            log.debug("stakeholder null");
176
//            // EXCEPTION - Parameter for Stakeholder is not accepted
177
//        }
178

    
179
        Stakeholder<String> stakeholder = new Stakeholder<>(stakeholderFull);
180

    
181
        List<String> topics = new ArrayList<>();
182
        for(Topic topic : stakeholderFull.getTopics()) {
183
            topics.add(topic.getId());
184
        }
185
        stakeholder.setTopics(topics);
186

    
187
        Stakeholder<String> stakeholderSaved = stakeholderDAO.save(stakeholder);
188
        stakeholderFull.setId(stakeholderSaved.getId());
189

    
190
        topics = null;
191
        stakeholder = null;
192
        stakeholderSaved = null;
193

    
194
        return stakeholderFull;
195
    }
196

    
197

    
198
    @RequestMapping(value = "/{stakeholderId}/delete", method = RequestMethod.DELETE)
199
    public boolean deleteStakeholder(@PathVariable("stakeholderId") String stakeholderId) {
200
        log.debug("delete stakeholder");
201

    
202
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
203

    
204
        if(stakeholder != null) {
205

    
206
            for(String topicId : stakeholder.getTopics()) {
207
                Topic<String> topic = topicDAO.findById(topicId);
208
                if (topic == null) {
209
                    // EXCEPTION - Topic not found
210
                    throw new EntityNotFoundException("Delete stakeholder: Topic with id: "+topicId+" not found (topic exists in stakeholder: "+stakeholderId+")");
211
                }
212

    
213
                for (String categoryId : topic.getCategories()) {
214
                    Category<String> category = categoryDAO.findById(categoryId);
215
                    if (category == null) {
216
                        // EXCEPTION - Category not found
217
                        throw new EntityNotFoundException("Delete stakeholder: Category with id: "+categoryId+" not found (category exists in topic: "+topicId+")");
218
                    }
219

    
220
                    for (String subCategoryId : category.getSubCategories()) {
221
                        SubCategory<String> subcategory = subCategoryDAO.findById(subCategoryId);
222
                        if (subcategory == null) {
223
                            // EXCEPTION - SubCategory not found
224
                            throw new EntityNotFoundException("Delete stakeholder: SubCategory with id: "+subCategoryId+" not found (subcategory exists in category: "+categoryId+")");
225
                        }
226

    
227
                        for (String chartId : subcategory.getCharts()) {
228
                            indicatorDAO.delete(chartId);
229
                        }
230
                        subcategory.setCharts(null);
231

    
232
                        for (String numberId : subcategory.getNumbers()) {
233
                            indicatorDAO.delete(numberId);
234
                        }
235
                        subcategory.setNumbers(null);
236

    
237
                        subCategoryDAO.delete(subCategoryId);
238
                    }
239
                    category.setSubCategories(null);
240
                    categoryDAO.delete(categoryId);
241
                }
242
                topic.setCategories(null);
243
                topicDAO.delete(topicId);
244
            }
245
            stakeholder.setTopics(null);
246
            stakeholderDAO.delete(stakeholderId);
247
            log.debug("Stakeholder deleted!");
248
        } else {
249
            // EXCEPTION - Stakeholder not found
250
            throw new EntityNotFoundException("Delete stakeholder: Stakeholder with id: "+stakeholderId+" not found");
251
        }
252
        return true;
253
    }
254

    
255

    
256
    // The following are not supposed to be used
257
//    @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
258
//    public List<Date> getAllStakeholderDates() {
259
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
260
//        List<Date> profileDates = new ArrayList<>();
261
//
262
//        int i=0;
263
//        for(Stakeholder profile : profiles) {
264
//            log.debug(profile.getCreationDate());
265
//            profileDates.add(profile.getCreationDate());
266
//            log.debug(profileDates.get(i));
267
//            i++;
268
//        }
269
//        return profileDates;
270
//    }
271
//
272
//    @RequestMapping(value = "/stakeholder/dates1", method = RequestMethod.GET)
273
//    public List<String> getAllStakeholderDates1() {
274
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
275
//        List<String> profileDates = new ArrayList<>();
276
//
277
//        for(Stakeholder profile : profiles) {
278
//            log.debug(profile.getCreationDate().toString());
279
//            profileDates.add(profile.getCreationDate().toString());
280
//        }
281
//        return profileDates;
282
//    }
283
//
284
//    @RequestMapping(value = "/stakeholder/dates2", method = RequestMethod.GET)
285
//    public List<String> getAllStakeholderDates2() {
286
//        List<Stakeholder> profiles = stakeholderDAO.findAll();
287
//        List<String> profileDates = new ArrayList<>();
288
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
289
//
290
//        for(Stakeholder profile : profiles) {
291
//            log.debug(format.format(profile.getCreationDate()));
292
//            profileDates.add(format.format(profile.getCreationDate()));
293
//        }
294
//        return profileDates;
295
//    }
296
}
(3-3/6)