FileMetadataServiceImpl.java

/*
 * 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.server.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.genesys.server.service.FileMetadataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * The Class FileMetadataServiceImpl.
 *
 * @author Maxym Borodenko
 */
@Service
public class FileMetadataServiceImpl implements FileMetadataService {

	/**
	 * The Constant LOG.
	 */
	private static final Logger LOG = LoggerFactory.getLogger(FileMetadataServiceImpl.class);

	/**
	 * The Constant ROWS_TO_READ.
	 */
	private static final int ROWS_TO_READ = 10;

	private final ObjectMapper mapper = new ObjectMapper();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ObjectNode getExcelMetadata(final File excelFile) throws IOException {
		final ObjectNode mainNode = mapper.createObjectNode();

//		try (final Workbook workbook = new XSSFWorkbook(excelFile)) {
//			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();
//				JsonNode headersNode = mapper.createArrayNode();
//				final ArrayNode rowsNode = mapper.createArrayNode();
//
//				while (iterator.hasNext()) {
//					final Row currentRow = iterator.next();
//					final Iterator<Cell> cellIterator = currentRow.iterator();
//					final List<Object> values = cellsValues(cellIterator);
//
//					if (currentRow.getRowNum() == 0) {
//						headersNode = mapper.convertValue(values, JsonNode.class);
//					}
//					if (currentRow.getRowNum() < ROWS_TO_READ) {
//						rowsNode.add(mapper.convertValue(values, JsonNode.class));
//					} else {
//						// We reached the rows limit
//						break;
//					}
//				}
//
//				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);
//		}

		return mainNode;
	}

	/**
	 * Get cells values from row
	 *
	 * @param cellIterator the cellIterator
	 * @return the list of cell values from row
	 */
	private List<Object> cellsValues(final Iterator<Cell> cellIterator) {
		final List<Object> list = new ArrayList<>();
//		while (cellIterator.hasNext()) {
//			final Cell currentCell = cellIterator.next();
//			switch (currentCell.getCellTypeEnum()) {
//			case BLANK:
//				list.add(null);
//				break;
//			case ERROR:
//				list.add("#ERROR");
//				break;
//			case STRING:
//				list.add(currentCell.getStringCellValue());
//				break;
//			case BOOLEAN:
//				list.add(currentCell.getBooleanCellValue());
//				break;
//			case NUMERIC:
//				if (DateUtil.isCellDateFormatted(currentCell)) {
//					list.add(currentCell.getDateCellValue());
//				} else {
//					list.add(currentCell.getNumericCellValue());
//				}
//				break;
//			default:
//				list.add(currentCell.toString());
//			}
//		}
		return list;
	}
}