/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.molgenis.byod.emx2rdf;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.ss.usermodel.WorkbookFactory;

/**
 *
 * @author David van Enckevort <david@allthingsdigital.nl>
 */
public class ExcelReader implements Iterator<Map<String, String>> {

    private final Sheet dataset;
    private final Sheet organisations;
    private final Sheet persons;
    private final Workbook wb;
    private String[] headers;
    private int rowNum;

    public ExcelReader(final Path location) throws IOException, InvalidFormatException {

        wb = WorkbookFactory.create(location.toFile());
        dataset = wb.getSheet("decode_cocos_20140601");
        organisations = wb.getSheet("decode_organisations");
        persons = wb.getSheet("decode_persons");
        Row row = dataset.getRow(0);
        headers = new String[row.getLastCellNum()];
        rowNum = 1;
        for (int cellNum = row.getFirstCellNum(); cellNum <= row.getLastCellNum(); cellNum++) {
            Cell cell = row.getCell(cellNum);
            if (cell == null || cell.getCellType() != Cell.CELL_TYPE_STRING) {
                break;
            }
            headers[cellNum] = cell.getStringCellValue();
        }
    }

    @Override
    public boolean hasNext() {
        return rowNum <= dataset.getLastRowNum();
    }

    @Override
    public Map<String, String> next() {
        Map<String, String> result = new HashMap<>();
        Row row = dataset.getRow(rowNum);
        rowNum++;
        for (int cellNum = 0; cellNum < headers.length; cellNum++) {
            Cell cell = row.getCell(cellNum);
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    result.put(headers[cellNum], row.getCell(cellNum).getStringCellValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    double value = cell.getNumericCellValue();
                    result.put(headers[cellNum], String.valueOf(value));
            }
        }
        return result;
    }
}

@@ -804,16 +804,18 @@
 ataset;%0A
+//
     priv
@@ -841,24 +841,26 @@
 anisations;%0A
+//
     private 
@@ -1164,24 +1164,26 @@
 20140601%22);%0A
+//
         orga
@@ -1227,24 +1227,26 @@
 isations%22);%0A
+//
         pers
@@ -2195,16 +2195,270 @@
 STRING:%0A
+                    if (%22organisation%22.equals(headers%5BcellNum%5D)) %7B%0A                        addOrganisation(result);%0A                    %7D else if (%22%22.equals(headers%5BcellNum%5D)) %7B%0A                        addPerson(result);%0A                    %7D else %7B%0A    
         
@@ -2538,24 +2538,47 @@
 llValue());%0A
+                    %7D%0A%0A
             
@@ -2661,23 +2661,52 @@
     
-double 
+result.put(headers%5BcellNum%5D, String.
 value
- = 
+Of(
 cell
@@ -2719,32 +2719,34 @@
 mericCellValue()
+))
 ;%0A              
@@ -2747,114 +2747,409 @@
     
-        result.put(headers%5BcellNum%5D, String.valueOf(value));%0A        
+%7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private void addOrganisation(Map%3CString,String%3E map) %7B%0A        map.put(%22organisationName%22, %22VUmc%22);%0A        map.put(%22homepageUrl%22, %22http://www.vumc.nl/%22);%0A
     %7D%0A
+%0A
     
-    %7D%0A        return result
+private void addPerson(Map%3CString, String%3E map) %7B%0A        map.put(%22fullName%22, %22Gerrit Meijer%22);%0A        map.put(%22emailAddress%22, %22ga.meijer@vumc.nl%22);%0A        map.put(%22phone%22, %22020-4444852%22)
 ;%0A  

Support contact and organisation