2 * Copyright 2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.log.impl;
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;
26 import java.util.Map.Entry;
28 import org.codehaus.jackson.JsonProcessingException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
35 public class Facitility {
36 private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName());
38 private Facitility() {
42 public static String oToJ(Object o) {
43 ObjectMapper om = new ObjectMapper();
44 Writer w = new StringWriter();
48 om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
52 } catch (IOException e) {
53 LOGGER.error("IOException" + e);
58 public static Map<String, String> readJson2Map(String json) {
59 ObjectMapper objectMapper = new ObjectMapper();
61 Map<String, String> maps = objectMapper.readValue(json, Map.class);
63 } catch (Exception e) {
64 LOGGER.error("IOException" + e);
69 public static String hashMapToJson(HashMap<String, String> map) {
71 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
72 Entry e = (Entry) it.next();
73 string += "\"" + e.getKey() + "\":";
74 string += "\"" + e.getValue() + "\",";
76 string = string.substring(0, string.lastIndexOf(","));
81 public static String dateFormat(Date date) {
82 SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
83 return time.format(date);
86 public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) {
87 StringBuilder errMsg = new StringBuilder();
88 java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator();
90 while (hashMapIt.hasNext()) {
91 String key = hashMapIt.next();
92 Object value = checkParamsMap.get(key);
93 if (value == null || "".equals(value)) {
96 if (count < checkParamsMap.size() - 1) {
97 errMsg.append(" and ");
103 errMsg.append(" can't be null or \"\"!\n ");
105 return errMsg.toString();
108 public static String checkRequiredJsonParam(String jsonParam, String key) {
109 StringBuilder errMsg = new StringBuilder();
111 ObjectMapper mapper = new ObjectMapper();
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)) {
122 "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
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();