summaryrefslogtreecommitdiffstats
path: root/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/ErrorCodeI18n.java
blob: 39738b59b10336159ad4ff3dfb0e245add502f65 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * 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.baseservice.i18n;

import org.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorCodeLevelUtil;
import org.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorItem2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;
import java.util.Map;
import java.util.Optional;

public interface ErrorCodeI18n {

  static final Logger logger = LoggerFactory.getLogger(ErrorCodeI18n.class);

  /**
   * Access to the internationalization of the error code examples, scanning the process of all
   * classes load path, loading all the *-errorcode-*.json files, and load all the international
   * language information
   * 
   * @return
   */
  static ErrorCodeI18n getInstance() {
    return DefaultErrorCodeI18n.getInstance();
  }

  /**
   * Gets the corresponding error item based on the error code (including the error description
   * information for all languages)
   *
   * @param errorCode
   * @return Optional<ErrorItem>
   */
  public Optional<ErrorItem> getErrorItem(int errorCode);

  public static interface ErrorItem {

    public int getErrorCode();

    public int getLevel();

    public Map<String, String> getLabels();

    public String getLabel(Locale theLocale);

    /**
     * All language error information description of assembly. <br>
     * 
     * some modules to store all the error information description or transfer, and finally the time
     * to choose the appropriate value according to the locale presentation. <br>
     * 
     * it is not necessary to pass the error code (code), but in order to take into account the
     * subsequent scalability, so also passed, the general module does not need this information.
     * Similar form:<br>
     * { "code":53501, "level":"INFO", "errlabels":{"zh_CN":"拓扑定制文件无效。","en_US":"The topology
     * customized file is invalid.","ru_RU":"Топология настроенный файл недействительно."} }
     * 
     * @param errorCode
     * @return
     */
    public String getCanonicalLabels(int errorCode);

    /**
     * With the use of the above interface, it is possible to obtain an international string
     * corresponding to a specific language from the combination of all error messages
     * 
     * @param labels Error message description string (Return value of the getCanonicalLabels
     *        method)
     * @param theLocale
     * @return
     */
    public static String getLabelFromCanonicalLabels(String labels, Locale theLocale) {
      if (labels == null || theLocale == null) {
        return null;
      }
      try {
        ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
        Map<String, String> errlabels = errorItem2.getErrlabels();
        return errlabels.get(I18nLocaleTransfer.transfer(theLocale, errlabels.keySet()));
      } catch (Exception e) {
        logger.info(
            "getLabelFromCanonicalLabels failed from " + labels + " with local " + theLocale, e);
        return null;
      }
    }

    /**
     * With the use of the above interface, we can get the error code corresponding to the specific
     * language from all the error information
     * 
     * @param labels Error message description string (Return value of the getCanonicalLabels
     *        method)
     * @param theLocale
     * @return errorCode
     */
    public static int getErrorcodeFromCanonicalLabels(String labels, Locale theLocale) {
      if (labels == null) {
        return -1;
      }
      try {
        ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
        return errorItem2.getErrorCode();
      } catch (Exception e) {
        logger.info(
            "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,
            e);
        return -1;
      }
    }

    /**
     * With the above interface, the error level of the corresponding string is obtained from all
     * the error messages
     *
     * @param labels Error message description string (Return value of the getCanonicalLabels
     *        method)
     * @param theLocale
     * @return error level
     */
    public static int getLevelFromCanonicalLabels(String labels, Locale theLocale) {
      if (labels == null) {
        return -1;
      }
      try {
        ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
        return ErrorCodeLevelUtil.transfer2Int(errorItem2.getLevel());
      } catch (Exception e) {
        logger.info(
            "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,
            e);
        return -1;
      }
    }
  }

}