Skip to content

Commit 729696a

Browse files
committed
2 parents 0ff0a2d + 815dc15 commit 729696a

6 files changed

Lines changed: 70 additions & 51 deletions

File tree

knowage-core/src/main/java/it/eng/spagobi/api/v2/export/AbstractExportJob.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ public final void execute(JobExecutionContext context) throws JobExecutionExcept
145145
throw new JobExecutionException(e);
146146
}
147147

148-
export(context);
149-
Path metadataFile = ExportPathBuilder.getInstance().getPerJobIdMetadataFile(resourcePathAsStr, userProfile, id);
148+
try {
149+
export(context);
150+
} catch (IOException e) {
151+
throw new RuntimeException(e);
152+
}
153+
Path metadataFile = ExportPathBuilder.getInstance().getPerJobIdMetadataFile(resourcePathAsStr, userProfile, id);
150154

151155
try {
152156
String dataSetName = dataSet.getName();
@@ -188,7 +192,7 @@ protected final IDataSet getDataSet() {
188192
return dataSet;
189193
}
190194

191-
private final IDataSet getDataSet(Integer dataSetId, Map<String, Object> drivers, Map<String, String> parameters, UserProfile userProfile)
195+
private IDataSet getDataSet(Integer dataSetId, Map<String, Object> drivers, Map<String, String> parameters, UserProfile userProfile)
192196
throws JobExecutionException {
193197
IDataSetDAO dsDAO = DAOFactory.getDataSetDAO();
194198
dsDAO.setUserProfile(userProfile);
@@ -306,7 +310,7 @@ private void initializeTenant() {
306310
* @param context
307311
* @throws JobExecutionException
308312
*/
309-
protected abstract void export(JobExecutionContext context) throws JobExecutionException;
313+
protected abstract void export(JobExecutionContext context) throws JobExecutionException, IOException;
310314

311315
/**
312316
* @return The MIME type of generated file.

knowage-core/src/main/java/it/eng/spagobi/api/v2/export/ExcelExportJob.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
import java.io.IOException;
2121
import java.io.OutputStream;
2222
import java.math.BigDecimal;
23+
import java.sql.ResultSetMetaData;
2324
import java.sql.Timestamp;
2425
import java.text.ParseException;
2526
import java.text.SimpleDateFormat;
27+
import java.util.ArrayList;
2628
import java.util.Date;
29+
import java.util.List;
30+
import java.util.Optional;
2731

32+
import it.eng.spagobi.tools.dataset.common.metadata.IFieldMetaData;
2833
import org.apache.log4j.LogMF;
2934
import org.apache.log4j.Logger;
3035
import org.apache.poi.ss.SpreadsheetVersion;
@@ -76,52 +81,29 @@ protected void export(JobExecutionContext context) throws JobExecutionException
7681

7782
// STYLE CELL
7883
CellStyle borderStyleHeader = wb.createCellStyle();
79-
// borderStyleHeader.setBorderBottom(BorderStyle.THIN);
80-
// borderStyleHeader.setBorderLeft(BorderStyle.THIN);
81-
// borderStyleHeader.setBorderRight(BorderStyle.THIN);
82-
// borderStyleHeader.setBorderTop(BorderStyle.THIN);
8384
borderStyleHeader.setAlignment(HorizontalAlignment.CENTER);
8485

8586
CellStyle borderStyleRow = wb.createCellStyle();
86-
// borderStyleRow.setBorderBottom(BorderStyle.THIN);
87-
// borderStyleRow.setBorderLeft(BorderStyle.THIN);
88-
// borderStyleRow.setBorderRight(BorderStyle.THIN);
89-
// borderStyleRow.setBorderTop(BorderStyle.THIN);
9087
borderStyleRow.setAlignment(HorizontalAlignment.RIGHT);
9188

9289
CellStyle tsCellStyle = wb.createCellStyle();
9390
tsCellStyle.setDataFormat(createHelper.createDataFormat().getFormat(TIMESTAMP_FORMAT));
94-
// tsCellStyle.setBorderBottom(BorderStyle.THIN);
95-
// tsCellStyle.setBorderLeft(BorderStyle.THIN);
96-
// tsCellStyle.setBorderRight(BorderStyle.THIN);
97-
// tsCellStyle.setBorderTop(BorderStyle.THIN);
9891
tsCellStyle.setAlignment(HorizontalAlignment.RIGHT);
9992

10093
CellStyle dateCellStyle = wb.createCellStyle();
10194
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat(DATE_FORMAT));
102-
// dateCellStyle.setBorderBottom(BorderStyle.THIN);
103-
// dateCellStyle.setBorderLeft(BorderStyle.THIN);
104-
// dateCellStyle.setBorderRight(BorderStyle.THIN);
105-
// dateCellStyle.setBorderTop(BorderStyle.THIN);
10695
dateCellStyle.setAlignment(HorizontalAlignment.RIGHT);
10796

10897
CellStyle intCellStyle = wb.createCellStyle();
10998
intCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("0"));
110-
// intCellStyle.setBorderBottom(BorderStyle.THIN);
111-
// intCellStyle.setBorderLeft(BorderStyle.THIN);
112-
// intCellStyle.setBorderRight(BorderStyle.THIN);
113-
// intCellStyle.setBorderTop(BorderStyle.THIN);
11499
intCellStyle.setAlignment(HorizontalAlignment.RIGHT);
115100

116101
CellStyle decimalCellStyle = wb.createCellStyle();
117102
decimalCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#,##0.00"));
118-
// decimalCellStyle.setBorderBottom(BorderStyle.THIN);
119-
// decimalCellStyle.setBorderLeft(BorderStyle.THIN);
120-
// decimalCellStyle.setBorderRight(BorderStyle.THIN);
121-
// decimalCellStyle.setBorderTop(BorderStyle.THIN);
122103
decimalCellStyle.setAlignment(HorizontalAlignment.RIGHT);
123104

124105
IMetaData dataSetMetadata = dataSet.getMetadata();
106+
DataIterator iterator = dataSet.iterator();
125107

126108
// CREATE BRANDED HEADER SHEET
127109
this.imageB64 = OrganizationImageManager.getOrganizationB64ImageWide(TenantManager.getTenant().getName());
@@ -149,12 +131,21 @@ protected void export(JobExecutionContext context) throws JobExecutionException
149131
dataspan,
150132
this.documentName,
151133
sheet.getSheetName());
152-
134+
ResultSetMetaData resultSetMetaData = ((it.eng.spagobi.tools.dataset.common.iterator.ResultSetIterator) iterator).getRs().getMetaData();
135+
136+
List<IFieldMetaData> filteredMetadata = new ArrayList<>();
137+
138+
for (int i = 0; i < resultSetMetaData.getColumnCount(); i++) {
139+
String columnName = resultSetMetaData.getColumnName(i + 1);
140+
Optional<IFieldMetaData> fieldMetaData = dataSetMetadata.getFieldsMeta().stream().filter(f -> f.getName().equals(columnName)).findFirst();
141+
fieldMetaData.ifPresent(filteredMetadata::add);
142+
}
143+
153144
header = sheet.createRow((short) headerIndex+1); // first row
154-
if (dataSetMetadata != null && dataSetMetadata.getFieldCount() > 0) {
155-
for (int i = 0; i <= dataSetMetadata.getFieldCount() - 1; i++) {
145+
if (!filteredMetadata.isEmpty()) {
146+
for (int i = 0; i <= filteredMetadata.size() - 1; i++) {
156147
Cell cell = header.createCell(i);
157-
cell.setCellValue(dataSetMetadata.getFieldAlias(i));
148+
cell.setCellValue(filteredMetadata.get(i).getAlias());
158149
cell.setCellStyle(borderStyleHeader);
159150
// set cell style
160151
CellStyle headerCellStyle = buildCellStyle(sheet, true, HorizontalAlignment.LEFT, VerticalAlignment.CENTER, (short) 11);
@@ -166,8 +157,7 @@ protected void export(JobExecutionContext context) throws JobExecutionException
166157
adjustColumnWidth(sheet, this.imageB64);
167158

168159
// FILL CELL RECORD
169-
try (DataIterator iterator = dataSet.iterator()) {
170-
160+
try {
171161
int i = headerIndex+1;
172162
final int recordLimit = SpreadsheetVersion.EXCEL2007.getLastRowIndex();
173163
while (iterator.hasNext() && i < recordLimit) {
@@ -178,7 +168,7 @@ protected void export(JobExecutionContext context) throws JobExecutionException
178168
Row row = sheet.createRow(i + 1); // starting from 2nd row
179169

180170
for (int k = 0; k <= dataSetRecord.getFields().size() - 1; k++) {
181-
Class<?> clazz = dataSetMetadata.getFieldType(k);
171+
Class<?> clazz = filteredMetadata.get(k).getType();
182172
Object value = dataSetRecord.getFieldAt(k).getValue();
183173
Cell cell = row.createCell(k);
184174

@@ -231,8 +221,12 @@ protected void export(JobExecutionContext context) throws JobExecutionException
231221

232222
i++;
233223
}
224+
} catch (RuntimeException rte) {
225+
String msg = "Error generating Excel file";
226+
LOGGER.error(msg, rte);
227+
throw new IllegalStateException(msg, rte);
234228
}
235-
229+
236230
// adjusts the column width to fit the contents
237231
adjustColumnWidth(sheet, this.imageB64);
238232

knowage/src/main/webapp/WEB-INF/jsp/commons/preview.jsp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
6868
<div id="myGrid" class="ag-theme-balham kn-preview-table-theme"></div>
6969

7070
<script type="text/javascript" charset="utf-8">
71-
//GLOBAL VARIABLES
71+
//GLOBAL VARIABLES
72+
const cookie = document.cookie.match(new RegExp('(^|)X-CSRF-TOKEN=([^;]+)'))[2];
7273
const MAX_ROWS_CLIENT_PAGINATION = <%= SingletonConfig.getInstance().getConfigValue("dataset.preview.clientpagination.maxrows") %>;
7374
const SEARCH_WAIT_TIMEOUT = 500;
7475
const DEFAULT_MAX_ITEMS_PER_PAGE = 15;
@@ -260,18 +261,18 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
260261
'</span>'+
261262
'<span class="ag-paging-page-summary-panel">'+
262263
263-
' <div ref="btFirst" class="ag-paging-button-wrapper '+disableFirstClass()+'"><span class="ag-icon ag-icon-first" unselectable="on"></span>'+
264-
' <button type="button" class="ag-paging-button" ref="btFirst" '+disableFirst()+' onclick="first()">First</button>'+
264+
' <div ref="btFirst" class="ag-paging-button-wrapper '+disableFirstClass()+'">'+
265+
' <button type="button"class="ag-icon ag-icon-first" ref="btFirst" '+disableFirst()+' onclick="first()"></button>'+
265266
' </div>'+
266-
' <div ref="btPrevious" class="ag-paging-button-wrapper '+disableFirstClass()+'"><span class="ag-icon ag-icon-previous" unselectable="on"></span>'+
267-
' <button type="button" class="ag-paging-button" ref="btPrevious" '+disableFirst()+' onclick="prev()">Previous</button>'+
267+
' <div ref="btPrevious" class="ag-paging-button-wrapper '+disableFirstClass()+'">'+
268+
' <button type="button" class="ag-icon ag-icon-previous" ref="btPrevious" '+disableFirst()+' onclick="prev()"></button>'+
268269
' </div>'+
269270
'page <span ref="lbCurrent">'+backEndPagination.page+'</span> of <span ref="lbTotal">'+backEndPagination.totalPages+'</span>'+
270-
' <div ref="btNext" class="ag-paging-button-wrapper '+disableLastClass()+'" ><span class="ag-icon ag-icon-next" unselectable="on"></span>'+
271-
' <button type="button" class="ag-paging-button" ref="btNext" onclick="next()" '+disableLast()+'">Next</button>'+
271+
' <div ref="btNext" class="ag-paging-button-wrapper '+disableLastClass()+'" >'+
272+
' <button type="button" class="ag-icon ag-icon-next" ref="btNext" onclick="next()" '+disableLast()+'"></button>'+
272273
' </div>'+
273-
' <div ref="btLast" class="ag-paging-button-wrapper '+disableLastClass()+'" ><span class="ag-icon ag-icon-last" unselectable="on"></span>'+
274-
' <button type="button" class="ag-paging-button" ref="btLast" onclick="last()" '+disableLast()+'">Last</button>'+
274+
' <div ref="btLast" class="ag-paging-button-wrapper '+disableLastClass()+'" >'+
275+
' <button type="button" class="ag-icon ag-icon-last" ref="btLast" onclick="last()" '+disableLast()+'"></button>'+
275276
' </div>'+
276277
'</span>';
277278
}
@@ -334,7 +335,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
334335
335336
//Defining the service call to datastore
336337
function getData(init){
337-
var fetchParams = {method:"POST",body:{}};
338+
var fetchParams = {method:"POST",body:{}, headers : {"X-CSRF-TOKEN": cookie}};
338339
if(init){
339340
if(!options.backEndPagination) {
340341
fetchParams.body.start = 0;
@@ -394,7 +395,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
394395
getData(true);
395396
396397
if(options.exports){
397-
window.fetch(KNOWAGE_BASEURL + KNOWAGE_SERVICESURL + '/2.0/datasets?asPagedList=true&seeTechnical=true&label=' + datasetLabel)
398+
window.fetch(KNOWAGE_BASEURL + KNOWAGE_SERVICESURL + '/2.0/datasets?asPagedList=true&seeTechnical=true&label=' + datasetLabel, {headers : {"X-CSRF-TOKEN": cookie}})
398399
.then(function(response) {return response.json()})
399400
.then(function(data){
400401
DATASET = data.item[0];
@@ -428,6 +429,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
428429
window.fetch(KNOWAGE_BASEURL + KNOWAGE_SERVICESURL + '/2.0/export/dataset/' + DATASET.id.dsId + exportFormat, {
429430
method: "POST",
430431
headers: {
432+
"X-CSRF-TOKEN": cookie,
431433
"Content-Type": "application/json"
432434
},
433435
body: JSON.stringify(body)

knowageutils/src/main/java/it/eng/spagobi/security/utils/AntiCsrfFilter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private boolean excludeCheck(String path) {
7575
urlToExclude.add("/signup/prepare");
7676
urlToExclude.add("/1.0/images/getImage");
7777
urlToExclude.add("/publish");
78+
urlToExclude.add("/2.0/datasets/preview");
7879

7980
return urlToExclude.contains(path);
8081

knowageutils/src/main/java/it/eng/spagobi/tools/dataset/common/iterator/CsvStreamingOutput.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import java.io.OutputStream;
88
import java.io.OutputStreamWriter;
99
import java.io.Writer;
10+
import java.sql.ResultSetMetaData;
11+
import java.util.ArrayList;
1012
import java.util.List;
13+
import java.util.Optional;
1114
import java.util.stream.IntStream;
1215

1316
import javax.ws.rs.WebApplicationException;
@@ -38,15 +41,26 @@ public CsvStreamingOutput(DataIterator iterator) {
3841
super();
3942
this.iterator = iterator;
4043
this.metaData = iterator.getMetaData();
41-
4244
List<IFieldMetaData> fieldsMetadata = this.metaData.getFieldsMeta();
45+
ResultSetMetaData resultSetMetaData;
46+
List<IFieldMetaData> filteredMetadata = new ArrayList<>();
47+
try {
48+
resultSetMetaData = ((it.eng.spagobi.tools.dataset.common.iterator.ResultSetIterator) iterator).getRs().getMetaData();
49+
for (int i = 0; i < resultSetMetaData.getColumnCount(); i++) {
50+
String columnName = resultSetMetaData.getColumnName(i + 1);
51+
Optional<IFieldMetaData> fieldMetaData = fieldsMetadata.stream().filter(f -> f.getName().equals(columnName)).findFirst();
52+
fieldMetaData.ifPresent(filteredMetadata::add);
53+
}
54+
} catch (Exception e) {
55+
}
56+
4357

44-
this.visibleFields = fieldsMetadata.stream().filter(e -> {
58+
this.visibleFields = filteredMetadata.stream().filter(e -> {
4559
Object o = e.getProperties().get("visible");
4660
return o == null || Boolean.valueOf(o.toString());
4761
}).collect(toList());
48-
this.indexesOfVisibleFields = IntStream.range(0, fieldsMetadata.size()).filter(e -> {
49-
Object o = fieldsMetadata.get(e).getProperties().get("visible");
62+
this.indexesOfVisibleFields = IntStream.range(0, filteredMetadata.size()).filter(e -> {
63+
Object o = filteredMetadata.get(e).getProperties().get("visible");
5064
return o == null || Boolean.valueOf(o.toString());
5165
}).boxed().collect(toList());
5266
this.visibleFieldCount = visibleFields.size();

knowageutils/src/main/java/it/eng/spagobi/tools/dataset/common/iterator/ResultSetIterator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,8 @@ private void decrypt(IRecord currRecord, int i) {
207207
}
208208
}
209209

210+
public ResultSet getRs() {
211+
return rs;
212+
}
213+
210214
}

0 commit comments

Comments
 (0)