62bf4335426f904e693ee7b5ce12431bed506284
[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             String sql = Globals.getGenerateSqlVisualDual();
138             DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
139                     + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
140             
141             if (ds.getRowCount() > 0)
142                 return ds.getString(0, 0);
143         } catch (Exception e) {
144         }
145
146         return origValue;
147     } // convertDateValue
148
149     private String convertNumericValue(String origValue) {
150
151         // Converts to [20 pos.5 pos] if possible
152         if (nvl(origValue).length() == 0)
153             return origValue;
154         boolean isNegative = false;
155  
156         StringBuffer integerValue = new StringBuffer();
157         StringBuffer fractionValue = new StringBuffer();
158
159         boolean beforeDecimalPoint = true;
160         for (int i = 0; i < origValue.length(); i++) {
161             char c = origValue.charAt(i);
162             if (c == '.')
163                 beforeDecimalPoint = false;
164             else if (c == '-' && integerValue.length() == 0)
165                 isNegative = true;
166             else if (Character.isDigit(c))
167                 if (beforeDecimalPoint)
168                     integerValue.append(c);
169                 else
170                     fractionValue.append(c);
171         } // for
172
173         while (integerValue.length() < 20)
174             integerValue.insert(0, '0');
175
176         while (fractionValue.length() < 5)
177             fractionValue.append('0');
178
179         integerValue.append('.');
180         integerValue.append(fractionValue);
181         integerValue.insert(0, (isNegative ? '-' : '+'));
182
183         return integerValue.toString();
184     } // convertNumericValue
185
186     private boolean isNumber(String value) { // As per Raptor def, like
187
188         // -$3,270.56
189         value = value.trim();
190         for (int i = 0; i < value.length(); i++) {
191             char c = value.charAt(i);
192             if (!(Character.isDigit(c) || c == '.' || c == '-' || c == '+' || c == ','
193                     || c == '$' || c == '%'))
194                 return false;
195         } // for
196
197         return true;
198     } // isNumber
199
200     private String convertUnknownValue(String origValue) {
201
202         return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
203     } // convertUnknownValue
204
205     private boolean isEqual(String value1, String value2) {
206
207         return value1.trim().equals(value2.trim());
208     } // isEqual
209
210     private boolean isLessThan(String value1, String value2) {
211
212         boolean compareAsNumbers = colType.equals(AppConstants.CT_NUMBER);
213         if ((!compareAsNumbers) && attemptNumericConversion)
214             compareAsNumbers = isNumber(value1) && isNumber(value2);
215         if (compareAsNumbers && value1.length()>0 && value2.length()>0) {
216             boolean value1IsNegative = (value1.charAt(0) == '-');
217             boolean value2IsNegative = (value2.charAt(0) == '-');
218             if (value1IsNegative && (!value2IsNegative)) {
219                 return true;
220             }
221             else if ((!value1IsNegative) && value2IsNegative) {
222                 return false;
223             }
224             return Double.parseDouble(value1)<Double.parseDouble(value2);
225         } // if
226         
227         return (value1.compareTo(value2) < 0);
228     } // isEqual
229
230     public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
231
232         if (semaphore == null)
233             return;
234
235         HtmlFormatter formatter = defaultFormatter;
236         HtmlFormatter anyFormatter = null; 
237         String sValue = convertValue(dv.getDisplayValue());
238         
239         String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
240         
241         String targetColId = null;
242         if(semaphore.getTarget()!=null)
243                 targetColId = semaphore.getTarget();
244         
245         DataValue targetDataValue = null;
246         /* compare the column id which is in comment and assign to sValue */
247         if(nvl(compareColId).length()>0) {
248                 for (dr.resetNext(); dr.hasNext();) {
249                         DataValue dv1 = dr.getNext();
250                         //add null check
251                         if(dv1.getColId()!=null) {
252                                 if(dv1.getColId().equals(compareColId))
253                                         sValue = convertValue(dv1.getDisplayValue());
254                                 if(targetColId!=null) {
255                                         if(dv1.getColId().equals(targetColId))
256                                                 targetDataValue = dv1;
257                                 }
258                         }
259                 }
260         }
261         
262         for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
263         .hasNext();) {
264                         FormatType fmt = (FormatType) iter.next();
265                         if(fmt.getLessThanValue().length() <= 0) { 
266                                 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
267                                 anyFormatter.setFormatId(fmt.getFormatId());
268                                 break;
269                         }
270         }
271
272         if( anyFormatter == null )
273                         anyFormatter = formatter; 
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                 if (fmt.getLessThanValue().length() > 0 && valueMatched) {
308                     formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
309                     formatter.setFormatId(fmt.getFormatId());
310                     formatModified = true;
311                 } else { 
312                         if(!formatModified)
313                                                 formatter = anyFormatter;
314                 }
315             }
316       
317         if(formatter != null) {
318         if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
319            
320             if (dr.getRowFormatter() == null || formatter != defaultFormatter) {
321                 // Making sure the default formatter doesn't overwrite
322                 // valid row formatter set from another column
323                 dr.setRowFormatter(formatter);
324                 dr.setFormatId(formatter.getFormatId());
325                 // This is added for excel download
326                     dr.setRowFormat(true);
327
328             }
329         } else {
330                 if(nvl(targetColId).length()>0) {
331                         if(targetDataValue!=null) {
332                         targetDataValue.setCellFormatter(formatter);
333                         targetDataValue.setFormatId(formatter.getFormatId());
334                         targetDataValue.setCellFormat(true);
335                             int count = 0;
336                                 for (dr.resetNext(); dr.hasNext();) {
337                                         DataValue dv1 = dr.getNext();
338                                         //add null check
339                                                 if(targetColId!=null) {
340                                                         if(dv1.getColId().equals(targetColId))
341                                                                 dr.setDataValue(count, targetDataValue);
342                                                 }
343                                         count++;        
344                                }
345                            }
346                 //}
347                         
348                 } else {
349             dv.setCellFormatter(formatter);
350             dv.setFormatId(formatter.getFormatId());  
351                 dv.setCellFormat(true);
352                 }
353         }
354         }
355     } // setHtmlFormatters
356
357 } // FormatProcessor