Project

General

Profile

« Previous | Next » 

Revision 57578

1. Stakeholder.java: Create constructor and copy constructor & change getters-setters for 'isDeafult', 'isPublic' and 'isActive' fields.
2. Topic.java: Create constructor and copy constructor & change getters-setters for 'isPublic' and 'isActive' fields.
3. Category.java: Create constructor and copy constructor & change getters-setters for 'isOverview, 'isPublic' and 'isActive' fields & remove field 'description'.
4. SubCategory.java: Create constructor and copy constructor & change getters-setters for 'isPublic' and 'isActive' fields & remove field 'description' &
make charts and numbers (List) generic (String or Indicator).
5. Indicator.java: Change getters-setters for 'indicatorPaths', 'isPublic' and 'isActive' fields & create method: public boolean hasType(String str), to check if "chart" or "number".
6. IndicatorPath.java: Add fields in schema (chartObject, parameters, filters) & add in enum IndicatorPathType types: pie, line, image.
7. MongoDBStakeholderDAO.java & StakeholderDAO.java: Add method: Stakeholder findByAlias(String Alias)
8. StakeholderController.java:
a. Add method: public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) to replace indicator ids with Indicator objects
(used by: getAllStakeholders, getAllDefaultStakeholders, getAllRealStakeholders, getStakeholder (by alias))
b. Add method: public Stakeholder getStakeholder(@PathVariable("alias") String alias) - /stakeholder/{alias}
c. Add method: public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) - /stakeholder/save
d. Add method: public boolean deleteIndicator(...) - /{stakeholder}/{topic}/{category}/{subcategory}/indicator/{id}
e. Add method: public Indicator saveIndicator(...) - /{stakeholder}/{topic}/{category}/{subcategory}/indicator/save (in body send Indicator object)

View differences:

modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/dao/MongoDBStakeholderDAO.java
7 7

  
8 8
public interface MongoDBStakeholderDAO extends StakeholderDAO, MongoRepository<Stakeholder, String> {
9 9
    List<Stakeholder> findAll();
10
    List<Stakeholder> findByType(String type);
10
    List<Stakeholder> findByType(String Type);
11 11

  
12
    List<Stakeholder> findByIsDefaultProfile(boolean isDefaultProfile);
13
    List<Stakeholder> findByIsDefaultProfileAndType(boolean isDefaultProfile, String type);
12
    List<Stakeholder> findByIsDefaultProfile(boolean IsDefaultProfile);
13
    List<Stakeholder> findByIsDefaultProfileAndType(boolean IsDefaultProfile, String Type);
14 14

  
15
    Stakeholder findByAlias(String Alias);
16

  
15 17
    Stakeholder save(Stakeholder stakeholder);
16 18
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/dao/StakeholderDAO.java
6 6

  
7 7
public interface StakeholderDAO {
8 8
    List<Stakeholder> findAll();
9
    List<Stakeholder> findByType(String type);
9
    List<Stakeholder> findByType(String Type);
10 10

  
11
    List<Stakeholder> findByIsDefaultProfile(boolean isDefaultProfile);
12
    List<Stakeholder> findByIsDefaultProfileAndType(boolean isDefaultProfile, String type);
11
    List<Stakeholder> findByIsDefaultProfile(boolean IsDefaultProfile);
12
    List<Stakeholder> findByIsDefaultProfileAndType(boolean IsDefaultProfile, String Type);
13 13

  
14
    Stakeholder findByAlias(String Alias);
15

  
14 16
    Stakeholder save(Stakeholder stakeholder);
15 17
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/Stakeholder.java
33 33

  
34 34
    private List<Topic> topics;
35 35

  
36
    public Stakeholder() {}
37
    public Stakeholder(Stakeholder stakeholder) {
38
        type = stakeholder.getType();
39
        index_id = stakeholder.getIndex_id();
40
        index_name = stakeholder.getIndex_name();
41
        index_shortName = stakeholder.getIndex_shortName();
42
        alias = stakeholder.getAlias();
43
        isDefaultProfile = stakeholder.getIsDefaultProfile();
44
        isActive = stakeholder.getIsActive();
45
        isPublic = stakeholder.getIsPublic();
46
        creationDate = stakeholder.getCreationDate();
47
        updateDate = stakeholder.getUpdateDate();
48
        managers = stakeholder.getManagers();
49
    }
50

  
36 51
    public String getId() {
37 52
        return id;
38 53
    }
......
81 96
        this.alias = alias;
82 97
    }
83 98

  
84
    public boolean isDefaultProfile() {
99
    public boolean getIsDefaultProfile() {
85 100
        return isDefaultProfile;
86 101
    }
87 102

  
88
    public void setDefaultProfile(boolean defaultProfile) {
89
        isDefaultProfile = defaultProfile;
103
    public void setIsDefaultProfile(boolean isDefaultProfile) {
104
        this.isDefaultProfile = isDefaultProfile;
90 105
    }
91 106

  
92
    public boolean isActive() {
107
    public boolean getIsActive() {
93 108
        return isActive;
94 109
    }
95 110

  
96
    public void setActive(boolean active) {
97
        isActive = active;
111
    public void setIsActive(boolean isActive) {
112
        this.isActive = isActive;
98 113
    }
99 114

  
100
    public boolean isPublic() {
115
    public boolean getIsPublic() {
101 116
        return isPublic;
102 117
    }
103 118

  
104
    public void setPublic(boolean aPublic) {
105
        isPublic = aPublic;
119
    public void setIsPublic(boolean isPublic) {
120
        this.isPublic = isPublic;
106 121
    }
107 122

  
108 123
    public Date getCreationDate() {
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/SubCategory.java
2 2

  
3 3
import java.util.List;
4 4

  
5
public class SubCategory {
5
public class SubCategory<StringOrIndicator> {
6 6
    private String name;
7 7
    private String alias;
8
    private String description;
9 8
    private boolean isActive;
10 9
    private boolean isPublic;
11
    private List<Indicator> charts;
12
    private List<Indicator> numbers;
10
    private List<StringOrIndicator> charts;
11
    private List<StringOrIndicator> numbers;
13 12

  
13
    public SubCategory() {}
14
    public SubCategory(SubCategory subCategory) {
15
        name = subCategory.getName();
16
        alias = subCategory.getAlias();
17
        isActive = subCategory.getIsActive();
18
        isPublic = subCategory.getIsPublic();
19
    }
20

  
14 21
    public String getName() {
15 22
        return name;
16 23
    }
......
27 34
        this.alias = alias;
28 35
    }
29 36

  
30
    public String getDescription() {
31
        return description;
32
    }
33

  
34
    public void setDescription(String description) {
35
        this.description = description;
36
    }
37

  
38
    public boolean isActive() {
37
    public boolean getIsActive() {
39 38
        return isActive;
40 39
    }
41 40

  
42
    public void setActive(boolean active) {
43
        isActive = active;
41
    public void setIsActive(boolean isActive) {
42
        this.isActive = isActive;
44 43
    }
45 44

  
46
    public boolean isPublic() {
45
    public boolean getIsPublic() {
47 46
        return isPublic;
48 47
    }
49 48

  
50
    public void setPublic(boolean aPublic) {
51
        isPublic = aPublic;
49
    public void setIsPublic(boolean isPublic) {
50
        this.isPublic = isPublic;
52 51
    }
53 52

  
54
    public List<Indicator> getCharts() {
53
    public List<StringOrIndicator> getCharts() {
55 54
        return charts;
56 55
    }
57 56

  
58
    public void setCharts(List<Indicator> charts) {
57
    public void setCharts(List<StringOrIndicator> charts) {
59 58
        this.charts = charts;
60 59
    }
61 60

  
62
    public List<Indicator> getNumbers() {
61
    public List<StringOrIndicator> getNumbers() {
63 62
        return numbers;
64 63
    }
65 64

  
66
    public void setNumbers(List<Indicator> numbers) {
65
    public void setNumbers(List<StringOrIndicator> numbers) {
67 66
        this.numbers = numbers;
68 67
    }
69 68
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/IndicatorPath.java
1 1
package eu.dnetlib.uoamonitorservice.entities;
2 2

  
3 3
import java.util.List;
4
import java.util.Map;
4 5

  
5 6
enum IndicatorPathType {
6 7
    // Do not rename or remove existring values. This may cause problems with already stored values in DB
7
    table, bar, column;
8
    table, bar, column, pie, line, image;
8 9
}
9 10

  
10 11
public class IndicatorPath {
......
12 13
    private String source; // for numbers is the service {statistics, search, metrics} for charts is the tool {stats-tool,old,metrics, fake}
13 14
    private String url;
14 15
    private List<String> jsonPath;
16
    private String chartObject;
17
    private Map<String, String> parameters;
18
    private Map<String, Map<String, String>> filters;
15 19

  
16 20
    public IndicatorPathType getType() {
17 21
        return type;
......
44 48
    public void setJsonPath(List<String> jsonPath) {
45 49
        this.jsonPath = jsonPath;
46 50
    }
51

  
52
    public String getChartObject() {
53
        return chartObject;
54
    }
55

  
56
    public void setChartObject(String chartObject) {
57
        this.chartObject = chartObject;
58
    }
59

  
60
    public Map<String, String> getParameters() {
61
        return parameters;
62
    }
63

  
64
    public void setParameters(Map<String, String> parameters) {
65
        this.parameters = parameters;
66
    }
67

  
68
    public Map<String, Map<String, String>> getFilters() {
69
        return filters;
70
    }
71

  
72
    public void setFilters(Map<String, Map<String, String>> filters) {
73
        this.filters = filters;
74
    }
47 75
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/Indicator.java
77 77
        this.tags = tags;
78 78
    }
79 79

  
80
    public boolean isActive() {
80
    public boolean getIsActive() {
81 81
        return isActive;
82 82
    }
83 83

  
84
    public void setActive(boolean isActive) {
84
    public void setIsActive(boolean isActive) {
85 85
        this.isActive = isActive;
86 86
    }
87 87

  
88
    public boolean isPublic() {
88
    public boolean getIsPublic() {
89 89
        return isPublic;
90 90
    }
91 91

  
92
    public void setPublic(boolean isPublic) {
92
    public void setIsPublic(boolean isPublic) {
93 93
        this.isPublic = isPublic;
94 94
    }
95 95

  
96
    public List<IndicatorPath> getNdicatorPaths() {
96
    public List<IndicatorPath> getIndicatorPaths() {
97 97
        return indicatorPaths;
98 98
    }
99 99

  
100
    public void setNdicatorPaths(List<IndicatorPath> ndicatorPaths) {
101
        this.indicatorPaths = ndicatorPaths;
100
    public void setIndicatorPaths(List<IndicatorPath> indicatorPaths) {
101
        this.indicatorPaths = indicatorPaths;
102 102
    }
103

  
104
    public boolean hasType(String str) {
105
        return this.type.equals(str);
106
    }
103 107
}
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/Category.java
5 5
public class Category {
6 6
    private String name;
7 7
    private String alias;
8
    private String description;
9 8
    private boolean isActive;
10 9
    private boolean isPublic;
11 10
    private boolean isOverview;
12 11
    private List<SubCategory> subCategories;
13 12

  
13
    public Category() {}
14
    public Category(Category category) {
15
        name = category.getName();
16
        alias = category.getAlias();
17
        isActive = category.getIsActive();
18
        isPublic = category.getIsPublic();
19
        isOverview = category.getIsOverview();
20
    }
21

  
14 22
    public String getName() {
15 23
        return name;
16 24
    }
......
27 35
        this.alias = alias;
28 36
    }
29 37

  
30
    public String getDescription() {
31
        return description;
32
    }
33

  
34
    public void setDescription(String description) {
35
        this.description = description;
36
    }
37

  
38
    public boolean isActive() {
38
    public boolean getIsActive() {
39 39
        return isActive;
40 40
    }
41 41

  
42
    public void setActive(boolean active) {
43
        isActive = active;
42
    public void setIsActive(boolean isActive) {
43
        this.isActive = isActive;
44 44
    }
45 45

  
46
    public boolean isPublic() {
46
    public boolean getIsPublic() {
47 47
        return isPublic;
48 48
    }
49 49

  
50
    public void setPublic(boolean aPublic) {
51
        isPublic = aPublic;
50
    public void setIsPublic(boolean isPublic) {
51
        this.isPublic = isPublic;
52 52
    }
53 53

  
54
    public boolean isOverview() {
54
    public boolean getIsOverview() {
55 55
        return isOverview;
56 56
    }
57 57

  
58
    public void setOverview(boolean overview) {
59
        isOverview = overview;
58
    public void setIsOverview(boolean isOverview) {
59
        this.isOverview = isOverview;
60 60
    }
61 61

  
62 62
    public List<SubCategory> getSubCategories() {
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/entities/Topic.java
10 10
    private boolean isPublic;
11 11
    private List<Category> categories;
12 12

  
13
    public Topic() {}
14
    public Topic(Topic topic) {
15
        name = topic.getName();
16
        alias = topic.getAlias();
17
        description = topic.getDescription();
18
        isActive = topic.getIsActive();
19
        isPublic = topic.getIsPublic();
20
    }
21

  
13 22
    public String getName() {
14 23
        return name;
15 24
    }
......
34 43
        this.description = description;
35 44
    }
36 45

  
37
    public boolean isActive() {
46
    public boolean getIsActive() {
38 47
        return isActive;
39 48
    }
40 49

  
41
    public void setActive(boolean active) {
42
        isActive = active;
50
    public void setIsActive(boolean isActive) {
51
        this.isActive = isActive;
43 52
    }
44 53

  
45
    public boolean isPublic() {
54
    public boolean getIsPublic() {
46 55
        return isPublic;
47 56
    }
48 57

  
49
    public void setPublic(boolean aPublic) {
50
        isPublic = aPublic;
58
    public void setIsPublic(boolean isPublic) {
59
        this.isPublic = isPublic;
51 60
    }
52 61

  
53 62
    public List<Category> getCategories() {
modules/uoa-monitor-service/trunk/src/main/java/eu/dnetlib/uoamonitorservice/controllers/StakeholderController.java
1 1
package eu.dnetlib.uoamonitorservice.controllers;
2 2

  
3
import com.fasterxml.jackson.core.type.TypeReference;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.uoamonitorservice.dao.IndicatorDAO;
3 6
import eu.dnetlib.uoamonitorservice.dao.StakeholderDAO;
4
import eu.dnetlib.uoamonitorservice.entities.Stakeholder;
7
import eu.dnetlib.uoamonitorservice.entities.*;
5 8
import org.apache.log4j.Logger;
6 9
import org.springframework.beans.factory.annotation.Autowired;
7 10
import org.springframework.web.bind.annotation.*;
......
9 12
import java.text.SimpleDateFormat;
10 13
import java.util.ArrayList;
11 14
import java.util.Date;
15
import java.util.Iterator;
12 16
import java.util.List;
13 17

  
14 18
@RestController
......
19 23
    @Autowired
20 24
    private StakeholderDAO stakeholderDAO;
21 25

  
26
    @Autowired
27
    private IndicatorDAO indicatorDAO;
28

  
29
    public Stakeholder setIndicatorsForStakeholder(Stakeholder stakeholder) {
30
        for (Topic topic: stakeholder.getTopics()) {
31
            for(Category category : topic.getCategories()) {
32
                List<SubCategory> subCategories = new ArrayList<>();
33

  
34
                for(SubCategory<String> subCategory : category.getSubCategories()) {
35
                    SubCategory subCategoryFull = new SubCategory<Indicator>(subCategory);
36

  
37
                    List<Indicator> charts = new ArrayList<>();
38
                    for(String indicatorId : subCategory.getCharts()) {
39
                        charts.add(indicatorDAO.findById(indicatorId));
40
                    }
41
                    subCategoryFull.setCharts(charts);
42

  
43
                    List<Indicator> numbers = new ArrayList<>();
44
                    for(String indicatorId : subCategory.getNumbers()) {
45
                        numbers.add(indicatorDAO.findById(indicatorId));
46
                    }
47
                    subCategoryFull.setNumbers(numbers);
48

  
49
                    subCategories.add(subCategoryFull);
50
                }
51

  
52
                category.setSubCategories(subCategories);
53
            }
54

  
55
        }
56
        return stakeholder;
57
    }
58

  
22 59
    @RequestMapping(value = "/stakeholder/all", method = RequestMethod.GET)
23 60
    public List<Stakeholder> getAllStakeholders(@RequestParam(required = false) String type) {
24 61
        List<Stakeholder> stakeholders;
......
27 64
        } else {
28 65
            stakeholders = stakeholderDAO.findByType(type);
29 66
        }
67

  
68
        for(Stakeholder stakeholder : stakeholders) {
69
            this.setIndicatorsForStakeholder(stakeholder);
70
        }
71

  
30 72
        return stakeholders;
31 73
    }
32 74

  
......
38 80
        } else {
39 81
            stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(true, type);
40 82
        }
83

  
84
        for(Stakeholder stakeholder : stakeholders) {
85
            this.setIndicatorsForStakeholder(stakeholder);
86
        }
41 87
        return stakeholders;
42 88
    }
43 89

  
......
49 95
        } else {
50 96
            stakeholders = stakeholderDAO.findByIsDefaultProfileAndType(false, type);
51 97
        }
98

  
99
        for(Stakeholder stakeholder : stakeholders) {
100
            this.setIndicatorsForStakeholder(stakeholder);
101
        }
52 102
        log.debug(new Date());
53 103

  
54 104
        return stakeholders;
55 105
    }
56 106

  
107
    @RequestMapping(value = "/stakeholder/{alias}", method = RequestMethod.GET)
108
    public Stakeholder getStakeholder(@PathVariable("alias") String alias) {
109
        Stakeholder stakeholder = stakeholderDAO.findByAlias(alias);
110
        this.setIndicatorsForStakeholder(stakeholder);
111

  
112
        return stakeholder;
113
    }
114

  
115

  
116
    @RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST)
117
    public Stakeholder saveStakeholder(@RequestBody Stakeholder stakeholder) {
118
        log.debug("save stakeholder");
119

  
120
        Stakeholder stakeholderFull = new Stakeholder(stakeholder);
121

  
122
        //this.minimizeIndicatorsForStakeholder(stakeholder);
123
        List<Topic> topicsFull = new ArrayList<>();
124

  
125
        for (Topic topic: stakeholder.getTopics()) {
126
            Topic topicFull = new Topic(topic);
127

  
128
            List<Category> categoriesFull = new ArrayList<>();
129

  
130
            for(Category category : topic.getCategories()) {
131
                Category categoryFull = new Category(category);
132

  
133
                List<SubCategory> subCategories = new ArrayList<>();
134
                List<SubCategory> subCategoriesFull = new ArrayList<>();
135

  
136
                for(SubCategory<Indicator> subCategoryFull : category.getSubCategories()) {
137
                    SubCategory subCategory = new SubCategory<String>(subCategoryFull);
138

  
139
                    List<String> charts = new ArrayList<>();
140

  
141
                    ObjectMapper mapper = new ObjectMapper();
142
                    //Jackson's use of generics
143
                    List<Indicator> chartsFull = mapper.convertValue(subCategoryFull.getCharts(), new TypeReference<List<Indicator>>(){});
144

  
145
                    //List<Indicator> chartsFull = (List<Indicator>)subCategoryFull.getCharts();
146
                    //log.debug(chartsFull);
147

  
148
                    for(Indicator indicator : chartsFull) {
149
                        charts.add(indicator.getId());
150
                    }
151

  
152
                    subCategory.setCharts(charts);
153
                    subCategoryFull.setCharts(chartsFull);
154

  
155
                    List<String> numbers = new ArrayList<>();
156
                    List<Indicator> numbersFull = mapper.convertValue(subCategoryFull.getNumbers(), new TypeReference<List<Indicator>>(){});
157

  
158
                    for(Indicator indicator : numbersFull) {
159
                        numbers.add(indicator.getId());
160
                    }
161
                    subCategory.setNumbers(numbers);
162
                    subCategoryFull.setNumbers(numbersFull);
163

  
164
                    subCategories.add(subCategory);
165
                    subCategoriesFull.add(subCategoryFull);
166
                }
167

  
168
                category.setSubCategories(subCategories);
169
                categoryFull.setSubCategories(subCategoriesFull);
170

  
171
                categoriesFull.add(categoryFull);
172
            }
173
            topicFull.setCategories(categoriesFull);
174
            topicsFull.add(topicFull);
175
        }
176
        stakeholderFull.setTopics(topicsFull);
177

  
178
        log.debug("after minimize stakeholder");
179
        Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder);
180
        log.debug("stakeholder saved!");
181

  
182
        stakeholderFull.setId(stakeholderSaved.getId());
183
        return stakeholderFull;
184
    }
185

  
186
    @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/{id}", method = RequestMethod.DELETE)
187
    public boolean deleteIndicator(@PathVariable("stakeholder") String stakeholder,
188
                            @PathVariable("topic") String topic,
189
                            @PathVariable("category") String category,
190
                            @PathVariable("subcategory") String subcategory,
191
                            @PathVariable("id") String id) {
192

  
193
        Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
194
        if(_stakeholder != null) {
195
            Topic _topic = _stakeholder.getTopics().stream()
196
                    .filter(current_topic -> current_topic.getAlias().equals(topic))
197
                    .findFirst()
198
                    .orElse(null);
199
            if(_topic != null) {
200
                Category _category = _topic.getCategories().stream()
201
                        .filter(current_category -> current_category.getAlias().equals(category))
202
                        .findFirst()
203
                        .orElse(null);
204
                if(_category != null) {
205
                    SubCategory _subCategory = _category.getSubCategories().stream()
206
                            .filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
207
                            .findFirst()
208
                            .orElse(null);
209
                    if(_subCategory != null) {
210
                        List<String> indicators = null;
211

  
212
                        Indicator indicator = indicatorDAO.findById(id);
213
                        if(indicator.hasType("chart")) {
214
                            indicators =_subCategory.getCharts();
215
                        } else if(indicator.hasType("number")) {
216
                            indicators =_subCategory.getNumbers();
217
                        }
218

  
219
                        if(indicators == null) {
220
                            // EXCEPTION - No indicators found
221
                        }
222

  
223
                        //List<String> finalIndicators = indicators;
224
                        //log.debug("Indicators size: "+finalIndicators.size());
225
//                        int index = IntStream.range(0, indicators.size())
226
//                                .filter(i -> indicatorId.equals(finalIndicators.get(i)))
227
//                                .findFirst()
228
//                                .orElse(-1);	// return -1 if target is not found
229

  
230
                        boolean indicatorFound = false;
231
                        Iterator<String> indicatorIterator = indicators.iterator();
232
                        while (indicatorIterator.hasNext()) {
233
                            String indicatorId = indicatorIterator.next();
234
                            log.debug(id + " vs "+indicatorId);
235
                            if(id.equals(indicatorId)) {
236
                                indicatorIterator.remove();
237
                                indicatorFound = true;
238
                                break;
239
                            }
240
                        }
241

  
242
                        log.debug(indicatorFound);
243
                        if(!indicatorFound) {
244
                            return false;
245
                            // EXCEPTION - Indicator not found
246
                        }
247
                        //indicators.remove(index);
248

  
249
                        stakeholderDAO.save(_stakeholder);
250
                        indicatorDAO.delete(id);
251
                    } else {
252
                        // EXCEPTION - Subcategory not found
253
                    }
254
                } else {
255
                    // EXCEPTION - Category not found
256
                }
257
            } else {
258
                // EXCEPTION - Topic not found
259
            }
260
        } else {
261
            // EXCEPTION - Stakeholder not found
262
        }
263
        return true;
264
    }
265

  
266
//    @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/delete", method = RequestMethod.POST)
267
//    public boolean deleteChartPost(@PathVariable("stakeholder") String stakeholder,
268
//                            @PathVariable("topic") String topic,
269
//                            @PathVariable("category") String category,
270
//                            @PathVariable("subcategory") String subcategory,
271
//                            @RequestBody String indicatorId) {
272
//        //String id = chart.getId();
273
//        return deleteIndicator(stakeholder, topic, category, subcategory, indicatorId);
274
//    }
275

  
276

  
277
    // path variables are alias-es. Each alias must be unique.
278
    @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/indicator/save", method = RequestMethod.POST)
279
    public Indicator saveIndicator(@PathVariable("stakeholder") String stakeholder,
280
                                     @PathVariable("topic") String topic,
281
                                     @PathVariable("category") String category,
282
                                     @PathVariable("subcategory") String subcategory,
283
                                     @RequestBody Indicator indicator) {
284

  
285
        Indicator indicatorSaved = null;
286
        if(indicator.getId() != null) {
287
            log.debug("indicator is already saved");
288
            indicatorSaved = indicatorDAO.save(indicator);
289
        } else {
290
            log.debug("to save indicator");
291
            Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
292
            if (_stakeholder != null) {
293
                Topic _topic = _stakeholder.getTopics().stream()
294
                        .filter(current_topic -> current_topic.getAlias().equals(topic))
295
                        .findFirst()
296
                        .orElse(null);
297
                if (_topic != null) {
298
                    Category _category = _topic.getCategories().stream()
299
                            .filter(current_category -> current_category.getAlias().equals(category))
300
                            .findFirst()
301
                            .orElse(null);
302
                    if (_category != null) {
303
                        SubCategory _subCategory = _category.getSubCategories().stream()
304
                                .filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
305
                                .findFirst()
306
                                .orElse(null);
307
                        if (_subCategory != null) {
308
                            indicatorSaved = indicatorDAO.save(indicator);
309

  
310
                            List<String> indicators = null;
311

  
312
                            if (indicator.hasType("chart")) {
313
                                indicators = _subCategory.getCharts();
314
                            } else if (indicator.hasType("number")) {
315
                                indicators = _subCategory.getNumbers();
316
                            }
317

  
318
                            String indicatorId;
319
                            if (indicator.getId() != null) {
320
                                indicatorId = indicators.stream()
321
                                        .filter(current_indicator -> current_indicator.equals(indicator.getId()))
322
                                        .findFirst()
323
                                        .orElse(null);
324

  
325
                                if (indicatorId == null) {   // indicator is not already at this position
326
                                    indicators.add(indicator.getId());
327
                                }
328
                            }
329
                            stakeholderDAO.save(_stakeholder);
330
                        } else {
331
                            // EXCEPTION - Subcategory not found
332
                        }
333
                    } else {
334
                        // EXCEPTION - Category not found
335
                    }
336
                } else {
337
                    // EXCEPTION - Topic not found
338
                }
339
            } else {
340
                // EXCEPTION - Stakeholder not found
341
            }
342
        }
343

  
344
        return indicatorSaved;
345
    }
346

  
347

  
348
    // The following are not supposed to be used
349

  
350
    // should i delete indicators that were in the list but they are not in the new list?
351
    @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/charts/save", method = RequestMethod.POST)
352
    public List<Indicator> saveCharts(@PathVariable("stakeholder") String stakeholder,
353
                                     @PathVariable("topic") String topic,
354
                                     @PathVariable("category") String category,
355
                                     @PathVariable("subcategory") String subcategory,
356
                                     @RequestBody List<Indicator> charts) {
357
        log.debug(charts);
358
        log.debug(charts.size());
359
        log.debug(charts.getClass().getName());
360
        log.debug(charts.get(0).getClass().getName());
361
        return saveIndicators(stakeholder, topic, category, subcategory, charts, "chart");
362
    }
363

  
364
    // should i delete indicators that were in the list but they are not in the new list?
365
    @RequestMapping(value = "/{stakeholder}/{topic}/{category}/{subcategory}/numbers/save", method = RequestMethod.POST)
366
    public List<Indicator> saveNumbers(@PathVariable("stakeholder") String stakeholder,
367
                                      @PathVariable("topic") String topic,
368
                                      @PathVariable("category") String category,
369
                                      @PathVariable("subcategory") String subcategory,
370
                                      @RequestBody List<Indicator> numbers) {
371
        return saveIndicators(stakeholder, topic, category, subcategory, numbers, "number");
372
    }
373

  
374
    public List<Indicator> saveIndicators(String stakeholder, String topic,
375
                                          String category, String subcategory,
376
                                          List<Indicator> indicators, String type) {
377
        log.debug("to save indicators: "+indicators.size());
378
        List<Indicator> indicatorsSaved = new ArrayList<>();
379
        for(Indicator indicator : indicators) {
380
            indicatorsSaved.add(indicatorDAO.save(indicator));
381
        }
382
        log.debug("saved indicators: "+indicators.size());
383

  
384
        Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
385
        if(_stakeholder != null) {
386
            Topic _topic = _stakeholder.getTopics().stream()
387
                    .filter(current_topic -> current_topic.getAlias().equals(topic))
388
                    .findFirst()
389
                    .orElse(null);
390
            if(_topic != null) {
391
                Category _category = _topic.getCategories().stream()
392
                        .filter(current_category -> current_category.getAlias().equals(category))
393
                        .findFirst()
394
                        .orElse(null);
395
                if(_category != null) {
396
                    SubCategory _subCategory = _category.getSubCategories().stream()
397
                            .filter(current_subCategory -> current_subCategory.getAlias().equals(subcategory))
398
                            .findFirst()
399
                            .orElse(null);
400
                    if(_subCategory != null) {
401
                        List<String> _indicators = null;
402
                        if(type.equals("chart")) {
403
                            _indicators = _subCategory.getCharts();
404
                        } else if(type.equals("number")) {
405
                            _indicators = _subCategory.getNumbers();
406
                        }
407

  
408
                        _indicators.clear();
409
                        for(Indicator indicator : indicators) {
410
                            _indicators.add(indicator.getId());
411
                        }
412
                        stakeholderDAO.save(_stakeholder);
413
                    } else {
414
                        // EXCEPTION - Subcategory not found
415
                    }
416
                } else {
417
                    // EXCEPTION - Vategory not found
418
                }
419
            } else {
420
                // EXCEPTION - Topic not found
421
            }
422
        } else {
423
            // EXCEPTION - Stakeholder not found
424
        }
425

  
426
        return indicatorsSaved;
427
    }
428

  
429

  
430

  
431
    // Remember to check if alias is not already used before saving
432
    @RequestMapping(value = "/{stakeholder}/topic/save", method = RequestMethod.POST)
433
    public Stakeholder saveTopic(@PathVariable("stakeholder") String stakeholder,
434
                                 @RequestBody Topic topic) {
435
        Stakeholder stakeholderSaved = null;
436
        Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
437
        if (stakeholder != null) {
438
            List<Topic> topics = _stakeholder.getTopics();
439
            Topic _topic = topics.stream()
440
                    .filter(current_topic -> current_topic.getAlias().equals(topic.getAlias()))
441
                    .findFirst()
442
                    .orElse(null);
443
            if(_topic != null) {
444
                _topic = topic;
445
            } else {
446
                topics.add(topic);
447
                _stakeholder.setTopics(topics);
448
            }
449
            stakeholderSaved = stakeholderDAO.save(_stakeholder);
450
        }
451
        return stakeholderSaved;
452
    }
453

  
454
    @RequestMapping(value = "/{stakeholder}/topics/save", method = RequestMethod.POST)
455
    public Stakeholder saveTopics(@PathVariable("stakeholder") String stakeholder,
456
                                  @RequestBody List<Topic> topics) {
457
        Stakeholder stakeholderSaved = null;
458
        Stakeholder _stakeholder = stakeholderDAO.findByAlias(stakeholder);
459
        if (stakeholder != null) {
460
            _stakeholder.setTopics(topics);
461
            stakeholderSaved = stakeholderDAO.save(_stakeholder);
462
        }
463
        return stakeholderSaved;
464
    }
465

  
466

  
57 467
    @RequestMapping(value = "/stakeholder/dates", method = RequestMethod.GET)
58 468
    public List<Date> getAllStakeholderDates() {
59 469
        List<Stakeholder> profiles = stakeholderDAO.findAll();
......
93 503
        }
94 504
        return profileDates;
95 505
    }
96

  
97

  
98
    @RequestMapping(value = "/stakeholder/save", method = RequestMethod.POST)
99
    public Stakeholder insertStakeholder(@RequestBody Stakeholder stakeholder) {
100
        //Stakeholder stakeholder = new Stakeholder();
101

  
102
        Stakeholder stakeholderSaved = stakeholderDAO.save(stakeholder);
103

  
104
        return stakeholderSaved;
105
    }
106 506
}

Also available in: Unified diff