summaryrefslogtreecommitdiffstats
path: root/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java
blob: 1205ce705ac58b7a32b9b4d09b0be60eeca6003e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.log.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.jackson.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Facitility {
  private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName());

  private Facitility() {

  }

  public static String oToJ(Object o) {
    ObjectMapper om = new ObjectMapper();
    Writer w = new StringWriter();
    String json = null;
    try {

      om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"));
      om.writeValue(w, o);
      json = w.toString();
      w.close();
    } catch (IOException e) {
      LOGGER.error("IOException" + e);
    }
    return json;
  }

  public static Map<String, String> readJson2Map(String json) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
      Map<String, String> maps = objectMapper.readValue(json, Map.class);
      return maps;
    } catch (Exception e) {
      LOGGER.error("IOException" + e);
      return null;
    }
  }

  public static String hashMapToJson(HashMap<String, String> map) {
    String string = "{";
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
      Entry e = (Entry) it.next();
      string += "\"" + e.getKey() + "\":";
      string += "\"" + e.getValue() + "\",";
    }
    string = string.substring(0, string.lastIndexOf(","));
    string += "}";
    return string;
  }

  public static String dateFormat(Date date) {
    SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    return time.format(date);
  }

  public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) {
    StringBuilder errMsg = new StringBuilder();
    java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator();
    int count = 0;
    while (hashMapIt.hasNext()) {
      String key = hashMapIt.next();
      Object value = checkParamsMap.get(key);
      if (value == null || "".equals(value)) {
        errMsg.append(key);
        count++;
        if (count < checkParamsMap.size() - 1) {
          errMsg.append(" and ");
        }
      }

    }
    if (count > 0) {
      errMsg.append(" can't be null or \"\"!\n ");
    }
    return errMsg.toString();
  }

  public static String checkRequiredJsonParam(String jsonParam, String key) {
    StringBuilder errMsg = new StringBuilder();
    try {
      ObjectMapper mapper = new ObjectMapper();
      JsonNode jsonValue;


      jsonValue = mapper.readTree(jsonParam.toString());
      Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
      while (elements.hasNext()) {
        Entry<String, JsonNode> node = elements.next();
        String childValue = node.getValue().asText();
        if (childValue == null || "".equals(childValue)) {
          errMsg.append(
              "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
          break;
        }
      }

      return errMsg.toString();
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      LOGGER.error("JsonProcessingException" + e);
      return errMsg.toString();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      LOGGER.error("IOException" + e);
      return errMsg.toString();
    }
  }
}