|
|
@ -1,5 +1,6 @@ |
|
|
|
package mx.gob.jumapacelaya.views.crearnuevoticket; |
|
|
|
|
|
|
|
import com.nimbusds.jose.shaded.gson.JsonArray; |
|
|
|
import com.nimbusds.jose.shaded.gson.JsonObject; |
|
|
|
import com.nimbusds.jose.shaded.gson.JsonParser; |
|
|
|
import com.vaadin.flow.component.button.Button; |
|
|
@ -13,7 +14,7 @@ import com.vaadin.flow.component.orderedlayout.VerticalLayout; |
|
|
|
import com.vaadin.flow.component.textfield.TextArea; |
|
|
|
import com.vaadin.flow.component.textfield.TextField; |
|
|
|
import com.vaadin.flow.component.upload.Upload; |
|
|
|
import com.vaadin.flow.component.upload.receivers.MemoryBuffer; |
|
|
|
import com.vaadin.flow.component.upload.receivers.MultiFileMemoryBuffer; |
|
|
|
import com.vaadin.flow.router.PageTitle; |
|
|
|
import com.vaadin.flow.router.Route; |
|
|
|
import jakarta.annotation.security.PermitAll; |
|
|
@ -26,7 +27,9 @@ import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
import static org.atmosphere.annotation.AnnotationUtil.logger; |
|
|
|
|
|
|
@ -37,14 +40,20 @@ public class CrearnuevoTicketView extends VerticalLayout { |
|
|
|
|
|
|
|
private final RedmineClient api; |
|
|
|
private final UserService userService; |
|
|
|
private MemoryBuffer buffer; |
|
|
|
private MultiFileMemoryBuffer buffer; |
|
|
|
private Upload uploadFile; |
|
|
|
private String fileUploadToken; |
|
|
|
private String selectedTrackerId; |
|
|
|
|
|
|
|
public CrearnuevoTicketView(RedmineClient api, UserService service) { |
|
|
|
this.api = api; |
|
|
|
this.userService = service; |
|
|
|
this.buffer = new MultiFileMemoryBuffer(); |
|
|
|
this.uploadFile = new Upload(buffer); |
|
|
|
this.uploadFile.setMaxFiles(5); //Numero maximo de archivos permitido |
|
|
|
this.uploadFile.setDropAllowed(true); |
|
|
|
this.uploadFile.setAcceptedFileTypes("image/jpeg", "image/png", "application/pdf", "image/gif", "image/svg", "text/plain", |
|
|
|
"application/zip", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
|
|
|
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //Tipos de archivos permitidos |
|
|
|
|
|
|
|
RedmineUser user = userService.getRedmineUser(); |
|
|
|
|
|
|
@ -66,16 +75,10 @@ public class CrearnuevoTicketView extends VerticalLayout { |
|
|
|
descripcion.setWidth("1000px"); |
|
|
|
descripcion.setHeight("250px"); |
|
|
|
|
|
|
|
// Campo para adjuntar archivos |
|
|
|
MemoryBuffer buffer = new MemoryBuffer(); |
|
|
|
Upload uploadFile = new Upload(buffer); |
|
|
|
uploadFile.setAcceptedFileTypes("image/jpeg", "image/png", "application/pdf", "image/gif", "image/svg", "text/plain", |
|
|
|
"application/zip", "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
|
|
|
"application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
|
|
|
|
|
|
// Listener para manejar el evento de subida exitosa de archivos |
|
|
|
uploadFile.addSucceededListener(event -> { |
|
|
|
String fileName = buffer.getFileName(); |
|
|
|
InputStream inputStream = buffer.getInputStream(); |
|
|
|
String fileName = event.getFileName(); |
|
|
|
InputStream inputStream = buffer.getInputStream(fileName); |
|
|
|
|
|
|
|
if (fileName != null && inputStream != null) { |
|
|
|
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
|
|
@ -123,21 +126,33 @@ public class CrearnuevoTicketView extends VerticalLayout { |
|
|
|
issueDetails.put("description", descripcion.getValue()); |
|
|
|
issueDetails.put("tracker_id", selectedTrackerId); // Añadir el tracker_id |
|
|
|
|
|
|
|
String fileUploadToken = null; |
|
|
|
String fileName = null; |
|
|
|
String contentType = null; |
|
|
|
|
|
|
|
if (buffer.getFileData() != null) { |
|
|
|
try { |
|
|
|
byte[] fileContent = buffer.getInputStream().readAllBytes(); |
|
|
|
fileName = buffer.getFileName(); |
|
|
|
contentType = buffer.getFileData().getMimeType(); |
|
|
|
fileUploadToken = api.uploadFile(fileContent, fileName); |
|
|
|
} catch (IOException e) { |
|
|
|
logger.error("Error al cargar el archivo: " + e.getMessage()); |
|
|
|
} |
|
|
|
// Process uploaded files |
|
|
|
List<Map<String, String>> uploads = buffer.getFiles().stream() |
|
|
|
.map(fileName -> { |
|
|
|
Map<String, String> fileDetails = new HashMap<>(); |
|
|
|
try { |
|
|
|
byte[] fileContent = buffer.getInputStream(fileName).readAllBytes(); |
|
|
|
String fileToken = api.uploadFile(fileContent, fileName); |
|
|
|
fileDetails.put("token", fileToken); |
|
|
|
fileDetails.put("filename", fileName); |
|
|
|
fileDetails.put("content_type", buffer.getFileData(fileName).getMimeType()); |
|
|
|
} catch (IOException e) { |
|
|
|
logger.error("Error al cargar el archivo: " + e.getMessage()); |
|
|
|
} |
|
|
|
return fileDetails; |
|
|
|
}) |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
JsonArray uploadsJsonArray = new JsonArray(); |
|
|
|
for (Map<String, String> upload : uploads) { |
|
|
|
JsonObject uploadJson = new JsonObject(); |
|
|
|
uploadJson.addProperty("token", upload.get("token")); |
|
|
|
uploadJson.addProperty("filename", upload.get("filename")); |
|
|
|
uploadJson.addProperty("content_type", upload.get("content_type")); |
|
|
|
uploadsJsonArray.add(uploadJson); |
|
|
|
} |
|
|
|
String response = api.createIssue(issueDetails, fileUploadToken, fileName, contentType, user.getKey()); |
|
|
|
|
|
|
|
String response = api.createIssue(issueDetails, uploadsJsonArray, user.getKey()); |
|
|
|
|
|
|
|
handleResponse(response, asunto, descripcion, tipoTickets); |
|
|
|
}); |
|
|
@ -172,7 +187,7 @@ public class CrearnuevoTicketView extends VerticalLayout { |
|
|
|
asunto.clear(); |
|
|
|
descripcion.clear(); |
|
|
|
tipoTickets.clear(); |
|
|
|
buffer = new MemoryBuffer(); |
|
|
|
buffer = new MultiFileMemoryBuffer(); |
|
|
|
uploadFile.setReceiver(buffer); |
|
|
|
uploadFile.clearFileList(); |
|
|
|
} |
|
|
|