7d33cab576a995f9fce8cd3a840c63cb701242d5
[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 public class SearchResult{
31         private int pageNo = -1;
32
33         private int pageSize = 50;
34
35         private int dataSize = -1;
36
37         private int writeAccessColIndex = -1;
38
39         private int ownerIndicatorColIndex = -1;
40
41         private String csvPageFileName = null;
42         
43
44
45         private String csvAllRowsFileName = null;
46         
47         private String excelAllRowsFileName = null;     
48
49         public ArrayList searchResultColumns = new ArrayList();
50         
51         public ArrayList searchResultRows = new ArrayList();
52
53         public SearchResult(int pageNo) {
54                 this(pageNo, Globals.getDefaultPageSize());
55         } // SearchResult
56
57         public SearchResult(int pageNo, int pageSize) {
58                 this(pageNo, pageSize, -1, -1);
59         } // SearchResult
60
61         public SearchResult(int pageNo, int pageSize, int writeAccessColIndex,
62                         int ownerIndicatorColIndex) {
63                 super();
64
65                 this.pageNo = pageNo;
66                 this.pageSize = pageSize;
67
68                 this.writeAccessColIndex = writeAccessColIndex;
69                 this.ownerIndicatorColIndex = ownerIndicatorColIndex;
70         } // SearchResult
71
72         public int getPageNo() {
73                 return pageNo;
74         }
75
76         public int getPageSize() {
77                 return pageSize;
78         }
79
80         public int getDataSize() {
81                 return dataSize;
82         }
83
84         public String getCsvPageFileName() {
85                 return csvPageFileName;
86         }
87
88         public String getCsvAllRowsFileName() {
89                 return csvAllRowsFileName;
90         }
91
92         public String getExcelAllRowsFileName() {
93                 return excelAllRowsFileName;
94         }
95         
96         private void setDataSize(int dataSize) {
97                 this.dataSize = dataSize;
98         }
99
100         public void setCsvPageFileName(String csvPageFileName) {
101                 this.csvPageFileName = csvPageFileName;
102         }
103
104
105         
106         public void setCsvAllRowsFileName(String csvAllRowsFileName) {
107                 this.csvAllRowsFileName = csvAllRowsFileName;
108         }
109
110
111
112         public void addColumn(SearchResultColumn column) {
113                 searchResultColumns.add(column);
114         } // addColumn
115
116         public SearchResultColumn getColumn(int index) {
117                 return (SearchResultColumn) searchResultColumns.get(index);
118         } // getColumn
119
120         public int getNumColumns() {
121                 return searchResultColumns.size();
122         } // getNumColumns
123
124         public int getNumRows() {
125                 return searchResultRows.size();
126         } // getNumRows
127
128         public SearchResultRow getRow(int index) {
129                 return (SearchResultRow) searchResultRows.get(index);
130         } // getRow
131
132         public void parseData(DataSet ds, HttpServletRequest request) throws RaptorException {
133                 // Presumes single ID field in the first column of the DataSet and row
134                 // number in the first SearchResultColumn
135                 String userID = AppUtils.getUserID(request);
136                 setDataSize(ds.getRowCount());
137
138                 int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
139                 int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount()) : ds
140                                 .getRowCount();
141                 for (int r = startRow; r < endRow; r++) {
142                         SearchResultRow row = new SearchResultRow();
143                         searchResultRows.add(row);
144
145                         String idValue = ds.getString(r, 0);
146
147                         boolean bCanEdit = true;
148                         if (writeAccessColIndex >= 0) {
149                                 String isReadOnlyValue = nvl(ds.getString(r, writeAccessColIndex), "Y");
150                                 bCanEdit = AppUtils.isSuperUser(request) || AppUtils.isAdminUser(request)
151                                                 || isReadOnlyValue.equals("N");
152                         }
153
154                         boolean bCanDelete = bCanEdit;
155                         if (Globals.getDeleteOnlyByOwner() && ownerIndicatorColIndex >= 0) {
156                                 String isOwnedByUserRecord = nvl(ds.getString(r, ownerIndicatorColIndex), "N");
157                                 bCanDelete = AppUtils.isSuperUser(request) || isOwnedByUserRecord.equals("Y");
158                         }
159                         
160                         boolean bCanSchedule = ds.getString(r, getNumColumns()-3).equals("Y");
161
162                         row.addSearchResultField(new SearchResultField("" + (r + 1), idValue,
163                                         getColumn(0), true));
164                         boolean isAuthorized = true;
165                         for (int c = 1; c < getNumColumns(); c++) {
166                                 SearchResultColumn column = getColumn(c);
167                                 isAuthorized = true;
168
169                                 if(column.isCopyLink()) 
170                                         isAuthorized = Globals.getCanCopyOnReadOnly()? true:bCanEdit;
171                                 else if (column.isDeleteLink())
172                                         isAuthorized = bCanDelete;
173                                 else if (column.isEditLink())
174                                         isAuthorized = bCanEdit;
175                                 else if (column.isScheduleLink())
176                                         isAuthorized = bCanSchedule;
177
178                                 row.addSearchResultField(new SearchResultField(
179                                                 (column.getLinkURL() == null) ? ds.getString(r, c) : column
180                                                                 .getLinkTitle(), idValue,  column, isAuthorized
181                                                 ));
182                         } // for
183                 } // for
184         } // parseData
185
186         public void truncateToPage(int pageNo) {
187                 if (this.pageNo >= 0 || pageNo < 0)
188                         return;
189
190                 this.pageNo = pageNo;
191
192                 int startRow = pageNo * pageSize;
193                 int endRow = Math.min(startRow + pageSize, dataSize);
194
195                 for (int r = getNumRows() - 1; r >= endRow; r--)
196                         searchResultRows.remove(r);
197
198                 for (int r = startRow - 1; r >= 0; r--)
199                         searchResultRows.remove(r);
200         } // truncateToPage
201
202         /** *********************************************************************** */
203
204         private String nvl(String s) {
205                 return (s == null) ? "" : s;
206         }
207
208         private String nvl(String s, String sDefault) {
209                 return nvl(s).equals("") ? sDefault : s;
210         }
211
212 } // SearchResult