|
| 1 | +// Copyright 2009 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.visualization.datasource.base; |
| 16 | + |
| 17 | +import com.google.common.collect.Maps; |
| 18 | + |
| 19 | +import com.ibm.icu.util.ULocale; |
| 20 | + |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * An enum for messages used in the DataSource library. Each element references a message |
| 26 | + * in {@code ErrorMessages}. For example all the error messages produced in query parsing are |
| 27 | + * listed here. |
| 28 | + * |
| 29 | + * @author Hillel M. |
| 30 | + */ |
| 31 | +public enum MessagesEnum { |
| 32 | + |
| 33 | + /** |
| 34 | + * Column in query is missing from the data table. |
| 35 | + * @param column id. |
| 36 | + */ |
| 37 | + NO_COLUMN, |
| 38 | + |
| 39 | + /** |
| 40 | + * Average and sum aggregation can be applied only on numeric columns. |
| 41 | + */ |
| 42 | + AVG_SUM_ONLY_NUMERIC, |
| 43 | + |
| 44 | + /** |
| 45 | + * Invalid aggregation type. |
| 46 | + * @param aggregation type. |
| 47 | + */ |
| 48 | + INVALID_AGG_TYPE, |
| 49 | + |
| 50 | + /** |
| 51 | + * An error when parsing the query. |
| 52 | + * @param detailed message. |
| 53 | + */ |
| 54 | + PARSE_ERROR, |
| 55 | + |
| 56 | + /** |
| 57 | + * Column can not be in group by because it has an aggregation. |
| 58 | + * @param column id |
| 59 | + */ |
| 60 | + CANNOT_BE_IN_GROUP_BY, |
| 61 | + |
| 62 | + /** |
| 63 | + * Column can not be in pivot because it has an aggregation. |
| 64 | + * @param column id |
| 65 | + */ |
| 66 | + CANNOT_BE_IN_PIVOT, |
| 67 | + |
| 68 | + /** |
| 69 | + * Column can not be in where because it has an aggregation. |
| 70 | + * @param column id |
| 71 | + */ |
| 72 | + CANNOT_BE_IN_WHERE, |
| 73 | + |
| 74 | + /** |
| 75 | + * Column can not appear in select with and without aggregation. |
| 76 | + * @param column id. |
| 77 | + */ |
| 78 | + SELECT_WITH_AND_WITHOUT_AGG, |
| 79 | + |
| 80 | + /** |
| 81 | + * Column which is aggregated in SELECT, cannot appear in GROUP BY. |
| 82 | + * @param column id |
| 83 | + */ |
| 84 | + COL_AGG_NOT_IN_SELECT, |
| 85 | + |
| 86 | + /** |
| 87 | + * Cannot use GROUP BY when no aggregations are defined in SELECT. |
| 88 | + */ |
| 89 | + CANNOT_GROUP_WITNOUT_AGG, |
| 90 | + |
| 91 | + /** |
| 92 | + * Cannot use PIVOT when no aggregations are defined in SELECT. |
| 93 | + */ |
| 94 | + CANNOT_PIVOT_WITNOUT_AGG, |
| 95 | + |
| 96 | + /** |
| 97 | + * Column which is aggregated in SELECT, cannot appear in PIVOT. |
| 98 | + * @param column id |
| 99 | + */ |
| 100 | + AGG_IN_SELECT_NO_PIVOT, |
| 101 | + |
| 102 | + /** |
| 103 | + * Column which is referenced in FORMAT, is not part of SELECT clause. |
| 104 | + * @param column id |
| 105 | + */ |
| 106 | + FORMAT_COL_NOT_IN_SELECT, |
| 107 | + |
| 108 | + /** |
| 109 | + * Column which is referenced in LABEL, is not part of SELECT clause. |
| 110 | + * @param column id |
| 111 | + */ |
| 112 | + LABEL_COL_NOT_IN_SELECT, |
| 113 | + |
| 114 | + /** |
| 115 | + * Column should be added to GROUP BY, removed from SELECT, or aggregated in SELECT. |
| 116 | + * @param column id |
| 117 | + */ |
| 118 | + ADD_COL_TO_GROUP_BY_OR_AGG, |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Aggregation found in ORDER BY but was not found in SELECT. |
| 123 | + * @param aggreation column |
| 124 | + */ |
| 125 | + AGG_IN_ORDER_NOT_IN_SELECT, |
| 126 | + |
| 127 | + /** |
| 128 | + * Column cannot be aggregated in ORDER BY when PIVOT is used. |
| 129 | + * @param aggregation column |
| 130 | + */ |
| 131 | + NO_AGG_IN_ORDER_WHEN_PIVOT, |
| 132 | + |
| 133 | + /** |
| 134 | + * Column which appears in ORDER BY, must be in SELECT as well, because SELECT |
| 135 | + * contains aggregated columns. |
| 136 | + */ |
| 137 | + COL_IN_ORDER_MUST_BE_IN_SELECT, |
| 138 | + |
| 139 | + /** |
| 140 | + * Column cannot appear both in GROUP BY and in PIVOT. |
| 141 | + * @param column id |
| 142 | + */ |
| 143 | + NO_COL_IN_GROUP_AND_PIVOT, |
| 144 | + |
| 145 | + /** |
| 146 | + * Invalid value for row offset. |
| 147 | + * @param value |
| 148 | + */ |
| 149 | + INVALID_OFFSET, |
| 150 | + |
| 151 | + /** |
| 152 | + * Column cannot appear more than once. |
| 153 | + * @param column id |
| 154 | + * @param clause |
| 155 | + */ |
| 156 | + COLUMN_ONLY_ONCE; |
| 157 | + |
| 158 | + /** |
| 159 | + * A mapping from reason type to message. |
| 160 | + */ |
| 161 | + private static final Map<MessagesEnum, String> |
| 162 | + QUERY_ERROR_TO_MESSAGE = Maps.newEnumMap(MessagesEnum.class); |
| 163 | + |
| 164 | + static { |
| 165 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.NO_COLUMN, |
| 166 | + "NO_COLUMN"); |
| 167 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.AVG_SUM_ONLY_NUMERIC, |
| 168 | + "AVG_SUM_ONLY_NUMERIC"); |
| 169 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.INVALID_AGG_TYPE, |
| 170 | + "INVALID_AGG_TYPE"); |
| 171 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.PARSE_ERROR, |
| 172 | + "PARSE_ERROR"); |
| 173 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.CANNOT_BE_IN_GROUP_BY, |
| 174 | + "CANNOT_BE_IN_GROUP_BY"); |
| 175 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.CANNOT_BE_IN_PIVOT, |
| 176 | + "CANNOT_BE_IN_PIVOT"); |
| 177 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.CANNOT_BE_IN_WHERE, |
| 178 | + "CANNOT_BE_IN_WHERE"); |
| 179 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.SELECT_WITH_AND_WITHOUT_AGG, |
| 180 | + "SELECT_WITH_AND_WITHOUT_AGG"); |
| 181 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.COL_AGG_NOT_IN_SELECT, |
| 182 | + "COL_AGG_NOT_IN_SELECT"); |
| 183 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.CANNOT_GROUP_WITNOUT_AGG, |
| 184 | + "CANNOT_GROUP_WITNOUT_AGG"); |
| 185 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.CANNOT_PIVOT_WITNOUT_AGG, |
| 186 | + "CANNOT_PIVOT_WITNOUT_AGG"); |
| 187 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.AGG_IN_SELECT_NO_PIVOT, |
| 188 | + "AGG_IN_SELECT_NO_PIVOT"); |
| 189 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.FORMAT_COL_NOT_IN_SELECT, |
| 190 | + "FORMAT_COL_NOT_IN_SELECT"); |
| 191 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.LABEL_COL_NOT_IN_SELECT, |
| 192 | + "LABEL_COL_NOT_IN_SELECT"); |
| 193 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.ADD_COL_TO_GROUP_BY_OR_AGG, |
| 194 | + "ADD_COL_TO_GROUP_BY_OR_AGG"); |
| 195 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.AGG_IN_ORDER_NOT_IN_SELECT, |
| 196 | + "AGG_IN_ORDER_NOT_IN_SELECT"); |
| 197 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.NO_AGG_IN_ORDER_WHEN_PIVOT, |
| 198 | + "NO_AGG_IN_ORDER_WHEN_PIVOT"); |
| 199 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.COL_IN_ORDER_MUST_BE_IN_SELECT, |
| 200 | + "COL_IN_ORDER_MUST_BE_IN_SELECT"); |
| 201 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.NO_COL_IN_GROUP_AND_PIVOT, |
| 202 | + "NO_COL_IN_GROUP_AND_PIVOT"); |
| 203 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.INVALID_OFFSET, |
| 204 | + "INVALID_OFFSET"); |
| 205 | + QUERY_ERROR_TO_MESSAGE.put(MessagesEnum.COLUMN_ONLY_ONCE, |
| 206 | + "COLUMN_ONLY_ONCE"); |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + /** |
| 211 | + * Returns a localized message for this reason type and locale. |
| 212 | + * |
| 213 | + * @param locale The locale. |
| 214 | + * @param args An array of arguments. |
| 215 | + * |
| 216 | + * @return A localized message given a reason type and locale. |
| 217 | + */ |
| 218 | + public String getMessageWithArgs(ULocale ulocale, String... args) { |
| 219 | + Locale locale = ulocale != null ? ulocale.toLocale() : null; |
| 220 | + return LocaleUtil.getLocalizedMessageFromBundleWithArguments( |
| 221 | + "com.google.visualization.datasource.base.ErrorMessages", QUERY_ERROR_TO_MESSAGE.get(this), |
| 222 | + args, locale); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Returns a localized message for this reason type and locale. |
| 227 | + * |
| 228 | + * @param locale The locale. |
| 229 | + * |
| 230 | + * @return A localized message given a reason type and locale. |
| 231 | + */ |
| 232 | + public String getMessage(ULocale ulocale) { |
| 233 | + Locale locale = ulocale != null ? ulocale.toLocale() : null; |
| 234 | + return LocaleUtil.getLocalizedMessageFromBundle( |
| 235 | + "com.google.visualization.datasource.base.ErrorMessages", QUERY_ERROR_TO_MESSAGE.get(this), |
| 236 | + locale); |
| 237 | + } |
| 238 | +} |
0 commit comments