1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
|
4
|
import eu.dnetlib.uoaadmintools.dao.DivHelpContentDAO;
|
5
|
import eu.dnetlib.uoaadmintools.dao.DivIdDAO;
|
6
|
import eu.dnetlib.uoaadmintools.dao.PageDAO;
|
7
|
import eu.dnetlib.uoaadmintools.entities.*;
|
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.List;
|
14
|
|
15
|
@RestController
|
16
|
@CrossOrigin(origins = "*")
|
17
|
public class DivHelpContentController {
|
18
|
private final Logger log = Logger.getLogger(this.getClass());
|
19
|
|
20
|
@Autowired
|
21
|
DivIdController divIdController;
|
22
|
|
23
|
@Autowired
|
24
|
private DivHelpContentDAO divHelpContentDAO;
|
25
|
|
26
|
@Autowired
|
27
|
private DivIdDAO divIdDAO;
|
28
|
|
29
|
@Autowired
|
30
|
private PageDAO pageDAO;
|
31
|
|
32
|
@Autowired
|
33
|
private CommunityDAO communityDAO;
|
34
|
|
35
|
@RequestMapping(value = "/divhelpcontent", method = RequestMethod.GET)
|
36
|
public List<DivHelpContentResponse> getDivHelpContents(@RequestParam(required = false) String community,
|
37
|
@RequestParam(required = false) String page,
|
38
|
@RequestParam(required = false) String div,
|
39
|
@RequestParam(required = false) String active) {
|
40
|
|
41
|
Community _community = communityDAO.findByPid(community);
|
42
|
String communityId = null;
|
43
|
if(_community != null) {
|
44
|
communityId = _community.getId();
|
45
|
}
|
46
|
|
47
|
DivId divId = divIdDAO.findByName(div);
|
48
|
String divIdId = null;
|
49
|
if(divId != null) {
|
50
|
divIdId = divId.getId();
|
51
|
}
|
52
|
|
53
|
List<DivHelpContentResponse> divHelpContentResponses = new ArrayList<>();
|
54
|
|
55
|
List<DivHelpContent> divHelpContents = null;
|
56
|
if(community != null && div != null && active != null) {
|
57
|
divHelpContents = divHelpContentDAO.findByCommunityAndDivIdAndIsActive(communityId, divIdId, Boolean.parseBoolean(active));
|
58
|
} else if(community != null && div != null) {
|
59
|
divHelpContents = divHelpContentDAO.findByCommunityAndDivId(communityId, divIdId);
|
60
|
} else if(community != null && active != null) {
|
61
|
divHelpContents = divHelpContentDAO.findByCommunityAndIsActive(communityId, Boolean.parseBoolean(active));
|
62
|
} else if(div != null && active != null) {
|
63
|
divHelpContents = divHelpContentDAO.findByDivIdAndIsActive(divIdId, Boolean.parseBoolean(active));
|
64
|
} else if(community != null) {
|
65
|
divHelpContents = divHelpContentDAO.findByCommunity(communityId);
|
66
|
} else if(div != null) {
|
67
|
divHelpContents = divHelpContentDAO.findByDivId(divIdId);
|
68
|
} else if(active != null){
|
69
|
divHelpContents = divHelpContentDAO.findByIsActive(Boolean.parseBoolean(active));
|
70
|
} else {
|
71
|
divHelpContents = divHelpContentDAO.findAll();
|
72
|
}
|
73
|
|
74
|
for (DivHelpContent divHelpContent : divHelpContents) {
|
75
|
DivIdResponse divIdResponse = null;
|
76
|
if(div == null) {
|
77
|
divId = divIdDAO.findById(divHelpContent.getDivId());
|
78
|
}
|
79
|
divIdResponse = divIdController.divIdResponseFromDivId(divId);
|
80
|
|
81
|
if(community == null) {
|
82
|
_community = communityDAO.findById(divHelpContent.getCommunity());
|
83
|
}
|
84
|
|
85
|
for(Page p : divIdResponse.getPages()) {
|
86
|
if (page == null || p.getRoute().equals(page)) {
|
87
|
DivHelpContentResponse divHelpContentResponse = new DivHelpContentResponse(divHelpContent);
|
88
|
divHelpContentResponse.setDivId(divIdResponse);
|
89
|
divHelpContentResponse.setCommunity(_community);
|
90
|
divHelpContentResponses.add(divHelpContentResponse);
|
91
|
break;
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
|
96
|
return divHelpContentResponses;
|
97
|
}
|
98
|
|
99
|
@RequestMapping(value = "/divhelpcontent/{id}", method = RequestMethod.GET)
|
100
|
public DivHelpContent getDivHelpContent(@PathVariable(value = "id") String id) {
|
101
|
return divHelpContentDAO.findById(id);
|
102
|
}
|
103
|
|
104
|
@RequestMapping(value = "/divhelpcontent", method = RequestMethod.POST)
|
105
|
public DivHelpContent insertOrUpdateDivHelpContent(@RequestBody DivHelpContent divHelpContent) {
|
106
|
return divHelpContentDAO.save(divHelpContent);
|
107
|
}
|
108
|
|
109
|
@RequestMapping(value = "/divhelpcontent/{id}", method = RequestMethod.DELETE)
|
110
|
public void deleteDivHelpContent(@PathVariable(value = "id") String id) {
|
111
|
divHelpContentDAO.delete(id);
|
112
|
}
|
113
|
|
114
|
@RequestMapping(value = "/divhelpcontent/delete", method = RequestMethod.POST)
|
115
|
public Boolean deleteDivHelpContents(@RequestBody List<String> divHelpContents) throws Exception {
|
116
|
for (String id: divHelpContents) {
|
117
|
divHelpContentDAO.delete(id);
|
118
|
}
|
119
|
return true;
|
120
|
}
|
121
|
|
122
|
@RequestMapping(value = "/divhelpcontent/toggle", method = RequestMethod.POST)
|
123
|
public List<String> toggleDivHelpContent(@RequestBody List<String> divHelpContents, @RequestParam String status) throws Exception {
|
124
|
for (String id: divHelpContents) {
|
125
|
log.debug("Id of divHelpContent: "+id);
|
126
|
DivHelpContent divHelpContent = divHelpContentDAO.findById(id);
|
127
|
divHelpContent.setIsActive(Boolean.parseBoolean(status));
|
128
|
divHelpContentDAO.save(divHelpContent);
|
129
|
}
|
130
|
return divHelpContents;
|
131
|
}
|
132
|
|
133
|
}
|