Merge "Fixes for eclipse warnings unused variables"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyNotificationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controller;
22
23
24 /*
25  * 
26  * */
27 import java.io.File;
28 import java.io.PrintWriter;
29 import java.util.List;
30
31 import javax.script.SimpleBindings;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.json.JSONObject;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.WatchPolicyNotificationTable;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.openecomp.portalsdk.core.web.support.UserUtils;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.servlet.ModelAndView;
44
45 import com.fasterxml.jackson.databind.DeserializationFeature;
46 import com.fasterxml.jackson.databind.JsonNode;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48 import com.fasterxml.jackson.databind.node.ArrayNode;
49
50 @Controller
51 @RequestMapping({"/"})
52 public class PolicyNotificationController extends RestrictedBaseController {
53
54         @Autowired
55         CommonClassDao commonClassDao;
56         
57         @RequestMapping(value={"/watchPolicy"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
58         public ModelAndView watchPolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{
59                 String path = "";
60                 String responseValue = "";
61                 try {
62                         String userId = UserUtils.getUserSession(request).getOrgUserId();
63                         System.out.println(userId);
64                         ObjectMapper mapper = new ObjectMapper();
65                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66                         JsonNode root = mapper.readTree(request.getReader());
67                         String name = root.get("watchData").get("name").toString();
68                         JsonNode pathList = root.get("watchData").get("path");
69                         String finalName = "";
70                         if(pathList.isArray()){
71                                 ArrayNode arrayNode = (ArrayNode) pathList;
72                                 for (int i = 0; i < arrayNode.size(); i++) {
73                                         JsonNode individualElement = arrayNode.get(i);
74                                         if(i == 0){
75                                                 path = path + individualElement.toString().replace("\"", "").trim();
76                                         }else{
77                                                 path = path + File.separator + individualElement.toString().replace("\"", "").trim();
78                                         }
79                                 }
80                         }
81                         
82                         if(pathList.size() > 0){
83                                 finalName = path + File.separator + name.replace("\"", "").trim();
84                         }else{
85                                 finalName = name.replace("\"", "").trim();
86                         }
87                         if(finalName.contains("\\")){
88                                 finalName = finalName.replace("\\", "\\\\");
89                         }
90                         String query = "from WatchPolicyNotificationTable where POLICYNAME = :finalName and LOGINIDS = :userId";
91                         SimpleBindings params = new SimpleBindings();
92                         params.put("finalName", finalName);
93                         params.put("userId", userId);
94                         List<Object> watchList = commonClassDao.getDataByQuery(query, params);
95                         if(watchList.isEmpty()){
96                                 if(finalName.contains("\\\\")){
97                                         finalName = finalName.replace("\\\\", File.separator);
98                                 }
99                                 WatchPolicyNotificationTable watch = new WatchPolicyNotificationTable();
100                                 watch.setPolicyName(finalName);
101                                 watch.setLoginIds(userId);
102                                 commonClassDao.save(watch);
103                                 responseValue = "You have Subscribed Successfully";
104                         }else{
105                                 commonClassDao.delete(watchList.get(0));
106                                 responseValue = "You have UnSubscribed Successfully";
107                         }
108                         
109                         response.setCharacterEncoding("UTF-8");
110                         response.setContentType("application / json");
111                         request.setCharacterEncoding("UTF-8");
112
113                         PrintWriter out = response.getWriter();
114                         String responseString = mapper.writeValueAsString(responseValue);
115                         JSONObject j = new JSONObject("{watchData: " + responseString + "}");
116                         out.write(j.toString());
117                         return null;
118                 }catch(Exception e){
119                         response.setCharacterEncoding("UTF-8");
120                         request.setCharacterEncoding("UTF-8");
121                         PrintWriter out = response.getWriter();
122                         out.write(e.getMessage());
123                 }
124                 return null;
125         }
126 }