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;
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 // DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
138 // + dateFormat + "'), 'YYYY-MM-DD') val FROM DUAL");
140 String sql = Globals.getGenerateSqlVisualDual();
141 DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
142 + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
144 if (ds.getRowCount() > 0)
145 return ds.getString(0, 0);
146 } catch (Exception e) {
150 } // convertDateValue
152 private String convertNumericValue(String origValue) {
154 // Converts to [20 pos.5 pos] if possible
155 if (nvl(origValue).length() == 0)
157 boolean isNegative = false;
159 StringBuffer integerValue = new StringBuffer();
160 StringBuffer fractionValue = new StringBuffer();
162 boolean beforeDecimalPoint = true;
163 for (int i = 0; i < origValue.length(); i++) {
164 char c = origValue.charAt(i);
166 beforeDecimalPoint = false;
167 else if (c == '-' && integerValue.length() == 0)
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);
175 fractionValue.append(c);
178 while (integerValue.length() < 20)
179 integerValue.insert(0, '0');
181 while (fractionValue.length() < 5)
182 fractionValue.append('0');
184 integerValue.append('.');
185 integerValue.append(fractionValue);
186 integerValue.insert(0, (isNegative ? '-' : '+'));
188 return integerValue.toString();
189 } // convertNumericValue
191 private boolean isNumber(String value) { // As per Raptor def, like
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 == '%'))
205 private String convertUnknownValue(String origValue) {
207 return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
208 } // convertUnknownValue
210 private boolean isEqual(String value1, String value2) {
212 return value1.trim().equals(value2.trim());
215 private boolean isLessThan(String value1, String value2) {
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)) {
226 else if ((!value1IsNegative) && value2IsNegative) {
229 return Double.parseDouble(value1)<Double.parseDouble(value2);
232 return (value1.compareTo(value2) < 0);
235 public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
237 if (semaphore == null)
240 HtmlFormatter formatter = defaultFormatter;
241 HtmlFormatter anyFormatter = null;
242 String sValue = convertValue(dv.getDisplayValue());
244 String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
246 String targetColId = null;
247 if(semaphore.getTarget()!=null)
248 targetColId = semaphore.getTarget();
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();
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;
267 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
269 FormatType fmt = (FormatType) iter.next();
270 if(fmt.getLessThanValue().length() <= 0) {
271 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
272 anyFormatter.setFormatId(fmt.getFormatId());
277 if( anyFormatter == null ) anyFormatter = formatter;
278 // String sValue = convertValue(dv.getDisplayValue());
279 //if (sValue.length() > 0) {
280 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
282 FormatType fmt = (FormatType) iter.next();
283 // For Excel Download
285 if ((fmt.getFormatId() == null) || (fmt.getFormatId().length() <= 0)) {
290 String formatterValue = nvl((String) convertedValues.get(fmt.getFormatId()));
291 boolean valueMatched = false;
292 if (fmt.getExpression().equals("=")) {
293 valueMatched = isEqual(sValue, formatterValue);
296 else if (fmt.getExpression().equals("<>"))
297 valueMatched = (!isEqual(sValue, formatterValue));
298 else if (fmt.getExpression().equals(">")) {
299 valueMatched = (!(isEqual(sValue, formatterValue) || isLessThan(sValue,
302 else if (fmt.getExpression().equals(">=")) {
303 valueMatched = /* isEqual(sValue, formatterValue)|| */(!isLessThan(
304 sValue, formatterValue));
306 else if (fmt.getExpression().equals("<")) {
307 valueMatched = isLessThan(sValue, formatterValue);
309 else if (fmt.getExpression().equals("<=")) {
310 valueMatched = isEqual(sValue, formatterValue)
311 || isLessThan(sValue, formatterValue);
313 //s_logger.debug("SYSOUT " + " " +sValue +" " +fmt.getBgColor() + " " + fmt.getLessThanValue()+ " " +valueMatched);
314 if (fmt.getLessThanValue().length() > 0 && valueMatched) {
315 formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
316 formatter.setFormatId(fmt.getFormatId());
317 formatModified = true;
318 //dv.setFormatId(fmt.getFormatId());
319 //dr.setFormatId(fmt.getFormatId());
322 if(!formatModified) formatter = anyFormatter;
323 //if(!((formatter!=null && formatter!=anyFormatter) || (defaultFormatter!=null && formatter!=defaultFormatter)))
324 // formatter = anyFormatter;
325 //formatter.setFormatId(anyFormatter.getFormatId());
327 /*else if ((fmt.getLessThanValue().length() <= 0)
328 && (fmt.getFormatId().length() > 0)) {
329 formatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
330 System.out.println("---------------lesser "+ fmt.getFormatId()+ " " + fmt.getBgColor());
331 dv.setFormatId(fmt.getFormatId());
332 dr.setFormatId(fmt.getFormatId());
337 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
339 FormatType fmt = (FormatType) iter.next();
340 if(fmt.getLessThanValue().length()<=0 && fmt.getExpression().length()<=0 && !fmt.isBold() && !fmt.isItalic() && !fmt.isUnderline() && fmt.getFontSize().equals("11")) {
341 formatter = defaultFormatter;
343 formatter = anyFormatter;
346 //formatter.setFormatId(anyFormatter.getFormatId());
348 if(formatter != null) {
349 if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
351 if (dr.getRowFormatter() == null || formatter != defaultFormatter) {
352 // Making sure the default formatter doesn't overwrite
353 // valid row formatter set from another column
354 dr.setRowFormatter(formatter);
355 dr.setFormatId(formatter.getFormatId());
356 // This is added for excel download
357 //if (!formatter.equals(defaultFormatter)) {
358 dr.setRowFormat(true);
363 if(nvl(targetColId).length()>0) {
364 if(targetDataValue!=null) {
365 targetDataValue.setCellFormatter(formatter);
366 targetDataValue.setFormatId(formatter.getFormatId());
367 //if (!formatter.equals(defaultFormatter)) {
368 targetDataValue.setCellFormat(true);
370 for (dr.resetNext(); dr.hasNext();) {
371 DataValue dv1 = dr.getNext();
373 if(targetColId!=null) {
374 if(dv1.getColId().equals(targetColId))
375 dr.setDataValue(count, targetDataValue);
383 dv.setCellFormatter(formatter);
384 dv.setFormatId(formatter.getFormatId());
385 //if (!formatter.equals(defaultFormatter)) {
386 dv.setCellFormat(true);
391 } // setHtmlFormatters