java: refactoring Format.java, FormatManager.java, FileType.java

This commit is contained in:
Aleksandr Fedorov
2023-09-13 11:24:51 +03:00
parent 0f3bb28d92
commit 5e2f812fee
5 changed files with 57 additions and 120 deletions

View File

@ -18,8 +18,17 @@
package entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;
public enum FileType {
Word,
Cell,
Slide
@JsonProperty("word")
@SerializedName("word")
WORD,
@JsonProperty("cell")
@SerializedName("cell")
CELL,
@JsonProperty("slide")
@SerializedName("slide")
SLIDE
}

View File

@ -21,51 +21,19 @@ package format;
import java.util.List;
import entities.FileType;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public final class Format {
private final String name;
public String getName() {
return this.name;
}
private String name;
private FileType type;
private List<String> actions;
private List<String> convert;
private List<String> mime;
private final FileType type;
public FileType getType() {
return this.type;
}
private final List<String> actions;
public List<String> getActions() {
return this.actions;
}
private final List<String> convert;
public List<String> getConvert() {
return this.convert;
}
private final List<String> mime;
public List<String> getMime() {
return this.mime;
}
public Format(final String nameParameter,
final FileType typeParameter,
final List<String> actionsParameter,
final List<String> convertParameter,
final List<String> mimeParameter) {
this.name = nameParameter;
this.type = typeParameter;
this.actions = actionsParameter;
this.convert = convertParameter;
this.mime = mimeParameter;
}
public String extension() {
return "." + this.name;
}
}

View File

@ -18,23 +18,18 @@
package format;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import entities.FileType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class FormatManager {
@ -44,12 +39,11 @@ public final class FormatManager {
formats = this.all();
}
public List<Format> getFormats() {
return this.formats;
}
public List<Format> getFormatsByAction(String action) {
public List<Format> getFormatsByAction(final String action) {
return this
.all()
.stream()
@ -57,11 +51,19 @@ public final class FormatManager {
.collect(Collectors.toList());
}
public List<String> allExtensions() {
return this
.formats
.stream()
.map(format -> format.getName())
.collect(Collectors.toList());
}
public List<String> fillableExtensions() {
return this
.getFormatsByAction("fill")
.stream()
.map(format -> format.extension())
.map(format -> format.getName())
.collect(Collectors.toList());
}
@ -69,49 +71,30 @@ public final class FormatManager {
return this
.getFormatsByAction("view")
.stream()
.map(format -> format.extension())
.map(format -> format.getName())
.collect(Collectors.toList());
}
public List<String> editableExtensions() {
return Stream.of(this.getFormatsByAction("edit"), this.getFormatsByAction("edit"))
return Stream.of(this.getFormatsByAction("edit"), this.getFormatsByAction("lossy-edit"))
.flatMap(x -> x.stream())
.map(format -> format.extension())
.map(format -> format.getName())
.collect(Collectors.toList());
}
public List<String> convertibleExtensions() throws URISyntaxException,
IOException,
JsonSyntaxException {
public List<String> autoConvertExtensions() {
return this
.convertible()
.stream()
.map(format -> format.extension())
.collect(Collectors.toList());
}
public List<Format> convertible() throws URISyntaxException,
IOException,
JsonSyntaxException {
return this
.formats
.stream()
.filter(format -> (
format.getType() == FileType.Cell && format.getConvert().contains("xlsx")
|| format.getType() == FileType.Slide && format.getConvert().contains("pptx")
|| format.getType() == FileType.Word && format.getConvert().contains("docx")
))
.collect(Collectors.toList());
.getFormatsByAction("auto-convert")
.stream()
.map(format -> format.getName())
.collect(Collectors.toList());
}
private List<Format> all() {
try {
ObjectMapper objectMapper = new ObjectMapper();
Path path = this.file();
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
String contents = String.join(System.lineSeparator(), lines);
Gson gson = new Gson();
Format[] formats = gson.fromJson(contents, Format[].class);
return Arrays.asList(formats);
return objectMapper.readValue(Files.readAllBytes(path), new TypeReference<List<Format>>() { });
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -83,49 +83,26 @@ public final class DocumentManager {
// get all the supported file extensions
public static List<String> getFileExts() {
List<String> res = new ArrayList<>();
res.addAll(getViewedExts());
res.addAll(getEditedExts());
res.addAll(getConvertExts());
res.addAll(getFillExts());
return res;
return DocumentManager.formatManager.allExtensions();
}
public static List<String> getFillExts() {
try {
return DocumentManager.formatManager.fillableExtensions();
} catch (Exception ex) {
return new ArrayList<>();
}
return DocumentManager.formatManager.fillableExtensions();
}
// get file extensions that can be viewed
public static List<String> getViewedExts() {
try {
return DocumentManager.formatManager.viewableExtensions();
} catch (Exception ex) {
return new ArrayList<>();
}
return DocumentManager.formatManager.viewableExtensions();
}
// get file extensions that can be edited
public static List<String> getEditedExts() {
try {
return DocumentManager.formatManager.editableExtensions();
} catch (Exception ex) {
return new ArrayList<>();
}
return DocumentManager.formatManager.editableExtensions();
}
// get file extensions that can be converted
public static List<String> getConvertExts() {
try {
return DocumentManager.formatManager.convertibleExtensions();
} catch (Exception ex) {
return new ArrayList<>();
}
return DocumentManager.formatManager.autoConvertExtensions();
}
// get current user host address
@ -513,17 +490,17 @@ public final class DocumentManager {
// get an editor internal extension
public static String getInternalExtension(final FileType fileType) {
// .docx for word file type
if (fileType.equals(FileType.Word)) {
if (fileType.equals(FileType.WORD)) {
return ".docx";
}
// .xlsx for cell file type
if (fileType.equals(FileType.Cell)) {
if (fileType.equals(FileType.CELL)) {
return ".xlsx";
}
// .pptx for slide file type
if (fileType.equals(FileType.Slide)) {
if (fileType.equals(FileType.SLIDE)) {
return ".pptx";
}
@ -535,17 +512,17 @@ public final class DocumentManager {
public static String getTemplateImageUrl(final FileType fileType) {
String path = getServerUrl(true) + "/css/img/";
// for word file type
if (fileType.equals(FileType.Word)) {
if (fileType.equals(FileType.WORD)) {
return path + "file_docx.svg";
}
// .xlsx for cell file type
if (fileType.equals(FileType.Cell)) {
if (fileType.equals(FileType.CELL)) {
return path + "file_xlsx.svg";
}
// .pptx for slide file type
if (fileType.equals(FileType.Slide)) {
if (fileType.equals(FileType.SLIDE)) {
return path + "file_pptx.svg";
}

View File

@ -44,7 +44,7 @@ public final class FileUtility {
}
// default file type is word
return FileType.Word;
return FileType.WORD;
}
// get file name from the url