d8a6635e68ed6d5418df2b4879297f252fc29898
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.util.upgrade;
39  
40 import java.io.*;
41 import java.sql.*;
42 import java.util.*;
43 import javax.servlet.http.*;
44
45 import org.onap.portalsdk.analytics.controller.*;
46 import org.onap.portalsdk.analytics.error.*;
47 import org.onap.portalsdk.analytics.model.*;
48 import org.onap.portalsdk.analytics.model.base.*;
49 import org.onap.portalsdk.analytics.model.definition.*;
50 import org.onap.portalsdk.analytics.model.runtime.*;
51 import org.onap.portalsdk.analytics.system.*;
52 import org.onap.portalsdk.analytics.util.*;
53 import org.onap.portalsdk.analytics.view.*;
54 import org.onap.portalsdk.analytics.xmlobj.*;
55
56 public class SystemUpgrade extends org.onap.portalsdk.analytics.RaptorObject {
57         private static final String upgradeFromVersion = "1.x";
58         private static final String upgradeToVersion   = "2.0";
59
60 /*  This script upgrades the Raptor database to a newer version
61         Make sure you execute the "upgrade_v0_to_v1_0_before_java.sql" before running this
62         and "upgrade_v0_to_v1_0_after_java.sql" afterwards 
63         
64         URL example: http://localhost:8082/databank/dispatcher?action=raptor&r_action=system_upgrade
65 */
66         public static String upgradeDB(HttpServletRequest request) {
67                 request.setAttribute("system_message", "System upgrade disabled");
68                 return "raptor/blank.jsp";
69                 
70 /*              try {
71                         if(upgradeFromVersion.equals("1.x")&&upgradeToVersion.equals("2.0"))
72                                 upgrateFromV1ToV2_0(request);
73                         else
74                                 throw new RuntimeException("Invalid version");
75                                 
76                         return "raptor/blank.jsp";
77                 } catch(Exception e) {
78                         return (new ErrorHandler()).processFatalError(request, e);
79                 }*/
80         }   // upgradeDB
81
82         private static void upgrateFromV1ToV2_0(HttpServletRequest request) throws Exception {
83                 StringBuffer log = new StringBuffer();
84                 log.append("Starting upgrade...<br>\n");
85                 
86                 DataSet ds = DbUtils.executeQuery("SELECT cr.rep_id, cr.sched_mailto_user_ids FROM cr_report cr");
87                 for(int i=0; i<ds.getRowCount(); i++) {
88                         String repId = ds.getString(i, 0);
89                         log.append("<li>Processing report ["+repId+"]: ");
90                         
91                         Connection connection = DbUtils.startTransaction();
92                         String emailIds = nvls(ds.getString(i, 1));
93                         if(emailIds.length()>0)
94                                 try {
95                                         log.append("Converting emails ");
96                                         StringTokenizer st = new StringTokenizer(emailIds, ",");
97                                         while(st.hasMoreTokens()) {
98                                                 String userId = nvls(st.nextToken());
99                                                 log.append(userId);
100                                                 if(userId.length()>0)
101                                                         DbUtils.executeUpdate(connection, "INSERT INTO cr_report_schedule_users (rep_id, user_id) VALUES ("+repId+", "+userId+")");
102                                                 log.append("-success, ");
103                                         }   // while
104                                         log.append(" <font color=green>COMPLETED</font>; ");
105                                 } catch(Exception e) {
106                                         log.append("-<font color=red>FAILED</font>; ");
107                                 }
108                         
109                         String reportXML = ReportLoader.loadCustomReportXML(repId);
110                         ReportDefinition rdef = ReportDefinition.unmarshal(reportXML, repId, request);
111                         ReportWrapper rw = new ReportWrapper(rdef.cloneCustomReport(), repId, null, null, null, null, null, null, false);
112                         
113                         for(Iterator iter=rw.getAllColumns().iterator(); iter.hasNext(); ) {
114                                 DataColumnType col = (DataColumnType) iter.next();
115                                 String drillDownURL = nvls(col.getDrillDownURL());
116                                 if(drillDownURL.startsWith("dispatcher?action=custrep.run&c_master=")) {
117                                         drillDownURL = AppUtils.getReportExecuteActionURL()+drillDownURL.substring("dispatcher?action=custrep.run&c_master=".length());
118                                         log.append("Drill-down processed; ");
119                                         col.setDrillDownURL(drillDownURL);
120                                 }
121                         }   // for
122                         
123                         reportXML = rw.marshal();
124
125                         /*PrintWriter xmlOut = new PrintWriter(new BufferedWriter(new FileWriter(new File(AppUtils.getTempFolderPath()+AppUtils.getUserID(request)))));
126                         xmlOut.println(reportXML);
127                         xmlOut.close();*/
128
129                         try {
130                                 ReportLoader.updateCustomReportRec(connection, rw, reportXML);
131                                 DbUtils.commitTransaction(connection);
132                                 log.append("<font color=green>REPORT UPDATED</font></li>\n");
133                         } catch(Exception e) {
134                                 log.append("<font color=red>REPORT UPDATE FAILED</font></li>\n");
135                                 DbUtils.rollbackTransaction(connection);
136                         } finally {
137                 DbUtils.clearConnection(connection);         
138             }
139                 }   // for
140                 
141                 log.append("<br>\nSystem upgrade successfully completed...<br>\n");
142                 request.setAttribute("system_message", log.toString());
143         }   // upgrateFromV1ToV2_0
144
145 }       // SystemUpgrade