Project

General

Profile

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 SectionController {
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 SectionDAO sectionDAO;
33

    
34
    @Autowired
35
    private IndicatorDAO indicatorDAO;
36

    
37
    public Section<Indicator> buildSection(Section<Indicator> sectionFull) {
38
        Section<String> section = new Section<>(sectionFull);
39

    
40
        List<String> indicators = new ArrayList<>();
41
        List<Indicator> indicatorsFull = new ArrayList<>();
42
        for(Indicator chart : sectionFull.getIndicators()) {
43
            Indicator chartSaved = indicatorDAO.save(chart);
44
            chart.setId(chartSaved.getId());
45
            indicatorsFull.add(chart);
46
            indicators.add(chartSaved.getId());
47
        }
48
        sectionFull.setIndicators(indicatorsFull);
49
        section.setIndicators(indicators);
50

    
51
        sectionDAO.save(section);
52

    
53
        sectionFull.setId(section.getId());
54
        return sectionFull;
55
    }
56

    
57
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/save/{index}", method = RequestMethod.POST)
58
    public Section saveSection(@PathVariable("stakeholderId") String stakeholderId,
59
                               @PathVariable("topicId") String topicId,
60
                               @PathVariable("categoryId") String categoryId,
61
                               @PathVariable("subcategoryId") String subcategoryId,
62
                               @PathVariable("index") String index,
63
                               @RequestBody Section<Indicator> sectionFull) {
64
        log.debug("save section");
65
        log.debug("Name: "+sectionFull.getTitle() + " - Id: "+sectionFull.getId() + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
66

    
67
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
68

    
69
        Section<String> oldSection = null;
70
        if(sectionFull.getId() != null) {
71
            oldSection = sectionDAO.findById(sectionFull.getId());
72
        }
73

    
74
        Section<String> section = new Section<>(sectionFull);
75

    
76
        String sectionId = sectionFull.getId();
77
        List<String> indicators = new ArrayList<>();
78
        for(Indicator indicator : sectionFull.getIndicators()) {
79
            indicators.add(indicator.getId());
80
        }
81
        section.setIndicators(indicators);
82
        sectionDAO.save(section);
83

    
84
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
85
        // this section belongs in default profile and it is new or it is updated
86
        if(stakeholder.getDefaultId() == null) {
87
            if(sectionId == null) {
88
                onSaveDefaultSection(section, topicId, categoryId, subcategoryId, stakeholder);
89
            }
90
            else {
91
                onUpdateDefaultSection(section, stakeholder, oldSection);
92
            }
93
        }
94

    
95
        List<String> sections = null;
96
        if(sectionFull.getType().equals("chart")) {
97
            sections = subCategory.getCharts();
98
        } else if(sectionFull.getType().equals("number")) {
99
            sections = subCategory.getNumbers();
100
        }
101

    
102
        int existing_index = sections.indexOf(section.getId());
103
        if (existing_index == -1) {
104
            if(Integer.parseInt(index) != -1) {
105
                sections.add(Integer.parseInt(index), section.getId());
106
            } else {
107
                sections.add(section.getId());
108
            }
109
            subCategoryDAO.save(subCategory);
110
            log.debug("Section saved!");
111

    
112
            sectionFull.setId(section.getId());
113
        }
114

    
115
        return sectionFull;
116
    }
117

    
118
    public void onSaveDefaultSection(Section section,
119
                                     String defaultTopicId, String defaultCategoryId,
120
                                     String defaultSubcategoryId, Stakeholder defaultStakeholder) {
121
        log.debug("On save default section");
122

    
123

    
124
        // new section in default profile - add it on profiles of the same type
125
        List<SubCategory> subCategories = subCategoryDAO.findByDefaultId(defaultSubcategoryId);
126

    
127
        for (SubCategory subCategory : subCategories) {
128
            Category parentCategory = categoryDAO.findBySubCategoriesContaining(subCategory.getId());
129
            Topic parentTopic = topicDAO.findByCategoriesContaining(parentCategory.getId());
130
            Stakeholder parentStakeholder = stakeholderDAO.findByTopicsContaining(parentTopic.getId());
131

    
132
            Section sectionNew = new Section();
133
            sectionNew.copyFromDefault(section);
134

    
135
            sectionNew.setStakeholderAlias(parentStakeholder.getAlias());
136

    
137
            sectionDAO.save(sectionNew);
138

    
139
            List<String> sections = null;
140
            if (section.getType().equals("chart")) {
141
                sections = subCategory.getCharts();
142
            } else if (section.getType().equals("number")) {
143
                sections = subCategory.getNumbers();
144
            }
145
            sections.add(sectionNew.getId());
146

    
147
            subCategoryDAO.save(subCategory);
148
        }
149
    }
150

    
151
    public void onUpdateDefaultSection(Section section, Stakeholder stakeholder, Section oldSection) {
152
        log.debug("On update default section");
153

    
154
        // section already exists - check if changed and update all sections based on it
155

    
156
        boolean changed = false;
157
        List<Section> sections = sectionDAO.findByDefaultId(section.getId());
158

    
159
        for(Section sectionBasedOnDefault : sections) {
160
            if(section.getTitle() != null && !section.getTitle().equals(sectionBasedOnDefault.getTitle())
161
                    && (oldSection.getTitle() == null || oldSection.getTitle().equals(sectionBasedOnDefault.getTitle()))) {
162

    
163
                sectionBasedOnDefault.setTitle(section.getTitle());
164
                changed = true;
165
            }
166

    
167
            if(!changed) {
168
//                break;
169
                continue;
170
            }
171

    
172
//            sectionBasedOnDefault.setTitle(section.getTitle());
173
            sectionDAO.save(sectionBasedOnDefault);
174
        }
175
    }
176

    
177
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/delete", method = RequestMethod.DELETE)
178
    public boolean deleteSection(@PathVariable("stakeholderId") String stakeholderId,
179
                                   @PathVariable("topicId") String topicId,
180
                                   @PathVariable("categoryId") String categoryId,
181
                                   @PathVariable("subcategoryId") String subcategoryId,
182
                                   @PathVariable("sectionId") String sectionId) {
183
        log.debug("delete section");
184
        log.debug("Id: "+sectionId + " - Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
185

    
186
        Section section = sectionDAO.findById(sectionId);
187
        if(section != null) {
188
            SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
189

    
190
            List<String> sections = null;
191
            if (section.getType().equals("chart")) {
192
                sections = subCategory.getCharts();
193
            } else if (section.getType().equals("number")) {
194
                sections = subCategory.getNumbers();
195
            }
196

    
197
            int index = sections.indexOf(sectionId);
198
            if (index != -1) {
199
                sections.remove(index);
200
                subCategoryDAO.save(subCategory);
201

    
202
                sectionDAO.delete(sectionId);
203
                log.debug("Section deleted!");
204
            } else {
205
                // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subcategory.getAlias();
206
                throw new PathNotValidException("Delete section: Section with id: "+sectionId+" not found in SubCategory: "+subcategoryId);
207
            }
208
        } else {
209
            // EXCEPTION - Section not found
210
            throw new EntityNotFoundException("Delete section: Section with id: "+sectionId+" not found");
211
        }
212
        return true;
213
    }
214

    
215
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{type}/reorder", method = RequestMethod.POST)
216
    public List<Section> reorderSections(@PathVariable("stakeholderId") String stakeholderId,
217
                                         @PathVariable("topicId") String topicId,
218
                                         @PathVariable("categoryId") String categoryId,
219
                                         @PathVariable("subcategoryId") String subcategoryId,
220
                                         @PathVariable("type") String type,
221
                                         @RequestBody List<String> sections) {
222
        log.debug("reorder sections of type: "+type);
223
        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
224

    
225
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
226

    
227
        if (type.equals("chart")) {
228
            subCategory.setCharts(sections);
229
        } else if (type.equals("number")) {
230
            subCategory.setNumbers(sections);
231
        }
232

    
233
        subCategoryDAO.save(subCategory);
234
        log.debug("Sections reordered!");
235

    
236
        List<Section> sectionsFull = new ArrayList<>();
237
        for(String sectionId : sections) {
238
            sectionsFull.add(sectionDAO.findById(sectionId));
239
        }
240
        return sectionsFull;
241
    }
242

    
243
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-status", method = RequestMethod.POST)
244
//    public Boolean toggleSectionStatus(@PathVariable("stakeholderId") String stakeholderId,
245
//                                       @PathVariable("topicId") String topicId,
246
//                                       @PathVariable("categoryId") String categoryId,
247
//                                       @PathVariable("subcategoryId") String subcategoryId,
248
//                                       @PathVariable("sectionId") String sectionId) {
249
//        log.debug("toggle section status (isActive)");
250
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId+ " - Section: "+sectionId);
251
//
252
//        Section section = sectionDAO.findById(sectionId);
253
//        if (section == null) {
254
//            // EXCEPTION - Section not found
255
//            throw new EntityNotFoundException("Toggle section status: Section with id: "+sectionId+" not found");
256
//        }
257
//        section.setIsActive(!section.getIsActive());
258
//
259
//        this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId, section);
260
//
261
//        return section.getIsActive();
262
//    }
263

    
264
//    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/{sectionId}/toggle-access", method = RequestMethod.POST)
265
//    public Boolean toggleSectionAccess(@PathVariable("stakeholderId") String stakeholderId,
266
//                                         @PathVariable("topicId") String topicId,
267
//                                         @PathVariable("categoryId") String categoryId,
268
//                                         @PathVariable("subcategoryId") String subcategoryId,
269
//                                         @PathVariable("sectionId") String sectionId) {
270
//        log.debug("toggle section access (isPublic)");
271
//        log.debug("Stakeholder: "+stakeholderId + " - Topic: "+topicId + " - Category: "+categoryId+ " - SubCategory: "+subcategoryId);
272
//
273
//        Section section = sectionDAO.findById(sectionId);
274
//        if (section == null) {
275
//            // EXCEPTION - Section not found
276
//            throw new EntityNotFoundException("Toggle section access: Section with id: "+sectionId+" not found");
277
//        }
278
//        section.setIsPublic(!section.getIsPublic());
279
//
280
//        this.toggleSection(stakeholderId, topicId, categoryId, subcategoryId);
281
//
282
//        return section.getIsPublic();
283
//    }
284

    
285
    public void toggleSection(String stakeholderId, String topicId, String categoryId, String subcategoryId, Section section) {
286
        SubCategory<String> subCategory = checkForExceptions(stakeholderId, topicId, categoryId, subcategoryId);
287

    
288
        List<String> sections = null;
289
        if (section.getType().equals("chart")) {
290
            sections = subCategory.getCharts();
291
        } else if (section.getType().equals("number")) {
292
            sections = subCategory.getNumbers();
293
        }
294

    
295
        if(sections.contains(section.getId())) {
296
            sectionDAO.save(section);
297
            log.debug("Section toggled!");
298
        } else {
299
            // EXCEPTION - Section not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias(); -> SubCategory: subCategory.getAlias();
300
            throw new PathNotValidException("Toggle section: Section with id: "+section.getId()+" not found in SubCategory: "+subcategoryId);
301
        }
302

    
303
    }
304

    
305
    private SubCategory checkForExceptions(String stakeholderId, String topicId, String categoryId, String subcategoryId) {
306

    
307
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
308

    
309
        if(stakeholder == null) {
310
            // EXCEPTION - Stakeholder not found
311
            throw new EntityNotFoundException("Save indicator: Stakeholder with id: " + stakeholderId + " not found");
312
        }
313

    
314
        Topic<String> topic = topicDAO.findById(topicId);
315
        if(topic == null) {
316
            // EXCEPTION - Topic not found
317
            throw new EntityNotFoundException("Save indicator: Topic with id: "+topicId+" not found");
318
        }
319

    
320
        if(!stakeholder.getTopics().contains(topicId)) {
321
            // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
322
            throw new PathNotValidException("Save indicator: Topic with id: " + topicId + " not found in Stakeholder: " + stakeholderId);
323
        }
324

    
325
        Category<String> category = categoryDAO.findById(categoryId);
326
        if(category == null) {
327
            // EXCEPTION - Category not found
328
            throw new EntityNotFoundException("Save indicator: Category with id: "+categoryId+" not found");
329
        }
330

    
331
        if(!topic.getCategories().contains(categoryId)) {
332
            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
333
            throw new PathNotValidException("Save indicator: Category with id: "+categoryId+" not found in Topic: "+topicId);
334
        }
335

    
336
        SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
337
        if(subcategory == null) {
338
            // EXCEPTION - SubCategory not found
339
            throw new EntityNotFoundException("Save indicator: SubCategory with id: "+subcategoryId+" not found");
340
        }
341

    
342
        if (!category.getSubCategories().contains(subcategoryId)) {
343
            // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
344
            throw new PathNotValidException("Save indicator: SubCategory with id: "+subcategoryId+" not found in Category: "+categoryId);
345
        }
346

    
347
        return  subcategory;
348
    }
349
}
(4-4/8)