1a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a1b4a26bSAndrew Rist  * distributed with this work for additional information
6a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a1b4a26bSAndrew Rist  *
11a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a1b4a26bSAndrew Rist  *
13a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18a1b4a26bSAndrew Rist  * under the License.
19a1b4a26bSAndrew Rist  *
20a1b4a26bSAndrew Rist  *************************************************************/
21a1b4a26bSAndrew Rist 
22a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.query;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.wizards.common.JavaTools;
28cdf0e10cSrcweir import com.sun.star.wizards.common.PropertyNames;
29cdf0e10cSrcweir import com.sun.star.wizards.common.Resource;
30cdf0e10cSrcweir import com.sun.star.wizards.db.FieldColumn;
31cdf0e10cSrcweir import com.sun.star.wizards.db.QueryMetaData;
32cdf0e10cSrcweir import com.sun.star.wizards.ui.FilterComponent;
33cdf0e10cSrcweir 
34*49029961SDamjan Jovanovic import static com.sun.star.wizards.ui.UIConsts.RID_QUERY;
35*49029961SDamjan Jovanovic 
36cdf0e10cSrcweir public class QuerySummary extends QueryMetaData
37cdf0e10cSrcweir {
38cdf0e10cSrcweir 
39cdf0e10cSrcweir     String sSummary;
40cdf0e10cSrcweir     Resource oResource;
41cdf0e10cSrcweir     // XMultiServiceFactory xMSF;
42cdf0e10cSrcweir     private String sSeparator;
43cdf0e10cSrcweir     private String sReturnChar;
44cdf0e10cSrcweir     private String sAnd;
45cdf0e10cSrcweir     private String sOr;
46cdf0e10cSrcweir 
QuerySummary(XMultiServiceFactory _xMSF, Resource _oResource)47cdf0e10cSrcweir     public QuerySummary(XMultiServiceFactory _xMSF, Resource _oResource)
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         super(_xMSF);
50cdf0e10cSrcweir         this.oResource = _oResource;
51cdf0e10cSrcweir         // this.xMSF = _xMSF;
52cdf0e10cSrcweir         sAnd = oResource.getResText(RID_QUERY + 38);
53cdf0e10cSrcweir         sOr = oResource.getResText(RID_QUERY + 39);
54cdf0e10cSrcweir         sSeparator = oResource.getResText(RID_QUERY + 91);
55cdf0e10cSrcweir         sReturnChar = String.valueOf((char) 13) + String.valueOf((char) 13);
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
setSummaryString()58cdf0e10cSrcweir     public void setSummaryString()
59cdf0e10cSrcweir     {
60cdf0e10cSrcweir         try
61cdf0e10cSrcweir         {
62cdf0e10cSrcweir             String sFieldNamesFraction = PropertyNames.EMPTY_STRING;
63cdf0e10cSrcweir             String sSortingFraction = PropertyNames.EMPTY_STRING;
64cdf0e10cSrcweir             String sFilterFraction = PropertyNames.EMPTY_STRING;
65cdf0e10cSrcweir             String sAggregateFraction = PropertyNames.EMPTY_STRING;
66cdf0e10cSrcweir             String sGroupByFraction = PropertyNames.EMPTY_STRING;
67cdf0e10cSrcweir             String sHavingFraction = PropertyNames.EMPTY_STRING;
68cdf0e10cSrcweir             sFieldNamesFraction = combineFieldNameFraction() + sReturnChar;
69cdf0e10cSrcweir             sSortingFraction = combinePartString(RID_QUERY + 51, getSortFieldNames(), RID_QUERY + 52, RID_QUERY + 93, new String[]
70cdf0e10cSrcweir                     {
71cdf0e10cSrcweir                         "<FIELDNAME>", "<SORTMODE>"
72cdf0e10cSrcweir                     }) + sReturnChar;
73cdf0e10cSrcweir             sFilterFraction = combineFilterNameFraction(this.getFilterConditions(), RID_QUERY + 53, RID_QUERY + 54) + sReturnChar;
74cdf0e10cSrcweir             //      if (xDBMetaData.getNumericFunctions().length() > 0)
75cdf0e10cSrcweir             //          sAggregateFraction = combinePartString(RID_QUERY + 55, AggregateFieldNames, RID_QUERY + 56, RID_QUERY + 95, new String[]{ "<CALCULATEDFUNCTION>", "<FIELDNAME>"}) + sReturnChar;
76cdf0e10cSrcweir             if (xDBMetaData.supportsGroupBy())
77cdf0e10cSrcweir             {
78cdf0e10cSrcweir                 sGroupByFraction = combinePartString(RID_QUERY + 57, GroupFieldNames, RID_QUERY + 58) + sReturnChar;
79cdf0e10cSrcweir                 sHavingFraction = combineFilterNameFraction(getGroupByFilterConditions(), RID_QUERY + 59, RID_QUERY + 60);
80cdf0e10cSrcweir             }
81cdf0e10cSrcweir             // TODO: remove the last return from the string
82cdf0e10cSrcweir             sSummary = sFieldNamesFraction + sSortingFraction + sFilterFraction + sAggregateFraction + sGroupByFraction + sHavingFraction;
83cdf0e10cSrcweir             sSummary = JavaTools.replaceSubString(sSummary, PropertyNames.EMPTY_STRING, "~");
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir         catch (com.sun.star.uno.Exception exception)
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             exception.printStackTrace(System.out);
88cdf0e10cSrcweir         }
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
getSummaryString()91cdf0e10cSrcweir     public String getSummaryString()
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         return sSummary;
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 
combineFilterNameFraction(PropertyValue[][] _filterconditions, int _InitResID, int _AlternativeResID)96cdf0e10cSrcweir     private String combineFilterNameFraction(PropertyValue[][] _filterconditions, int _InitResID, int _AlternativeResID)
97cdf0e10cSrcweir     {
98cdf0e10cSrcweir         if (_filterconditions != null && _filterconditions.length > 0)
99cdf0e10cSrcweir         {
100cdf0e10cSrcweir             String sconditions = PropertyNames.EMPTY_STRING;
101cdf0e10cSrcweir             String sStart = oResource.getResText(_InitResID);
102cdf0e10cSrcweir             String BaseString = oResource.getResText(RID_QUERY + 96);
103cdf0e10cSrcweir             if (_filterconditions.length == 1)
104cdf0e10cSrcweir             {
105cdf0e10cSrcweir                 PropertyValue[] curfilterconditions = _filterconditions[0];
106cdf0e10cSrcweir                 for (int i = 0; i < curfilterconditions.length; i++)
107cdf0e10cSrcweir                 {
108cdf0e10cSrcweir                     sconditions += FilterComponent.getDisplayCondition(BaseString, _filterconditions[0][i], this);
109cdf0e10cSrcweir                     sconditions = appendClauseSeparator(sconditions, PropertyNames.SPACE + sAnd + PropertyNames.SPACE, i, curfilterconditions.length);
110cdf0e10cSrcweir                 }
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir             else
113cdf0e10cSrcweir             {
114cdf0e10cSrcweir 
115cdf0e10cSrcweir                 for (int i = 0; i < _filterconditions.length; i++)
116cdf0e10cSrcweir                 {
117cdf0e10cSrcweir                     sconditions += FilterComponent.getDisplayCondition(BaseString, _filterconditions[i][0], this);
118cdf0e10cSrcweir                     sconditions = appendClauseSeparator(sconditions, PropertyNames.SPACE + sOr + PropertyNames.SPACE, i, _filterconditions.length);
119cdf0e10cSrcweir                 }
120cdf0e10cSrcweir             }
121cdf0e10cSrcweir             return sStart + sconditions;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir         return oResource.getResText(_AlternativeResID);
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
combineFieldNameFraction()126cdf0e10cSrcweir     private String combineFieldNameFraction()
127cdf0e10cSrcweir     {
128cdf0e10cSrcweir         String CurString = PropertyNames.EMPTY_STRING;
129cdf0e10cSrcweir         String sReturn = oResource.getResText(RID_QUERY + 50);
130cdf0e10cSrcweir         String BaseString = oResource.getResText(RID_QUERY + 92);
131cdf0e10cSrcweir         for (int i = 0; i < FieldColumns.length; i++)
132cdf0e10cSrcweir         {
133cdf0e10cSrcweir             CurString = BaseString;
134cdf0e10cSrcweir             FieldColumn CurDBFieldColumn = super.getFieldColumnByDisplayName(FieldColumns[i].getDisplayFieldName());
135cdf0e10cSrcweir             int iAggregate = getAggregateIndex(FieldColumns[i].getFieldName());
136cdf0e10cSrcweir             if (iAggregate > -1)
137cdf0e10cSrcweir             {
138cdf0e10cSrcweir                 String sAggregateDisplay = AggregateFieldNames[iAggregate][1] + "(" + AggregateFieldNames[iAggregate][0] + ")";
139cdf0e10cSrcweir                 CurString = JavaTools.replaceSubString(CurString, sAggregateDisplay, "<FIELDNAME>");
140cdf0e10cSrcweir             }
141cdf0e10cSrcweir             else
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 CurString = JavaTools.replaceSubString(CurString, CurDBFieldColumn.getDisplayFieldName(), "<FIELDNAME>");
144cdf0e10cSrcweir             }
145cdf0e10cSrcweir             sReturn += JavaTools.replaceSubString(CurString, CurDBFieldColumn.getFieldTitle(), "<FIELDTITLE>");
146cdf0e10cSrcweir             sReturn = appendClauseSeparator(sReturn, sSeparator, i, FieldColumns.length);
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir         return sReturn;
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
appendClauseSeparator(String _basestring, String _suffix, int _i, int _fieldcount)151cdf0e10cSrcweir     private String appendClauseSeparator(String _basestring, String _suffix, int _i, int _fieldcount)
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         if (_i < _fieldcount - 1)
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             _basestring += _suffix;
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir         return _basestring;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir     // TODO: How can you merge the following two methods to a single one in a smarter way??
160cdf0e10cSrcweir 
combinePartString(int _InitResID, String[] _FieldNames, int _AlternativeResID)161cdf0e10cSrcweir     public String combinePartString(int _InitResID, String[] _FieldNames, int _AlternativeResID)
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         if (_FieldNames != null && _FieldNames.length > 0)
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             return ArrayFieldsToString(_InitResID, _FieldNames);
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         return oResource.getResText(_AlternativeResID);
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir 
ArrayFieldsToString(int _InitResID, String[] _FieldNames)170cdf0e10cSrcweir     protected String ArrayFieldsToString(int _InitResID, String[] _FieldNames)
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         String sReturn = oResource.getResText(_InitResID);
173cdf0e10cSrcweir         int FieldCount = _FieldNames.length;
174cdf0e10cSrcweir         for (int i = 0; i < FieldCount; i++)
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             sReturn += this.getFieldColumnByDisplayName(_FieldNames[i]).getFieldTitle();
177cdf0e10cSrcweir             if (i < FieldCount - 1)
178cdf0e10cSrcweir             {
179cdf0e10cSrcweir                 sReturn += sSeparator;
180cdf0e10cSrcweir             }
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir         return (sReturn);
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
combinePartString(int _InitResID, String[][] _FieldNames, int _AlternativeResID, int _BaseStringID, String[] _ReplaceTags)185cdf0e10cSrcweir     public String combinePartString(int _InitResID, String[][] _FieldNames, int _AlternativeResID, int _BaseStringID, String[] _ReplaceTags)
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         if (_FieldNames != null && _FieldNames.length > 0)
188cdf0e10cSrcweir         {
189cdf0e10cSrcweir             return ArrayFieldsToString(_InitResID, _FieldNames, _BaseStringID, _ReplaceTags);
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir         return oResource.getResText(_AlternativeResID);
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
ArrayFieldsToString(int _InitResID, String[][] _FieldNames, int _BaseStringID, String[] _ReplaceTags)194cdf0e10cSrcweir     public String ArrayFieldsToString(int _InitResID, String[][] _FieldNames, int _BaseStringID, String[] _ReplaceTags)
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         String CurString = PropertyNames.EMPTY_STRING;
197cdf0e10cSrcweir         String sReturn = oResource.getResText(_InitResID);
198cdf0e10cSrcweir         int FieldCount = _FieldNames.length;
199cdf0e10cSrcweir         if (FieldCount > 0)
200cdf0e10cSrcweir         {
201cdf0e10cSrcweir             int DimCount = _FieldNames[0].length;
202cdf0e10cSrcweir             String BaseString = oResource.getResText(_BaseStringID);
203cdf0e10cSrcweir             for (int i = 0; i < FieldCount; i++)
204cdf0e10cSrcweir             {
205cdf0e10cSrcweir                 for (int a = 0; a < DimCount; a++)
206cdf0e10cSrcweir                 {
207cdf0e10cSrcweir                     if (a == 0)
208cdf0e10cSrcweir                     {
209cdf0e10cSrcweir                         int iAggregate = getAggregateIndex(this.getFieldColumnByDisplayName(_FieldNames[i][a]).getDisplayFieldName());
210cdf0e10cSrcweir                         if (iAggregate > -1)
211cdf0e10cSrcweir                         {
212cdf0e10cSrcweir                             String sAggregateDisplay = AggregateFieldNames[iAggregate][1] + "(" + AggregateFieldNames[iAggregate][0] + ")";
213cdf0e10cSrcweir                             CurString = JavaTools.replaceSubString(BaseString, sAggregateDisplay, _ReplaceTags[a]);
214cdf0e10cSrcweir                         }
215cdf0e10cSrcweir                         else
216cdf0e10cSrcweir                         {
217cdf0e10cSrcweir                             CurString = JavaTools.replaceSubString(BaseString, this.getFieldColumnByDisplayName(_FieldNames[i][a]).getFieldTitle(), _ReplaceTags[a]);
218cdf0e10cSrcweir                         }
219cdf0e10cSrcweir                     }
220cdf0e10cSrcweir                     else
221cdf0e10cSrcweir                     {
222cdf0e10cSrcweir                         CurString = JavaTools.replaceSubString(CurString, _FieldNames[i][a], _ReplaceTags[a]);
223cdf0e10cSrcweir                     }
224cdf0e10cSrcweir                 }
225cdf0e10cSrcweir                 sReturn += CurString;
226cdf0e10cSrcweir                 if (i < FieldCount - 1)
227cdf0e10cSrcweir                 {
228cdf0e10cSrcweir                     sReturn += sSeparator;
229cdf0e10cSrcweir                 }
230cdf0e10cSrcweir             }
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir         return sReturn;
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir }
235