9d66717af1e5b0b0af6e1404c4ae04d68f0dce6d
[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.definition;
21
22 import java.io.Serializable;
23 import java.sql.Connection;
24 import java.util.Calendar;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.StringTokenizer;
29 import java.util.Vector;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.xml.bind.JAXBException;
35 import javax.xml.datatype.DatatypeConfigurationException;
36 import javax.xml.datatype.DatatypeFactory;
37 //import javax.xml.transform.stream.*;
38
39 import org.openecomp.portalsdk.analytics.controller.WizardSequence;
40 import org.openecomp.portalsdk.analytics.controller.WizardSequenceCrossTab;
41 import org.openecomp.portalsdk.analytics.controller.WizardSequenceDashboard;
42 import org.openecomp.portalsdk.analytics.controller.WizardSequenceLinear;
43 import org.openecomp.portalsdk.analytics.controller.WizardSequenceSQLBasedCrossTab;
44 import org.openecomp.portalsdk.analytics.controller.WizardSequenceSQLBasedHive;
45 import org.openecomp.portalsdk.analytics.controller.WizardSequenceSQLBasedLinear;
46 import org.openecomp.portalsdk.analytics.controller.WizardSequenceSQLBasedLinearDatamining;
47 import org.openecomp.portalsdk.analytics.error.RaptorException;
48 import org.openecomp.portalsdk.analytics.model.DataCache;
49 import org.openecomp.portalsdk.analytics.model.ReportLoader;
50 import org.openecomp.portalsdk.analytics.model.base.OrderBySeqComparator;
51 import org.openecomp.portalsdk.analytics.model.base.OrderSeqComparator;
52 import org.openecomp.portalsdk.analytics.model.base.ReportWrapper;
53 import org.openecomp.portalsdk.analytics.model.runtime.FormField;
54 import org.openecomp.portalsdk.analytics.system.AppUtils;
55 import org.openecomp.portalsdk.analytics.system.DbUtils;
56 import org.openecomp.portalsdk.analytics.system.Globals;
57 import org.openecomp.portalsdk.analytics.util.AppConstants;
58 import org.openecomp.portalsdk.analytics.util.DataSet;
59 import org.openecomp.portalsdk.analytics.util.Utils;
60 import org.openecomp.portalsdk.analytics.xmlobj.ChartAdditionalOptions;
61 import org.openecomp.portalsdk.analytics.xmlobj.ChartDrillOptions;
62 import org.openecomp.portalsdk.analytics.xmlobj.ColFilterType;
63 import org.openecomp.portalsdk.analytics.xmlobj.CustomReportType;
64 import org.openecomp.portalsdk.analytics.xmlobj.DataColumnList;
65 import org.openecomp.portalsdk.analytics.xmlobj.DataColumnType;
66 import org.openecomp.portalsdk.analytics.xmlobj.DataSourceType;
67 import org.openecomp.portalsdk.analytics.xmlobj.DataminingOptions;
68 import org.openecomp.portalsdk.analytics.xmlobj.FormFieldList;
69 import org.openecomp.portalsdk.analytics.xmlobj.FormFieldType;
70 import org.openecomp.portalsdk.analytics.xmlobj.FormatList;
71 import org.openecomp.portalsdk.analytics.xmlobj.FormatType;
72 import org.openecomp.portalsdk.analytics.xmlobj.JavascriptItemType;
73 import org.openecomp.portalsdk.analytics.xmlobj.ObjectFactory;
74 import org.openecomp.portalsdk.analytics.xmlobj.PredefinedValueList;
75 import org.openecomp.portalsdk.analytics.xmlobj.SemaphoreType;
76 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
77
78 /**<HR/>
79  * This class is part of <B><I>RAPTOR (Rapid Application Programming Tool for OLAP Reporting)</I></B><BR/> 
80  * <HR/>
81  *
82  * --------------------------------------------------------------------------------------------------<BR/>
83  * <B>ReportDefinition.java</B> - This involves in creating and modifying RAPTOR reports.   
84  * --------------------------------------------------------------------------------------------------<BR/>
85  *
86  *
87  * <U>Change Log</U><BR/><BR/>
88  * 
89  * 18-Aug-2009 : Version 8.5.1 (Sundar);<UL><LI> request Object is passed to prevent caching user/roles - Datamining/Hosting. </LI></UL>        
90  * 27-Jul-2009 : Version 8.4 (Sundar); <UL><LI>userIsAuthorizedToSeeLog is checked for Admin User instead of Super User.</LI></UL>                                                      
91  * 22-Jun-2009 : Version 8.4 (Sundar); <UL><LI>A new type ChartAdditionalOptions is introduced in RAPTOR XSD. 
92  * For this type a create procedure is added to this class.</LI></UL>                                                   
93  *
94  */
95
96 public class ReportDefinition extends ReportWrapper implements Serializable {
97         
98         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ReportDefinition.class);
99
100         
101         private ReportSchedule reportSchedule = null;
102
103         private WizardSequence wizardSequence = null;
104         
105
106         private boolean reportUpdateLogged = false; // Used to avoid multiple
107                                                                                                 // entries in the report log
108                                                                                                 // when persisting report on
109                                                                                                 // each step
110         
111         private ReportDefinition(CustomReportType crType, String reportID, String ownerID,
112                         String createID, String createDate, String updateID, String updateDate,
113                         String menuID, boolean menuApproved, HttpServletRequest request) throws RaptorException {
114                 super(crType, reportID, ownerID, createID, createDate, updateID, updateDate, menuID,
115                                 menuApproved);
116         if(reportID.equals("-1"))
117                 reportSchedule = new ReportSchedule(getReportID(), getOwnerID(), false, request);
118         else
119                 reportSchedule = new ReportSchedule(getReportID(), getOwnerID(), true, request);
120                 generateWizardSequence(null);
121         } // ReportDefinition
122
123         public ReportDefinition(ReportWrapper rw, HttpServletRequest request)throws RaptorException {
124                 super(rw);
125
126                 reportSchedule = new ReportSchedule(reportID, rw.getOwnerID(),false, request);
127                 generateWizardSequence(null);
128         } // ReportDefinition
129
130         private void setReportID(String reportID) {
131                 this.reportID = reportID;
132                 reportSchedule.setReportID(reportID);
133                 reportSchedule.setScheduleUserID(getOwnerID());
134         } // setReportID
135
136         public ReportSchedule getReportSchedule() {
137                 return reportSchedule;
138         }
139
140         public static ReportDefinition unmarshal(String reportXML, String reportID, HttpServletRequest request)
141                         throws RaptorException {
142                 ReportDefinition rn = null;
143                 CustomReportType crType = ReportWrapper.unmarshalCR(reportXML);
144                         //Log.write("Report [" + reportID + "]: XML unmarshalled", 4);
145                         logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] Report [" + reportID + "]: XML unmarshalled"));
146         
147                         rn = new ReportDefinition(crType, reportID, null, null, null, null, null, null, false, request);
148                         return rn;
149         } // unmarshal
150
151         public static ReportDefinition createBlank(HttpServletRequest request) throws RaptorException {
152                 String curTime = Utils.getCurrentDateTime();
153                 String userID = AppUtils.getUserID(request);
154                 ReportDefinition rd = new ReportDefinition(ReportWrapper.createBlankCR(userID), "-1",
155                                 userID, userID, curTime, userID, curTime, "", false, request);
156
157                 // Necessary initialization
158
159                 return rd;
160         } // ReportDefinition
161
162         public void setAsCopy(HttpServletRequest request) throws RaptorException  {
163                 verifySQLBasedReportAccess(request);
164
165                 replaceCustomReportWithClone();
166
167                 setReportID("-1");
168                 setReportName("Copy: " + getReportName());
169         } // setAsCopy
170
171         public WizardSequence getWizardSequence() {
172                 return wizardSequence;
173         } // getWizardSequence
174
175         public void generateWizardSequence(HttpServletRequest request) throws RaptorException {
176                 boolean userIsAuthorizedToSeeLog = false;
177                 String userId = null;
178                 if(request!=null) {
179                         userId = AppUtils.getUserID(request);
180                 if (userId != null)
181                         userIsAuthorizedToSeeLog = AppUtils.isAdminUser(request)
182                                         || AppUtils.isAdminUser(request);
183                 //System.out.println("******** Report Type  "+getReportType() + " userIsAuthorizedToSeeLog " + userIsAuthorizedToSeeLog);
184                 }
185                 if (getReportType().equals(AppConstants.RT_LINEAR)){
186                         if (getReportDefType().equals(AppConstants.RD_SQL_BASED))
187                                 wizardSequence = new WizardSequenceSQLBasedLinear(userIsAuthorizedToSeeLog);
188                         else if (getReportDefType().equals(AppConstants.RD_SQL_BASED_DATAMIN))
189                                 wizardSequence = new WizardSequenceSQLBasedLinearDatamining(userIsAuthorizedToSeeLog);
190                         else
191                                 wizardSequence = new WizardSequenceLinear(userIsAuthorizedToSeeLog);
192                 } else if (getReportType().equals(AppConstants.RT_CROSSTAB)) {
193                         if (getReportDefType().equals(AppConstants.RD_SQL_BASED))
194                                 wizardSequence = new WizardSequenceSQLBasedCrossTab(userIsAuthorizedToSeeLog);
195                         else
196                                 wizardSequence = new WizardSequenceCrossTab(userIsAuthorizedToSeeLog);
197                 } else if (getReportType().equals(AppConstants.RT_DASHBOARD)) {
198                         wizardSequence = new WizardSequenceDashboard(userIsAuthorizedToSeeLog);
199                 } else if (getReportType().equals(AppConstants.RT_HIVE)) {
200                         wizardSequence = new WizardSequenceSQLBasedHive(userIsAuthorizedToSeeLog);
201                 } else 
202                         wizardSequence = new WizardSequence();
203         } // generateWizardSequence
204
205         private boolean canPersistDashboard() {
206            //System.out.println(" getDashBoardReports().getReportsList().size() " + getDashBoardReports().getReportsList().size());
207                 /* Commented for New DashBoard
208            if (getDashBoardReports()!=null && getDashBoardReports().getReportsList()!=null && getDashBoardReports().getReportsList().size() > 0) {
209                    for (Iterator iter = getDashBoardReports().getReportsList().iterator(); iter.hasNext();) {
210                            Reports report = (Reports)iter.next();
211                            try {
212                            if(Integer.parseInt(report.getReportId())>0) return true;
213                            } catch (NumberFormatException ex) {}
214                    } // for
215            } //if
216            */
217                 
218                 //if( )
219            return nvl(getDashboardLayoutHTML()).length() > 0;
220         } //canPersistDashboard
221         
222         private boolean canPersistLinearReport() {
223                 System.out.println("&&&&&&&&&&&&&&&&&&&&&& canPersistLinearReport");
224                 boolean visibleColExist = false;
225
226                 if (getDataSourceList().getDataSource().size() > 0) {
227                         for (Iterator iter = getAllColumns().iterator(); iter.hasNext();) {
228                                 DataColumnType dct = (DataColumnType) iter.next();
229
230                                 if (dct.isVisible()) {
231                                         visibleColExist = true;
232                                         break;
233                                 }
234                         } // for
235                 } // if
236
237                 System.out.println("&&&&&&&&&&&&&&&&&&&&&& visibleColExist " + visibleColExist);
238                 return visibleColExist;
239         } // canPersistLinearReport
240
241         private boolean canPersistCrossTabReport() {
242                 boolean rowColExist = false;
243                 boolean colColExist = false;
244                 boolean valColExist = false;
245
246                 if (getDataSourceList().getDataSource().size() > 0) {
247                         for (Iterator iter = getAllColumns().iterator(); iter.hasNext();) {
248                                 DataColumnType dct = (DataColumnType) iter.next();
249
250                                 if (nvl(dct.getCrossTabValue()).equals(AppConstants.CV_ROW))
251                                         rowColExist = true;
252                                 if (nvl(dct.getCrossTabValue()).equals(AppConstants.CV_COLUMN))
253                                         colColExist = true;
254                                 if (nvl(dct.getCrossTabValue()).equals(AppConstants.CV_VALUE))
255                                         valColExist = true;
256                         } // for
257                 } // if
258
259                 return rowColExist && colColExist && valColExist;
260         } // canPersistCrossTabReport
261
262         private boolean canPersistReport() {
263                 return getReportType().equals(AppConstants.RT_CROSSTAB) ? canPersistCrossTabReport()
264                                 : (getReportType().equals(AppConstants.RT_LINEAR)? canPersistLinearReport():((getReportType().equals(AppConstants.RT_HIVE)? canPersistLinearReport():canPersistDashboard())));
265         } // canPersistReport
266
267         public void persistReport(HttpServletRequest request) throws RaptorException {
268                 if (!canPersistReport()) {
269                         System.out.println("&&&&&&&&&&&&&&&&&&&&&& In !canPersistReport ReportType: " + getReportType());
270                         return;
271                 } else {
272                         System.out.println("&&&&&&&&&&&&&&&&&&&&&&  Not In !canPersistReport");
273                 }
274
275                 Connection connection = null;
276                 try {
277                         String userID = AppUtils.getUserID(request);
278                         String reportXML = marshal();
279                         logger.debug(EELFLoggerDelegate.debugLogger, ("Ocurring during Schedule "));
280                         System.out.println("&&&&&&&&&&&&&&&&&&&&&&  Report ID " + reportID);
281                         if (nvl(reportID, "-1").equals("-1")) {
282                 connection = DbUtils.startTransaction();
283                                 // Add report
284                 String sql = "";
285                 if (nvl(Globals.getAdhocReportSequence()).length()> 0 && nvl(Globals.getAdhocUserRoldId()).length() > 0 && AppUtils.isUserInRole(request, Globals.getAdhocUserRoldId()) && !AppUtils.isAdminUser(request)) {
286                         //sql = "SELECT "+ Globals.getAdhocReportSequence() + ".nextval FROM dual";
287                         sql = Globals.getPersistReportAdhoc();
288                         sql = sql.replace("[Globals.getAdhocReportSequence()]", Globals.getAdhocReportSequence());
289                         
290                 } else{ 
291                         //sql = "SELECT seq_cr_report.nextval FROM dual";
292                         sql = Globals.getNewReportData();
293                 }
294                 DataSet ds = DbUtils.executeQuery(connection,sql);
295                                 setReportID(ds.getString(0, 0));
296
297                                 reportSecurity.reportCreate(reportID, userID, isPublic());
298                                 ReportLoader.createCustomReportRec(connection, this, reportXML);
299                                 ReportLoader.createReportLogEntry(connection, reportID, userID,
300                                                 AppConstants.RLA_CREATE, "", "");
301                                 reportUpdateLogged = true;
302                                 logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] DB insert report " + reportID + " succesfull"));
303                         } else {
304                                 // Update report
305                                 verifySQLBasedReportAccess(request);
306                                 reportSecurity.reportUpdate(request);
307                 connection = DbUtils.startTransaction();
308                                 ReportLoader.updateCustomReportRec(connection, this, reportXML);
309                                 if (!reportUpdateLogged) {
310                                         ReportLoader.createReportLogEntry(connection, reportID, userID,
311                                                         AppConstants.RLA_UPDATE,"","");
312                                         reportUpdateLogged = true;
313                                 } // if
314                                 logger.debug(EELFLoggerDelegate.debugLogger, ("[DEBUG MESSAGE FROM RAPTOR] DB update report " + reportID + " succesfull"));
315                         }
316
317                         getReportSchedule().persistScheduleData(connection, request);
318
319                         DbUtils.commitTransaction(connection);
320                 } catch (RaptorException e) {
321             e.printStackTrace();
322                         DbUtils.rollbackTransaction(connection);
323                         throw e;
324                 } finally {
325                 DbUtils.clearConnection(connection);      
326         }
327         } // persistReport
328
329         public String getCrossTabDisplayValue(String crossTabValue) {
330                 return nvl(crossTabValue).equals(AppConstants.CV_ROW) ? "Row headings" : (nvl(
331                                 crossTabValue).equals(AppConstants.CV_COLUMN) ? "Column headings" : (nvl(
332                                 crossTabValue).equals(AppConstants.CV_VALUE) ? "Report values" : "Invisible/Filter"));
333         } // getCrossTabDisplayValue
334
335         public String getCrossTabDisplayValue(DataColumnType dct) {
336                 return getCrossTabDisplayValue(dct.getCrossTabValue());
337         } // getCrossTabDisplayValue
338
339         public String getColumnLabel(DataColumnType dct) throws Exception {
340                 String tableName = getTableById(dct.getTableId()).getTableName();
341                 Vector dbColumns = null;
342                 dbColumns = DataCache.getReportTableDbColumns(tableName, cr.getDbInfo());
343                 if (dbColumns != null)
344                         for (int i = 0; i < dbColumns.size(); i++) {
345                                 DBColumnInfo dbCol = (DBColumnInfo) dbColumns.get(i);
346                                 if (dct.getDbColName().equals(dbCol.getColName()))
347                                         return dbCol.getLabel();
348                         } // for
349
350                 return "";
351         } // getCrossTabDisplayValue
352
353         public String getFilterLabel(ColFilterType cft) {
354                 StringBuffer fLabel = new StringBuffer();
355
356                 fLabel.append(cft.getExpression());
357                 fLabel.append(" ");
358                 if (cft.getArgType() != null)
359                         if (cft.getArgType().equals(AppConstants.AT_FORMULA)) {
360                                 fLabel.append("[" + cft.getArgValue() + "]");
361                         } else if (cft.getArgType().equals(AppConstants.AT_VALUE)) {
362                                 fLabel.append(cft.getArgValue());
363                         } else if (cft.getArgType().equals(AppConstants.AT_LIST)) {
364                                 fLabel.append("(" + cft.getArgValue() + ")");
365                         } else if (cft.getArgType().equals(AppConstants.AT_COLUMN)) {
366                                 DataColumnType dctFilter = getColumnById(cft.getArgValue());
367                                 fLabel.append("[" + dctFilter.getDisplayName() + "]");
368                         } else if (cft.getArgType().equals(AppConstants.AT_FORM)) {
369                                 fLabel.append("[Form Field]");
370                         }
371
372                 return fLabel.toString();
373         } // getFilterLabel
374
375         public Vector getReportUsers(HttpServletRequest request) throws RaptorException {
376                 return reportSecurity.getReportUsers(request);
377         } // getReportUsers
378
379         public Vector getReportRoles(HttpServletRequest request) throws RaptorException {
380                 return reportSecurity.getReportRoles(request);
381         } // getReportRoles
382
383         /** ************************************************************************************************* */
384
385         public void clearAllDrillDowns() {
386                 List reportCols = getAllColumns();
387                 for (int i = 0; i < reportCols.size(); i++) {
388                         DataColumnType dct = (DataColumnType) reportCols.get(i);
389                         dct.setDrillDownURL(null);
390                         dct.setDrillDownParams(null);
391                         dct.setDrillDownType(null);
392                 } // for
393         } // clearAllDrillDowns
394
395         public void setOuterJoin(DataSourceType curTable, String joinType) {
396                 String refDefinition = nvl(curTable.getRefDefinition());
397                 int outerJoinIdx = refDefinition.indexOf(" (+)");
398                 if (outerJoinIdx >= 0)
399                         // Clear existing outer join
400                         if (outerJoinIdx == (refDefinition.length() - 4))
401                                 refDefinition = refDefinition.substring(0, outerJoinIdx);
402                         else
403                                 refDefinition = refDefinition.substring(0, outerJoinIdx)
404                                                 + refDefinition.substring(outerJoinIdx + 4);
405
406                 int equalSignIdx = refDefinition.indexOf("=");
407                 if (equalSignIdx < 0)
408                         // Ref. definition not present
409                         return;
410
411                 if (refDefinition.indexOf(curTable.getTableId()) < equalSignIdx) {
412                         // Cur. table is on the left side
413                         if (nvl(joinType).equals(AppConstants.OJ_CURRENT))
414                                 refDefinition = refDefinition.substring(0, equalSignIdx) + " (+)"
415                                                 + refDefinition.substring(equalSignIdx);
416                         else if (nvl(joinType).equals(AppConstants.OJ_JOINED))
417                                 refDefinition = refDefinition + " (+)";
418                 } else {
419                         // Joined table is on the left side
420                         if (nvl(joinType).equals(AppConstants.OJ_CURRENT))
421                                 refDefinition = refDefinition + " (+)";
422                         else if (nvl(joinType).equals(AppConstants.OJ_JOINED))
423                                 refDefinition = refDefinition.substring(0, equalSignIdx) + " (+)"
424                                                 + refDefinition.substring(equalSignIdx);
425                 }
426
427                 curTable.setRefDefinition(refDefinition);
428         } // setOuterJoin
429
430         public void addDataSourceType(ObjectFactory objFactory, String tableId, String tableName,
431                         String tablePK, String displayName, String refTableId, String refDefinition,
432                         String comment) throws RaptorException {
433                 DataSourceType dst = objFactory.createDataSourceType();
434
435                 dst.setTableId(tableId);
436                 dst.setTableName(tableName);
437                 dst.setTablePK(tablePK);
438                 dst.setDisplayName(displayName);
439                 if (nvl(refTableId).length() > 0)
440                         dst.setRefTableId(refTableId);
441                 if (nvl(refDefinition).length() > 0)
442                         dst.setRefDefinition(refDefinition);
443                 if (nvl(comment).length() > 0)
444                         dst.setComment(comment);
445
446                 DataColumnList dataColumnList = objFactory.createDataColumnList();
447                 dst.setDataColumnList(dataColumnList);
448
449                 getDataSourceList().getDataSource().add(dst);
450
451                 resetCache(true);
452         } // addDataSourceType
453
454         public void deleteDataSourceType(String tableId) {
455                 super.deleteDataSourceType(tableId);
456         } // deleteDataSourceType
457
458         public String getUniqueColumnId(String colName) {
459                 String colId = "";
460
461                 int colIdN = getAllColumns().size() + 1;
462                 do {
463                         colId = colName.substring(0, 2).toLowerCase() + (colIdN++);
464                 } while (getColumnById(colId) != null);
465
466                 return colId;
467         } // getUniqueColumnId
468
469         public DataColumnType addDataColumnType(ObjectFactory objFactory, String colId,
470                         String tableId, // Table to which the new column belongs
471                         String dbColName, String crossTabValue, String colName, String displayName,
472                         int displayWidth, String displayAlignment, int orderSeq, boolean visible,
473                         boolean calculated, String colType, String colFormat, boolean groupBreak,
474                         int orderBySeq, String orderByAscDesc, String displayTotal, String colOnChart,
475                         int chartSeq, String drillDownType, String drillDownURL, String drillDownParams,
476                         String semaphoreId, String comment) throws RaptorException {
477                 DataColumnType dct = null;
478                         dct = objFactory.createDataColumnType();
479         
480                         dct.setColId(colId);
481                         dct.setTableId(tableId);
482                         dct.setDbColName(dbColName);
483                         if (nvl(crossTabValue).length() > 0)
484                                 dct.setCrossTabValue(crossTabValue);
485                         dct.setColName(colName);
486                         dct.setDisplayName(displayName);
487                         if (displayWidth > 0)
488                                 dct.setDisplayWidth(displayWidth);
489                         if (nvl(displayAlignment).length() > 0)
490                                 dct.setDisplayAlignment(displayAlignment);
491                         if (orderSeq > 0)
492                                 dct.setOrderSeq(orderSeq);
493                         else
494                                 dct.setOrderSeq(getAllColumns().size() + 1);
495                         dct.setVisible(visible);
496                         dct.setCalculated(calculated);
497                         // dct.setColType(colType);
498                         if (nvl(colFormat).length() > 0)
499                                 dct.setColFormat(colFormat);
500                         dct.setGroupBreak(groupBreak);
501                         if (orderBySeq > 0)
502                                 dct.setOrderBySeq(orderBySeq);
503                         if (nvl(orderByAscDesc).length() > 0)
504                                 dct.setOrderByAscDesc(orderByAscDesc);
505                         if (nvl(displayTotal).length() > 0)
506                                 dct.setDisplayTotal(displayTotal);
507                         if (nvl(colOnChart).length() > 0)
508                                 dct.setColOnChart(colOnChart);
509                         if (chartSeq > 0)
510                                 dct.setChartSeq(chartSeq);
511                         if (nvl(drillDownType).length() > 0)
512                                 dct.setDrillDownType(drillDownType);
513                         if (nvl(drillDownURL).length() > 0)
514                                 dct.setDrillDownURL(drillDownURL);
515                         if (nvl(drillDownParams).length() > 0)
516                                 dct.setDrillDownParams(drillDownParams);
517                         if (nvl(semaphoreId).length() > 0)
518                                 dct.setSemaphoreId(semaphoreId);
519                         if (nvl(comment).length() > 0)
520                                 dct.setComment(comment);
521         
522                         dct.setDbColType(colType);
523                         adjustColumnType(dct);
524         
525                         // ColFilterList colFilterList = objFactory.createColFilterList();
526                         // dct.setColFilterList(colFilterList);
527         
528                         getTableById(tableId).getDataColumnList().getDataColumn().add(dct);
529         
530                         resetCache(false);
531
532                 return dct;
533         } // addDataColumnType
534
535         public void deleteDataColumnType(String colId) {
536                 int colOrder = getColumnById(colId).getOrderSeq();
537
538                 List dcList = getColumnTableById(colId).getDataColumnList().getDataColumn();
539                 for (Iterator iterC = dcList.iterator(); iterC.hasNext();) {
540                         DataColumnType dct = (DataColumnType) iterC.next();
541
542                         if (dct.getColId().equals(colId) && dct.getOrderSeq() == colOrder)
543                                 iterC.remove();
544                         else if (dct.getOrderSeq() > colOrder)
545                                 dct.setOrderSeq(dct.getOrderSeq() - 1);
546                 } // for
547
548                 if (getFormFieldList() != null)
549                         for (Iterator iter = getFormFieldList().getFormField().iterator(); iter.hasNext();) {
550                                 FormFieldType fft = (FormFieldType) iter.next();
551                                 if (nvl(fft.getColId()).equals(colId)) {
552                                         fft.setColId("");
553                                         fft.setFieldType(FormField.FFT_TEXT);
554                                         if (nvl(fft.getDefaultValue()).equals(AppConstants.FILTER_MAX_VALUE)
555                                                         || nvl(fft.getDefaultValue())
556                                                                         .equals(AppConstants.FILTER_MIN_VALUE))
557                                                 fft.setDefaultValue("");
558                                 } // if
559                         } // for
560
561                 resetCache(false);
562                 resetColumnOrderValues();
563         } // deleteDataColumnType
564
565         public void shiftColumnOrderUp(String colId) {
566                 List reportCols = getAllColumns();
567                 for (int i = 0; i < reportCols.size(); i++) {
568                         DataColumnType dct = (DataColumnType) reportCols.get(i);
569
570                         if (dct.getColId().equals(colId) && (i > 0)) {
571                                 DataColumnType dctUp = (DataColumnType) reportCols.get(i - 1);
572                                 dctUp.setOrderSeq(dctUp.getOrderSeq() + 1);
573                                 dct.setOrderSeq(dct.getOrderSeq() - 1);
574                                 break;
575                         } // if
576                 } // for
577
578                 Collections.sort(reportCols, new OrderSeqComparator());
579                 resetCache(true);
580                 resetColumnOrderValues();
581         } // shiftColumnOrderUp
582
583         public void shiftColumnOrderDown(String colId) {
584                 List reportCols = getAllColumns();
585                 for (int i = 0; i < reportCols.size(); i++) {
586                         DataColumnType dct = (DataColumnType) reportCols.get(i);
587
588                         if (dct.getColId().equals(colId) && (i < reportCols.size() - 1)) {
589                                 DataColumnType dctDown = (DataColumnType) reportCols.get(i + 1);
590                                 dctDown.setOrderSeq(dctDown.getOrderSeq() - 1);
591                                 dct.setOrderSeq(dct.getOrderSeq() + 1);
592                                 break;
593                         } // if
594                 } // for
595
596                 Collections.sort(reportCols, new OrderSeqComparator());
597                 resetCache(true);
598                 resetColumnOrderValues();
599         } // shiftColumnOrderDown
600
601         public void resetColumnOrderValues() {
602                 List reportCols = getAllColumns();
603
604                 int colOrder = 0;
605                 for (Iterator iter = reportCols.iterator(); iter.hasNext();) {
606                         DataColumnType dct = (DataColumnType) iter.next();
607                         dct.setOrderSeq(++colOrder);
608                 } // for
609
610                 Collections.sort(reportCols, new OrderSeqComparator());
611         } // resetColumnOrderValues
612
613         public void addColFilterType(ObjectFactory objFactory, String colId, // Column
614                                                                                                                                                         // to
615                                                                                                                                                         // which
616                                                                                                                                                         // the
617                                                                                                                                                         // new
618                                                                                                                                                         // filter
619                                                                                                                                                         // belongs
620                         String joinCondition, String openBrackets, String expression, String argType,
621                         String argValue, String closeBrackets, String comment) throws RaptorException {
622                         ColFilterType cft = objFactory.createColFilterType();
623         
624                         cft.setColId(colId);
625                         cft.setJoinCondition(nvl(joinCondition, "AND"));
626                         if (nvl(openBrackets).length() > 0)
627                                 cft.setOpenBrackets(openBrackets);
628                         cft.setExpression(expression);
629                         if (nvl(argType).length() > 0)
630                                 cft.setArgType(argType);
631                         if (nvl(argValue).length() > 0)
632                                 cft.setArgValue(argValue);
633                         if (nvl(closeBrackets).length() > 0)
634                                 cft.setCloseBrackets(closeBrackets);
635                         if (nvl(comment).length() > 0)
636                                 cft.setComment(comment);
637         
638                         DataColumnType dct = getColumnById(colId);
639                         if (dct != null) {
640                                 if (dct.getColFilterList() == null)
641                                         dct.setColFilterList(objFactory.createColFilterList());
642         
643                                 cft.setFilterSeq(dct.getColFilterList().getColFilter().size());
644                                 dct.getColFilterList().getColFilter().add(cft);
645                         } // if
646         
647                         resetCache(true);
648         } // addColFilterType
649
650         public void removeColumnFilter(String colId, int filterPos) {
651                 DataColumnType dct = getColumnById(colId);
652
653                 if (dct.getColFilterList() != null)
654                         try {
655                                 dct.getColFilterList().getColFilter().remove(filterPos);
656                         } catch (IndexOutOfBoundsException e) {
657                         }
658
659                 resetCache(true);
660         } // removeColumnFilter
661
662         public void addColumnSort(String colId, String ascDesc) {
663                 addColumnSort(colId, ascDesc, -1);
664         } // addColumnSort
665
666         public void addColumnSort(String colId, String ascDesc, int sortOrder) {
667                 if (sortOrder <= 0) {
668                         sortOrder = 1;
669                         List reportCols = getAllColumns();
670                         for (Iterator iter = reportCols.iterator(); iter.hasNext();)
671                                 if (((DataColumnType) iter.next()).getOrderBySeq() > 0)
672                                         sortOrder++;
673                 } // if
674
675                 DataColumnType dct = getColumnById(colId);
676                 dct.setOrderBySeq(sortOrder);
677                 dct.setOrderByAscDesc(ascDesc);
678
679                 resetCache(true);
680         } // addColumnSort
681
682         public void removeColumnSort(String colId) {
683                 DataColumnType dct = getColumnById(colId);
684                 int sortOrder = dct.getOrderBySeq();
685
686                 dct.setOrderBySeq(0);
687                 dct.setOrderByAscDesc(null);
688
689                 if (sortOrder > 0) {
690                         List reportCols = getAllColumns();
691                         for (Iterator iter = reportCols.iterator(); iter.hasNext();) {
692                                 DataColumnType dct2 = (DataColumnType) iter.next();
693
694                                 if (dct2.getOrderBySeq() > sortOrder)
695                                         dct2.setOrderBySeq(dct2.getOrderBySeq() - 1);
696                         } // for
697                 } // if
698
699                 resetCache(true);
700         } // removeColumnSort
701
702         public void shiftColumnSortUp(String colId) {
703                 List reportCols = getAllColumns();
704                 Collections.sort(reportCols, new OrderBySeqComparator());
705
706                 for (int i = 0; i < reportCols.size(); i++) {
707                         DataColumnType dct = (DataColumnType) reportCols.get(i);
708
709                         if (dct.getColId().equals(colId) && (dct.getOrderBySeq() > 0)) {
710                                 DataColumnType dctUp = (DataColumnType) reportCols.get(i - 1);
711                                 if (dctUp.getOrderBySeq() > 0)
712                                         dctUp.setOrderBySeq(dctUp.getOrderBySeq() + 1);
713                                 dct.setOrderBySeq(dct.getOrderBySeq() - 1);
714                                 break;
715                         } // if
716                 } // for
717
718                 Collections.sort(reportCols, new OrderSeqComparator());
719                 resetCache(true);
720         } // shiftColumnSortUp
721
722         public void shiftColumnSortDown(String colId) {
723                 List reportCols = getAllColumns();
724                 Collections.sort(reportCols, new OrderBySeqComparator());
725
726                 for (int i = 0; i < reportCols.size(); i++) {
727                         DataColumnType dct = (DataColumnType) reportCols.get(i);
728
729                         if (dct.getColId().equals(colId) && (dct.getOrderBySeq() > 0)) {
730                                 DataColumnType dctDown = (DataColumnType) reportCols.get(i + 1);
731                                 if (dctDown.getOrderBySeq() > 0)
732                                         dctDown.setOrderBySeq(dctDown.getOrderBySeq() - 1);
733                                 dct.setOrderBySeq(dct.getOrderBySeq() + 1);
734                                 break;
735                         } // if
736                 } // for
737
738                 Collections.sort(reportCols, new OrderSeqComparator());
739                 resetCache(true);
740         } // shiftColumnSortDown
741
742         /** ************************************************************************************************* */
743
744         public String generateNewSemaphoreId() {
745                 if (getSemaphoreList() == null)
746                         return "sem1";
747
748                 String semaphoreId = null;
749                 boolean idExists = true;
750                 for (int i = 1; idExists; i++) {
751                         semaphoreId = "sem" + i;
752                         idExists = false;
753                         for (Iterator iter = getSemaphoreList().getSemaphore().iterator(); iter.hasNext();)
754                                 if (semaphoreId.equals(((SemaphoreType) iter.next()).getSemaphoreId())) {
755                                         idExists = true;
756                                         break;
757                                 }
758                 } // for
759
760                 return semaphoreId;
761         } // generateNewSemaphoreId
762
763         public SemaphoreType addSemaphore(ObjectFactory objFactory, SemaphoreType semaphoreType)
764                         throws RaptorException {
765                 SemaphoreType sem =  null;
766                 try { 
767                 if (getSemaphoreList() == null)
768                         setSemaphoreList(objFactory.createSemaphoreList());
769
770                 String semaphoreName = null;
771                 boolean nameExists = true;
772                 for (int i = 1; nameExists; i++) {
773                         semaphoreName = semaphoreType.getSemaphoreName() + ((i > 1) ? (" v" + i) : "");
774                         nameExists = false;
775                         for (Iterator iter2 = getSemaphoreList().getSemaphore().iterator(); iter2
776                                         .hasNext();)
777                                 if (semaphoreName.equals(((SemaphoreType) iter2.next()).getSemaphoreName())) {
778                                         nameExists = true;
779                                         break;
780                                 }
781                 } // for
782
783                 sem = cloneSemaphoreType(objFactory, semaphoreType);
784                 getSemaphoreList().getSemaphore().add(sem);
785
786                 sem.setSemaphoreId(generateNewSemaphoreId());
787                 sem.setSemaphoreName(semaphoreName);
788                 } catch (JAXBException ex) {
789                         throw new RaptorException(ex.getMessage(), ex.getCause());
790                 }
791
792                 return sem;
793         } // addSemaphore
794
795         public SemaphoreType addSemaphoreType(ObjectFactory objFactory, String semaphoreName,
796                         String semaphoreType, String comment) throws RaptorException {
797                 SemaphoreType sem =  null;
798                         if (getSemaphoreList() == null)
799                                 setSemaphoreList(objFactory.createSemaphoreList());
800         
801                         sem = objFactory.createSemaphoreType();
802                         getSemaphoreList().getSemaphore().add(sem);
803         
804                         sem.setSemaphoreId(generateNewSemaphoreId());
805                         sem.setSemaphoreName(semaphoreName);
806                         sem.setSemaphoreType(nvl(semaphoreType));
807                         if (nvl(comment).length() > 0)
808                                 sem.setComment(comment);
809         
810                         FormatList formatList = objFactory.createFormatList();
811                         sem.setFormatList(formatList);
812                 return sem;
813         } // addSemaphoreType
814         
815
816         public String getNextIdForJavaScriptElement (ObjectFactory objFactory, String fieldId) throws RaptorException {
817                 String id = "";
818                 JavascriptItemType jit = null;
819                 int incr = 0;
820                         if (getJavascriptList() == null) {
821                                 setJavascriptList(objFactory.createJavascriptList());
822                                 return fieldId + "|1";
823                         } else {
824                                 if(getJavascriptList().getJavascriptItem().iterator().hasNext()) {
825                                 for (Iterator iter = getJavascriptList().getJavascriptItem().iterator(); iter.hasNext();) {
826                                         jit = (JavascriptItemType) iter.next();
827                                         logger.debug(EELFLoggerDelegate.debugLogger, ("^^^^^JAVASCRIPTITEMTYPE " + jit.getFieldId() + " " + fieldId + " " + id));
828                                         if(nvl(jit.getFieldId()).length()>0 && jit.getFieldId().equals(fieldId)) {
829                                                 ++incr;
830                                         }
831                                 } // for
832                                 return fieldId + "|"+incr;
833                                 } else {
834                                         return fieldId + "|1";
835                                 }
836                                 
837                         }
838                 //return null;
839         }
840         
841         public JavascriptItemType addJavascriptType(ObjectFactory objFactory, String id) throws RaptorException {
842                 JavascriptItemType javascriptItemType =  null;
843                 int flag = 0; // checking whether id existing in the list
844                         if (getJavascriptList() == null) {
845                                 setJavascriptList(objFactory.createJavascriptList());
846                                 javascriptItemType = objFactory.createJavascriptItemType();
847                                 getJavascriptList().getJavascriptItem().add(javascriptItemType);
848                                 return javascriptItemType;
849                         } else {
850                                 
851                                 for (Iterator iter = getJavascriptList().getJavascriptItem().iterator(); iter.hasNext();) {
852                                         javascriptItemType  = (JavascriptItemType)iter.next();
853                                         if(javascriptItemType.getId().equals(id) && !id.startsWith("-1")) {
854                                                 flag = 1;
855                                                 break;
856                                         }
857                                 }
858                                 if(flag == 1) return javascriptItemType;
859                                 else {
860                                         javascriptItemType = objFactory.createJavascriptItemType();
861                                         getJavascriptList().getJavascriptItem().add(javascriptItemType);
862                                         return javascriptItemType;
863                                 }
864                         }
865                         
866         } // addSemaphoreType   
867         
868         public boolean deleteJavascriptType(String id) throws RaptorException {
869                 JavascriptItemType javascriptType =  null;
870                 if (getJavascriptList() == null)
871                         return true;
872                 for (Iterator iter = getJavascriptList().getJavascriptItem().iterator(); iter.hasNext();) {
873                         javascriptType  = (JavascriptItemType)iter.next();
874                         if(javascriptType.getId().equals(id)) {
875                                 iter.remove();
876                                 return true;
877                         }
878                 }
879                 return false;
880         } // addSemaphoreType           
881
882         public static FormatType addEmptyFormatType(ObjectFactory objFactory,
883                         SemaphoreType semaphore) throws RaptorException {
884                 FormatType fmt = null;
885                         fmt = objFactory.createFormatType();
886                         semaphore.getFormatList().getFormat().add(fmt);
887         
888                         String formatId = null;
889                         boolean idExists = true;
890                         for (int i = 1; idExists; i++) {
891                                 formatId = semaphore.getSemaphoreId() + "_fmt" + i;
892                                 idExists = false;
893                                 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter
894                                                 .hasNext();)
895                                         if (formatId.equals(((FormatType) iter.next()).getFormatId())) {
896                                                 idExists = true;
897                                                 break;
898                                         }
899                         } // for
900                         fmt.setFormatId(formatId);
901                 return fmt;
902         } // addEmptyFormatType
903
904         public static void deleteFormatType(SemaphoreType semaphore, String formatId) {
905                 for (Iterator iter = semaphore.getFormatList().getFormat().iterator(); iter.hasNext();)
906                         if (formatId.equals(((FormatType) iter.next()).getFormatId())) {
907                                 iter.remove();
908                                 break;
909                         } // if
910         } // deleteFormatType
911
912         public FormFieldType addFormFieldType(ObjectFactory objFactory, String fieldName,
913                         String colId, String fieldType, String validationType, String mandatory,
914                         String defaultValue, String fieldSQL, String comment, Calendar rangeStartDate, Calendar rangeEndDate,
915                         String rangeStartDateSQL, String rangeEndDateSQL) throws RaptorException {
916                 FormFieldType fft = null;
917                         fft = objFactory.createFormFieldType();
918         
919                         fft.setFieldName(fieldName);
920                         fft.setColId(colId);
921                         fft.setFieldType(fieldType);
922                         fft.setValidationType(validationType);
923                         fft.setMandatory(nvl(mandatory, "N"));
924                         fft.setDefaultValue(nvl(defaultValue));
925                         fft.setOrderBySeq((getFormFieldList() == null) ? 1 : getFormFieldList().getFormField()
926                                         .size() + 1);
927                         fft.setFieldSQL(fieldSQL);
928                         //fft.setRangeStartDate(rangeStartDate);
929                         //fft.setRangeEndDate(rangeEndDate);
930
931             try {
932                 fft.setRangeStartDate(DatatypeFactory.newInstance()
933                         .newXMLGregorianCalendar(rangeStartDate.YEAR, rangeStartDate.MONTH, rangeStartDate.DAY_OF_WEEK, rangeStartDate.HOUR, rangeStartDate.MINUTE, rangeStartDate.SECOND, rangeStartDate.MILLISECOND, rangeStartDate.ZONE_OFFSET));
934                 fft.setRangeStartDate(DatatypeFactory.newInstance()
935                         .newXMLGregorianCalendar(rangeEndDate.YEAR, rangeEndDate.MONTH, rangeEndDate.DAY_OF_WEEK, rangeEndDate.HOUR, rangeEndDate.MINUTE, rangeEndDate.SECOND, rangeEndDate.MILLISECOND, rangeEndDate.ZONE_OFFSET));
936                 /*currField.setRangeEndDate(DatatypeFactory.newInstance()
937                         .newXMLGregorianCalendar(end));*/
938             } catch (DatatypeConfigurationException ex) {
939                 
940             }
941                         
942                         fft.setRangeStartDateSQL(rangeStartDateSQL);
943                         fft.setRangeEndDateSQL(rangeEndDateSQL);
944                         if (nvl(comment).length() > 0)
945                                 fft.setComment(comment);
946         
947                         String fieldId = null;
948                         boolean idExists = true;
949                         for (int i = 1; idExists; i++) {
950                                 fieldId = "ff" + i;
951                                 idExists = false;
952                                 if (getFormFieldList() != null)
953                                         for (Iterator iter = getFormFieldList().getFormField().iterator(); iter
954                                                         .hasNext();)
955                                                 if (fieldId.equals(((FormFieldType) iter.next()).getFieldId())) {
956                                                         idExists = true;
957                                                         break;
958                                                 }
959                         } // for
960                         fft.setFieldId(fieldId);
961         
962                         if (getFormFieldList() == null) {
963                                 FormFieldList formFieldList = objFactory.createFormFieldList();
964                                 setFormFieldList(formFieldList);
965                         }
966         
967                         getFormFieldList().getFormField().add(fft);
968                 return fft;
969         } // addFormFieldType
970
971         //addCustomizedTextForParameters
972         public void addCustomizedTextForParameters(String comment) throws RaptorException {
973                         getFormFieldList().setComment(comment);
974         }
975         
976         public FormFieldType addFormFieldBlank(ObjectFactory objFactory) throws RaptorException {
977                 FormFieldType fft = null;
978                         fft = objFactory.createFormFieldType();
979         
980                         fft.setFieldName("BLANK");
981                 fft.setColId("bk");
982                 fft.setFieldType(FormField.FFT_BLANK);
983                 fft.setOrderBySeq((getFormFieldList() == null) ? 1 : getFormFieldList().getFormField()
984                                         .size() + 1);
985                         String fieldId = null;
986                         boolean idExists = true;
987                         for (int i = 1; idExists; i++) {
988                                 fieldId = "ff" + i;
989                                 idExists = false;
990                                 if (getFormFieldList() != null)
991                                         for (Iterator iter = getFormFieldList().getFormField().iterator(); iter
992                                                         .hasNext();)
993                                                 if (fieldId.equals(((FormFieldType) iter.next()).getFieldId())) {
994                                                         idExists = true;
995                                                         break;
996                                                 }
997                         } // for
998                         fft.setFieldId(fieldId);
999         
1000                         if (getFormFieldList() == null) {
1001                                 FormFieldList formFieldList = objFactory.createFormFieldList();
1002                                 setFormFieldList(formFieldList);
1003                         }
1004         
1005                         getFormFieldList().getFormField().add(fft);
1006                 return fft;
1007         } // addFormFieldBlank
1008         
1009         public void replaceFormFieldReferences(String fieldName, String replaceWith) {
1010                 if (fieldName.equals(replaceWith))
1011                         return;
1012
1013                 for (Iterator iter = getAllColumns().iterator(); iter.hasNext();) {
1014                         DataColumnType dct = (DataColumnType) iter.next();
1015
1016                         if (dct.isCalculated() && dct.getColName().indexOf(fieldName) >= 0)
1017                                 dct.setColName(Utils.replaceInString(dct.getColName(), fieldName, nvl(
1018                                                 replaceWith, "NULL")));
1019
1020                         if (dct.getColFilterList() != null)
1021                                 for (Iterator iter2 = dct.getColFilterList().getColFilter().iterator(); iter2
1022                                                 .hasNext();) {
1023                                         ColFilterType cft = (ColFilterType) iter2.next();
1024
1025                                         if (nvl(cft.getArgType()).equals(AppConstants.AT_FORM)
1026                                                         && nvl(cft.getArgValue()).equals(fieldName))
1027                                                 cft.setArgValue(replaceWith);
1028                                 } // for
1029                 } // for
1030         } // replaceFormFieldReferences
1031
1032         public void deleteFormField(String fieldId) {
1033                 String fieldDisplayName = null;
1034
1035                 int orderBySeq = Integer.MAX_VALUE;
1036                 if (getFormFieldList() != null)
1037                         for (Iterator iter = getFormFieldList().getFormField().iterator(); iter.hasNext();) {
1038                                 FormFieldType fft = (FormFieldType) iter.next();
1039
1040                                 if (fieldId.equals(fft.getFieldId())) {
1041                                         //orderBySeq = fft.getOrderBySeq();
1042                                         fieldDisplayName = getFormFieldDisplayName(fft);
1043                                         iter.remove();
1044                                 } else if (fft.getOrderBySeq()!=null && (fft.getOrderBySeq().intValue() > orderBySeq))
1045                                         fft.setOrderBySeq(fft.getOrderBySeq() - 1);
1046                         } // for
1047
1048                 if (fieldDisplayName != null)
1049                         replaceFormFieldReferences(fieldDisplayName, "");
1050         } // deleteFormField
1051
1052         public void shiftFormFieldUp(String fieldId) {
1053                 if (getFormFieldList() == null)
1054                         return;
1055
1056                 for (int i = 0; i < getFormFieldList().getFormField().size(); i++) {
1057                         FormFieldType fft = (FormFieldType) getFormFieldList().getFormField().get(i);
1058
1059                         if (fft.getFieldId().equals(fieldId) && (i > 0)) {
1060                                 FormFieldType prevFft = (FormFieldType) getFormFieldList().getFormField().get(
1061                                                 i - 1);
1062                                 prevFft.setOrderBySeq(prevFft.getOrderBySeq() + 1);
1063                                 fft.setOrderBySeq((fft.getOrderBySeq() == null)?0:fft.getOrderBySeq() - 1);
1064
1065                                 getFormFieldList().getFormField().remove(i);
1066                                 getFormFieldList().getFormField().add(i - 1, fft);
1067                                 return;
1068                         } // if
1069                 } // for
1070         } // shiftFormFieldUp
1071
1072         public void shiftFormFieldDown(String fieldId) {
1073                 if (getFormFieldList() == null)
1074                         return;
1075
1076                 for (int i = 0; i < getFormFieldList().getFormField().size(); i++) {
1077                         FormFieldType fft = (FormFieldType) getFormFieldList().getFormField().get(i);
1078
1079                         if (fft.getFieldId().equals(fieldId)
1080                                         && (i < getFormFieldList().getFormField().size() - 1)) {
1081                                 FormFieldType nextFft = (FormFieldType) getFormFieldList().getFormField().get(
1082                                                 i + 1);
1083                                 nextFft.setOrderBySeq((nextFft.getOrderBySeq() == null)?0:nextFft.getOrderBySeq() - 1);
1084                                 fft.setOrderBySeq((fft.getOrderBySeq() == null)?0:fft.getOrderBySeq() + 1);
1085
1086                                 getFormFieldList().getFormField().remove(i + 1);
1087                                 getFormFieldList().getFormField().add(i, nextFft);
1088                                 return;
1089                         } // if
1090                 } // for
1091         } // shiftFormFieldDown
1092
1093         public static void addFormFieldPredefinedValue(ObjectFactory objFactory,
1094                         FormFieldType formField, String predefinedValue) throws RaptorException {
1095                         if (formField.getPredefinedValueList() == null) {
1096                                 PredefinedValueList predefinedValueList = objFactory.createPredefinedValueList();
1097                                 formField.setPredefinedValueList(predefinedValueList);
1098                         } // if
1099         
1100                         if (predefinedValue.length() > 0) {
1101                                 formField.getPredefinedValueList().getPredefinedValue().add(predefinedValue);
1102                                 Collections.sort(formField.getPredefinedValueList().getPredefinedValue());
1103                         } // if
1104         } // addFormFieldPredefinedValue
1105
1106         public static void deleteFormFieldPredefinedValue(FormFieldType formField,
1107                         String predefinedValue) {
1108                 if (formField != null && formField.getPredefinedValueList() != null
1109                                 && predefinedValue.length() > 0)
1110                         for (Iterator iter = formField.getPredefinedValueList().getPredefinedValue()
1111                                         .iterator(); iter.hasNext();)
1112                                 if (predefinedValue.equals((String) iter.next())) {
1113                                         iter.remove();
1114                                         break;
1115                                 } // if
1116         } // deleteFormFieldPredefinedValue
1117
1118         /** ************************************************************************************************* */
1119
1120         private int curSQLParsePos = 0;
1121
1122         private String getNextSQLParseToken(String sql, boolean updateParsePos) {
1123                 int braketCount = 0;
1124                 boolean isInsideQuote = false;
1125                 StringBuffer nextToken = new StringBuffer();
1126                 for (int idxNext = curSQLParsePos; idxNext < sql.length(); idxNext++) {
1127                         char ch = sql.charAt(idxNext);
1128
1129                         if (Character.isWhitespace(ch) || ch == ',') {
1130                                 if (ch == ',')
1131                                         nextToken.append(ch);
1132
1133                                 if (nextToken.length() == 0)
1134                                         continue;
1135                                 else if (braketCount == 0 && (!isInsideQuote)) {
1136                                         if (updateParsePos)
1137                                                 curSQLParsePos = idxNext + ((ch == ',') ? 1 : 0);
1138                                         break;
1139                                 } else if (ch != ',' && nextToken.charAt(nextToken.length() - 1) != ' ')
1140                                         nextToken.append(' ');
1141                         } else {
1142                                 nextToken.append(ch);
1143
1144                                 if (ch == '(' || ch == '[')
1145                                         braketCount++;
1146                                 else if (ch == ')' || ch == ']')
1147                                         braketCount--;
1148                                 else if (ch == '\''/* ||ch=='\"' */)
1149                                         isInsideQuote = (!isInsideQuote);
1150                         } // else
1151                 } // for
1152
1153                 return nextToken.toString();
1154         } // getNextSQLParseToken
1155
1156         private boolean isParseSQLColID(String token) {
1157                 if (nvl(token).length() == 0)
1158                         return false;
1159
1160                 for (int i = 0; i < token.length(); i++) {
1161                         char ch = token.charAt(i);
1162
1163                         if (i == 0 && ch == '_')
1164                                 return false;
1165
1166                         if (!(Character.isLetterOrDigit(ch) || ch == '_'))
1167                                 return false;
1168                 } // for
1169
1170                 return true;
1171         } // isParseSQLColID
1172
1173         private DataColumnType getParseSQLDataColumn(String sqlExpression, String colId,
1174                         StringBuffer parsedSQL, Vector updatedReportCols, boolean isCYMBALScript) throws RaptorException {
1175                 DataColumnType dct = null;
1176
1177                 if (colId != null) {
1178                         if (!isParseSQLColID(colId))
1179                                 throw new org.openecomp.portalsdk.analytics.error.ValidationException(
1180                                                 "["
1181                                                                 + colId
1182                                                                 + "] must either be a valid column id consisting only of letters, numbers, and underscores, or there must be a comma in front of it.");
1183
1184                         dct = getColumnById(colId);
1185                 } else {
1186                         // Getting unique column id
1187                         colId = "";
1188                         int colIdN = 0;
1189                         for (int i = 0; (i < sqlExpression.length()) && (colIdN < 2); i++)
1190                                 if (Character.isLetter(sqlExpression.charAt(i))) {
1191                                         colId += sqlExpression.toLowerCase().charAt(i);
1192                                         colIdN++;
1193                                 } // if
1194
1195                         colIdN = getAllColumns().size() + updatedReportCols.size();
1196                         for (boolean idAlreadyUsed = true; idAlreadyUsed; colIdN++) {
1197                                 String newColId = colId + colIdN;
1198                                 idAlreadyUsed = false;
1199
1200                                 for (Iterator iter = getAllColumns().iterator(); iter.hasNext();)
1201                                         if (newColId.equals(((DataColumnType) iter.next()).getColId())) {
1202                                                 idAlreadyUsed = true;
1203                                                 break;
1204                                         }
1205
1206                                 if (!idAlreadyUsed)
1207                                         for (Iterator iter = updatedReportCols.iterator(); iter.hasNext();)
1208                                                 if (newColId.equals(((DataColumnType) iter.next()).getColId())) {
1209                                                         idAlreadyUsed = true;
1210                                                         break;
1211                                                 }
1212                         } // for
1213
1214                         colId += (colIdN - 1);
1215                 } // else
1216
1217                 if (dct == null) {
1218                         dct = (new ObjectFactory()).createDataColumnType();
1219                         dct.setColId(colId);
1220                         dct.setDisplayWidth(10);
1221                         dct.setDisplayAlignment("Left");
1222                         dct.setVisible(true);
1223                         dct.setGroupBreak(false); // ???
1224                         if(!isCYMBALScript) {
1225                                 boolean isValidIdentifier = Character.isLetterOrDigit(sqlExpression.charAt(0));
1226                                 for (int i = 0; i < sqlExpression.length(); i++)
1227                                         if (!(Character.isLetterOrDigit(sqlExpression.charAt(i))
1228                                                         || (sqlExpression.charAt(i) == '_') || (sqlExpression.charAt(i) == '$'))) {
1229                                                 isValidIdentifier = false;
1230                                                 break;
1231                                         } // if
1232         
1233                                 if (isValidIdentifier) {
1234                                         dct.setDisplayName(sqlExpression);
1235                                 } else {
1236                                         dct.setDisplayName(colId);
1237                                 } // else
1238                         } else dct.setDisplayName(colId);
1239                 } // if
1240                  if(!isCYMBALScript)
1241                 sqlExpression = sqlExpression.replaceAll(", '", ",'");
1242                 dct.setDbColName(sqlExpression);
1243                 dct.setColName(sqlExpression);
1244                 dct.setCalculated(true);
1245                 dct.setColType(AppConstants.CT_CHAR);
1246                 dct.setDbColType(AppConstants.CT_CHAR);
1247                 adjustColumnType(dct); // ???
1248                 if(!isCYMBALScript) {
1249                         if (parsedSQL.toString().equals("SELECT ")
1250                                         || parsedSQL.toString().equals("SELECT DISTINCT "))
1251                                 parsedSQL.append("\n\t");
1252                         else
1253                                 parsedSQL.append(", \n\t");
1254                         parsedSQL.append(sqlExpression);
1255                         parsedSQL.append(" ");
1256                         parsedSQL.append(colId);
1257                 }
1258
1259                 return dct;
1260         } // getParseSQLDataColumn
1261
1262         public void parseReportSQL(String sql) throws RaptorException {
1263                 StringBuffer parsedSQL = new StringBuffer();
1264
1265                 Vector updatedReportCols = new Vector();
1266
1267                 curSQLParsePos = 0;
1268                 int lastParsePos = curSQLParsePos;
1269                 String lastToken = null;
1270                 String nextToken = getNextSQLParseToken(sql, true);
1271
1272         String  dbInfo          = getDBInfo();
1273         boolean isCYMBALScript = false;
1274                 if (!isNull(dbInfo) && (!dbInfo.equals(AppConstants.DB_LOCAL))) {
1275                         try {
1276                          org.openecomp.portalsdk.analytics.util.RemDbInfo remDbInfo = new org.openecomp.portalsdk.analytics.util.RemDbInfo();
1277                          String dbType = remDbInfo.getDBType(dbInfo);
1278                          if (dbType.equals("DAYTONA") && !(nextToken.toUpperCase().equals("SELECT"))) {
1279                                  isCYMBALScript = true;
1280                          }
1281                         } catch (Exception ex) {
1282                    throw new RaptorException(ex);                       
1283                         }
1284                 }
1285                 if ( isCYMBALScript == false ) {
1286                         while (nextToken.length() > 0) {
1287                                 if (parsedSQL.length() == 0) {
1288                                         if (nextToken.toUpperCase().equals("SELECT"))
1289                                                 parsedSQL.append("SELECT ");
1290                                         else
1291                                                 throw new org.openecomp.portalsdk.analytics.error.ValidationException(
1292                                                                 "The SQL must start with the SELECT keyword.");
1293                                 } else if (nextToken.toUpperCase().equals("DISTINCT")
1294                                                 && parsedSQL.toString().equals("SELECT ")) {
1295                                         parsedSQL.append("DISTINCT ");
1296                                 } else if (nextToken.equals("*")
1297                                                 && (parsedSQL.toString().equals("SELECT ") || parsedSQL.toString().equals(
1298                                                                 "SELECT DISTINCT "))) {
1299                                         throw new org.openecomp.portalsdk.analytics.error.ValidationException(
1300                                                         "You cannot use \"SELECT *\". Please specify select columns/expressions.");
1301                                 } else if (nextToken.toUpperCase().equals("FROM")) {
1302                                         if (lastToken != null) {
1303                                                 updatedReportCols.add(getParseSQLDataColumn(lastToken, null, parsedSQL,
1304                                                                 updatedReportCols, false));
1305                                                 lastToken = null;
1306                                         }
1307         
1308                                         parsedSQL.append(" \n");
1309                                         while (lastParsePos < sql.length()
1310                                                         && Character.isWhitespace(sql.charAt(lastParsePos)))
1311                                                 lastParsePos++;
1312                                         parsedSQL.append(sql.substring(lastParsePos));
1313                                         break;
1314                                 } else {
1315                                         if (nextToken.charAt(nextToken.length() - 1) == ',') {
1316                                                 // The token ends with ,
1317                                                 nextToken = nextToken.substring(0, nextToken.length() - 1);
1318         
1319                                                 if (nextToken.length() == 0) {
1320                                                         if (lastToken != null) {
1321                                                                 updatedReportCols.add(getParseSQLDataColumn(lastToken, null,
1322                                                                                 parsedSQL, updatedReportCols, false));
1323                                                                 lastToken = null;
1324                                                         } // else just comma => ignore it
1325                                                 } else {
1326                                                         if (lastToken != null) {
1327                                                                 updatedReportCols.add(getParseSQLDataColumn(lastToken, nextToken,
1328                                                                                 parsedSQL, updatedReportCols, false));
1329                                                                 lastToken = null;
1330                                                         } else
1331                                                                 updatedReportCols.add(getParseSQLDataColumn(nextToken, null,
1332                                                                                 parsedSQL, updatedReportCols, false));
1333                                                 }
1334                                         } else {
1335                                                 // The token doesn't end with ,
1336                                                 if (lastToken == null)
1337                                                         lastToken = nextToken;
1338                                                 else {
1339                                                         String token = getNextSQLParseToken(sql, false);
1340                                                         if (!token.toUpperCase().equals("FROM"))
1341                                                                 throw new org.openecomp.portalsdk.analytics.error.ValidationException(
1342                                                                                 "|FROM keyword or a comma expected after [" + nextToken
1343                                                                                                 + "].");
1344         
1345                                                         updatedReportCols.add(getParseSQLDataColumn(lastToken, nextToken,
1346                                                                         parsedSQL, updatedReportCols, false));
1347                                                         lastToken = null;
1348                                                 } // else
1349                                         } // else
1350                                 } // else
1351         
1352                                 lastParsePos = curSQLParsePos;
1353                                 nextToken = getNextSQLParseToken(sql, true);
1354                         } // while
1355                 } else { // if CYMBAL Script 
1356                         curSQLParsePos = 0;
1357                         Pattern re                      = null;
1358                         Matcher matcher         = null;
1359                         String extracted        = null;
1360                         nextToken = getNextCYMBALSQLParseToken(sql,true);
1361                         while (nextToken.length() > 0) {
1362                                 if (lastToken == null) lastToken = nextToken;
1363                                 
1364                                 if( lastToken.toUpperCase().startsWith("DO DISPLAY")) {
1365                                         re              = Pattern.compile("each(.*)\\[.(.*?)\\]");   //\\[(.*?)\\]
1366                                         matcher = re.matcher(nextToken);
1367                                         if (matcher.find()) {
1368                                                 extracted = matcher.group();
1369                                                 re              = Pattern.compile("\\[(.*?)\\]");
1370                                         matcher = re.matcher(nextToken);
1371                                         if(matcher.find()) {
1372                                                 extracted = matcher.group();
1373                                                 extracted = extracted.substring(1,extracted.length()-1);
1374                                                 StringTokenizer sToken = new StringTokenizer(extracted, ",");
1375                                                 while(sToken.hasMoreTokens()) {
1376                                                 String str1 = sToken.nextToken().trim().substring(1);
1377                                             updatedReportCols.add(getParseSQLDataColumn("", str1,
1378                                                         new StringBuffer(""), updatedReportCols, true));
1379                                                 }
1380                                         }
1381                                                 
1382                                         }
1383                                         
1384                                 }
1385                                 lastToken = nextToken;
1386                                 nextToken = getNextCYMBALSQLParseToken(sql, true);
1387                         }
1388                 
1389                 }
1390                 if (updatedReportCols.size() == 0)
1391                         throw new org.openecomp.portalsdk.analytics.error.ValidationException(
1392                                         "The SQL statement must have at least one column in the SELECT clause.");
1393                 if (getDataSourceList().getDataSource().size() == 0)
1394                         addDataSourceType(new ObjectFactory(), "du0", "DUAL", "", "DUAL", null, null, null);
1395                 DataSourceType dst = (DataSourceType) getDataSourceList().getDataSource().get(0);
1396                 dst.getDataColumnList().getDataColumn().clear();
1397
1398                 for (int i = 0; i < updatedReportCols.size(); i++) {
1399                         DataColumnType dct = (DataColumnType) updatedReportCols.get(i);
1400                         dct.setTableId(dst.getTableId());
1401                         dct.setOrderSeq(i + 1);
1402                         dst.getDataColumnList().getDataColumn().add(dct);
1403                 } // for                
1404                 setReportSQL(parsedSQL.toString());
1405                 resetCache(false);
1406         } // parseReportSQL
1407         
1408         private String getNextCYMBALSQLParseToken(String sql, boolean updateParsePos) {
1409                 int braketCount = 0;
1410                 boolean isInsideQuote = false;
1411                 StringBuffer nextToken = new StringBuffer();
1412                 for (int idxNext = curSQLParsePos; idxNext < sql.length(); idxNext++) {
1413                         char ch = sql.charAt(idxNext);
1414
1415                         if (ch!='\n') {
1416                                         nextToken.append(ch);
1417                                         if (updateParsePos)
1418                                                 curSQLParsePos = idxNext;
1419                         }
1420                         else {
1421                                 curSQLParsePos = idxNext+1;
1422                                 break;
1423                         }
1424                 } // for
1425
1426                 return nextToken.toString();
1427         } // getNextSQLParseToken
1428         
1429         public void addChartAdditionalOptions(ObjectFactory objFactory) throws RaptorException {
1430                         ChartAdditionalOptions chartOptions = objFactory.createChartAdditionalOptions();
1431                         cr.setChartAdditionalOptions(chartOptions);
1432         }
1433         
1434         public void addChartDrillOptions(ObjectFactory objFactory) throws RaptorException {
1435                 ChartDrillOptions chartOptions = objFactory.createChartDrillOptions();
1436                 cr.setChartDrillOptions(chartOptions);
1437 }
1438
1439         public void addDataminingOptions(ObjectFactory objFactory) throws RaptorException {
1440                         DataminingOptions dataminingOptions = objFactory.createDataminingOptions();
1441                         cr.setDataminingOptions(dataminingOptions);
1442         }       
1443         /*public void addChartAdditionalOptions(ObjectFactory objFactory, String chartType, String chartMultiplePieOrder, String chartMultiplePieLabelDisplay,
1444                         String chartOrientation, String secondaryChartRenderer, String chartDisplay, String legendPosition,
1445                         String labelAngle) throws RaptorException {
1446                 try { 
1447                         ChartAdditionalOptions chartOptions = objFactory.createChartAdditionalOptions();
1448                 
1449                         if (nvl(chartMultiplePieOrder).length() > 0)
1450                                 chartOptions.setChartMultiplePieOrder(chartMultiplePieOrder);
1451                         if (nvl(chartMultiplePieLabelDisplay).length() > 0)
1452                                 chartOptions.setChartMultiplePieLabelDisplay(chartMultiplePieLabelDisplay);
1453                         if (nvl(chartOrientation).length() > 0)
1454                                 chartOptions.setChartOrientation(chartOrientation);
1455                         if (nvl(secondaryChartRenderer).length() > 0)
1456                                 chartOptions.setSecondaryChartRenderer(secondaryChartRenderer);
1457                         if (nvl(chartDisplay).length() > 0)
1458                                 chartOptions.setChartDisplay(chartDisplay);
1459                         if (nvl(legendPosition).length() > 0)
1460                                 chartOptions.setLegendPosition(legendPosition);
1461                         if (nvl(labelAngle).length() > 0)
1462                                 chartOptions.setLabelAngle(labelAngle);
1463         
1464                 cr.setChartAdditionalOptions(chartOptions);
1465                 } catch (JAXBException ex) {
1466                         throw new RaptorException(ex.getMessage(), ex.getCause());
1467                 }       
1468         } // addChartAdditionalOptions*/
1469         
1470
1471 } // ReportDefinition