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