9c2255b40c6cbc3e5220a41653dc543557c5dbc0
[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.sql.Connection;
41 import java.util.Iterator;
42 import java.util.StringTokenizer;
43
44 import javax.servlet.http.HttpServletRequest;
45
46 import org.onap.portalsdk.analytics.model.ReportLoader;
47 import org.onap.portalsdk.analytics.model.base.ReportWrapper;
48 import org.onap.portalsdk.analytics.model.definition.ReportDefinition;
49 import org.onap.portalsdk.analytics.system.AppUtils;
50 import org.onap.portalsdk.analytics.system.DbUtils;
51 import org.onap.portalsdk.analytics.util.DataSet;
52 import org.onap.portalsdk.analytics.xmlobj.DataColumnType;
53
54 public class SystemUpgrade extends org.onap.portalsdk.analytics.RaptorObject {
55         private static final String upgradeFromVersion = "1.x";
56         private static final String upgradeToVersion   = "2.0";
57
58 /*  This script upgrades the Raptor database to a newer version
59         Make sure you execute the "upgrade_v0_to_v1_0_before_java.sql" before running this
60         and "upgrade_v0_to_v1_0_after_java.sql" afterwards 
61         
62         URL example: http://localhost:8082/databank/dispatcher?action=raptor&r_action=system_upgrade
63 */
64         public static String upgradeDB(HttpServletRequest request) {
65                 request.setAttribute("system_message", "System upgrade disabled");
66                 return "raptor/blank.jsp";
67                 
68 /*              try {
69                         if(upgradeFromVersion.equals("1.x")&&upgradeToVersion.equals("2.0"))
70                                 upgrateFromV1ToV2_0(request);
71                         else
72                                 throw new RuntimeException("Invalid version");
73                                 
74                         return "raptor/blank.jsp";
75                 } catch(Exception e) {
76                         return (new ErrorHandler()).processFatalError(request, e);
77                 }*/
78         }   // upgradeDB
79
80         private static void upgrateFromV1ToV2_0(HttpServletRequest request) throws Exception {
81                 StringBuffer log = new StringBuffer();
82                 log.append("Starting upgrade...<br>\n");
83                 
84                 DataSet ds = DbUtils.executeQuery("SELECT cr.rep_id, cr.sched_mailto_user_ids FROM cr_report cr");
85                 for(int i=0; i<ds.getRowCount(); i++) {
86                         String repId = ds.getString(i, 0);
87                         log.append("<li>Processing report ["+repId+"]: ");
88                         
89                         Connection connection = DbUtils.startTransaction();
90                         String emailIds = nvls(ds.getString(i, 1));
91                         if(emailIds.length()>0)
92                                 try {
93                                         log.append("Converting emails ");
94                                         StringTokenizer st = new StringTokenizer(emailIds, ",");
95                                         while(st.hasMoreTokens()) {
96                                                 String userId = nvls(st.nextToken());
97                                                 log.append(userId);
98                                                 if(userId.length()>0)
99                                                         DbUtils.executeUpdate(connection, "INSERT INTO cr_report_schedule_users (rep_id, user_id) VALUES ("+repId+", "+userId+")");
100                                                 log.append("-success, ");
101                                         }   // while
102                                         log.append(" <font color=green>COMPLETED</font>; ");
103                                 } catch(Exception e) {
104                                         log.append("-<font color=red>FAILED</font>; ");
105                                 }
106                         
107                         String reportXML = ReportLoader.loadCustomReportXML(repId);
108                         ReportDefinition rdef = ReportDefinition.unmarshal(reportXML, repId, request);
109                         ReportWrapper rw = new ReportWrapper(rdef.cloneCustomReport(), repId, null, null, null, null, null, null, false);
110                         
111                         for(Iterator iter=rw.getAllColumns().iterator(); iter.hasNext(); ) {
112                                 DataColumnType col = (DataColumnType) iter.next();
113                                 String drillDownURL = nvls(col.getDrillDownURL());
114                                 if(drillDownURL.startsWith("dispatcher?action=custrep.run&c_master=")) {
115                                         drillDownURL = AppUtils.getReportExecuteActionURL()+drillDownURL.substring("dispatcher?action=custrep.run&c_master=".length());
116                                         log.append("Drill-down processed; ");
117                                         col.setDrillDownURL(drillDownURL);
118                                 }
119                         }   // for
120                         
121                         reportXML = rw.marshal();
122
123                         /*PrintWriter xmlOut = new PrintWriter(new BufferedWriter(new FileWriter(new File(AppUtils.getTempFolderPath()+AppUtils.getUserID(request)))));
124                         xmlOut.println(reportXML);
125                         xmlOut.close();*/
126
127                         try {
128                                 ReportLoader.updateCustomReportRec(connection, rw, reportXML);
129                                 DbUtils.commitTransaction(connection);
130                                 log.append("<font color=green>REPORT UPDATED</font></li>\n");
131                         } catch(Exception e) {
132                                 log.append("<font color=red>REPORT UPDATE FAILED</font></li>\n");
133                                 DbUtils.rollbackTransaction(connection);
134                         } finally {
135                 DbUtils.clearConnection(connection);         
136             }
137                 }   // for
138                 
139                 log.append("<br>\nSystem upgrade successfully completed...<br>\n");
140                 request.setAttribute("system_message", log.toString());
141         }   // upgrateFromV1ToV2_0
142
143 }       // SystemUpgrade