dd18a1a225214f1b8e9342ba556454063c5a6053
[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  * 
37  */
38 package org.onap.portalsdk.analytics.system.fusion.web;
39
40 import java.util.HashMap;
41 import java.util.TreeMap;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
44
45 import javax.servlet.ServletContext;
46 import javax.servlet.http.HttpServletRequest;
47
48 import org.onap.portalsdk.analytics.model.ReportHandler;
49 import org.onap.portalsdk.analytics.model.ReportLoader;
50 import org.onap.portalsdk.analytics.model.base.ReportWrapper;
51 import org.onap.portalsdk.analytics.model.runtime.DashboardRunJSON;
52 import org.onap.portalsdk.analytics.system.Globals;
53 import org.onap.portalsdk.analytics.xmlobj.CustomReportType;
54 import org.onap.portalsdk.core.controller.RestrictedBaseController;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.springframework.web.bind.annotation.PathVariable;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RequestMethod;
59 import org.springframework.web.bind.annotation.ResponseBody;
60 import org.springframework.web.bind.annotation.RestController;
61
62 /**
63  * For dashboard reports.
64  */
65 @RestController
66 @RequestMapping("/")
67 public class RaptorDashboardController extends RestrictedBaseController {
68
69         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RaptorDashboardController.class);
70
71         @RequestMapping(value = { "raptor/dashboard/run/{parameter}" }, method = RequestMethod.GET, produces = "application/json")
72         @ResponseBody
73         public DashboardRunJSON dashboardRun(@PathVariable("parameter") String reportId, HttpServletRequest request) {
74                 ServletContext servletContext = request.getSession().getServletContext();
75                 if (!Globals.isSystemInitialized()) {
76                         Globals.initializeSystem(servletContext);
77                 }
78                 DashboardRunJSON dashboardRunJSON = new DashboardRunJSON();
79                 dashboardRunJSON.setReportId(reportId);
80                 try {
81                         ReportHandler rh1 = new ReportHandler();
82                         if (reportId != null) {
83                                 String reportXML = ReportLoader.loadCustomReportXML(reportId);
84                                 CustomReportType crType = ReportWrapper.unmarshalCR(reportXML);
85                                 dashboardRunJSON.setDashboardLayoutJSON(crType.getDashboardLayoutJSON());
86                                 
87                                 String strHTML = crType.getDashboardLayoutHTML();
88                                 dashboardRunJSON.setReportsFromDashBoardHTML(getListOfReportsFromDashBoardHTML(strHTML));
89                                 
90                                 reportXML = ReportLoader.loadCustomReportXML(((String)dashboardRunJSON.getReportsFromDashBoardHTML().get("1")).substring(1));
91                                 crType = ReportWrapper.unmarshalCR(reportXML);
92                                 dashboardRunJSON.setFormFieldList(crType.getFormFieldList());
93                                 dashboardRunJSON.setFormFieldGroupsJSON(crType.getFormFieldGroupsJSON());
94                         }
95                 } catch (Exception ex) {
96                         logger.error(EELFLoggerDelegate.errorLogger, "getManifest failed", ex);
97                         dashboardRunJSON.setErrorMessage(ex.toString());
98                 }
99                 return dashboardRunJSON;
100         }
101         
102         private TreeMap getListOfReportsFromDashBoardHTML(String htmlString) {
103                 String sourcestring = htmlString;
104                 Pattern re = Pattern.compile("\\[(.*?)\\]");
105                 Matcher m = re.matcher(sourcestring);
106                 HashMap hashReports = new HashMap();
107                 int mIdx = 0;
108                 while (m.find()) {
109                         for (int groupIdx = 0; groupIdx < m.groupCount(); groupIdx++) {
110                                 String str = m.group(groupIdx);
111                                 hashReports.put(new String(Integer.toString(mIdx + 1)),
112                                                 (str.substring(1).toLowerCase().startsWith("chart") ? "c" : "d")
113                                                                 + str.substring(str.indexOf("#") + 1, str.length() - 1));
114                         }
115                         mIdx++;
116                 }
117                 return new TreeMap(hashReports);
118         }
119 }