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============================================
38 package org.onap.portalsdk.analytics.model.runtime;
40 import java.util.HashMap;
41 import java.util.Iterator;
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;
54 public class FormatProcessor extends RaptorObject {
57 private SemaphoreType semaphore = null;
59 private String colType = null;
61 private String dateFormat = null;
63 private HtmlFormatter defaultFormatter = null;
65 private HashMap formatters = null;
67 private HashMap convertedValues = null;
69 private boolean attemptNumericConversion = false;
71 public FormatProcessor(SemaphoreType sem, String colType, String dateFormat,
72 boolean attemptNumericConversion) {
80 this.colType = colType;
81 this.dateFormat = dateFormat;
83 this.attemptNumericConversion = attemptNumericConversion;
84 if (attemptNumericConversion)
85 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
87 FormatType fmt = (FormatType) iter.next();
88 if (!isNumber(fmt.getLessThanValue())) {
89 this.attemptNumericConversion = false;
94 formatters = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
95 convertedValues = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
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());
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()));
112 private String convertValue(String origValue) {
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);
124 private String convertDateValue(String origValue) {
126 // Converts to YYYY-MM-DD if possible
127 if (nvl(dateFormat).length() == 0 || nvl(origValue).length() == 0)
130 if (dateFormat.equals("MM/DD/YYYY") && origValue.length() == 10)
131 // Special processing for the default date format - for saving DB
133 return origValue.substring(6, 10) + "-" + origValue.substring(0, 2) + "-"
134 + origValue.substring(3, 5);
137 String sql = Globals.getGenerateSqlVisualDual();
138 DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
139 + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
141 if (ds.getRowCount() > 0)
142 return ds.getString(0, 0);
143 } catch (Exception e) {
147 } // convertDateValue
149 private String convertNumericValue(String origValue) {
151 // Converts to [20 pos.5 pos] if possible
152 if (nvl(origValue).length() == 0)
154 boolean isNegative = false;
156 StringBuffer integerValue = new StringBuffer();
157 StringBuffer fractionValue = new StringBuffer();
159 boolean beforeDecimalPoint = true;
160 for (int i = 0; i < origValue.length(); i++) {
161 char c = origValue.charAt(i);
163 beforeDecimalPoint = false;
164 else if (c == '-' && integerValue.length() == 0)
166 else if (Character.isDigit(c))
167 if (beforeDecimalPoint)
168 integerValue.append(c);
170 fractionValue.append(c);
173 while (integerValue.length() < 20)
174 integerValue.insert(0, '0');
176 while (fractionValue.length() < 5)
177 fractionValue.append('0');
179 integerValue.append('.');
180 integerValue.append(fractionValue);
181 integerValue.insert(0, (isNegative ? '-' : '+'));
183 return integerValue.toString();
184 } // convertNumericValue
186 private boolean isNumber(String value) { // As per Raptor def, like
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 == '%'))
200 private String convertUnknownValue(String origValue) {
202 return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
203 } // convertUnknownValue
205 private boolean isEqual(String value1, String value2) {
207 return value1.trim().equals(value2.trim());
210 private boolean isLessThan(String value1, String value2) {
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)) {
221 else if ((!value1IsNegative) && value2IsNegative) {
224 return Double.parseDouble(value1)<Double.parseDouble(value2);
227 return (value1.compareTo(value2) < 0);
230 public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
232 if (semaphore == null)
235 HtmlFormatter formatter = defaultFormatter;
236 HtmlFormatter anyFormatter = null;
237 String sValue = convertValue(dv.getDisplayValue());
239 String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
241 String targetColId = null;
242 if(semaphore.getTarget()!=null)
243 targetColId = semaphore.getTarget();
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();
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;
262 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
264 FormatType fmt = (FormatType) iter.next();
265 if(fmt.getLessThanValue().length() <= 0) {
266 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
267 anyFormatter.setFormatId(fmt.getFormatId());
272 if( anyFormatter == null )
273 anyFormatter = formatter;
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 if (fmt.getLessThanValue().length() > 0 && valueMatched) {
308 formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
309 formatter.setFormatId(fmt.getFormatId());
310 formatModified = true;
313 formatter = anyFormatter;
317 if(formatter != null) {
318 if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
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);
330 if(nvl(targetColId).length()>0) {
331 if(targetDataValue!=null) {
332 targetDataValue.setCellFormatter(formatter);
333 targetDataValue.setFormatId(formatter.getFormatId());
334 targetDataValue.setCellFormat(true);
336 for (dr.resetNext(); dr.hasNext();) {
337 DataValue dv1 = dr.getNext();
339 if(targetColId!=null) {
340 if(dv1.getColId().equals(targetColId))
341 dr.setDataValue(count, targetDataValue);
349 dv.setCellFormatter(formatter);
350 dv.setFormatId(formatter.getFormatId());
351 dv.setCellFormat(true);
355 } // setHtmlFormatters