Skip to content

Commit

Permalink
Merge pull request #1 from kadioglumf/csv-reader
Browse files Browse the repository at this point in the history
feat: add csv reader.
  • Loading branch information
kadioglumf authored Nov 14, 2023
2 parents ff2fabe + 140806c commit 5b48d6b
Show file tree
Hide file tree
Showing 24 changed files with 476 additions and 258 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
Expand Down Expand Up @@ -77,5 +82,11 @@
<version>${apache-commons-loggin.version}</version>
</dependency>

<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</version>
</dependency>

</dependencies>
</project>
20 changes: 1 addition & 19 deletions src/main/java/com/kadioglumf/Main.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
package com.kadioglumf;

import com.kadioglumf.dto.excel.TestExcelDto;
import com.kadioglumf.enums.FileExtension;
import com.kadioglumf.service.ReaderService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.context.i18n.LocaleContextHolder;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Locale;

public class Main {



public static void main(String[] args) throws Exception {
LocaleContextHolder.setLocale(new Locale("tr", "TR"));

File file = new File(System.getProperty("user.dir") + "/src/main/resources/test.xlsx");
ReaderService readerService = new ReaderService();

List<TestExcelDto> list = readerService.readFile(TestExcelDto.class, new FileInputStream(file), FilenameUtils.getExtension(file.getName()), true);
list.forEach(System.out::println);
public static void main(String[] args) {
}
}
27 changes: 0 additions & 27 deletions src/main/java/com/kadioglumf/NumberFormat.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/com/kadioglumf/annotations/csv/CsvColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kadioglumf.annotations.csv;

import com.kadioglumf.cellprocessor.CellProcessor;
import com.kadioglumf.cellprocessor.StringCell;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface CsvColumn {
int columnIndex();
Class<? extends CellProcessor> cellProcessor() default StringCell.class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kadioglumf.annotations.csv;

import java.lang.annotation.*;

@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExportCsvSettings {
char quoteChar() default '"';
char delimiterChar() default ',';
String endOfLineSymbols() default "\r\n";
boolean isFirstRowHeader() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcelColumn {

String columnName() default "";
int columnIndex();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kadioglumf.annotations.excel;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExportExcelSettings {
boolean isFirstRowHeader() default true;
}
6 changes: 6 additions & 0 deletions src/main/java/com/kadioglumf/cellprocessor/CellProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kadioglumf.cellprocessor;

public interface CellProcessor {

Object execute(Object var1, Class<?> targetType);
}
14 changes: 14 additions & 0 deletions src/main/java/com/kadioglumf/cellprocessor/DateCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kadioglumf.cellprocessor;

import com.kadioglumf.util.DateUtils;

public class DateCell implements CellProcessor {

@Override
public Object execute(Object var1, Class<?> targetType) {
if (var1 == null) {
return null;
}
return DateUtils.parseStringToDateFormatted(targetType, String.valueOf(var1));
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/kadioglumf/cellprocessor/NumberCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.kadioglumf.cellprocessor;

import com.kadioglumf.util.NumberUtils;

import java.text.ParseException;

public class NumberCell implements CellProcessor {


@Override
public Object execute(Object var1, Class<?> targetType) {
if (var1 == null) {
return null;
}

try {
return NumberUtils.parseNumericValue(String.valueOf(var1), targetType);
}
catch (ParseException ex) {
throw new RuntimeException(); //TODO
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/kadioglumf/cellprocessor/StringCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.kadioglumf.cellprocessor;

public class StringCell implements CellProcessor {


@Override
public Object execute(Object var1, Class<?> targetType) {
if (var1 == null) {
return null;
}
return String.valueOf(var1);
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/kadioglumf/dto/Tuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.kadioglumf.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tuple<T, V> implements Serializable {
private transient T first;
private transient V second;
}
9 changes: 7 additions & 2 deletions src/main/java/com/kadioglumf/service/ReaderService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kadioglumf.service;

import com.kadioglumf.enums.FileExtension;
import com.kadioglumf.util.csv.CsvReaderUtils;
import com.kadioglumf.util.excel.ExcelReaderUtil;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
Expand All @@ -11,11 +12,15 @@
@Service
public class ReaderService {

public <T> List<T> readFile(@NonNull Class<T> clazz, @NonNull InputStream inputStream, @NonNull String fileExtension, @NonNull boolean isFirstRowHeader) throws Exception {
public <T> List<T> readFile(@NonNull Class<T> clazz, @NonNull InputStream inputStream, @NonNull String fileExtension) throws Exception {
if (FileExtension.XLS.getValue().equals(fileExtension)
|| FileExtension.XLSX.getValue().equals(fileExtension)) {
ExcelReaderUtil readerUtil = new ExcelReaderUtil(inputStream, FileExtension.getFileExtensionByValue(fileExtension));
return readerUtil.read(clazz, isFirstRowHeader);
return readerUtil.read(clazz);
}
else if (FileExtension.CSV.getValue().equals(fileExtension)) {
CsvReaderUtils readerUtil = new CsvReaderUtils(inputStream, clazz);
return readerUtil.read(clazz);
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/kadioglumf/util/BaseReaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public abstract class BaseReaderUtils {

protected abstract <T> List<T> read(Class<T> clazz, boolean isFirstRowHeader) throws Exception;
protected abstract <T> List<T> read(Class<T> clazz) throws Exception;
}
Loading

0 comments on commit 5b48d6b

Please sign in to comment.