Commit 3865ff81 authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Excel metadata controller

use temp file for analysis
parent 01cedbc7
......@@ -233,6 +233,13 @@
<version>2.3</version>
</dependency>
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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.genesys.catalog.service;
import java.io.IOException;
import java.io.InputStream;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* The Class MetadataService.
*
* @author Maxym Borodenko
*/
public interface MetadataService {
/**
* generate JSON with Excel metadata
*
* @param inputStream
* @return generated JSON object with Excel metadata
*/
ObjectNode getExcelMetadata(InputStream inputStream) throws IOException;
}
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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.genesys.catalog.service.impl;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.genesys.catalog.service.MetadataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* The Class MetadataServiceImpl.
*
* @author Maxym Borodenko
*/
@Service
public class MetadataServiceImpl implements MetadataService {
/**
* The Constant LOG.
*/
private static final Logger LOG = LoggerFactory.getLogger(MetadataServiceImpl.class);
/**
* The Constant ROWS_TO_READ.
*/
private static final int ROWS_TO_READ = 10;
/**
* {@inheritDoc}
*/
@Override
public ObjectNode getExcelMetadata(final InputStream inputStream) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode mainNode = mapper.createObjectNode();
final File tempFile = new File("temp");
try (final Workbook workbook = new XSSFWorkbook(tempFile)) {
FileUtils.copyInputStreamToFile(inputStream, tempFile);
final ArrayNode sheetsArrayNode = mapper.createArrayNode();
final int numberOfSheets = workbook.getNumberOfSheets();
for (int i = 0; i < numberOfSheets; i++) {
final Sheet datatypeSheet = workbook.getSheetAt(i);
final Iterator<Row> iterator = datatypeSheet.iterator();
final ArrayNode headersNode = mapper.createArrayNode();
final ArrayNode rowsNode = mapper.createArrayNode();
while (iterator.hasNext()) {
final Row currentRow = iterator.next();
final Iterator<Cell> cellIterator = currentRow.iterator();
final List<String> values = cellsValues(cellIterator);
if (currentRow.getRowNum() == 0) {
for (final String header : values) {
headersNode.add(header);
}
}
if (currentRow.getRowNum() < ROWS_TO_READ) {
final ArrayNode cellNode = mapper.createArrayNode();
for (final String cellValue : values) {
cellNode.add(cellValue);
}
rowsNode.add(cellNode);
}
}
final ObjectNode sheetNode = mapper.createObjectNode();
sheetNode.put("name", datatypeSheet.getSheetName());
sheetNode.set("headers", headersNode);
sheetNode.set("rows", rowsNode);
sheetNode.put("rowCount", (datatypeSheet.getLastRowNum() + 1));
sheetsArrayNode.add(sheetNode);
}
// leave metadata blank temporarily
mainNode.set("metadata", mapper.createObjectNode());
mainNode.set("sheets", sheetsArrayNode);
} catch (final IOException | InvalidFormatException e) {
LOG.error("Error writing metadata. {}", e.getMessage());
throw new IOException("Error writing metadata", e);
} finally {
FileUtils.deleteQuietly(tempFile);
}
return mainNode;
}
/**
* Get cells values from row
*
* @param cellIterator the cellIterator
* @return the list of cell values from row
*/
private List<String> cellsValues(final Iterator<Cell> cellIterator) {
final List<String> list = new ArrayList<>();
while (cellIterator.hasNext()) {
final Cell currentCell = cellIterator.next();
list.add(currentCell.toString());
}
return list;
}
}
/*
* Copyright 2018 Global Crop Diversity Trust
*
* 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.genesys.catalog.server.controller.api.v0;
import java.io.IOException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.genesys.catalog.service.MetadataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* The Class MetadataController.
*
* @author Maxym Borodenko
*/
@RestController
@RequestMapping(MetadataController.API_BASE)
@PreAuthorize("isAuthenticated()")
public class MetadataController {
/**
* The Constant API_BASE.
*/
protected static final String API_BASE = "/api/v0/metadata";
/**
* The Constant LOG.
*/
private static final Logger LOG = LoggerFactory.getLogger(MetadataController.class);
@Autowired
private MetadataService metadataService;
@PostMapping(consumes = {"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
public ObjectNode excelFileUpload(@RequestParam("file") final MultipartFile file) throws IOException {
LOG.info("Uploading Excel file {}", file.getOriginalFilename());
//TODO store the file to the repository
return metadataService.getExcelMetadata(file.getInputStream());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment