Skip to content

Commit

Permalink
Merge pull request #416 from devgateway/fix/OCVN-400/export-all-butto…
Browse files Browse the repository at this point in the history
…n-saveas

OCVN-400 Export all button not working on prod
  • Loading branch information
mpostelnicu authored Jun 8, 2017
2 parents d47df9a + 8671b92 commit 9973789
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 52 deletions.
24 changes: 12 additions & 12 deletions ui/oce/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,22 @@ class OCApp extends React.Component{
}

exportBtn(){
if(this.state.exporting){
return (
<div className="filters">
<div className="progress">
<div className="progress-bar progress-bar-danger" role="progressbar" style={{width: "100%"}}>
{this.t('export:exporting')}
</div>
</div>
</div>
)
const { filters, selectedYears, locale, selectedMonths } = this.state;
let url = new URI('/api/ocds/excelExport')
.addSearch(filters.toJS())
.addSearch('year', selectedYears.toArray())
.addSearch('language', locale);

if(selectedYears.count() == 1){
url = url.addSearch('month', selectedMonths && selectedMonths.toJS())
.addSearch('monthly', true);
}
return <div className="filters" onClick={e => this.downloadExcel()}>

return <a className="filters" href={url} download="export.zip">
<img className="top-nav-icon" src="assets/icons/export.svg" width="100%" height="100%"/>
{this.t('export:export')}
<i className="glyphicon glyphicon-menu-down"></i>
</div>
</a>
}

toggleDashboardSwitcher(e){
Expand Down
5 changes: 5 additions & 0 deletions ui/oce/style.less
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@
}
}
}

a.filters{
text-decoration: none;
color: white;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package org.devgateway.ocds.web.rest.controller.export;

import io.swagger.annotations.ApiOperation;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.io.FileCleaningTracker;
import org.apache.commons.io.IOUtils;
import org.devgateway.ocds.persistence.mongo.Release;
import org.devgateway.ocds.persistence.mongo.constants.MongoConstants;
import org.devgateway.ocds.web.rest.controller.GenericOCDSController;
Expand All @@ -29,6 +17,13 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static org.springframework.data.mongodb.core.query.Query.query;

Expand All @@ -39,19 +34,15 @@
@RestController
public class ExcelExportController extends GenericOCDSController {
protected final Logger logger = LoggerFactory.getLogger(ExcelExportController.class);

@Autowired
protected AdminSettingsRepository adminSettingsRepository;
@Autowired
private SettingsUtils settingsUtils;

@Autowired
private ExcelGenerator excelGenerator;

@Autowired
private FileCleaningTracker fileCleaningTracker;

@Autowired
protected AdminSettingsRepository adminSettingsRepository;

@ApiOperation(value = "Export releases in Excel format.")
@RequestMapping(value = "/api/ocds/excelExport", method = {RequestMethod.GET, RequestMethod.POST})
public void excelExport(@ModelAttribute @Valid final YearFilterPagingRequest filter,
Expand All @@ -74,39 +65,25 @@ public void excelExport(@ModelAttribute @Valid final YearFilterPagingRequest fil
if (numberOfReleases <= settingsUtils.getExcelBatchSize()) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + "excel-export.xlsx");

response.getOutputStream().write(excelGenerator.getExcelDownload(filter));
} else {
File file = File.createTempFile("createZip", ".zip");
logger.info("Created temp file: " + file.getAbsolutePath());

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

ZipOutputStream zout = new ZipOutputStream(bos);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=" + "excel-export.zip");
response.flushBuffer();
ZipOutputStream zout = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
zout.setMethod(ZipOutputStream.DEFLATED);
zout.setLevel(Deflater.BEST_SPEED);

zout.setLevel(Deflater.NO_COMPRESSION);
int numberOfPages = (int) Math.ceil((double) numberOfReleases / filter.getPageSize());
for (int i = 0; i < numberOfPages; i++) {
filter.setPageNumber(i);

ZipEntry ze = new ZipEntry("excel-export-page " + (i + 1) + ".xlsx");

zout.putNextEntry(ze);
byte[] bytes = excelGenerator.getExcelDownload(filter);
zout.write(bytes, 0, bytes.length);

zout.closeEntry();
response.flushBuffer();
}
zout.close();
fileCleaningTracker.track(file, file);
IOUtils.closeQuietly(bos);

response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=" + "excel-export.zip");

InputStream is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.devgateway.ocds.web.util;

import java.util.List;
import org.devgateway.toolkit.persistence.dao.AdminSettings;
import org.devgateway.toolkit.persistence.repository.AdminSettingsRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author idobre
* @since 6/22/16
Expand All @@ -18,7 +19,7 @@ public class SettingsUtils {

public static final String DEFAULT_LANGUAGE = "en_US";

private static final Integer EXCELBATCHSIZEDEFAULT = 10000;
private static final Integer EXCELBATCHSIZEDEFAULT = 5000;

@Autowired
private AdminSettingsRepository adminSettingsRepository;
Expand Down

0 comments on commit 9973789

Please sign in to comment.