1205ce705ac58b7a32b9b4d09b0be60eeca6003e
[vfc/nfvo/wfengine.git] / logging-sdk / src / main / java / org / openo / log / impl / Facitility.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.log.impl;
17
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.io.Writer;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 import org.codehaus.jackson.JsonProcessingException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class Facitility {
36   private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName());
37
38   private Facitility() {
39
40   }
41
42   public static String oToJ(Object o) {
43     ObjectMapper om = new ObjectMapper();
44     Writer w = new StringWriter();
45     String json = null;
46     try {
47
48       om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
49       om.writeValue(w, o);
50       json = w.toString();
51       w.close();
52     } catch (IOException e) {
53       LOGGER.error("IOException" + e);
54     }
55     return json;
56   }
57
58   public static Map<String, String> readJson2Map(String json) {
59     ObjectMapper objectMapper = new ObjectMapper();
60     try {
61       Map<String, String> maps = objectMapper.readValue(json, Map.class);
62       return maps;
63     } catch (Exception e) {
64       LOGGER.error("IOException" + e);
65       return null;
66     }
67   }
68
69   public static String hashMapToJson(HashMap<String, String> map) {
70     String string = "{";
71     for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
72       Entry e = (Entry) it.next();
73       string += "\"" + e.getKey() + "\":";
74       string += "\"" + e.getValue() + "\",";
75     }
76     string = string.substring(0, string.lastIndexOf(","));
77     string += "}";
78     return string;
79   }
80
81   public static String dateFormat(Date date) {
82     SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
83     return time.format(date);
84   }
85
86   public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) {
87     StringBuilder errMsg = new StringBuilder();
88     java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator();
89     int count = 0;
90     while (hashMapIt.hasNext()) {
91       String key = hashMapIt.next();
92       Object value = checkParamsMap.get(key);
93       if (value == null || "".equals(value)) {
94         errMsg.append(key);
95         count++;
96         if (count < checkParamsMap.size() - 1) {
97           errMsg.append(" and ");
98         }
99       }
100
101     }
102     if (count > 0) {
103       errMsg.append(" can't be null or \"\"!\n ");
104     }
105     return errMsg.toString();
106   }
107
108   public static String checkRequiredJsonParam(String jsonParam, String key) {
109     StringBuilder errMsg = new StringBuilder();
110     try {
111       ObjectMapper mapper = new ObjectMapper();
112       JsonNode jsonValue;
113
114
115       jsonValue = mapper.readTree(jsonParam.toString());
116       Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
117       while (elements.hasNext()) {
118         Entry<String, JsonNode> node = elements.next();
119         String childValue = node.getValue().asText();
120         if (childValue == null || "".equals(childValue)) {
121           errMsg.append(
122               "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
123           break;
124         }
125       }
126
127       return errMsg.toString();
128     } catch (JsonProcessingException e) {
129       // TODO Auto-generated catch block
130       LOGGER.error("JsonProcessingException" + e);
131       return errMsg.toString();
132     } catch (IOException e) {
133       // TODO Auto-generated catch block
134       LOGGER.error("IOException" + e);
135       return errMsg.toString();
136     }
137   }
138 }