2bd278e4605b26b240cff63cc2164e99c2517324
[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.model.search;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 import javax.servlet.http.HttpServletRequest;
44
45 import org.onap.portalsdk.analytics.error.RaptorException;
46 import org.onap.portalsdk.analytics.system.AppUtils;
47 import org.onap.portalsdk.analytics.system.Globals;
48 import org.onap.portalsdk.analytics.util.AppConstants;
49 import org.onap.portalsdk.analytics.util.DataSet;
50 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 class MetaReport {
54     private boolean pagination = true;
55     private int pageSize;
56     private int totalSize;
57     private int pageNo;
58
59     public boolean isPagination() {
60         return pagination;
61     }
62
63     public void setPagination(boolean pagination) {
64         this.pagination = pagination;
65     }
66
67     public int getPageSize() {
68         return pageSize;
69     }
70
71     public void setPageSize(int pageSize) {
72         this.pageSize = pageSize;
73     }
74
75     public int getTotalSize() {
76         return totalSize;
77     }
78
79     public void setTotalSize(int totalSize) {
80         this.totalSize = totalSize;
81     }
82
83     public int getPageNo() {
84         return pageNo;
85     }
86
87     public void setPageNo(int pageNo) {
88         this.pageNo = pageNo;
89     }
90
91 }
92
93
94 class SearchReport {
95     private MetaReport metaReport;
96
97     public MetaReport getMetaReport() {
98         return metaReport;
99     }
100
101     public void setMetaReport(MetaReport metaReport) {
102         this.metaReport = metaReport;
103     }
104
105     private List<ArrayList<SearchResultColumn>> columns = new ArrayList<>();
106     private List<ArrayList<SearchResultRow>> rows = new ArrayList<>();
107
108     public List<ArrayList<SearchResultColumn>> getColumns() {
109         return columns;
110     }
111
112     public void setColumns(List<ArrayList<SearchResultColumn>> columns) {
113         this.columns = columns;
114     }
115
116     public List<ArrayList<SearchResultRow>> getRows() {
117         return rows;
118     }
119
120     public void setRows(List<ArrayList<SearchResultRow>> rows) {
121         this.rows = rows;
122     }
123
124 }
125
126
127 public class ReportSearchResultJSON extends SearchResultJSON {
128
129     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ReportSearchResultJSON.class);
130     private static final String HTML_FORM = "forma";
131     private String jsonString = "";
132     private SearchReport searchReport;
133
134
135     public ReportSearchResultJSON(int pageNo) {
136         this(pageNo, Globals.getDefaultPageSize(), -1, -1);
137     } // ReportSearchResult
138
139     public ReportSearchResultJSON(int pageNo, int writeAccessColIndex, int ownerIndicatorColIndex) {
140         this(pageNo, Globals.getDefaultPageSize(), writeAccessColIndex, ownerIndicatorColIndex);
141     } // ReportSearchResult
142
143     public ReportSearchResultJSON(int pageNo, int pageSize, int writeAccessColIndex,
144             int ownerIndicatorColIndex) {
145
146         searchReport = new SearchReport();
147         MetaReport metaReport = new MetaReport();
148         searchReport.setMetaReport(metaReport);
149         metaReport.setPageNo(pageNo);
150         metaReport.setPageSize(pageSize);
151         metaReport.setPagination(true);
152         addColumn(new SearchResultColumn("no", "No", "5%", "Center"));
153         addColumn(new SearchResultColumn("rep_id", "Report ID", "5%", "Center"));
154         addColumn(new SearchResultColumn("rep_name", "Report Name", "25%", "Left"));
155         addColumn(new SearchResultColumn("descr", "Description", "30%", "Left"));
156         addColumn(new SearchResultColumn("owner", "Report Owner", "10%", "Center"));
157         addColumn(new SearchResultColumn("create_date", "Create Date", "10%", "Center"));
158         addColumn(new SearchResultColumn("copy", "Copy", "5%", "Center",
159                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
160                         + ".value='report.copy';",
161                 "Copy report", HTML_FORM,
162                 "Are you sure you want to create a copy of this report?", AppUtils
163                         .getImgFolderURL()
164                         + "modify_icon.gif",
165                 "13", "12", true, false, false));
166
167         addColumn(new SearchResultColumn("edit", "Edit", "5%", "Center",
168                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
169                         + ".value='report.edit';",
170                 "Edit report", HTML_FORM, null, AppUtils
171                         .getImgFolderURL()
172                         + "pen_paper.gif",
173                 "12", "12", false, true, false));
174         addColumn(new SearchResultColumn("delete", "Delete", "5%", "Center", "document." + HTML_FORM
175                 + "." + AppConstants.RI_ACTION + ".value='report.delete';", "Delete report",
176                 HTML_FORM, "Are you sure you want to delete this report?", AppUtils
177                         .getImgFolderURL()
178                         + "deleteicon.gif",
179                 "12", "12", false, false, true));
180         addColumn(new SearchResultColumn("schedule", "Schedule", "5%", "Center", "document." + HTML_FORM
181                 + "." + AppConstants.RI_ACTION + ".value='report.schedule.report.submit_wmenu';", "Schedule report",
182                 HTML_FORM, null, AppUtils
183                         .getImgFolderURL()
184                         + "calendar_icon.gif",
185                 "20", "20", false, false, false, true));
186         addColumn(new SearchResultColumn("run", "Run", "5%", "Center",
187                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
188                         + ".value='report.run';",
189                 "Run report", HTML_FORM, null, AppUtils
190                         .getImgFolderURL()
191                         + "test_run.gif",
192                 "12", "12"));
193         searchReport.getColumns().add(searchResultColumns);
194     } // ReportSearchResult
195
196     public void parseData(DataSet ds, HttpServletRequest request, int pageNumber, int pageSize, int writeAccessColIndex,
197             int ownerIndicatorColIndex) throws RaptorException {
198         // Presumes single ID field in the first column of the DataSet and row
199         // number in the first SearchResultColumn
200
201         int pageNo = AppUtils.getRequestNvlValue(request, "r_page").length() > 0
202                 ? Integer.parseInt(AppUtils.getRequestNvlValue(request, "r_page"))
203                 : 0;
204
205         int dataSize = ds.getRowCount();
206
207
208         if (searchReport.getMetaReport() != null) {
209             searchReport.getMetaReport().setPageNo(pageNo);
210             pageSize = searchReport.getMetaReport().getPageSize();
211             searchReport.getMetaReport().setTotalSize(dataSize);
212         }
213         int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
214         int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount())
215                 : ds
216                         .getRowCount();
217         for (int r = startRow; r < endRow; r++) {
218             SearchResultRow row = new SearchResultRow();
219             searchResultRows.add(row);
220
221             String idValue = ds.getString(r, 0);
222
223             boolean bCanEdit = true;
224             if (writeAccessColIndex >= 0) {
225                 String isReadOnlyValue = nvl(ds.getString(r, writeAccessColIndex), "Y");
226                 bCanEdit = AppUtils.isSuperUser(request) || AppUtils.isAdminUser(request);
227                 if (isReadOnlyValue != null) {
228                     bCanEdit = bCanEdit || isReadOnlyValue.equals("N");
229                 }
230
231             }
232
233             boolean bCanDelete = bCanEdit;
234             if (Globals.getDeleteOnlyByOwner() && ownerIndicatorColIndex >= 0) {
235                 String isOwnedByUserRecord = nvl(ds.getString(r, ownerIndicatorColIndex), "N");
236                 bCanDelete = AppUtils.isSuperUser(request);
237                 if (isOwnedByUserRecord != null) {
238                     bCanDelete = bCanDelete || isOwnedByUserRecord.equals("Y");
239                 }
240             }
241
242             boolean bCanSchedule = ds.getString(r, getNumColumns() - 3).equals("Y");
243
244             row.addColumnContent(
245                     new ColumnContent(getColumn(0).getColumnId(), new SearchResultField("" + (r + 1), idValue,
246                             getColumn(0), true)));
247             boolean isAuthorized = true;
248             for (int c = 1; c < getNumColumns(); c++) {
249                 SearchResultColumn column = getColumn(c);
250                 isAuthorized = true;
251
252                 if (column.isCopyLink())
253                     isAuthorized = Globals.getCanCopyOnReadOnly() ? true : bCanEdit;
254                 else if (column.isDeleteLink())
255                     isAuthorized = bCanDelete;
256                 else if (column.isEditLink())
257                     isAuthorized = bCanEdit;
258                 else if (column.isScheduleLink())
259                     isAuthorized = bCanSchedule;
260                 row.addColumnContent(new ColumnContent(column.getColumnId(), new SearchResultField(
261                         (column.getLinkURL() == null) ? ds.getString(r, c)
262                                 : column
263                                         .getLinkTitle(),
264                         idValue, column, isAuthorized)));
265             } // for
266         } // for
267         searchReport.getRows().add(searchResultRows);
268         ObjectMapper mapper = new ObjectMapper();
269         String jsonInString = "";
270         try {
271             jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(searchReport);
272         } catch (Exception ex) {
273             logger.warn("Exception in parseData", ex);
274         }
275         logger.debug(jsonInString);
276         this.jsonString = jsonInString;
277     } // parseData
278
279     private String nvl(String s) {
280         return (s == null) ? "" : s;
281     }
282
283     private String nvl(String s, String sDefault) {
284         return nvl(s).equals("") ? sDefault : s;
285     }
286
287     public String getJSONString() {
288         return jsonString;
289     }
290
291     public void setJSONString(String jSONString) {
292         jsonString = jSONString;
293     }
294
295 } // ReportSearchResult