summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/sdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/model/search/SearchResult.java
blob: 5ab7559011d2c5ced614fda9dee65f27b6aa10c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.analytics.model.search;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.openecomp.portalsdk.analytics.error.RaptorException;
import org.openecomp.portalsdk.analytics.system.AppUtils;
import org.openecomp.portalsdk.analytics.system.Globals;
import org.openecomp.portalsdk.analytics.util.DataSet;

public class SearchResult{
	private int pageNo = -1;

	private int pageSize = 50;

	private int dataSize = -1;

	private int writeAccessColIndex = -1;

	private int ownerIndicatorColIndex = -1;

	private String csvPageFileName = null;
	


	private String csvAllRowsFileName = null;
	
	private String excelAllRowsFileName = null;	

	public ArrayList searchResultColumns = new ArrayList();
	
	public ArrayList searchResultRows = new ArrayList();

	public SearchResult(int pageNo) {
		this(pageNo, Globals.getDefaultPageSize());
	} // SearchResult

	public SearchResult(int pageNo, int pageSize) {
		this(pageNo, pageSize, -1, -1);
	} // SearchResult

	public SearchResult(int pageNo, int pageSize, int writeAccessColIndex,
			int ownerIndicatorColIndex) {
		super();

		this.pageNo = pageNo;
		this.pageSize = pageSize;

		this.writeAccessColIndex = writeAccessColIndex;
		this.ownerIndicatorColIndex = ownerIndicatorColIndex;
	} // SearchResult

	public int getPageNo() {
		return pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getDataSize() {
		return dataSize;
	}

	public String getCsvPageFileName() {
		return csvPageFileName;
	}

	public String getCsvAllRowsFileName() {
		return csvAllRowsFileName;
	}

	public String getExcelAllRowsFileName() {
		return excelAllRowsFileName;
	}
	
	private void setDataSize(int dataSize) {
		this.dataSize = dataSize;
	}

	public void setCsvPageFileName(String csvPageFileName) {
		this.csvPageFileName = csvPageFileName;
	}


	
	public void setCsvAllRowsFileName(String csvAllRowsFileName) {
		this.csvAllRowsFileName = csvAllRowsFileName;
	}



	public void addColumn(SearchResultColumn column) {
		searchResultColumns.add(column);
	} // addColumn

	public SearchResultColumn getColumn(int index) {
		return (SearchResultColumn) searchResultColumns.get(index);
	} // getColumn

	public int getNumColumns() {
		return searchResultColumns.size();
	} // getNumColumns

	public int getNumRows() {
		return searchResultRows.size();
	} // getNumRows

	public SearchResultRow getRow(int index) {
		return (SearchResultRow) searchResultRows.get(index);
	} // getRow

	public void parseData(DataSet ds, HttpServletRequest request) throws RaptorException {
		// Presumes single ID field in the first column of the DataSet and row
		// number in the first SearchResultColumn
		String userID = AppUtils.getUserID(request);
		setDataSize(ds.getRowCount());

		int startRow = (pageNo >= 0) ? (pageNo * pageSize) : 0;
		int endRow = (pageNo >= 0) ? Math.min(startRow + pageSize, ds.getRowCount()) : ds
				.getRowCount();
		for (int r = startRow; r < endRow; r++) {
			SearchResultRow row = new SearchResultRow();
			searchResultRows.add(row);

			String idValue = ds.getString(r, 0);

			boolean bCanEdit = true;
			if (writeAccessColIndex >= 0) {
				String isReadOnlyValue = nvl(ds.getString(r, writeAccessColIndex), "Y");
				bCanEdit = AppUtils.isSuperUser(request) || AppUtils.isAdminUser(request)
						|| isReadOnlyValue.equals("N");
			}

			boolean bCanDelete = bCanEdit;
			if (Globals.getDeleteOnlyByOwner() && ownerIndicatorColIndex >= 0) {
				String isOwnedByUserRecord = nvl(ds.getString(r, ownerIndicatorColIndex), "N");
				bCanDelete = AppUtils.isSuperUser(request) || isOwnedByUserRecord.equals("Y");
			}
			
			boolean bCanSchedule = ds.getString(r, getNumColumns()-3).equals("Y");

			row.addSearchResultField(new SearchResultField("" + (r + 1), idValue,
					getColumn(0), true));
			boolean isAuthorized = true;
			for (int c = 1; c < getNumColumns(); c++) {
				SearchResultColumn column = getColumn(c);
				isAuthorized = true;

				if(column.isCopyLink()) 
					isAuthorized = Globals.getCanCopyOnReadOnly()? true:bCanEdit;
				else if (column.isDeleteLink())
					isAuthorized = bCanDelete;
				else if (column.isEditLink())
					isAuthorized = bCanEdit;
				else if (column.isScheduleLink())
					isAuthorized = bCanSchedule;

				row.addSearchResultField(new SearchResultField(
						(column.getLinkURL() == null) ? ds.getString(r, c) : column
								.getLinkTitle(), idValue,  column, isAuthorized
						));
			} // for
		} // for
	} // parseData

	public void truncateToPage(int pageNo) {
		if (this.pageNo >= 0 || pageNo < 0)
			return;

		this.pageNo = pageNo;

		int startRow = pageNo * pageSize;
		int endRow = Math.min(startRow + pageSize, dataSize);

		for (int r = getNumRows() - 1; r >= endRow; r--)
			searchResultRows.remove(r);

		for (int r = startRow - 1; r >= 0; r--)
			searchResultRows.remove(r);
	} // truncateToPage

	/** *********************************************************************** */

	private String nvl(String s) {
		return (s == null) ? "" : s;
	}

	private String nvl(String s, String sDefault) {
		return nvl(s).equals("") ? sDefault : s;
	}

} // SearchResult