Newer
Older
/*
* Copyright 2011 ETH Zuerich, CISD
*
* 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 eu.basynthec.cisd.dss;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import ch.systemsx.cisd.common.logging.LogCategory;
import ch.systemsx.cisd.common.logging.LogFactory;
import ch.systemsx.cisd.openbis.dss.generic.shared.utils.ExcelFileReader;
* An abstraction for accessing time series data following the BaSynthec conventions from an Excel
* file.
*
* @author Chandrasekhar Ramakrishnan
*/
public class TimeSeriesDataExcel
{
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
private static final String OPENBIS_METADATA_SHEET_NAME = "openbis-metadata";
private static final String OPENBIS_DATA_SHEET_NAME = "openbis-data";
private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION,
TimeSeriesDataExcel.class);
/**
* Create a time series data from the given file name.
*
* @param fileName
* @return A TimeSeriesDataExcel or null if one could not be created.
*/
public static TimeSeriesDataExcel createTimeSeriesDataExcel(String fileName)
{
File file = new File(fileName);
Workbook workbook;
try
{
workbook = ExcelFileReader.getExcelWorkbook(file);
ExcelFileReader fileReader = new ExcelFileReader(workbook, true);
return new TimeSeriesDataExcel(file, fileReader);
} catch (IllegalArgumentException ex)
{
operationLog.error("Could not open file [" + fileName + "] as Excel data.", ex);
} catch (IOException ex)
{
operationLog.error("Could not open file [" + fileName + "] as Excel data.", ex);
}
return null;
}
private final File file;
private final ExcelFileReader fileReader;
/**
* Constructor
*/
private TimeSeriesDataExcel(File file, ExcelFileReader fileReader)
{
super();
this.file = file;
this.fileReader = fileReader;
}
/**
* Get the raw lines of the metadata sheet.
*/
public List<String[]> getRawMetadataLines()
{
try
{
return fileReader.readLines(OPENBIS_METADATA_SHEET_NAME);
} catch (IOException ex)
{
operationLog.error("Could not read data from [file: " + file.getPath() + ", sheet: "
+ OPENBIS_METADATA_SHEET_NAME + "]", ex);
}
return new ArrayList<String[]>();
}
/**
* Get the raw lines of the data sheet.
*/
public List<String[]> getRawDataLines()
{
try
{
return fileReader.readLines(OPENBIS_DATA_SHEET_NAME);
} catch (IOException ex)
{
operationLog.error("Could not read data from [file: " + file.getPath() + ", sheet: "
+ OPENBIS_DATA_SHEET_NAME + "]", ex);
}
return new ArrayList<String[]>();
}
/**
* Return the metadata has a hashmap, with all keys uppercased.
* <p>
* Assumes the metadata sheet corresponds to the following format: [Property] [Value] [... stuff
* that can be ignored], that is the property name is in column 1 and property value is in
* column 2, and everything else can be ignored.
*/
public Map<String, String> getMetadataMap()
{
HashMap<String, String> metadataMap = new HashMap<String, String>();
List<String[]> metadataLines = getRawMetadataLines();
// Skip the first line, this is just the header
for (int i = 1; i < metadataLines.size(); ++i)
{
String[] line = metadataLines.get(i);
String value = line[1];
if ("BLANK".equals(value))
{
value = null;
}
metadataMap.put(line[0].toUpperCase(), value);
return metadataMap;
}