diff --git a/src/main/java/com/casaculturaqxd/sgec/DAO/ServiceFileDAO.java b/src/main/java/com/casaculturaqxd/sgec/DAO/ServiceFileDAO.java new file mode 100644 index 00000000..0a669f13 --- /dev/null +++ b/src/main/java/com/casaculturaqxd/sgec/DAO/ServiceFileDAO.java @@ -0,0 +1,141 @@ +package com.casaculturaqxd.sgec.DAO; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.SortedSet; + +import com.casaculturaqxd.sgec.models.Evento; +import com.casaculturaqxd.sgec.models.arquivo.ServiceFile; +import com.casaculturaqxd.sgec.service.Service; + +public class ServiceFileDAO { + Connection connection; + Service service; + + ServiceFileDAO(Service service){ + this.service = service; + } + + public void setConnection(Connection connection){ + this.connection = connection; + } + + public boolean inserirArquivo(ServiceFile arquivo){ + try { + setService(arquivo); + service.enviarArquivo(arquivo); + String sql = "insert into service_file (file_key,suffix,service,bucket,ultima_modificacao)" + + " values(?,?,?,?,?)"; + PreparedStatement stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, arquivo.getFileKey()); + stmt.setString(2, arquivo.getSuffix()); + stmt.setString(3, arquivo.getService().toString()); + stmt.setString(4, arquivo.getBucket()); + stmt.setDate(5, arquivo.getUltimaModificacao()); + int numRemocoes = stmt.executeUpdate(); + ResultSet rs = stmt.getGeneratedKeys(); + if (rs.next()) { + arquivo.setServiceFileId(rs.getInt("id_service_file")); + } + stmt.close(); + return numRemocoes > 0; + } catch (Exception e) { + return false; + } + } + + public ServiceFile getArquivo(ServiceFile arquivo){ + try { + setService(arquivo); + service.getArquivo(arquivo.getBucket(), arquivo.getFileKey()); + String sql = "select * from service_file where id_service_file=?"; + PreparedStatement stmt = connection.prepareStatement(sql); + stmt.setInt(1, arquivo.getServiceFileId()); + ResultSet resultSet = stmt.executeQuery(); + ServiceFile arquivoRetorno = null; + if (resultSet.next()) { + arquivoRetorno = arquivo; + arquivoRetorno.setServiceFileId(resultSet.getInt("id_service_file")); + arquivoRetorno.setFileKey(resultSet.getString("file_key")); + arquivoRetorno.setSuffix(resultSet.getString("suffix")); + arquivoRetorno.setService(resultSet.getString("service")); + arquivoRetorno.setBucket(resultSet.getString("bucket")); + arquivoRetorno.setUltimaModificacao(resultSet.getDate("ultima_modificacao")); + } + return arquivo; + } catch (Exception e) { + return null; + } + } + + public boolean deleteArquivo(ServiceFile arquivo){ + try { + setService(arquivo); + service.deletarArquivo(arquivo.getBucket(), arquivo.getFileKey()); + String sql = "delete from service_file where id_service_file=?"; + PreparedStatement stmt = connection.prepareStatement(sql); + stmt.setInt(1, arquivo.getServiceFileId()); + int numRemocoes = stmt.executeUpdate(); + stmt.close(); + return numRemocoes > 0; + } catch (Exception e) { + return false; + } + } + + public boolean vincularAllArquivos(Evento evento){ + SortedSet listaArquivos = evento.getListaArquivos(); + for (Integer idServiceFile : listaArquivos) { + boolean temp = vincularArquivo(idServiceFile, evento.getIdEvento()); + if(temp == false) + return false; + } + return true; + } + + public boolean vincularArquivo(int idServiceFile, int idEvento){ + String sql = "INSERT INTO service_file_evento(id_evento, id_service_file) VALUES (?, ?);"; + try { + PreparedStatement stmt = connection.prepareStatement(sql); + stmt.setInt(1, idEvento); + stmt.setInt(2, idServiceFile); + stmt.execute(); + stmt.close(); + return true; + } catch (SQLException e) { + return false; + } + } + + public boolean desvincularArquivo(int idServiceFile, int idEvento) { + String sql = "delete from service_file_evento where id_service_file=? and id_evento=?"; + try { + PreparedStatement stmt = connection.prepareStatement(sql); + stmt.setInt(1, idServiceFile); + stmt.setInt(2, idEvento); + stmt.execute(); + stmt.close(); + return true; + } catch (Exception e) { + return false; + } + } + + public boolean desvincularAllArquivos(Evento evento) { + SortedSet listaArquivos = evento.getListaArquivos(); + for (Integer idServiceFile : listaArquivos) { + boolean temp = desvincularArquivo(idServiceFile, evento.getIdEvento()); + if(temp == false) + return false; + } + evento.setListaArquivos(null); + return true; + } + + public void setService(ServiceFile arquivo) { + this.service = arquivo.getService(); + } +} diff --git a/src/main/java/com/casaculturaqxd/sgec/enums/ServiceType.java b/src/main/java/com/casaculturaqxd/sgec/enums/ServiceType.java index 419052fd..0d8047c9 100644 --- a/src/main/java/com/casaculturaqxd/sgec/enums/ServiceType.java +++ b/src/main/java/com/casaculturaqxd/sgec/enums/ServiceType.java @@ -3,13 +3,17 @@ public enum ServiceType { S3("s3"); - private final String type; + private static String type; ServiceType(String type){ - this.type = type; + type = type; + } + + public void setType(String type) { + this.type = type; } - String getType(){ - return this.type; + public static String getType(){ + return type; } } diff --git a/src/main/java/com/casaculturaqxd/sgec/models/arquivo/ServiceFile.java b/src/main/java/com/casaculturaqxd/sgec/models/arquivo/ServiceFile.java index 3521fe7d..d3ecd00a 100644 --- a/src/main/java/com/casaculturaqxd/sgec/models/arquivo/ServiceFile.java +++ b/src/main/java/com/casaculturaqxd/sgec/models/arquivo/ServiceFile.java @@ -3,10 +3,16 @@ import java.io.File; import java.sql.Date; + +import com.casaculturaqxd.sgec.enums.ServiceType; +import com.casaculturaqxd.sgec.service.Service; +import com.casaculturaqxd.sgec.service.ServiceFactory; + public class ServiceFile { private Integer serviceFileId; private String fileKey; - private String service; + private String suffix; + private Service service; private String bucket; private Date ultimaModificacao; private File preview; @@ -16,6 +22,7 @@ public ServiceFile(File content, String bucket) { this.content = content; this.bucket = bucket; this.fileKey = content.getName(); + this.service = ServiceFactory.getService(ServiceType.S3, "ACCESS_KEY","SECRET_KEY"); } @@ -24,6 +31,7 @@ public ServiceFile(String fileKey, String bucket, Date ultimaModificacao, File c this.bucket = bucket; this.ultimaModificacao = ultimaModificacao; this.content = content; + this.service = ServiceFactory.getService(ServiceType.S3, "ACCESS_KEY","SECRET_KEY"); } @@ -43,12 +51,21 @@ public void setFileKey(String fileKey) { this.fileKey = fileKey; } - public String getService() { - return service; + + public String getSuffix() { + return suffix; + } + + public void setSuffix(String suffix) { + this.suffix = suffix; + } + + public Service getService() { + return this.service; } - public void setService(String service) { - this.service = service; + public void setService(String serviceType) { + this.service = ServiceFactory.getService(ServiceType.valueOf(serviceType), "ACCESS_KEY", "SECRET_KEY"); } public String getBucket() { @@ -75,7 +92,6 @@ public void setUltimaModificacao(Date ultimaModificacao) { this.ultimaModificacao = ultimaModificacao; } - @Override public String toString() { return "ServiceFile [serviceFileId=" + serviceFileId + ", fileKey=" + fileKey + ", service=" diff --git a/src/main/java/com/casaculturaqxd/sgec/service/S3Service.java b/src/main/java/com/casaculturaqxd/sgec/service/S3Service.java index 21a53f81..8e02b769 100644 --- a/src/main/java/com/casaculturaqxd/sgec/service/S3Service.java +++ b/src/main/java/com/casaculturaqxd/sgec/service/S3Service.java @@ -16,7 +16,6 @@ import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; -import com.amazonaws.services.s3.model.S3Object; import com.casaculturaqxd.sgec.models.arquivo.ServiceFile; import io.github.cdimascio.dotenv.Dotenv; @@ -114,4 +113,9 @@ private String findFileSuffix(String fileName) { } return null; } + + @Override + public String toString() { + return "s3"; + } }