2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.analytics.model.runtime;
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.*;
30 public class FormatProcessor extends RaptorObject {
33 private SemaphoreType semaphore = null;
35 private String colType = null;
37 private String dateFormat = null;
39 private HtmlFormatter defaultFormatter = null;
41 private HashMap formatters = null;
43 private HashMap convertedValues = null;
45 private boolean attemptNumericConversion = false;
47 public FormatProcessor(SemaphoreType sem, String colType, String dateFormat,
48 boolean attemptNumericConversion) {
56 this.colType = colType;
57 this.dateFormat = dateFormat;
59 this.attemptNumericConversion = attemptNumericConversion;
60 if (attemptNumericConversion)
61 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
63 FormatType fmt = (FormatType) iter.next();
64 if (!isNumber(fmt.getLessThanValue())) {
65 this.attemptNumericConversion = false;
70 formatters = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
71 convertedValues = new HashMap(semaphore.getFormatList().getFormat().size() * 4 / 3);
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());
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()));
88 private String convertValue(String origValue) {
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);
100 private String convertDateValue(String origValue) {
102 // Converts to YYYY-MM-DD if possible
103 if (nvl(dateFormat).length() == 0 || nvl(origValue).length() == 0)
106 if (dateFormat.equals("MM/DD/YYYY") && origValue.length() == 10)
107 // Special processing for the default date format - for saving DB
109 return origValue.substring(6, 10) + "-" + origValue.substring(0, 2) + "-"
110 + origValue.substring(3, 5);
113 // DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
114 // + dateFormat + "'), 'YYYY-MM-DD') val FROM DUAL");
116 String sql = Globals.getGenerateSqlVisualDual();
117 DataSet ds = DbUtils.executeQuery("SELECT TO_CHAR(TO_DATE('" + origValue + "', '"
118 + dateFormat + "'), 'YYYY-MM-DD') val"+sql);
120 if (ds.getRowCount() > 0)
121 return ds.getString(0, 0);
122 } catch (Exception e) {
126 } // convertDateValue
128 private String convertNumericValue(String origValue) {
130 // Converts to [20 pos.5 pos] if possible
131 if (nvl(origValue).length() == 0)
133 boolean isNegative = false;
135 StringBuffer integerValue = new StringBuffer();
136 StringBuffer fractionValue = new StringBuffer();
138 boolean beforeDecimalPoint = true;
139 for (int i = 0; i < origValue.length(); i++) {
140 char c = origValue.charAt(i);
142 beforeDecimalPoint = false;
143 else if (c == '-' && integerValue.length() == 0)
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);
151 fractionValue.append(c);
154 while (integerValue.length() < 20)
155 integerValue.insert(0, '0');
157 while (fractionValue.length() < 5)
158 fractionValue.append('0');
160 integerValue.append('.');
161 integerValue.append(fractionValue);
162 integerValue.insert(0, (isNegative ? '-' : '+'));
164 return integerValue.toString();
165 } // convertNumericValue
167 private boolean isNumber(String value) { // As per Raptor def, like
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 == '%'))
181 private String convertUnknownValue(String origValue) {
183 return isNumber(origValue) ? convertNumericValue(origValue) : origValue;
184 } // convertUnknownValue
186 private boolean isEqual(String value1, String value2) {
188 return value1.trim().equals(value2.trim());
191 private boolean isLessThan(String value1, String value2) {
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)) {
202 else if ((!value1IsNegative) && value2IsNegative) {
205 return Double.parseDouble(value1)<Double.parseDouble(value2);
208 return (value1.compareTo(value2) < 0);
211 public void setHtmlFormatters(DataValue dv, DataRow dr, boolean formatModified) {
213 if (semaphore == null)
216 HtmlFormatter formatter = defaultFormatter;
217 HtmlFormatter anyFormatter = null;
218 String sValue = convertValue(dv.getDisplayValue());
220 String compareColId = semaphore.getComment(); // When Column Id compare is different from formatting.
222 String targetColId = null;
223 if(semaphore.getTarget()!=null)
224 targetColId = semaphore.getTarget();
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();
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;
243 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
245 FormatType fmt = (FormatType) iter.next();
246 if(fmt.getLessThanValue().length() <= 0) {
247 anyFormatter = (HtmlFormatter) formatters.get(fmt.getFormatId());
248 anyFormatter.setFormatId(fmt.getFormatId());
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
258 FormatType fmt = (FormatType) iter.next();
259 // For Excel Download
261 if ((fmt.getFormatId() == null) || (fmt.getFormatId().length() <= 0)) {
266 String formatterValue = nvl((String) convertedValues.get(fmt.getFormatId()));
267 boolean valueMatched = false;
268 if (fmt.getExpression().equals("=")) {
269 valueMatched = isEqual(sValue, formatterValue);
272 else if (fmt.getExpression().equals("<>"))
273 valueMatched = (!isEqual(sValue, formatterValue));
274 else if (fmt.getExpression().equals(">")) {
275 valueMatched = (!(isEqual(sValue, formatterValue) || isLessThan(sValue,
278 else if (fmt.getExpression().equals(">=")) {
279 valueMatched = /* isEqual(sValue, formatterValue)|| */(!isLessThan(
280 sValue, formatterValue));
282 else if (fmt.getExpression().equals("<")) {
283 valueMatched = isLessThan(sValue, formatterValue);
285 else if (fmt.getExpression().equals("<=")) {
286 valueMatched = isEqual(sValue, formatterValue)
287 || isLessThan(sValue, formatterValue);
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());
298 if(!formatModified) formatter = anyFormatter;
299 //if(!((formatter!=null && formatter!=anyFormatter) || (defaultFormatter!=null && formatter!=defaultFormatter)))
300 // formatter = anyFormatter;
301 //formatter.setFormatId(anyFormatter.getFormatId());
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());
313 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
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;
319 formatter = anyFormatter;
322 //formatter.setFormatId(anyFormatter.getFormatId());
324 if(formatter != null) {
325 if (semaphore.getSemaphoreType().equals(AppConstants.ST_ROW)) {
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);
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);
346 for (dr.resetNext(); dr.hasNext();) {
347 DataValue dv1 = dr.getNext();
349 if(targetColId!=null) {
350 if(dv1.getColId().equals(targetColId))
351 dr.setDataValue(count, targetDataValue);
359 dv.setCellFormatter(formatter);
360 dv.setFormatId(formatter.getFormatId());
361 //if (!formatter.equals(defaultFormatter)) {
362 dv.setCellFormat(true);
367 } // setHtmlFormatters