4633b255c5a7ef04d13423854a34caa699a8d4c9
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.model.runtime;
39
40 import java.util.*;
41
42 import org.onap.portalsdk.analytics.*;
43 import org.onap.portalsdk.analytics.system.*;
44 import org.onap.portalsdk.analytics.util.*;
45 import org.onap.portalsdk.analytics.view.*;
46 import org.onap.portalsdk.analytics.xmlobj.*;
47
48 public class FormatProcessor extends RaptorObject {
49
50
51         private SemaphoreType semaphore                = null;
52
53     private String        colType                  = null;
54
55     private String        dateFormat               = null;
56
57     private HtmlFormatter defaultFormatter         = null;
58
59     private HashMap       formatters               = null;
60
61     private HashMap       convertedValues          = null;
62
63     private boolean       attemptNumericConversion = false;
64
65     public FormatProcessor(SemaphoreType sem, String colType, String dateFormat,
66             boolean attemptNumericConversion) {
67
68         super();
69
70         if (sem == null)
71             return;
72
73         this.semaphore = sem;
74         this.colType = colType;
75         this.dateFormat = dateFormat;
76
77         this.attemptNumericConversion = attemptNumericConversion;
78         if (attemptNumericConversion)
79             for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
80                     .hasNext();) {
81                 FormatType fmt = (FormatType) iter.next();
82                 if (!isNumber(fmt.getLessThanValue())) {
83                     this.attemptNumericConversion = false;
84                     break;
85                 } // if
86             } // for
87
88         formatters = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
89         convertedValues = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
90
91         for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter.hasNext();) {
92             FormatType fmt = (FormatType) iter.next();
93             if ((fmt.getFormatId() == null) || (fmt.getFormatId().length() <= 0)) {
94                 defaultFormatter = new HtmlFormatter(fmt.isBold(), fmt.isItalic(), fmt
95                         .isUnderline(), fmt.getBgColor(), fmt.getFontColor(), fmt
96                         .getFontFace(), fmt.getFontSize(), fmt.getAlignment());
97             } else {
98                 formatters.put(fmt.getFormatId(), new HtmlFormatter(fmt.isBold(), fmt
99                         .isItalic(), fmt.isUnderline(), fmt.getBgColor(), fmt.getFontColor(),
100                         fmt.getFontFace(), fmt.getFontSize(), fmt.getAlignment()));
101                 convertedValues.put(fmt.getFormatId(), convertValue(fmt.getLessThanValue()));
102             }
103         } // for
104     } // FormatProcessor
105
106     private String convertValue(String origValue) {
107
108         if (colType.equals(AppConstants.CT_DATE))
109             return convertDateValue(origValue);
110         else if (colType.equals(AppConstants.CT_NUMBER))
111             return convertNumericValue(origValue);
112         else if (attemptNumericConversion)
113             return convertUnknownValue(origValue);
114         else
115             return origValue;
116     } // convertValue
117
118     private String convertDateValue(String origValue) {
119
120         // Converts to YYYY-MM-DD if possible
121         if (nvl(dateFormat).length() == 0 || nvl(origValue).length() == 0)
122             return origValue;
123
124         if (dateFormat.equals("MM/DD/YYYY") && origValue.length() == 10)
125             // Special processing for the default date format - for saving DB
126             // calls
127             return origValue.substring(6, 10) + "-" + origValue.substring(0, 2) + "-"
128                     + origValue.substring(3, 5);
129
130         try {
131            // DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
132              //       + dateFormat + "'), 'YYYY-MM-DD') val FROM DUAL");
133             
134             String sql = Globals.getGenerateSqlVisualDual();
135             DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
136                     + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
137             
138             if (ds.getRowCount() > 0)
139                 return ds.getString(0, 0);
140         } catch (Exception e) {
141         }
142
143         return origValue;
144     } // convertDateValue
145
146     private String convertNumericValue(String origValue) {
147
148         // Converts to [20 pos.5 pos] if possible
149         if (nvl(origValue).length() == 0)
150             return origValue;
151         boolean isNegative = false;
152  
153         StringBuffer integerValue = new StringBuffer();
154         StringBuffer fractionValue = new StringBuffer();
155
156         boolean beforeDecimalPoint = true;
157         for (int i = 0; i < origValue.length(); i++) {
158             char c = origValue.charAt(i);
159             if (c == '.')
160                 beforeDecimalPoint = false;
161             else if (c == '-' && integerValue.length() == 0)
162                 isNegative = true;
163             // else
164             // if(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9')
165             else if (Character.isDigit(c))
166                 if (beforeDecimalPoint)
167                     integerValue.append(c);
168                 else
169                     fractionValue.append(c);
170         } // for
171
172         while (integerValue.length() < 20)
173             integerValue.insert(0, '0');
174
175         while (fractionValue.length() < 5)
176             fractionValue.append('0');
177
178         integerValue.append('.');
179         integerValue.append(fractionValue);
180         integerValue.insert(0, (isNegative ? '-' : '+'));
181
182         return integerValue.toString();
183     } // convertNumericValue
184
185     private boolean isNumber(String value) { // As per Raptor def, like
186
187         // -$3,270.56
188         value = value.trim();
189         for (int i = 0; i < value.length(); i++) {
190             char c = value.charAt(i);
191             if (!(Character.isDigit(c) || c == '.' || c == '-' || c == '+' || c == ','
192                     || c == '$' || c == '%'))
193                 return false;
194         } // for
195
196         return true;
197     } // isNumber
198
199     private String convertUnknownValue(String origValue) {
200
201         return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
202     } // convertUnknownValue
203
204     private boolean isEqual(String value1, String value2) {
205
206         return value1.trim().equals(value2.trim());
207     } // isEqual
208
209     private boolean isLessThan(String value1, String value2) {
210
211         boolean compareAsNumbers = colType.equals(AppConstants.CT_NUMBER);
212         if ((!compareAsNumbers) && attemptNumericConversion)
213             compareAsNumbers = isNumber(value1) && isNumber(value2);
214         if (compareAsNumbers && value1.length()>0 && value2.length()>0) {
215             boolean value1IsNegative = (value1.charAt(0) == '-');
216             boolean value2IsNegative = (value2.charAt(0) == '-');
217             if (value1IsNegative && (!value2IsNegative)) {
218                 return true;
219             }
220             else if ((!value1IsNegative) && value2IsNegative) {
221                 return false;
222             }
223             return Double.parseDouble(value1)<Double.parseDouble(value2);
224         } // if
225         
226         return (value1.compareTo(value2) < 0);
227     } // isEqual
228
229     public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
230
231         if (semaphore == null)
232             return;
233
234         HtmlFormatter formatter = defaultFormatter;
235         HtmlFormatter anyFormatter = null; 
236         String sValue = convertValue(dv.getDisplayValue());
237         
238         String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
239         
240         String targetColId = null;
241         if(semaphore.getTarget()!=null)
242                 targetColId = semaphore.getTarget();
243         
244         DataValue targetDataValue = null;
245         /* compare the column id which is in comment and assign to sValue */
246         if(nvl(compareColId).length()>0) {
247                 for (dr.resetNext(); dr.hasNext();) {
248                         DataValue dv1 = dr.getNext();
249                         //add null check
250                         if(dv1.getColId()!=null) {
251                                 if(dv1.getColId().equals(compareColId))
252                                         sValue = convertValue(dv1.getDisplayValue());
253                                 if(targetColId!=null) {
254                                         if(dv1.getColId().equals(targetColId))
255                                                 targetDataValue = dv1;
256                                 }
257                         }
258                 }
259         }
260         
261         for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
262         .hasNext();) {
263                         FormatType fmt = (FormatType) iter.next();
264                         if(fmt.getLessThanValue().length() <= 0) { 
265                                 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
266                                 anyFormatter.setFormatId(fmt.getFormatId());
267                                 break;
268                         }
269         }
270
271         if( anyFormatter == null ) anyFormatter = formatter; 
272        // String sValue = convertValue(dv.getDisplayValue());
273         //if (sValue.length() > 0) {
274             for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
275                     .hasNext();) {
276                 FormatType fmt = (FormatType) iter.next();
277                 // For Excel Download
278
279                 if ((fmt.getFormatId() == null) || (fmt.getFormatId().length() <= 0)) {
280                     // Default formatter
281                     continue;
282                 }
283
284                 String formatterValue = nvl((String) convertedValues.get(fmt.getFormatId()));
285                 boolean valueMatched = false;
286                 if (fmt.getExpression().equals("=")) {
287                     valueMatched = isEqual(sValue, formatterValue);
288                 }
289
290                 else if (fmt.getExpression().equals("<>"))
291                     valueMatched = (!isEqual(sValue, formatterValue));
292                 else if (fmt.getExpression().equals(">")) {
293                     valueMatched = (!(isEqual(sValue, formatterValue) || isLessThan(sValue,
294                             formatterValue)));
295                 }
296                 else if (fmt.getExpression().equals(">=")) {
297                     valueMatched = /* isEqual(sValue, formatterValue)|| */(!isLessThan(
298                             sValue, formatterValue));
299                 }
300                 else if (fmt.getExpression().equals("<")) {
301                     valueMatched = isLessThan(sValue, formatterValue);
302                 }
303                 else if (fmt.getExpression().equals("<=")) {
304                     valueMatched = isEqual(sValue, formatterValue)
305                             || isLessThan(sValue, formatterValue);
306                 }
307                 //s_logger.debug("SYSOUT " + " " +sValue  +" " +fmt.getBgColor() + " " + fmt.getLessThanValue()+ " " +valueMatched);
308                 if (fmt.getLessThanValue().length() > 0 && valueMatched) {
309                     formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
310                     formatter.setFormatId(fmt.getFormatId());
311                     formatModified = true;
312                     //dv.setFormatId(fmt.getFormatId());
313                     //dr.setFormatId(fmt.getFormatId());
314                     //break;
315                 } else { // if
316                         if(!formatModified) formatter = anyFormatter;
317                         //if(!((formatter!=null && formatter!=anyFormatter) || (defaultFormatter!=null && formatter!=defaultFormatter)))
318                         //      formatter = anyFormatter;
319                         //formatter.setFormatId(anyFormatter.getFormatId());
320                 }
321                 /*else if ((fmt.getLessThanValue().length() <= 0)
322                         && (fmt.getFormatId().length() > 0)) {
323                     formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
324                         System.out.println("---------------lesser "+ fmt.getFormatId()+ " " + fmt.getBgColor());
325                     dv.setFormatId(fmt.getFormatId());
326                     dr.setFormatId(fmt.getFormatId());
327                     // break;
328                 } // else if*/
329             } // for
330         /*} else {
331             for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
332             .hasNext();) {
333                 FormatType fmt = (FormatType) iter.next();
334                 if(fmt.getLessThanValue().length()<=0 && fmt.getExpression().length()<=0 && !fmt.isBold() && !fmt.isItalic() && !fmt.isUnderline() && fmt.getFontSize().equals("11")) {
335                         formatter = defaultFormatter;
336                 } else
337                         formatter = anyFormatter;               
338             }
339                 
340                 //formatter.setFormatId(anyFormatter.getFormatId());
341         } */
342         if(formatter != null) {
343         if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
344            
345             if (dr.getRowFormatter() == null || formatter != defaultFormatter) {
346                 // Making sure the default formatter doesn't overwrite
347                 // valid row formatter set from another column
348                 dr.setRowFormatter(formatter);
349                 dr.setFormatId(formatter.getFormatId());
350                 // This is added for excel download
351                 //if (!formatter.equals(defaultFormatter)) {
352                     dr.setRowFormat(true);
353                 //}
354
355             }
356         } else {
357                 if(nvl(targetColId).length()>0) {
358                         if(targetDataValue!=null) {
359                         targetDataValue.setCellFormatter(formatter);
360                         targetDataValue.setFormatId(formatter.getFormatId());            
361                         //if (!formatter.equals(defaultFormatter)) {
362                         targetDataValue.setCellFormat(true);
363                             int count = 0;
364                                 for (dr.resetNext(); dr.hasNext();) {
365                                         DataValue dv1 = dr.getNext();
366                                         //add null check
367                                                 if(targetColId!=null) {
368                                                         if(dv1.getColId().equals(targetColId))
369                                                                 dr.setDataValue(count, targetDataValue);
370                                                 }
371                                         count++;        
372                                }
373                            }
374                 //}
375                         
376                 } else {
377             dv.setCellFormatter(formatter);
378             dv.setFormatId(formatter.getFormatId());            
379             //if (!formatter.equals(defaultFormatter)) {
380                 dv.setCellFormat(true);
381             //}
382                 }
383         }// else
384         }
385     } // setHtmlFormatters
386
387 } // FormatProcessor