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 SubCategoryController {
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
    public SubCategory<Indicator> buildSubCategory(SubCategory<Indicator> subcategoryFull) {
35
        SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
36

    
37
        List<String> charts = new ArrayList<>();
38
        List<Indicator> chartsFull = new ArrayList<>();
39
        for(Indicator chart : subcategoryFull.getCharts()) {
40
            Indicator chartSaved = indicatorDAO.save(chart);
41
            chart.setId(chartSaved.getId());
42
            chartsFull.add(chart);
43
            charts.add(chartSaved.getId());
44
        }
45
        subcategoryFull.setCharts(chartsFull);
46
        subCategory.setCharts(charts);
47

    
48
        List<String> numbers = new ArrayList<>();
49
        List<Indicator> numbersFull = new ArrayList<>();
50
        for(Indicator numbr : subcategoryFull.getNumbers()) {
51
            Indicator numberSaved = indicatorDAO.save(numbr);
52
            numbr.setId(numberSaved.getId());
53
            numbersFull.add(numbr);
54
            numbers.add(numberSaved.getId());
55
        }
56
        subcategoryFull.setNumbers(numbersFull);
57
        subCategory.setNumbers(numbers);
58

    
59
        SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
60

    
61
        subcategoryFull.setId(subCategorySaved.getId());
62
        return subcategoryFull;
63
    }
64

    
65
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/save", method = RequestMethod.POST)
66
    public SubCategory<Indicator> saveSubCategory(@PathVariable("stakeholderId") String stakeholderId,
67
                                                  @PathVariable("topicId") String topicId,
68
                                                  @PathVariable("categoryId") String categoryId,
69
                                                  @RequestBody SubCategory<Indicator> subcategoryFull) {
70
        log.debug("save subcategory");
71

    
72
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
73

    
74
        if(stakeholder != null) {
75

    
76
            Topic<String> topic = topicDAO.findById(topicId);
77
            if(topic != null) {
78
                if(stakeholder.getTopics().contains(topicId)) {
79

    
80
                    Category<String> category = categoryDAO.findById(categoryId);
81
                    if(category != null) {
82
                        if(topic.getCategories().contains(categoryId)) {
83
                            SubCategory<String> subCategory = new SubCategory<>(subcategoryFull);
84

    
85
                            List<String> charts = new ArrayList<>();
86
                            for(Indicator chart : subcategoryFull.getCharts()) {
87
                                charts.add(chart.getId());
88
                            }
89
                            subCategory.setCharts(charts);
90

    
91
                            List<String> numbers = new ArrayList<>();
92
                            for(Indicator numbr : subcategoryFull.getNumbers()) {
93
                                numbers.add(numbr.getId());
94
                            }
95
                            subCategory.setNumbers(numbers);
96

    
97
                            SubCategory<String> subCategorySaved = subCategoryDAO.save(subCategory);
98

    
99
                            List<String> subcategories = category.getSubCategories();
100
                            int index = subcategories.indexOf(subCategorySaved.getId());
101
                            if(index == -1) {
102
                                subcategories.add(subCategorySaved.getId());
103
                                categoryDAO.save(category);
104
                                log.debug("Subcategory saved!");
105

    
106
                                subcategoryFull.setId(subCategorySaved.getId());
107
                            }
108

    
109
                            charts = null;
110
                            numbers = null;
111
                            subCategory = null;
112
                        } else {
113
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
114
                            throw new PathNotValidException("Save subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
115
                        }
116
                    } else {
117
                        // EXCEPTION - Category not found
118
                        throw new EntityNotFoundException("Save subcategory: Category with id: "+categoryId+" not found");
119
                    }
120
                } else {
121
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
122
                    throw new PathNotValidException("Save subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
123
                }
124
            } else {
125
                // EXCEPTION - Topic not found
126
                throw new EntityNotFoundException("Save subcategory: Topic with id: "+topicId+" not found");
127
            }
128
        } else {
129
            // EXCEPTION - Stakeholder not found
130
            throw new EntityNotFoundException("Save subcategory: Stakeholder with id: "+stakeholderId+" not found");
131
        }
132
        return subcategoryFull;
133
    }
134

    
135
    @RequestMapping(value = "/{stakeholderId}/{topicId}/{categoryId}/{subcategoryId}/delete", method = RequestMethod.DELETE)
136
    public boolean deleteSubCategory(@PathVariable("stakeholderId") String stakeholderId,
137
                                     @PathVariable("topicId") String topicId,
138
                                     @PathVariable("categoryId") String categoryId,
139
                                     @PathVariable("subcategoryId") String subcategoryId) {
140
        log.debug("delete subcategory");
141

    
142
        Stakeholder<String> stakeholder = stakeholderDAO.findById(stakeholderId);
143

    
144
        if(stakeholder != null) {
145

    
146
            Topic<String> topic = topicDAO.findById(topicId);
147
            if(topic != null) {
148
                if(stakeholder.getTopics().contains(topicId)) {
149

    
150
                    Category<String> category = categoryDAO.findById(categoryId);
151
                    if(category != null) {
152
                        if(topic.getCategories().contains(categoryId)) {
153

    
154
                            SubCategory<String> subcategory = subCategoryDAO.findById(subcategoryId);
155
                            if(subcategory != null) {
156
                                List<String> subcategories = category.getSubCategories();
157
                                int index = subcategories.indexOf(subcategoryId);
158
                                if(index != -1) {
159
                                    for(String chartId : subcategory.getCharts()) {
160
                                        indicatorDAO.delete(chartId);
161
                                    }
162
                                    subcategory.setCharts(null);
163

    
164
                                    for(String numberId : subcategory.getNumbers()) {
165
                                        indicatorDAO.delete(numberId);
166
                                    }
167
                                    subcategory.setNumbers(null);
168

    
169
                                    subcategories.remove(index);
170
                                    categoryDAO.save(category);
171

    
172
                                    subCategoryDAO.delete(subcategoryId);
173
                                    log.debug("Subcategory deleted!");
174
                                } else {
175
                                    // EXCEPTION - SubCategory not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias(); -> Category: category.getAlias();
176
                                    throw new PathNotValidException("Delete subcategory: Subcategory with id: "+subcategoryId+" not found in Category: "+categoryId);
177
                                }
178

    
179
                            } else {
180
                                // EXCEPTION - SubCategory not found
181
                                throw new EntityNotFoundException("Delete subcategory: SubCategory with id: "+subcategoryId+" not found");
182
                            }
183
                        } else {
184
                            // EXCEPTION - Category not found in Stakeholder: stakeholder.getAlias(); -> Topic: topic.getAlias();
185
                            throw new PathNotValidException("Delete subcategory: Category with id: "+categoryId+" not found in Topic: "+topicId);
186
                        }
187
                    } else {
188
                        // EXCEPTION - Category not found
189
                        throw new EntityNotFoundException("Delete subcategory: Category with id: "+categoryId+" not found");
190
                    }
191
                } else {
192
                    // EXCEPTION - Topic not found in Stakeholder: stakeholder.getAlias();
193
                    throw new PathNotValidException("Delete subcategory: Topic with id: "+topicId+" not found in Stakeholder: "+stakeholderId);
194
                }
195
            } else {
196
                // EXCEPTION - Topic not found
197
                throw new EntityNotFoundException("Delete subcategory: Topic with id: "+topicId+" not found");
198
            }
199
        } else {
200
            // EXCEPTION - Stakeholder not found
201
            throw new EntityNotFoundException("Delete subcategory: Stakeholder with id: "+stakeholderId+" not found");
202
        }
203
        return true;
204
    }
205

    
206
}
(4-4/6)