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