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