2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.analytics.model.runtime;
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.*;
48 public class FormatProcessor extends RaptorObject {
51 private SemaphoreType semaphore = null;
53 private String colType = null;
55 private String dateFormat = null;
57 private HtmlFormatter defaultFormatter = null;
59 private HashMap formatters = null;
61 private HashMap convertedValues = null;
63 private boolean attemptNumericConversion = false;
65 public FormatProcessor(SemaphoreType sem, String colType, String dateFormat,
66 boolean attemptNumericConversion) {
74 this.colType = colType;
75 this.dateFormat = dateFormat;
77 this.attemptNumericConversion = attemptNumericConversion;
78 if (attemptNumericConversion)
79 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
81 FormatType fmt = (FormatType) iter.next();
82 if (!isNumber(fmt.getLessThanValue())) {
83 this.attemptNumericConversion = false;
88 formatters = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
89 convertedValues = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
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());
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()));
106 private String convertValue(String origValue) {
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);
118 private String convertDateValue(String origValue) {
120 // Converts to YYYY-MM-DD if possible
121 if (nvl(dateFormat).length() == 0 || nvl(origValue).length() == 0)
124 if (dateFormat.equals("MM/DD/YYYY") && origValue.length() == 10)
125 // Special processing for the default date format - for saving DB
127 return origValue.substring(6, 10) + "-" + origValue.substring(0, 2) + "-"
128 + origValue.substring(3, 5);
131 // DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
132 // + dateFormat + "'), 'YYYY-MM-DD') val FROM DUAL");
134 String sql = Globals.getGenerateSqlVisualDual();
135 DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
136 + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
138 if (ds.getRowCount() > 0)
139 return ds.getString(0, 0);
140 } catch (Exception e) {
144 } // convertDateValue
146 private String convertNumericValue(String origValue) {
148 // Converts to [20 pos.5 pos] if possible
149 if (nvl(origValue).length() == 0)
151 boolean isNegative = false;
153 StringBuffer integerValue = new StringBuffer();
154 StringBuffer fractionValue = new StringBuffer();
156 boolean beforeDecimalPoint = true;
157 for (int i = 0; i < origValue.length(); i++) {
158 char c = origValue.charAt(i);
160 beforeDecimalPoint = false;
161 else if (c == '-' && integerValue.length() == 0)
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);
169 fractionValue.append(c);
172 while (integerValue.length() < 20)
173 integerValue.insert(0, '0');
175 while (fractionValue.length() < 5)
176 fractionValue.append('0');
178 integerValue.append('.');
179 integerValue.append(fractionValue);
180 integerValue.insert(0, (isNegative ? '-' : '+'));
182 return integerValue.toString();
183 } // convertNumericValue
185 private boolean isNumber(String value) { // As per Raptor def, like
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 == '%'))
199 private String convertUnknownValue(String origValue) {
201 return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
202 } // convertUnknownValue
204 private boolean isEqual(String value1, String value2) {
206 return value1.trim().equals(value2.trim());
209 private boolean isLessThan(String value1, String value2) {
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)) {
220 else if ((!value1IsNegative) && value2IsNegative) {
223 return Double.parseDouble(value1)<Double.parseDouble(value2);
226 return (value1.compareTo(value2) < 0);
229 public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
231 if (semaphore == null)
234 HtmlFormatter formatter = defaultFormatter;
235 HtmlFormatter anyFormatter = null;
236 String sValue = convertValue(dv.getDisplayValue());
238 String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
240 String targetColId = null;
241 if(semaphore.getTarget()!=null)
242 targetColId = semaphore.getTarget();
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();
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;
261 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
263 FormatType fmt = (FormatType) iter.next();
264 if(fmt.getLessThanValue().length() <= 0) {
265 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
266 anyFormatter.setFormatId(fmt.getFormatId());
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
276 FormatType fmt = (FormatType) iter.next();
277 // For Excel Download
279 if ((fmt.getFormatId() == null) || (fmt.getFormatId().length() <= 0)) {
284 String formatterValue = nvl((String) convertedValues.get(fmt.getFormatId()));
285 boolean valueMatched = false;
286 if (fmt.getExpression().equals("=")) {
287 valueMatched = isEqual(sValue, formatterValue);
290 else if (fmt.getExpression().equals("<>"))
291 valueMatched = (!isEqual(sValue, formatterValue));
292 else if (fmt.getExpression().equals(">")) {
293 valueMatched = (!(isEqual(sValue, formatterValue) || isLessThan(sValue,
296 else if (fmt.getExpression().equals(">=")) {
297 valueMatched = /* isEqual(sValue, formatterValue)|| */(!isLessThan(
298 sValue, formatterValue));
300 else if (fmt.getExpression().equals("<")) {
301 valueMatched = isLessThan(sValue, formatterValue);
303 else if (fmt.getExpression().equals("<=")) {
304 valueMatched = isEqual(sValue, formatterValue)
305 || isLessThan(sValue, formatterValue);
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());
316 if(!formatModified) formatter = anyFormatter;
317 //if(!((formatter!=null && formatter!=anyFormatter) || (defaultFormatter!=null && formatter!=defaultFormatter)))
318 // formatter = anyFormatter;
319 //formatter.setFormatId(anyFormatter.getFormatId());
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());
331 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
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;
337 formatter = anyFormatter;
340 //formatter.setFormatId(anyFormatter.getFormatId());
342 if(formatter != null) {
343 if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
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);
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);
364 for (dr.resetNext(); dr.hasNext();) {
365 DataValue dv1 = dr.getNext();
367 if(targetColId!=null) {
368 if(dv1.getColId().equals(targetColId))
369 dr.setDataValue(count, targetDataValue);
377 dv.setCellFormatter(formatter);
378 dv.setFormatId(formatter.getFormatId());
379 //if (!formatter.equals(defaultFormatter)) {
380 dv.setCellFormat(true);
385 } // setHtmlFormatters