1c170d36a7617c295e73e262208d4ac11b2b31a6
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.model.search;
21
22 import java.util.*;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import org.openecomp.portalsdk.analytics.error.RaptorException;
27 import org.openecomp.portalsdk.analytics.system.*;
28 import org.openecomp.portalsdk.analytics.util.*;
29
30 import com.fasterxml.jackson.databind.ObjectMapper;
31
32 class MetaReport { 
33         private boolean pagination = true;
34         private int pageSize;
35         private int totalSize;
36         private int pageNo;
37         public boolean isPagination() {
38                 return pagination;
39         }
40         public void setPagination(boolean pagination) {
41                 this.pagination = pagination;
42         }
43         public int getPageSize() {
44                 return pageSize;
45         }
46         public void setPageSize(int pageSize) {
47                 this.pageSize = pageSize;
48         }
49         public int getTotalSize() {
50                 return totalSize;
51         }
52         public void setTotalSize(int totalSize) {
53                 this.totalSize = totalSize;
54         }
55         public int getPageNo() {
56                 return pageNo;
57         }
58         public void setPageNo(int pageNo) {
59                 this.pageNo = pageNo;
60         }
61         
62         
63 }
64 class SearchReport {
65         private MetaReport metaReport;
66         public MetaReport getMetaReport() {
67                 return metaReport;
68         }
69         public void setMetaReport(MetaReport metaReport) {
70                 this.metaReport = metaReport;
71         }
72         private ArrayList<ArrayList<SearchResultColumn>> columns = new ArrayList<ArrayList<SearchResultColumn>>();
73         private ArrayList<ArrayList<SearchResultRow>> rows = new ArrayList<ArrayList<SearchResultRow>>();
74         public ArrayList<ArrayList<SearchResultColumn>> getColumns() {
75                 return columns;
76         }
77         public void setColumns(ArrayList<ArrayList<SearchResultColumn>> columns) {
78                 this.columns = columns;
79         }
80         public ArrayList<ArrayList<SearchResultRow>> getRows() {
81                 return rows;
82         }
83         public void setRows(ArrayList<ArrayList<SearchResultRow>> rows) {
84                 this.rows = rows;
85         }
86         
87 }
88
89 public class ReportSearchResultJSON extends SearchResultJSON {
90         private static final String HTML_FORM = "forma";
91         private String JSONString= "";
92         private SearchReport searchReport;
93         //private ArrayList<ArrayList<SearchResultColumn>> columns = new ArrayList<ArrayList<SearchResultColumn>>();
94         //private ArrayList<ArrayList<SearchResultRow>> rows = new ArrayList<ArrayList<SearchResultRow>>();
95
96         
97         public ReportSearchResultJSON(int pageNo) {
98                 this(pageNo, Globals.getDefaultPageSize(), -1, -1);
99         } // ReportSearchResult
100
101         public ReportSearchResultJSON(int pageNo, int writeAccessColIndex, int ownerIndicatorColIndex) {
102                 this(pageNo, Globals.getDefaultPageSize(), writeAccessColIndex, ownerIndicatorColIndex);
103         } // ReportSearchResult
104
105         public ReportSearchResultJSON(int pageNo, int pageSize, int writeAccessColIndex,
106                         int ownerIndicatorColIndex) { 
107
108                 searchReport = new SearchReport();
109                 MetaReport metaReport = new MetaReport();
110                 //if(searchReport.getMetaReport()!=null)
111                         searchReport.setMetaReport(metaReport); 
112                 metaReport.setPageNo(pageNo);
113                 metaReport.setPageSize(pageSize); 
114                 metaReport.setPagination(true);
115                 addColumn(new SearchResultColumn("no", "No", "5%", "Center"));
116                 addColumn(new SearchResultColumn("rep_id", "Report ID", "5%", "Center"));
117                 addColumn(new SearchResultColumn("rep_name", "Report Name", "25%", "Left"));
118                 addColumn(new SearchResultColumn("descr", "Description", "30%", "Left"));
119                 addColumn(new SearchResultColumn("owner", "Report Owner", "10%", "Center"));
120                 addColumn(new SearchResultColumn("create_date", "Create Date", "10%", "Center"));
121                 addColumn(new SearchResultColumn("copy", "Copy", "5%", "Center",
122                                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
123                                                 + ".value='report.copy';", "Copy report", HTML_FORM,
124                                 "Are you sure you want to create a copy of this report?", AppUtils
125                                                 .getImgFolderURL()
126                                                 + "modify_icon.gif", "13", "12", true, false, false));
127                 /*addColumn(new SearchResultColumn("&nbsp;&nbsp;Schedule&nbsp;&nbsp;", "5%", "Center",
128                                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
129                                                 + ".value='report.schedule_only';", "Schedule report", HTML_FORM,
130                                 null, AppUtils
131                                                 .getImgFolderURL()
132                                                 + "calendar_icon.gif", "13", "12", true, false, false));
133                                                 */
134
135                 addColumn(new SearchResultColumn("edit", "Edit", "5%", "Center",
136                                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
137                                                 + ".value='report.edit';", "Edit report", HTML_FORM, null, AppUtils
138                                                 .getImgFolderURL()
139                                                 + "pen_paper.gif", "12", "12", false, true, false));
140                 addColumn(new SearchResultColumn("delete", "Delete", "5%", "Center", "document." + HTML_FORM
141                                 + "." + AppConstants.RI_ACTION + ".value='report.delete';", "Delete report",
142                                 HTML_FORM, "Are you sure you want to delete this report?", AppUtils
143                                                 .getImgFolderURL()
144                                                 + "deleteicon.gif", "12", "12", false, false, true));
145                 addColumn(new SearchResultColumn("schedule", "Schedule", "5%", "Center", "document." + HTML_FORM
146                                 + "." + AppConstants.RI_ACTION + ".value='report.schedule.report.submit_wmenu';", "Schedule report",
147                                 HTML_FORM, null, AppUtils
148                                                 .getImgFolderURL()
149                                                 + "calendar_icon.gif", "20", "20", false, false, false, true));
150                 addColumn(new SearchResultColumn("run", "Run", "5%", "Center",
151                                 "document." + HTML_FORM + "." + AppConstants.RI_ACTION
152                                                 + ".value='report.run';", "Run report", HTML_FORM, null, AppUtils
153                                                 .getImgFolderURL()
154                                                 + "test_run.gif", "12", "12"));
155                 searchReport.getColumns().add(searchResultColumns);
156         } // ReportSearchResult
157
158         public void parseData(DataSet ds, HttpServletRequest request, int pageNo, int pageSize, int writeAccessColIndex, int ownerIndicatorColIndex) throws RaptorException {
159                 // Presumes single ID field in the first column of the DataSet and row
160                 // number in the first SearchResultColumn
161
162                 pageNo = AppUtils.getRequestNvlValue(request, "r_page").length()>0?Integer.parseInt(AppUtils.getRequestNvlValue(request, "r_page")):0;
163                 String userID = AppUtils.getUserID(request);
164                 int dataSize = ds.getRowCount();
165                 //pageSize = 0;
166
167                 if(searchReport.getMetaReport()!=null) {
168                         searchReport.getMetaReport().setPageNo(pageNo);
169                         //searchReport.getMetaReport().setPageSize(pageSize);
170                         pageSize = searchReport.getMetaReport().getPageSize();
171                         searchReport.getMetaReport().setTotalSize(dataSize);
172                 }
173                 int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
174                 int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount()) : ds
175                                 .getRowCount();
176                 for (int r = startRow; r < endRow; r++) {
177                         SearchResultRow row = new SearchResultRow();
178                         searchResultRows.add(row);
179
180                         String idValue = ds.getString(r, 0);
181
182                         boolean bCanEdit = true;
183                         if (writeAccessColIndex >= 0) {
184                                 String isReadOnlyValue = nvl(ds.getString(r, writeAccessColIndex), "Y");
185                                 bCanEdit = AppUtils.isSuperUser(request) || AppUtils.isAdminUser(request)
186                                                 || isReadOnlyValue.equals("N");
187                         }
188
189                         boolean bCanDelete = bCanEdit;
190                         if (Globals.getDeleteOnlyByOwner() && ownerIndicatorColIndex >= 0) {
191                                 String isOwnedByUserRecord = nvl(ds.getString(r, ownerIndicatorColIndex), "N");
192                                 bCanDelete = AppUtils.isSuperUser(request) || isOwnedByUserRecord.equals("Y");
193                         }
194                         
195                         boolean bCanSchedule = ds.getString(r, getNumColumns()-3).equals("Y");
196
197                         row.addColumnContent(new ColumnContent(getColumn(0).getColumnId(), new SearchResultField("" + (r + 1), idValue,
198                                         getColumn(0), true)));
199                         boolean isAuthorized = true;
200                         for (int c = 1; c < getNumColumns(); c++) {
201                                 SearchResultColumn column = getColumn(c);
202                                 isAuthorized = true;
203
204                                 if(column.isCopyLink()) 
205                                         isAuthorized = Globals.getCanCopyOnReadOnly()? true:bCanEdit;
206                                 else if (column.isDeleteLink())
207                                         isAuthorized = bCanDelete;
208                                 else if (column.isEditLink())
209                                         isAuthorized = bCanEdit;
210                                 else if (column.isScheduleLink())
211                                         isAuthorized = bCanSchedule;
212                 row.addColumnContent(new ColumnContent(column.getColumnId(), new SearchResultField(
213                                                 (column.getLinkURL() == null) ? ds.getString(r, c) : column
214                                                                 .getLinkTitle(), idValue,  column, isAuthorized
215                                                 )));
216                         } // for
217                 } // for
218                 searchReport.getRows().add(searchResultRows);
219                 ObjectMapper mapper = new ObjectMapper();
220                 String jsonInString = "";
221                 try {
222                         jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(searchReport);
223                 } catch (Exception ex) {
224                         ex.printStackTrace();
225                         
226                 }
227                 System.out.println(jsonInString);
228                 this.JSONString = jsonInString;
229         } // parseData
230
231         private String nvl(String s) {
232                 return (s == null) ? "" : s;
233         }
234         
235         private String nvl(String s, String sDefault) {
236                 return nvl(s).equals("") ? sDefault : s;
237         }
238
239         public String getJSONString() {
240                 return JSONString;
241         }
242
243         public void setJSONString(String jSONString) {
244                 JSONString = jSONString;
245         }
246         
247         
248 } // ReportSearchResult
249