2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.policy.pap.xacml.rest.controller;
 
  23 import java.io.IOException;
 
  24 import java.io.PrintWriter;
 
  25 import java.io.UnsupportedEncodingException;
 
  26 import java.util.ArrayList;
 
  27 import java.util.HashMap;
 
  28 import java.util.List;
 
  31 import javax.servlet.http.HttpServletRequest;
 
  32 import javax.servlet.http.HttpServletResponse;
 
  34 import org.json.JSONObject;
 
  35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  36 import org.onap.policy.common.logging.flexlogger.Logger;
 
  37 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
 
  38 import org.onap.policy.rest.dao.CommonClassDao;
 
  39 import org.onap.policy.rest.jpa.EnforcingType;
 
  40 import org.springframework.beans.factory.annotation.Autowired;
 
  41 import org.springframework.http.MediaType;
 
  42 import org.springframework.stereotype.Controller;
 
  43 import org.springframework.web.bind.annotation.RequestMapping;
 
  44 import org.springframework.web.servlet.ModelAndView;
 
  46 import com.fasterxml.jackson.databind.DeserializationFeature;
 
  47 import com.fasterxml.jackson.databind.JsonNode;
 
  48 import com.fasterxml.jackson.databind.ObjectMapper;
 
  51 public class EnforcerDictionaryController {
 
  53         private static final Logger LOGGER      = FlexLogger.getLogger(EnforcerDictionaryController.class);
 
  56         CommonClassDao commonClassDao;
 
  58         @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
 
  59         public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
 
  61                         Map<String, Object> model = new HashMap<>();
 
  62                         ObjectMapper mapper = new ObjectMapper();
 
  63                         List<Object> list = commonClassDao.getData(EnforcingType.class);
 
  64                         List<String> dictList = new ArrayList<>();
 
  65                         for(int i = 0; i < list.size(); i++){
 
  66                                 EnforcingType dict = (EnforcingType) list.get(i);
 
  67                                 dictList.add(dict.getEnforcingType());
 
  69                         model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
 
  70                         org.onap.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
 
  71                         JSONObject j = new JSONObject(msg);
 
  72                         response.getWriter().write(j.toString());
 
  75                         LOGGER.error("Exception Occured"+e);
 
  79         @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
 
  80         public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
 
  82                         ObjectMapper mapper = new ObjectMapper();
 
  83                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
  84                         JsonNode root = mapper.readTree(request.getReader());
 
  85                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
 
  86                         if(enforcingType.getId() == 0){
 
  87                                 commonClassDao.save(enforcingType);
 
  89                                 commonClassDao.update(enforcingType); 
 
  91                         response.setCharacterEncoding("UTF-8");
 
  92                         response.setContentType("application / json");
 
  93                         request.setCharacterEncoding("UTF-8");
 
  95                         PrintWriter out = response.getWriter();
 
  96                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
 
  97                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
 
  99                         out.write(j.toString());
 
 104                         response.setCharacterEncoding("UTF-8");
 
 105                         request.setCharacterEncoding("UTF-8");
 
 106                         PrintWriter out = response.getWriter();
 
 107                         out.write(e.getMessage());
 
 113         @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
 
 114         public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
 
 116                         ObjectMapper mapper = new ObjectMapper();
 
 117                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
 118                         JsonNode root = mapper.readTree(request.getReader());
 
 119                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
 
 120                         commonClassDao.delete(enforcingType);
 
 121                         response.setCharacterEncoding("UTF-8");
 
 122                         response.setContentType("application / json");
 
 123                         request.setCharacterEncoding("UTF-8");
 
 125                         PrintWriter out = response.getWriter();
 
 127                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
 
 128                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
 
 129                         out.write(j.toString());
 
 134                         System.out.println(e);
 
 135                         response.setCharacterEncoding("UTF-8");
 
 136                         request.setCharacterEncoding("UTF-8");
 
 137                         PrintWriter out = response.getWriter();
 
 138                         out.write(e.getMessage());