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