This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
781384f
commit a9df7c5
Showing
12 changed files
with
223 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,11 +192,11 @@ | |
</dependency> | ||
|
||
<!-- ReCaptcha --> | ||
<dependency> | ||
<groupId>net.tanesha.recaptcha4j</groupId> | ||
<artifactId>recaptcha4j</artifactId> | ||
<version>0.0.7</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.tanesha.recaptcha4j</groupId> | ||
<artifactId>recaptcha4j</artifactId> | ||
<version>0.0.7</version> | ||
</dependency> | ||
|
||
<!-- FIxme remove ? --> | ||
<dependency> | ||
|
@@ -208,6 +208,19 @@ | |
</dependencies> | ||
|
||
<build> | ||
<pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-release-plugin</artifactId> | ||
<version>2.5.2</version> | ||
<configuration> | ||
<remoteTagging>true</remoteTagging> | ||
<autoVersionSubmodules>true</autoVersionSubmodules> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
|
@@ -255,6 +268,6 @@ | |
<url>https://github.com/gaetancollaud/fablab-manager</url> | ||
<connection>scm:git:git://github.com/gaetancollaud/fablab-manager.git</connection> | ||
<developerConnection>scm:git:[email protected]:gaetancollaud/fablab-manager.git</developerConnection> | ||
<tag>HEAD</tag> | ||
</scm> | ||
<tag>HEAD</tag> | ||
</scm> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/net/collaud/fablab/manager/export/CsvExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.collaud.fablab.manager.export; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* | ||
* @author Gaetan Collaud | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface CsvExport { | ||
public String fileName(); | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
src/main/java/net/collaud/fablab/manager/export/CsvExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.collaud.fablab.manager.export; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.BinaryOperator; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* | ||
* @author Gaetan Collaud | ||
*/ | ||
@Slf4j | ||
public class CsvExporter<T> { | ||
|
||
public static final String FIELD_SEPARATOR = ";"; | ||
public static final String LINE_SEPARATOR = "\n"; | ||
public static final String ESCAPE_CHAR = "\""; | ||
|
||
private final Map<Field, CsvField> fields; | ||
private final CsvExport classAnnotation; | ||
private final StringBuilder builder; | ||
|
||
public CsvExporter(Class<T> type) { | ||
fields = new LinkedHashMap<>(); | ||
builder = new StringBuilder(); | ||
classAnnotation = Optional.ofNullable(type.getAnnotation(CsvExport.class)) | ||
.orElseThrow(() -> new IllegalArgumentException("Class " + type.getSimpleName() + " has no CsvExport annotation")); | ||
Arrays.stream(type.getDeclaredFields()) | ||
.filter(f -> f.getAnnotation(CsvField.class) != null) | ||
.forEach(f -> fields.put(f, f.getAnnotation(CsvField.class))); | ||
} | ||
|
||
public CsvExporter writeHeader() { | ||
return writeLine(fields.values().stream() | ||
.map(f -> f.headerName()) | ||
.reduce(reduce()) | ||
.orElse("")); | ||
} | ||
|
||
public CsvExporter writeRow(T obj) { | ||
return writeLine(fields.keySet().stream() | ||
.map(f -> getFieldValue(obj, f)) | ||
.reduce(reduce()) | ||
.orElse("")); | ||
} | ||
|
||
private BinaryOperator<String> reduce() { | ||
return (l, r) ->l + FIELD_SEPARATOR + r ; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return builder.toString(); | ||
} | ||
|
||
public String getFileName() { | ||
return classAnnotation.fileName(); | ||
} | ||
|
||
private String getFieldValue(T obj, Field f) { | ||
try { | ||
f.setAccessible(true); | ||
return Optional.ofNullable(f.get(obj)) | ||
.map(v -> v.toString()) | ||
.orElse(""); | ||
} catch (IllegalAccessException ex) { | ||
log.error("Cannot access field " + f.getName(), ex); | ||
return ""; | ||
} | ||
} | ||
|
||
private CsvExporter writeLine(String l) { | ||
builder.append(l); | ||
builder.append(LINE_SEPARATOR); | ||
return this; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/net/collaud/fablab/manager/export/CsvField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.collaud.fablab.manager.export; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* | ||
* @author Gaetan Collaud | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD}) | ||
public @interface CsvField { | ||
public String headerName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.