workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / baseservice-i18n / src / main / java / org / openo / baseservice / i18n / ErrorCodeI18n.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.baseservice.i18n;
17
18 import org.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorCodeLevelUtil;
19 import org.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorItem2;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.util.Locale;
24 import java.util.Map;
25 import java.util.Optional;
26
27 public interface ErrorCodeI18n {
28
29   static final Logger logger = LoggerFactory.getLogger(ErrorCodeI18n.class);
30
31   /**
32    * Access to the internationalization of the error code examples, scanning the process of all
33    * classes load path, loading all the *-errorcode-*.json files, and load all the international
34    * language information
35    * 
36    * @return
37    */
38   static ErrorCodeI18n getInstance() {
39     return DefaultErrorCodeI18n.getInstance();
40   }
41
42   /**
43    * Gets the corresponding error item based on the error code (including the error description
44    * information for all languages)
45    *
46    * @param errorCode
47    * @return Optional<ErrorItem>
48    */
49   public Optional<ErrorItem> getErrorItem(int errorCode);
50
51   public static interface ErrorItem {
52
53     public int getErrorCode();
54
55     public int getLevel();
56
57     public Map<String, String> getLabels();
58
59     public String getLabel(Locale theLocale);
60
61     /**
62      * All language error information description of assembly. <br>
63      * 
64      * some modules to store all the error information description or transfer, and finally the time
65      * to choose the appropriate value according to the locale presentation. <br>
66      * 
67      * it is not necessary to pass the error code (code), but in order to take into account the
68      * subsequent scalability, so also passed, the general module does not need this information.
69      * Similar form:<br>
70      * { "code":53501, "level":"INFO", "errlabels":{"zh_CN":"拓扑定制文件无效。","en_US":"The topology
71      * customized file is invalid.","ru_RU":"Топология настроенный файл недействительно."} }
72      * 
73      * @param errorCode
74      * @return
75      */
76     public String getCanonicalLabels(int errorCode);
77
78     /**
79      * With the use of the above interface, it is possible to obtain an international string
80      * corresponding to a specific language from the combination of all error messages
81      * 
82      * @param labels Error message description string (Return value of the getCanonicalLabels
83      *        method)
84      * @param theLocale
85      * @return
86      */
87     public static String getLabelFromCanonicalLabels(String labels, Locale theLocale) {
88       if (labels == null || theLocale == null) {
89         return null;
90       }
91       try {
92         ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
93         Map<String, String> errlabels = errorItem2.getErrlabels();
94         return errlabels.get(I18nLocaleTransfer.transfer(theLocale, errlabels.keySet()));
95       } catch (Exception e) {
96         logger.info(
97             "getLabelFromCanonicalLabels failed from " + labels + " with local " + theLocale, e);
98         return null;
99       }
100     }
101
102     /**
103      * With the use of the above interface, we can get the error code corresponding to the specific
104      * language from all the error information
105      * 
106      * @param labels Error message description string (Return value of the getCanonicalLabels
107      *        method)
108      * @param theLocale
109      * @return errorCode
110      */
111     public static int getErrorcodeFromCanonicalLabels(String labels, Locale theLocale) {
112       if (labels == null) {
113         return -1;
114       }
115       try {
116         ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
117         return errorItem2.getErrorCode();
118       } catch (Exception e) {
119         logger.info(
120             "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,
121             e);
122         return -1;
123       }
124     }
125
126     /**
127      * With the above interface, the error level of the corresponding string is obtained from all
128      * the error messages
129      *
130      * @param labels Error message description string (Return value of the getCanonicalLabels
131      *        method)
132      * @param theLocale
133      * @return error level
134      */
135     public static int getLevelFromCanonicalLabels(String labels, Locale theLocale) {
136       if (labels == null) {
137         return -1;
138       }
139       try {
140         ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
141         return ErrorCodeLevelUtil.transfer2Int(errorItem2.getLevel());
142       } catch (Exception e) {
143         logger.info(
144             "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,
145             e);
146         return -1;
147       }
148     }
149   }
150
151 }