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