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