Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
import org.apache.fesod.sheet.converters.shortconverter.ShortBooleanConverter;
import org.apache.fesod.sheet.converters.shortconverter.ShortNumberConverter;
import org.apache.fesod.sheet.converters.shortconverter.ShortStringConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter;
import org.apache.fesod.sheet.converters.string.StringBooleanConverter;
import org.apache.fesod.sheet.converters.string.StringErrorConverter;
import org.apache.fesod.sheet.converters.string.StringNumberConverter;
Expand Down Expand Up @@ -122,6 +125,8 @@ private static void initAllConverter() {

putAllConverter(new LocalTimeNumberConverter());
putAllConverter(new LocalTimeStringConverter());
putAllConverter(new SqlTimeNumberConverter());
putAllConverter(new SqlTimeStringConverter());

putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter());
Expand Down Expand Up @@ -160,6 +165,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new LocalTimeDateConverter());
putWriteConverter(new SqlTimeDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
Expand All @@ -181,6 +187,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new LocalTimeStringConverter());
putWriteStringConverter(new SqlTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fesod.sheet.converters.sqltime;

import java.sql.Time;
import java.time.LocalDateTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.fesod.sheet.util.WorkBookUtil;

/** java.sql.Time and date converter. */
public class SqlTimeDateConverter implements Converter<Time> {
@Override
public Class<?> supportJavaTypeKey() {
return Time.class;
}

@Override
public WriteCellData<?> convertToExcelData(
Time value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
LocalDateTime localDateTime = value == null ? null : value.toLocalTime().atDate(DateUtils.EPOCH);
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(
cellData, format == null || format.isEmpty() ? null : format, DateUtils.DEFAULT_LOCAL_TIME_FORMAT);
return cellData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fesod.sheet.converters.sqltime;

import java.math.BigDecimal;
import java.sql.Time;
import java.time.LocalTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;

/** java.sql.Time and number converter. */
public class SqlTimeNumberConverter implements Converter<Time> {
@Override
public Class<?> supportJavaTypeKey() {
return Time.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}

@Override
public Time convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
LocalTime localTime = DateUtils.getLocalTime(
cellData.getNumberValue().doubleValue(), DateUtils.isDate1904(contentProperty, globalConfiguration));
return localTime == null ? null : Time.valueOf(localTime);
}

@Override
public WriteCellData<?> convertToExcelData(
Time value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(BigDecimal.valueOf(DateUtil.getExcelDate(
value.toLocalTime().atDate(DateUtils.EPOCH),
DateUtils.isDate1904(contentProperty, globalConfiguration))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fesod.sheet.converters.sqltime;

import java.sql.Time;
import java.time.LocalTime;
import org.apache.fesod.sheet.converters.Converter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.metadata.data.WriteCellData;
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
import org.apache.fesod.sheet.util.DateUtils;

/** java.sql.Time and string converter. */
public class SqlTimeStringConverter implements Converter<Time> {
@Override
public Class<?> supportJavaTypeKey() {
return Time.class;
}

@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}

@Override
public Time convertToJavaData(
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String format = contentProperty == null || contentProperty.getDateTimeFormatProperty() == null
? null
: contentProperty.getDateTimeFormatProperty().getFormat();
LocalTime localTime =
DateUtils.parseLocalTime(cellData.getStringValue(), format, globalConfiguration.getLocale());
return Time.valueOf(localTime);
}

@Override
public WriteCellData<?> convertToExcelData(
Time value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String format = contentProperty == null || contentProperty.getDateTimeFormatProperty() == null
? null
: contentProperty.getDateTimeFormatProperty().getFormat();
return new WriteCellData<>(DateUtils.format(value.toLocalTime(), format, globalConfiguration.getLocale()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fesod.sheet.converter;

import java.sql.Time;
import lombok.Getter;
import lombok.Setter;
import org.apache.fesod.sheet.annotation.ExcelProperty;

@Getter
@Setter
public class SqlTimeRoundTripData {
@ExcelProperty("time")
private Time time;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.apache.fesod.sheet.converter;

import java.io.File;
import java.sql.Time;
import java.util.Collections;
import java.util.List;
import org.apache.fesod.sheet.testkit.Tags;
import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper;
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;

@Tag(Tags.ROUND_TRIP)
class SqlTimeRoundTripTest extends AbstractExcelTest {
@ParameterizedTest
@ExcelFormatSource
void roundTrip(ExcelFormat format) throws Exception {
File file = createTempFile(format);
SqlTimeRoundTripData input = new SqlTimeRoundTripData();
input.setTime(Time.valueOf("12:34:56"));
List<SqlTimeRoundTripData> result =
RoundTripHelper.writeAndRead(file, SqlTimeRoundTripData.class, Collections.singletonList(input));
Assertions.assertEquals(1, result.size());
Assertions.assertEquals(input.getTime(), result.get(0).getTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

package org.apache.fesod.sheet.converters;

import java.sql.Time;
import java.time.LocalTime;
import java.util.Map;
import org.apache.fesod.sheet.converters.ConverterKeyBuild.ConverterKey;
import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeNumberConverter;
import org.apache.fesod.sheet.converters.localtime.LocalTimeStringConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter;
import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -67,6 +71,23 @@ void loadConvertersRegistersLocalTimeFamily() {
writeConverter.get(ConverterKeyBuild.buildKey(LocalTime.class, CellDataTypeEnum.STRING)));
}

@Test
void loadConvertersRegistersSqlTimeFamily() {
Map<ConverterKey, Converter<?>> allConverter = DefaultConverterLoader.loadAllConverter();
Assertions.assertInstanceOf(
SqlTimeNumberConverter.class,
allConverter.get(ConverterKeyBuild.buildKey(Time.class, CellDataTypeEnum.NUMBER)));
Assertions.assertInstanceOf(
SqlTimeStringConverter.class,
allConverter.get(ConverterKeyBuild.buildKey(Time.class, CellDataTypeEnum.STRING)));
Map<ConverterKey, Converter<?>> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter();
Assertions.assertInstanceOf(
SqlTimeDateConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(Time.class)));
Assertions.assertInstanceOf(
SqlTimeStringConverter.class,
writeConverter.get(ConverterKeyBuild.buildKey(Time.class, CellDataTypeEnum.STRING)));
}

private static void assertLoadIsImmutableAndCopyIsMutable(
Map<ConverterKey, Converter<?>> loaded, Map<ConverterKey, Converter<?>> copy) {
Map.Entry<ConverterKey, Converter<?>> entry =
Expand Down
Loading
Loading