9c04ce89bae9adc1e086304c6e8b0098e6e0dce8
[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.util.SystemProperties;
34 import org.openecomp.portalsdk.core.web.support.AppUtils;
35 import org.openecomp.portalsdk.core.web.support.JsonMessage;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 @Controller
47 @RequestMapping("/")
48 public class BroadcastController extends RestrictedBaseController {
49
50         @Autowired
51         private BroadcastService broadcastService;
52
53         @RequestMapping(value = { "/broadcast" }, method = RequestMethod.GET)
54         public ModelAndView broadcast(HttpServletRequest request) {
55                 Map<String, Object> model = new HashMap<String, Object>();
56                 ObjectMapper mapper = new ObjectMapper();
57
58                 try {
59                         model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
60                         model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
61                 } catch (Exception e) {
62                         e.printStackTrace();
63                 }
64                 return new ModelAndView(getViewName(), model);
65         }
66
67         @RequestMapping(value = { "/get_broadcast" }, method = RequestMethod.GET)
68         public void getBroadcast(HttpServletRequest request, HttpServletResponse response) {
69                 Map<String, Object> model = new HashMap<String, Object>();
70                 ObjectMapper mapper = new ObjectMapper();
71
72                 try {
73
74                         model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request)));
75                         model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites")));
76                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
77                         JSONObject j = new JSONObject(msg);
78                         response.getWriter().write(j.toString());
79
80                 } catch (Exception e) {
81                         e.printStackTrace();
82                 }
83
84         }
85
86         @SuppressWarnings({ "unchecked", "rawtypes" })
87         protected Map referenceData(HttpServletRequest request) {
88                 Map lookupData = new HashMap();
89
90                 if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) {
91                         lookupData.put("broadcastSites", AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd",
92                                         "broadcast_site_descr", "", "broadcast_site_descr"));
93                 }
94
95                 return lookupData;
96         }
97
98         @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST)
99         public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception {
100
101                 try {
102
103                         ObjectMapper mapper = new ObjectMapper();
104                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
105                         JsonNode root = mapper.readTree(request.getReader());
106                         BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(),
107                                         BroadcastMessage.class);
108
109                         broadcastService.saveBroadcastMessage(broadcastMessage);
110
111                         response.setCharacterEncoding("UTF-8");
112                         response.setContentType("application / json");
113                         request.setCharacterEncoding("UTF-8");
114
115                         PrintWriter out = response.getWriter();
116                         String responseString = mapper.writeValueAsString(broadcastMessage);
117                         JSONObject j = new JSONObject("{broadcastMessage: " + responseString + "}");
118
119                         out.write(j.toString());
120
121                         return null;
122                 } catch (Exception e) {
123                         response.setCharacterEncoding("UTF-8");
124                         request.setCharacterEncoding("UTF-8");
125                         PrintWriter out = response.getWriter();
126                         out.write(e.getMessage());
127                         return null;
128                 }
129
130         }
131 }