Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/service file dao #121

Merged
merged 15 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions src/main/java/com/casaculturaqxd/sgec/DAO/ServiceFileDAO.java
robsonad07 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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();
}
}
12 changes: 8 additions & 4 deletions src/main/java/com/casaculturaqxd/sgec/enums/ServiceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}


Expand All @@ -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");
}


Expand All @@ -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() {
Expand All @@ -75,7 +92,6 @@ public void setUltimaModificacao(Date ultimaModificacao) {
this.ultimaModificacao = ultimaModificacao;
}


@Override
public String toString() {
return "ServiceFile [serviceFileId=" + serviceFileId + ", fileKey=" + fileKey + ", service="
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/casaculturaqxd/sgec/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,4 +113,9 @@ private String findFileSuffix(String fileName) {
}
return null;
}

@Override
public String toString() {
return "s3";
}
}