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