Small code cleaning

This commit is contained in:
Julien Kirch 2022-03-21 11:36:25 +01:00 committed by GitHub
parent 64ec0f63ca
commit d5832c48e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 30 deletions

View File

@ -7,7 +7,6 @@ import com.sismics.util.context.ThreadLocalContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -49,10 +48,9 @@ public class FileDao {
* @param limit Limit * @param limit Limit
* @return List of files * @return List of files
*/ */
@SuppressWarnings("unchecked")
public List<File> findAll(int offset, int limit) { public List<File> findAll(int offset, int limit) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.deleteDate is null", File.class);
q.setFirstResult(offset); q.setFirstResult(offset);
q.setMaxResults(limit); q.setMaxResults(limit);
return q.getResultList(); return q.getResultList();
@ -64,10 +62,9 @@ public class FileDao {
* @param userId User ID * @param userId User ID
* @return List of files * @return List of files
*/ */
@SuppressWarnings("unchecked")
public List<File> findByUserId(String userId) { public List<File> findByUserId(String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.userId = :userId and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.userId = :userId and f.deleteDate is null", File.class);
q.setParameter("userId", userId); q.setParameter("userId", userId);
return q.getResultList(); return q.getResultList();
} }
@ -76,14 +73,14 @@ public class FileDao {
* Returns an active file. * Returns an active file.
* *
* @param id File ID * @param id File ID
* @return Document * @return File
*/ */
public File getFile(String id) { public File getFile(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null", File.class);
q.setParameter("id", id); q.setParameter("id", id);
try { try {
return (File) q.getSingleResult(); return q.getSingleResult();
} catch (NoResultException e) { } catch (NoResultException e) {
return null; return null;
} }
@ -94,15 +91,15 @@ public class FileDao {
* *
* @param id File ID * @param id File ID
* @param userId User ID * @param userId User ID
* @return Document * @return File
*/ */
public File getFile(String id, String userId) { public File getFile(String id, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.userId = :userId and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.id = :id and f.userId = :userId and f.deleteDate is null", File.class);
q.setParameter("id", id); q.setParameter("id", id);
q.setParameter("userId", userId); q.setParameter("userId", userId);
try { try {
return (File) q.getSingleResult(); return q.getSingleResult();
} catch (NoResultException e) { } catch (NoResultException e) {
return null; return null;
} }
@ -118,9 +115,9 @@ public class FileDao {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the file // Get the file
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null", File.class);
q.setParameter("id", id); q.setParameter("id", id);
File fileDb = (File) q.getSingleResult(); File fileDb = q.getSingleResult();
// Delete the file // Delete the file
Date dateNow = new Date(); Date dateNow = new Date();
@ -140,9 +137,9 @@ public class FileDao {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the file // Get the file
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null", File.class);
q.setParameter("id", file.getId()); q.setParameter("id", file.getId());
File fileDb = (File) q.getSingleResult(); File fileDb = q.getSingleResult();
// Update the file // Update the file
fileDb.setDocumentId(file.getDocumentId()); fileDb.setDocumentId(file.getDocumentId());
@ -164,10 +161,10 @@ public class FileDao {
*/ */
public File getActiveById(String id) { public File getActiveById(String id) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null"); TypedQuery<File> q = em.createQuery("select f from File f where f.id = :id and f.deleteDate is null", File.class);
q.setParameter("id", id); q.setParameter("id", id);
try { try {
return (File) q.getSingleResult(); return q.getSingleResult();
} catch (NoResultException e) { } catch (NoResultException e) {
return null; return null;
} }
@ -210,10 +207,9 @@ public class FileDao {
* @param versionId Version ID * @param versionId Version ID
* @return List of files * @return List of files
*/ */
@SuppressWarnings("unchecked")
public List<File> getByVersionId(String versionId) { public List<File> getByVersionId(String versionId) {
EntityManager em = ThreadLocalContext.get().getEntityManager(); EntityManager em = ThreadLocalContext.get().getEntityManager();
Query q = em.createQuery("select f from File f where f.versionId = :versionId and f.deleteDate is null order by f.order asc"); TypedQuery<File> q = em.createQuery("select f from File f where f.versionId = :versionId and f.deleteDate is null order by f.order asc", File.class);
q.setParameter("versionId", versionId); q.setParameter("versionId", versionId);
return q.getResultList(); return q.getResultList();
} }

View File

@ -1,6 +1,5 @@
package com.sismics.docs.core.util; package com.sismics.docs.core.util;
import com.google.common.base.Charsets;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.CharStreams; import com.google.common.io.CharStreams;
@ -28,6 +27,7 @@ import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
@ -76,7 +76,7 @@ public class FileUtil {
// Consume the data as text // Consume the data as text
try (InputStream is = process.getInputStream()) { try (InputStream is = process.getInputStream()) {
return CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); return CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
} }
} }

View File

@ -11,6 +11,7 @@ import org.apache.pdfbox.pdmodel.PDDocument;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -33,7 +34,7 @@ public class TextPlainFormatHandler implements FormatHandler {
PdfWriter.getInstance(output, pdfOutputStream); PdfWriter.getInstance(output, pdfOutputStream);
output.open(); output.open();
String content = new String(Files.readAllBytes(file), Charsets.UTF_8); String content = Files.readString(file, StandardCharsets.UTF_8);
Font font = FontFactory.getFont("LiberationMono-Regular"); Font font = FontFactory.getFont("LiberationMono-Regular");
Paragraph paragraph = new Paragraph(content, font); Paragraph paragraph = new Paragraph(content, font);
paragraph.setAlignment(Element.ALIGN_LEFT); paragraph.setAlignment(Element.ALIGN_LEFT);
@ -46,7 +47,7 @@ public class TextPlainFormatHandler implements FormatHandler {
@Override @Override
public String extractContent(String language, Path file) throws Exception { public String extractContent(String language, Path file) throws Exception {
return new String(Files.readAllBytes(file), "UTF-8"); return Files.readString(file, StandardCharsets.UTF_8);
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package com.sismics.docs.core.util.format; package com.sismics.docs.core.util.format;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Closer; import com.google.common.io.Closer;
@ -13,6 +12,7 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -65,7 +65,7 @@ public class VideoFormatHandler implements FormatHandler {
// Consume the data as a string // Consume the data as a string
try (InputStream is = process.getInputStream()) { try (InputStream is = process.getInputStream()) {
return new String(ByteStreams.toByteArray(is), Charsets.UTF_8); return new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8);
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }

View File

@ -29,6 +29,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
@ -87,7 +88,7 @@ public class EmailUtil {
try { try {
// Build email headers // Build email headers
HtmlEmail email = new HtmlEmail(); HtmlEmail email = new HtmlEmail();
email.setCharset("UTF-8"); email.setCharset(StandardCharsets.UTF_8.name());
ConfigDao configDao = new ConfigDao(); ConfigDao configDao = new ConfigDao();
// Hostname // Hostname

View File

@ -1,6 +1,5 @@
package com.sismics.util; package com.sismics.util;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing; import com.google.common.hash.Hashing;
import javax.imageio.IIOImage; import javax.imageio.IIOImage;
@ -13,6 +12,7 @@ import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster; import java.awt.image.WritableRaster;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Iterator; import java.util.Iterator;
/** /**
@ -80,7 +80,7 @@ public class ImageUtil {
} }
return Hashing.md5().hashString( return Hashing.md5().hashString(
email.trim().toLowerCase(), Charsets.UTF_8) email.trim().toLowerCase(), StandardCharsets.UTF_8)
.toString(); .toString();
} }

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.*; import java.util.*;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
@ -53,7 +54,7 @@ public class ResourceUtil {
// Extract the JAR path // Extract the JAR path
String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8")); JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8));
Set<String> fileSet = new HashSet<String>(); Set<String> fileSet = new HashSet<String>();
try { try {

View File

@ -43,6 +43,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
@ -124,7 +125,7 @@ public class FileResource extends BaseResource {
try { try {
String name = fileBodyPart.getContentDisposition() != null ? String name = fileBodyPart.getContentDisposition() != null ?
URLDecoder.decode(fileBodyPart.getContentDisposition().getFileName(), "UTF-8") : null; URLDecoder.decode(fileBodyPart.getContentDisposition().getFileName(), StandardCharsets.UTF_8) : null;
String fileId = FileUtil.createFile(name, previousFileId, unencryptedFile, fileSize, documentDto == null ? String fileId = FileUtil.createFile(name, previousFileId, unencryptedFile, fileSize, documentDto == null ?
null : documentDto.getLanguage(), principal.getId(), documentId); null : documentDto.getLanguage(), principal.getId(), documentId);