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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.fasterxml.jackson.databind.ObjectMapper;
33 public class Facitility {
34 private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName());
36 private Facitility() {
40 public static String oToJ(Object o) {
41 ObjectMapper om = new ObjectMapper();
42 Writer w = new StringWriter();
46 om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
50 } catch (IOException e) {
51 LOGGER.error("IOException", e);
56 public static Map<String, String> readJson2Map(String json) {
57 ObjectMapper objectMapper = new ObjectMapper();
59 Map<String, String> maps = objectMapper.readValue(json, Map.class);
61 } catch (Exception e) {
62 LOGGER.error("IOException", e);
67 public static String hashMapToJson(HashMap<String, String> map) {
69 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
70 Entry e = (Entry) it.next();
71 string += "\"" + e.getKey() + "\":";
72 string += "\"" + e.getValue() + "\",";
74 string = string.substring(0, string.lastIndexOf(","));
79 public static String dateFormat(Date date) {
80 SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
81 return time.format(date);
84 public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) {
85 StringBuilder errMsg = new StringBuilder();
86 java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator();
88 while (hashMapIt.hasNext()) {
89 String key = hashMapIt.next();
90 Object value = checkParamsMap.get(key);
91 if (value == null || "".equals(value)) {
94 if (count < checkParamsMap.size() - 1) {
95 errMsg.append(" and ");
101 errMsg.append(" can't be null or \"\"!\n ");
103 return errMsg.toString();
106 public static String checkRequiredJsonParam(String jsonParam, String key) {
108 // StringBuilder errMsg = new StringBuilder();
110 // ObjectMapper mapper = new ObjectMapper();
111 // JsonNode jsonValue;
114 // jsonValue = mapper.readTree(jsonParam.toString());
115 // Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
116 // while (elements.hasNext()) {
117 // Entry<String, JsonNode> node = elements.next();
118 // String childValue = node.getValue().asText();
119 // if (childValue == null || "".equals(childValue)) {
121 // "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
126 // return errMsg.toString();
127 // } catch (JsonProcessingException e) {
128 // // TODO Auto-generated catch block
129 // LOGGER.error("JsonProcessingException" , e);
130 // return errMsg.toString();
131 // } catch (IOException e) {
132 // // TODO Auto-generated catch block
133 // LOGGER.error("IOException" , e);
134 // return errMsg.toString();