ba90f46f234078e47d75c60c3eb18a8228f3ad26
[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  * 
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.logging.logic.EELFLoggerDelegate;
51 import org.onap.portalsdk.core.service.BroadcastService;
52 import org.onap.portalsdk.core.util.SystemProperties;
53 import org.onap.portalsdk.core.web.support.AppUtils;
54 import org.onap.portalsdk.core.web.support.JsonMessage;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.stereotype.Controller;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RequestMethod;
59 import org.springframework.web.servlet.ModelAndView;
60
61 import com.fasterxml.jackson.databind.DeserializationFeature;
62 import com.fasterxml.jackson.databind.JsonNode;
63 import com.fasterxml.jackson.databind.ObjectMapper;
64
65 @Controller
66 @RequestMapping("/")
67 public class BroadcastController extends RestrictedBaseController {
68
69         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastController.class);
70         
71         @Autowired
72         private BroadcastService broadcastService;
73
74         @RequestMapping(value = { "/broadcast" }, method = RequestMethod.GET)
75         public ModelAndView broadcast(HttpServletRequest request) {
76                 Map<String, Object> model = new HashMap<>();
77                 ObjectMapper mapper = new ObjectMapper();
78
79                 try {
80                         model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
81                         model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
82                 } catch (Exception e) {
83                         logger.error(EELFLoggerDelegate.errorLogger, "broadcast() failed", e);
84                 }
85                 return new ModelAndView(getViewName(), model);
86         }
87
88         @RequestMapping(value = { "/get_broadcast" }, method = RequestMethod.GET)
89         public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
90                 Map<String, Object> model = new HashMap<>();
91                 ObjectMapper mapper = new ObjectMapper();
92
93                 try {
94
95                         model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
96                         model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
97                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
98                         JSONObject j = new JSONObject(msg);
99                         response.getWriter().write(j.toString());
100
101                 } catch (Exception e) {
102                         logger.error(EELFLoggerDelegate.errorLogger, "getBroadcast() failed", e);
103                 }
104
105         }
106
107         @SuppressWarnings({ "unchecked", "rawtypes" })
108         protected Map referenceData(HttpServletRequest request) {
109                 Map lookupData = new HashMap();
110
111                 if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) {
112                         lookupData.put("broadcastSites", AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd",
113                                         "broadcast_site_descr", "", "broadcast_site_descr"));
114                 }
115
116                 return lookupData;
117         }
118
119         @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST)
120         public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
121
122                 try {
123
124                         ObjectMapper mapper = new ObjectMapper();
125                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
126                         JsonNode root = mapper.readTree(request.getReader());
127                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
128                                         BroadcastMessage.class);
129
130                         broadcastService.saveBroadcastMessage(broadcastMessage);
131
132                         response.setCharacterEncoding("UTF-8");
133                         response.setContentType("application / json");
134                         request.setCharacterEncoding("UTF-8");
135
136                         PrintWriter out = response.getWriter();
137                         String responseString = mapper.writeValueAsString(broadcastMessage);
138                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
139
140                         out.write(j.toString());
141
142                         return null;
143                 } catch (Exception e) {
144                         response.setCharacterEncoding("UTF-8");
145                         request.setCharacterEncoding("UTF-8");
146                         PrintWriter out = response.getWriter();
147                         out.write("An error occurred while saving the BroadcastMessage in the save () mapping-/broadcast/save   ");
148                         logger.error(EELFLoggerDelegate.errorLogger, "save() failed", e);
149                         return null;
150                 }
151
152         }
153 }