c3fc0a7be858e2778519090f1363e6eb80dea639
[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
42 import javax.servlet.http.HttpServletRequest;
43
44 import org.onap.portalsdk.analytics.error.RaptorException;
45 import org.onap.portalsdk.analytics.system.AppUtils;
46 import org.onap.portalsdk.analytics.system.DbUtils;
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 ArrayList<ArrayList<SearchResultColumn>> columns = new ArrayList<>();
106     private ArrayList<ArrayList<SearchResultRow>> rows = new ArrayList<>();
107
108     public ArrayList<ArrayList<SearchResultColumn>> getColumns() {
109         return columns;
110     }
111
112     public void setColumns(ArrayList<ArrayList<SearchResultColumn>> columns) {
113         this.columns = columns;
114     }
115
116     public ArrayList<ArrayList<SearchResultRow>> getRows() {
117         return rows;
118     }
119
120     public void setRows(ArrayList<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     // private ArrayList<ArrayList<SearchResultColumn>> columns = new
134     // ArrayList<ArrayList<SearchResultColumn>>();
135     // private ArrayList<ArrayList<SearchResultRow>> rows = new ArrayList<ArrayList<SearchResultRow>>();
136
137     public ReportSearchResultJSON(int pageNo) {
138         this(pageNo, Globals.getDefaultPageSize(), -1, -1);
139     } // ReportSearchResult
140
141     public ReportSearchResultJSON(int pageNo, int writeAccessColIndex, int ownerIndicatorColIndex) {
142         this(pageNo, Globals.getDefaultPageSize(), writeAccessColIndex, ownerIndicatorColIndex);
143     } // ReportSearchResult
144
145     public ReportSearchResultJSON(int pageNo, int pageSize, int writeAccessColIndex,
146             int ownerIndicatorColIndex) {
147
148         searchReport = new SearchReport();
149         MetaReport metaReport = new MetaReport();
150         // if(searchReport.getMetaReport()!=null)
151         searchReport.setMetaReport(metaReport);
152         metaReport.setPageNo(pageNo);
153         metaReport.setPageSize(pageSize);
154         metaReport.setPagination(true);
155         addColumn(new SearchResultColumn("no", "No", "5%", "Center"));
156         addColumn(new SearchResultColumn("rep_id", "Report ID", "5%", "Center"));
157         addColumn(new SearchResultColumn("rep_name", "Report Name", "25%", "Left"));
158         addColumn(new SearchResultColumn("descr", "Description", "30%", "Left"));
159         addColumn(new SearchResultColumn("owner", "Report Owner", "10%", "Center"));
160         addColumn(new SearchResultColumn("create_date", "Create Date", "10%", "Center"));
161         addColumn(new SearchResultColumn("copy", "Copy", "5%", "Center",
162                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
163                         + ".value='report.copy';",
164                 "Copy report", HTML_FORM,
165                 "Are you sure you want to create a copy of this report?", AppUtils
166                         .getImgFolderURL()
167                         + "modify_icon.gif",
168                 "13", "12", true, false, false));
169         /*
170          * addColumn(new SearchResultColumn("&nbsp;&nbsp;Schedule&nbsp;&nbsp;", "5%", "Center", "document."
171          * + HTML_FORM + "." + AppConstants.RI_ACTION + ".value='report.schedule_only';", "Schedule report",
172          * HTML_FORM, null, AppUtils .getImgFolderURL() + "calendar_icon.gif", "13", "12", true, false,
173          * false));
174          */
175
176         addColumn(new SearchResultColumn("edit", "Edit", "5%", "Center",
177                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
178                         + ".value='report.edit';",
179                 "Edit report", HTML_FORM, null, AppUtils
180                         .getImgFolderURL()
181                         + "pen_paper.gif",
182                 "12", "12", false, true, false));
183         addColumn(new SearchResultColumn("delete", "Delete", "5%", "Center", "document." + HTML_FORM
184                 + "." + AppConstants.RI_ACTION + ".value='report.delete';", "Delete report",
185                 HTML_FORM, "Are you sure you want to delete this report?", AppUtils
186                         .getImgFolderURL()
187                         + "deleteicon.gif",
188                 "12", "12", false, false, true));
189         addColumn(new SearchResultColumn("schedule", "Schedule", "5%", "Center", "document." + HTML_FORM
190                 + "." + AppConstants.RI_ACTION + ".value='report.schedule.report.submit_wmenu';", "Schedule report",
191                 HTML_FORM, null, AppUtils
192                         .getImgFolderURL()
193                         + "calendar_icon.gif",
194                 "20", "20", false, false, false, true));
195         addColumn(new SearchResultColumn("run", "Run", "5%", "Center",
196                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
197                         + ".value='report.run';",
198                 "Run report", HTML_FORM, null, AppUtils
199                         .getImgFolderURL()
200                         + "test_run.gif",
201                 "12", "12"));
202         searchReport.getColumns().add(searchResultColumns);
203     } // ReportSearchResult
204
205     public void parseData(DataSet ds, HttpServletRequest request, int pageNo, int pageSize, int writeAccessColIndex,
206             int ownerIndicatorColIndex) throws RaptorException {
207         // Presumes single ID field in the first column of the DataSet and row
208         // number in the first SearchResultColumn
209
210         pageNo = AppUtils.getRequestNvlValue(request, "r_page").length() > 0
211                 ? Integer.parseInt(AppUtils.getRequestNvlValue(request, "r_page"))
212                 : 0;
213         String userID = AppUtils.getUserID(request);
214         int dataSize = ds.getRowCount();
215         // pageSize = 0;
216
217         if (searchReport.getMetaReport() != null) {
218             searchReport.getMetaReport().setPageNo(pageNo);
219             // searchReport.getMetaReport().setPageSize(pageSize);
220             pageSize = searchReport.getMetaReport().getPageSize();
221             searchReport.getMetaReport().setTotalSize(dataSize);
222         }
223         int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
224         int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount())
225                 : ds
226                         .getRowCount();
227         for (int r = startRow; r < endRow; r++) {
228             SearchResultRow row = new SearchResultRow();
229             searchResultRows.add(row);
230
231             String idValue = ds.getString(r, 0);
232
233             boolean bCanEdit = true;
234             if (writeAccessColIndex >= 0) {
235                 String isReadOnlyValue = nvl(ds.getString(r, writeAccessColIndex), "Y");
236                 bCanEdit = AppUtils.isSuperUser(request) || AppUtils.isAdminUser(request);
237                 if (isReadOnlyValue != null) {
238                     bCanEdit = bCanEdit || isReadOnlyValue.equals("N");
239                 }
240
241             }
242
243             boolean bCanDelete = bCanEdit;
244             if (Globals.getDeleteOnlyByOwner() && ownerIndicatorColIndex >= 0) {
245                 String isOwnedByUserRecord = nvl(ds.getString(r, ownerIndicatorColIndex), "N");
246                 bCanDelete = AppUtils.isSuperUser(request);
247                 if (isOwnedByUserRecord != null) {
248                     bCanDelete = bCanDelete || isOwnedByUserRecord.equals("Y");
249                 }
250             }
251
252             boolean bCanSchedule = ds.getString(r, getNumColumns() - 3).equals("Y");
253
254             row.addColumnContent(
255                     new ColumnContent(getColumn(0).getColumnId(), new SearchResultField("" + (r + 1), idValue,
256                             getColumn(0), true)));
257             boolean isAuthorized = true;
258             for (int c = 1; c < getNumColumns(); c++) {
259                 SearchResultColumn column = getColumn(c);
260                 isAuthorized = true;
261
262                 if (column.isCopyLink())
263                     isAuthorized = Globals.getCanCopyOnReadOnly() ? true : bCanEdit;
264                 else if (column.isDeleteLink())
265                     isAuthorized = bCanDelete;
266                 else if (column.isEditLink())
267                     isAuthorized = bCanEdit;
268                 else if (column.isScheduleLink())
269                     isAuthorized = bCanSchedule;
270                 row.addColumnContent(new ColumnContent(column.getColumnId(), new SearchResultField(
271                         (column.getLinkURL() == null) ? ds.getString(r, c)
272                                 : column
273                                         .getLinkTitle(),
274                         idValue, column, isAuthorized)));
275             } // for
276         } // for
277         searchReport.getRows().add(searchResultRows);
278         ObjectMapper mapper = new ObjectMapper();
279         String jsonInString = "";
280         try {
281             jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(searchReport);
282         } catch (Exception ex) {
283             logger.warn("Exception in parseData", ex);
284         }
285         logger.debug(jsonInString);
286         this.jsonString = jsonInString;
287     } // parseData
288
289     private String nvl(String s) {
290         return (s == null) ? "" : s;
291     }
292
293     private String nvl(String s, String sDefault) {
294         return nvl(s).equals("") ? sDefault : s;
295     }
296
297     public String getJSONString() {
298         return jsonString;
299     }
300
301     public void setJSONString(String jSONString) {
302         jsonString = jSONString;
303     }
304
305 } // ReportSearchResult