2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalsdk.analytics.model.search;
40 import java.util.ArrayList;
41 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
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;
54 private boolean pagination = true;
56 private int totalSize;
59 public boolean isPagination() {
63 public void setPagination(boolean pagination) {
64 this.pagination = pagination;
67 public int getPageSize() {
71 public void setPageSize(int pageSize) {
72 this.pageSize = pageSize;
75 public int getTotalSize() {
79 public void setTotalSize(int totalSize) {
80 this.totalSize = totalSize;
83 public int getPageNo() {
87 public void setPageNo(int pageNo) {
95 private MetaReport metaReport;
97 public MetaReport getMetaReport() {
101 public void setMetaReport(MetaReport metaReport) {
102 this.metaReport = metaReport;
105 private List<ArrayList<SearchResultColumn>> columns = new ArrayList<>();
106 private List<ArrayList<SearchResultRow>> rows = new ArrayList<>();
108 public List<ArrayList<SearchResultColumn>> getColumns() {
112 public void setColumns(List<ArrayList<SearchResultColumn>> columns) {
113 this.columns = columns;
116 public List<ArrayList<SearchResultRow>> getRows() {
120 public void setRows(List<ArrayList<SearchResultRow>> rows) {
127 public class ReportSearchResultJSON extends SearchResultJSON {
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;
135 public ReportSearchResultJSON(int pageNo) {
136 this(pageNo, Globals.getDefaultPageSize(), -1, -1);
137 } // ReportSearchResult
139 public ReportSearchResultJSON(int pageNo, int writeAccessColIndex, int ownerIndicatorColIndex) {
140 this(pageNo, Globals.getDefaultPageSize(), writeAccessColIndex, ownerIndicatorColIndex);
141 } // ReportSearchResult
143 public ReportSearchResultJSON(int pageNo, int pageSize, int writeAccessColIndex,
144 int ownerIndicatorColIndex) {
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
165 "13", "12", true, false, false));
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
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
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
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
193 searchReport.getColumns().add(searchResultColumns);
194 } // ReportSearchResult
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
201 int pageNo = AppUtils.getRequestNvlValue(request, "r_page").length() > 0
202 ? Integer.parseInt(AppUtils.getRequestNvlValue(request, "r_page"))
205 int dataSize = ds.getRowCount();
208 if (searchReport.getMetaReport() != null) {
209 searchReport.getMetaReport().setPageNo(pageNo);
210 pageSize = searchReport.getMetaReport().getPageSize();
211 searchReport.getMetaReport().setTotalSize(dataSize);
213 int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
214 int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount())
217 for (int r = startRow; r < endRow; r++) {
218 SearchResultRow row = new SearchResultRow();
219 searchResultRows.add(row);
221 String idValue = ds.getString(r, 0);
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");
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");
242 boolean bCanSchedule = ds.getString(r, getNumColumns() - 3).equals("Y");
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);
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)
264 idValue, column, isAuthorized)));
267 searchReport.getRows().add(searchResultRows);
268 ObjectMapper mapper = new ObjectMapper();
269 String jsonInString = "";
271 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(searchReport);
272 } catch (Exception ex) {
273 logger.warn("Exception in parseData", ex);
275 logger.debug(jsonInString);
276 this.jsonString = jsonInString;
279 private String nvl(String s) {
280 return (s == null) ? "" : s;
283 private String nvl(String s, String sDefault) {
284 return nvl(s).equals("") ? sDefault : s;
287 public String getJSONString() {
291 public void setJSONString(String jSONString) {
292 jsonString = jSONString;
295 } // ReportSearchResult