1717import com .google .common .collect .Lists ;
1818import com .google .common .collect .Sets ;
1919import com .google .visualization .datasource .base .InvalidQueryException ;
20+ import com .google .visualization .datasource .base .MessagesEnum ;
21+
22+ import com .ibm .icu .util .ULocale ;
2023
2124import org .apache .commons .lang .text .StrBuilder ;
2225import org .apache .commons .logging .Log ;
@@ -61,17 +64,19 @@ public class Query {
6164 *
6265 * @param selectionColumns The list to check for duplicates.
6366 * @param clauseName The clause name to report in the exception message.
67+ * @param userLocale The user locale.
6468 *
6569 * @throws InvalidQueryException Thrown if a duplicate was found.
6670 */
6771 private static <T > void checkForDuplicates (List <T >
68- selectionColumns , String clauseName ) throws InvalidQueryException {
72+ selectionColumns , String clauseName , ULocale userLocale ) throws InvalidQueryException {
6973 for (int i = 0 ; i < selectionColumns .size (); i ++) {
7074 T col = selectionColumns .get (i );
7175 for (int j = i + 1 ; j < selectionColumns .size (); j ++) {
7276 if (col .equals (selectionColumns .get (j ))) {
73- String messageToLogAndUser = "Column [" + col .toString () + "] "
74- + "cannot appear more than once in " + clauseName + "." ;
77+ String [] args = {col .toString (), clauseName };
78+ String messageToLogAndUser = MessagesEnum .COLUMN_ONLY_ONCE .getMessageWithArgs (userLocale ,
79+ args );
7580 log .error (messageToLogAndUser );
7681 throw new InvalidQueryException (messageToLogAndUser );
7782 }
@@ -140,6 +145,11 @@ private static<T> void checkForDuplicates(List<T>
140145 * Column formatting patterns as specified in the query.
141146 */
142147 private QueryFormat userFormatOptions = null ;
148+
149+ /**
150+ * The user locale, Used to create localized messages.
151+ */
152+ private ULocale localeForUserMessages = null ;
143153
144154 /**
145155 * Constructs a new, empty, query.
@@ -372,7 +382,8 @@ public int getRowOffset() {
372382 */
373383 public void setRowOffset (int rowOffset ) throws InvalidQueryException {
374384 if (rowOffset < 0 ) {
375- String messageToLogAndUser = "Invalid value for row offset: " + rowOffset ;
385+ String messageToLogAndUser = MessagesEnum .INVALID_OFFSET .getMessageWithArgs (
386+ localeForUserMessages , Integer .toString (rowOffset ));
376387 log .error (messageToLogAndUser );
377388 throw new InvalidQueryException (messageToLogAndUser );
378389 }
@@ -490,6 +501,15 @@ public boolean isEmpty() {
490501 && !hasRowLimit () && !hasRowOffset () && !hasUserFormatOptions () && !hasLabels ()
491502 && !hasOptions ());
492503 }
504+
505+
506+ /**
507+ * Sets the user locale for creating localized messages.
508+ * @param userLocale the user locale.
509+ */
510+ public void setLocaleForUserMessages (ULocale localeForUserMessges ) {
511+ this .localeForUserMessages = localeForUserMessges ;
512+ }
493513
494514 /**
495515 * Copies all information from the given query to this query.
@@ -557,17 +577,17 @@ public void validate() throws InvalidQueryException {
557577 : Lists .<AggregationColumn >newArrayList ();
558578
559579 // Check for duplicates.
560- checkForDuplicates (selectionColumns , "SELECT" );
561- checkForDuplicates (sortColumns , "ORDER BY" );
562- checkForDuplicates (groupColumnIds , "GROUP BY" );
563- checkForDuplicates (pivotColumnIds , "PIVOT" );
580+ checkForDuplicates (selectionColumns , "SELECT" , localeForUserMessages );
581+ checkForDuplicates (sortColumns , "ORDER BY" , localeForUserMessages );
582+ checkForDuplicates (groupColumnIds , "GROUP BY" , localeForUserMessages );
583+ checkForDuplicates (pivotColumnIds , "PIVOT" , localeForUserMessages );
564584
565585 // Cannot have aggregations in either group by, pivot, or where.
566586 if (hasGroup ()) {
567587 for (AbstractColumn column : group .getColumns ()) {
568588 if (!column .getAllAggregationColumns ().isEmpty ()) {
569- String messageToLogAndUser = "Column [" + column . toQueryString () + "] connot be in"
570- + " GROUP BY because it has an aggregation." ;
589+ String messageToLogAndUser = MessagesEnum . CANNOT_BE_IN_GROUP_BY . getMessageWithArgs (
590+ localeForUserMessages , column . toQueryString ()) ;
571591 log .error (messageToLogAndUser );
572592 throw new InvalidQueryException (messageToLogAndUser );
573593 }
@@ -576,8 +596,8 @@ public void validate() throws InvalidQueryException {
576596 if (hasPivot ()) {
577597 for (AbstractColumn column : pivot .getColumns ()) {
578598 if (!column .getAllAggregationColumns ().isEmpty ()) {
579- String messageToLogAndUser = "Column [" + column . toQueryString () + "] connot be in"
580- + " PIVOT because it has an aggregation." ;
599+ String messageToLogAndUser = MessagesEnum . CANNOT_BE_IN_PIVOT . getMessageWithArgs (
600+ localeForUserMessages , column . toQueryString ()) ;
581601 log .error (messageToLogAndUser );
582602 throw new InvalidQueryException (messageToLogAndUser );
583603 }
@@ -586,8 +606,8 @@ public void validate() throws InvalidQueryException {
586606 if (hasFilter ()) {
587607 List <AggregationColumn > filterAggregations = filter .getAggregationColumns ();
588608 if (!filterAggregations .isEmpty ()) {
589- String messageToLogAndUser = "Column [" + filterAggregations . get ( 0 ). toQueryString () + "] "
590- + " cannot appear in WHERE because it has an aggregation." ;
609+ String messageToLogAndUser = MessagesEnum . CANNOT_BE_IN_WHERE . getMessageWithArgs (
610+ localeForUserMessages , filterAggregations . get ( 0 ). toQueryString ()); ;
591611 log .error (messageToLogAndUser );
592612 throw new InvalidQueryException (messageToLogAndUser );
593613 }
@@ -599,8 +619,8 @@ public void validate() throws InvalidQueryException {
599619 String id = column1 .getColumnId ();
600620 for (AggregationColumn column2 : selectionAggregated ) {
601621 if (id .equals (column2 .getAggregatedColumn ().getId ())) {
602- String messageToLogAndUser = "Column [" + id + "] cannot be "
603- + "selected both with and without aggregation in SELECT." ;
622+ String messageToLogAndUser = MessagesEnum . SELECT_WITH_AND_WITHOUT_AGG . getMessageWithArgs (
623+ localeForUserMessages , id ) ;
604624 log .error (messageToLogAndUser );
605625 throw new InvalidQueryException (messageToLogAndUser );
606626 }
@@ -621,8 +641,8 @@ public void validate() throws InvalidQueryException {
621641 for (AggregationColumn column : selectionAggregated ) {
622642 String id = column .getAggregatedColumn ().getId ();
623643 if (groupColumnIds .contains (id )) {
624- String messageToLogAndUser = "Column [" + id + "] which is "
625- + "aggregated in SELECT, cannot appear in GROUP BY." ;
644+ String messageToLogAndUser = MessagesEnum . COL_AGG_NOT_IN_SELECT . getMessageWithArgs (
645+ localeForUserMessages , id ) ;
626646 log .error (messageToLogAndUser );
627647 throw new InvalidQueryException (messageToLogAndUser );
628648 }
@@ -632,14 +652,14 @@ public void validate() throws InvalidQueryException {
632652 // Cannot use grouping or pivoting when no aggregations are defined in the
633653 // selection.
634654 if (hasGroup () && selectionAggregated .isEmpty ()) {
635- String messageToLogAndUser = "Cannot use GROUP BY when no "
636- + "aggregations are defined in SELECT." ;
655+ String messageToLogAndUser = MessagesEnum . CANNOT_GROUP_WITNOUT_AGG . getMessage (
656+ localeForUserMessages ) ;
637657 log .error (messageToLogAndUser );
638658 throw new InvalidQueryException (messageToLogAndUser );
639659 }
640660 if (hasPivot () && selectionAggregated .isEmpty ()) {
641- String messageToLogAndUser = "Cannot use PIVOT when no "
642- + "aggregations are defined in SELECT." ;
661+ String messageToLogAndUser = MessagesEnum . CANNOT_PIVOT_WITNOUT_AGG . getMessage (
662+ localeForUserMessages ) ;
643663 log .error (messageToLogAndUser );
644664 throw new InvalidQueryException (messageToLogAndUser );
645665 }
@@ -648,9 +668,8 @@ public void validate() throws InvalidQueryException {
648668 // are defined.
649669 if (hasSort () && !selectionAggregated .isEmpty ()) {
650670 for (AbstractColumn column : sort .getColumns ()) {
651- String messageToLogAndUser = "Column [" + column .toQueryString () + "] which "
652- + "appears in ORDER BY, must be in SELECT as well, because SELECT"
653- + " contains aggregated columns." ;
671+ String messageToLogAndUser = MessagesEnum .COL_IN_ORDER_MUST_BE_IN_SELECT .getMessageWithArgs (
672+ localeForUserMessages , column .toQueryString ());
654673 checkColumnInList (selection .getColumns (), column ,
655674 messageToLogAndUser );
656675 }
@@ -661,8 +680,8 @@ public void validate() throws InvalidQueryException {
661680 for (AggregationColumn column : selectionAggregated ) {
662681 String id = column .getAggregatedColumn ().getId ();
663682 if (pivotColumnIds .contains (id )) {
664- String messageToLogAndUser = "Column [" + id + "] which is "
665- + "aggregated in SELECT, cannot appear in PIVOT." ;
683+ String messageToLogAndUser = MessagesEnum . AGG_IN_SELECT_NO_PIVOT . getMessageWithArgs (
684+ localeForUserMessages , id ) ;
666685 log .error (messageToLogAndUser );
667686 throw new InvalidQueryException (messageToLogAndUser );
668687 }
@@ -673,8 +692,8 @@ public void validate() throws InvalidQueryException {
673692 if (hasGroup () && hasPivot ()) {
674693 for (String id : groupColumnIds ) {
675694 if (pivotColumnIds .contains (id )) {
676- String messageToLogAndUser = "Column [" + id + "] cannot appear" +
677- " both in GROUP BY and in PIVOT." ;
695+ String messageToLogAndUser = MessagesEnum . NO_COL_IN_GROUP_AND_PIVOT . getMessageWithArgs (
696+ localeForUserMessages , id ) ;
678697 log .error (messageToLogAndUser );
679698 throw new InvalidQueryException (messageToLogAndUser );
680699 }
@@ -684,18 +703,17 @@ public void validate() throws InvalidQueryException {
684703 // Cannot order by aggregation column when pivoting is used.
685704 if (hasPivot () && !sortAggregated .isEmpty ()) {
686705 AggregationColumn column = sortAggregated .get (0 );
687- String messageToLogAndUser = "Column [" +
688- column .getAggregatedColumn ().getId () + "] " + "cannot be aggregated "
689- + "in ORDER BY when PIVOT is used." ;
706+ String messageToLogAndUser = MessagesEnum .NO_AGG_IN_ORDER_WHEN_PIVOT .getMessageWithArgs (
707+ localeForUserMessages , column .getAggregatedColumn ().getId ());
690708 log .error (messageToLogAndUser );
691709 throw new InvalidQueryException (messageToLogAndUser );
692710 }
693711
694712 // Cannot order by aggregation columns that weren't defined in the
695713 // selection.
696714 for (AggregationColumn column : sortAggregated ) {
697- String messageToLogAndUser = "Aggregation [" + column . toQueryString () + "] "
698- + "found in ORDER BY but was not found in SELECT" ;
715+ String messageToLogAndUser = MessagesEnum . AGG_IN_ORDER_NOT_IN_SELECT . getMessageWithArgs (
716+ localeForUserMessages , column . toQueryString ()) ;
699717 checkColumnInList (selectionAggregated , column , messageToLogAndUser );
700718 }
701719
@@ -707,16 +725,16 @@ public void validate() throws InvalidQueryException {
707725 if (hasSelection ()) {
708726 for (AbstractColumn col : labelColumns ) {
709727 if (!selectionColumns .contains (col )) {
710- String messageToLogAndUser = "Column [" + col . toQueryString () + "] which"
711- + " is referenced in LABEL, is not part of SELECT clause." ;
728+ String messageToLogAndUser = MessagesEnum . LABEL_COL_NOT_IN_SELECT . getMessageWithArgs (
729+ localeForUserMessages , col . toQueryString ()) ;
712730 log .error (messageToLogAndUser );
713731 throw new InvalidQueryException (messageToLogAndUser );
714732 }
715733 }
716734 for (AbstractColumn col : formatColumns ) {
717735 if (!selectionColumns .contains (col )) {
718- String messageToLogAndUser = "Column [" + col . toQueryString () + "] which"
719- + " is referenced in FORMAT, is not part of SELECT clause." ;
736+ String messageToLogAndUser = MessagesEnum . FORMAT_COL_NOT_IN_SELECT . getMessageWithArgs (
737+ localeForUserMessages , col . toQueryString ()) ;
720738 log .error (messageToLogAndUser );
721739 throw new InvalidQueryException (messageToLogAndUser );
722740 }
@@ -878,9 +896,8 @@ private void checkSelectedColumnWithGrouping(
878896 throws InvalidQueryException {
879897 if (col instanceof SimpleColumn ) {
880898 if (!groupColumns .contains (col )) {
881- String messageToLogAndUser = "Column [" + col .getId ()
882- + "] should be added to GROUP BY, removed from SELECT, or "
883- + "aggregated in SELECT." ;
899+ String messageToLogAndUser = MessagesEnum .ADD_COL_TO_GROUP_BY_OR_AGG .getMessageWithArgs (
900+ localeForUserMessages , col .getId ());
884901 log .error (messageToLogAndUser );
885902 throw new InvalidQueryException (messageToLogAndUser );
886903 }
0 commit comments