4160346021e7d17d51aab5405f74ee5bef4a424d
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.sample;
21
22 import java.io.PrintWriter;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.json.JSONObject;
30 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
31 import org.openecomp.portalsdk.core.domain.BroadcastMessage;
32 import org.openecomp.portalsdk.core.service.BroadcastService;
33 import org.openecomp.portalsdk.core.web.support.JsonMessage;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38 import org.springframework.web.servlet.ModelAndView;
39
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.JsonNode;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 @Controller
45 @RequestMapping("/")
46 public class BroadcastListController extends RestrictedBaseController {
47
48         @Autowired
49         private BroadcastService broadcastService;
50
51         @RequestMapping(value = { "/broadcast_list" }, method = RequestMethod.GET)
52         public ModelAndView broadcastList(HttpServletRequest request) {
53                 Map<String, Object> model = new HashMap<String, Object>();
54
55                 model.put("model", broadcastService.getBcModel(request));
56                 return new ModelAndView(getViewName(), model);
57         }
58
59         @RequestMapping(value = { "/get_broadcast_list" }, method = RequestMethod.GET)
60         public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
61                 Map<String, Object> model = new HashMap<String, Object>();
62                 ObjectMapper mapper = new ObjectMapper();
63                 try {
64                         model.put("model", broadcastService.getBcModel(request));
65                         model.put("messagesList", broadcastService.getBcModel(request).get("messagesList"));
66                         model.put("messageLocations", broadcastService.getBcModel(request).get("messageLocations"));
67                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
68                         JSONObject j = new JSONObject(msg);
69                         response.getWriter().write(j.toString());
70                 } catch (Exception e) {
71                         e.printStackTrace();
72                 }
73
74         }
75
76         @RequestMapping(value = { "/broadcast_list/remove" }, method = RequestMethod.POST)
77         public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws Exception {
78
79                 try {
80
81                         ObjectMapper mapper = new ObjectMapper();
82                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
83                         JsonNode root = mapper.readTree(request.getReader());
84                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
85                                         BroadcastMessage.class);
86
87                         broadcastService.removeBroadcastMessage(broadcastMessage);
88
89                         response.setCharacterEncoding("UTF-8");
90                         response.setContentType("application / json");
91                         request.setCharacterEncoding("UTF-8");
92
93                         PrintWriter out = response.getWriter();
94                         String responseString = mapper.writeValueAsString(broadcastMessage);
95                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
96
97                         out.write(j.toString());
98
99                         return null;
100                 } catch (Exception e) {
101                         response.setCharacterEncoding("UTF-8");
102                         request.setCharacterEncoding("UTF-8");
103                         PrintWriter out = response.getWriter();
104                         out.write(e.getMessage());
105                         return null;
106                 }
107
108         }
109
110         @RequestMapping(value = { "/broadcast_list/toggleActive" }, method = RequestMethod.POST)
111         public ModelAndView toggleActive(HttpServletRequest request, HttpServletResponse response) throws Exception {
112
113                 try {
114
115                         ObjectMapper mapper = new ObjectMapper();
116                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
117                         JsonNode root = mapper.readTree(request.getReader());
118                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
119                                         BroadcastMessage.class);
120
121                         broadcastService.saveBroadcastMessage(broadcastMessage);
122
123                         response.setCharacterEncoding("UTF-8");
124                         response.setContentType("application / json");
125                         request.setCharacterEncoding("UTF-8");
126
127                         PrintWriter out = response.getWriter();
128                         String responseString = mapper.writeValueAsString(broadcastMessage);
129                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
130
131                         out.write(j.toString());
132
133                         return null;
134                 } catch (Exception e) {
135                         response.setCharacterEncoding("UTF-8");
136                         request.setCharacterEncoding("UTF-8");
137                         PrintWriter out = response.getWriter();
138                         out.write(e.getMessage());
139                         return null;
140                 }
141
142         }
143 }