rajah 1 рік тому
батько
коміт
c1aac933a4

+ 79 - 4
src/main/java/fr/triplea/demovote/persistence/dao/ProductionRepository.java

@@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.NativeQuery;
 import org.springframework.data.repository.query.Param;
 
+import fr.triplea.demovote.persistence.dto.ProductionFile;
 import fr.triplea.demovote.persistence.dto.ProductionShort;
 import fr.triplea.demovote.persistence.model.Participant;
 import fr.triplea.demovote.persistence.model.Production;
@@ -16,12 +17,86 @@ public interface ProductionRepository extends JpaRepository<Production, Integer>
   @NativeQuery("SELECT DISTINCT p.* FROM vote.productions AS p WHERE p.numero_production = :id AND p.flag_actif IS TRUE ")
   Production findById(@Param("id") int id);
   
-  @NativeQuery("SELECT DISTINCT TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, p.numero_production, CAST(p.adresse_ip AS VARCHAR) AS adresse_ip, p.type, p.titre, p.auteurs, p.groupes, p.plateforme, p.commentaire, p.informations_privees, p.numero_participant, CONCAT(g.pseudonyme, ' = ', g.nom, ' ', g.prenom) AS nom_gestionnaire, p.nom_archive, p.vignette, p.numero_version FROM vote.productions AS p INNER JOIN vote.participants AS g ON p.numero_participant = g.numero_participant WHERE p.flag_actif IS TRUE ORDER BY p.titre ASC ")
-  List<ProductionShort> findAllEnabled();
+  @NativeQuery("SELECT DISTINCT " 
+              + "TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
+              + "TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, "
+              + "p.numero_production, "
+              + "CAST(p.adresse_ip AS VARCHAR) AS adresse_ip, "
+              + "p.type, "
+              + "p.titre, "
+              + "p.auteurs, "
+              + "p.groupes, "
+              + "p.plateforme, "
+              + "p.commentaire, "
+              + "p.informations_privees, "
+              + "p.numero_participant AS numero_gestionnaire, "
+              + "CONCAT(g.pseudonyme, ' = ', g.nom, ' ', g.prenom) AS nom_gestionnaire, "
+              + "p.nom_archive, "
+              + "p.vignette, "
+              + "p.numero_version "
+              + "FROM vote.productions AS p "
+              + "INNER JOIN vote.participants AS g ON p.numero_participant = g.numero_participant "
+              + "WHERE p.flag_actif IS TRUE ORDER BY p.titre ASC ")
+  List<ProductionShort> findAllWithoutArchive();
   
-  @NativeQuery("SELECT DISTINCT p.date_creation, p.date_modification, p.numero_production, p.adresse_ip, p.type, p.titre, p.auteurs, p.groupes, p.plateforme, p.commentaire, p.informations_privees, p.numero_participant, p.nom_archive, p.vignette, p.numero_version FROM vote.productions AS p WHERE p.numero_participant = :participant AND p.flag_actif IS TRUE ")
-  List<ProductionShort> findByParticipant(@Param("participant") Participant participant);
+  @NativeQuery("SELECT DISTINCT " 
+              + "TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
+              + "TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, "
+              + "p.numero_production, "
+              + "CAST(p.adresse_ip AS VARCHAR) AS adresse_ip, "
+              + "p.type, "
+              + "p.titre, "
+              + "p.auteurs, "
+              + "p.groupes, "
+              + "p.plateforme, "
+              + "p.commentaire, "
+              + "p.informations_privees, "
+              + "p.numero_participant AS numero_gestionnaire, "
+              + "CONCAT(g.pseudonyme, ' = ', g.nom, ' ', g.prenom) AS nom_gestionnaire, "
+              + "p.nom_archive, "
+              + "p.vignette, "
+              + "p.numero_version "
+              + "FROM vote.productions AS p "
+              + "INNER JOIN vote.participants AS g ON p.numero_participant = g.numero_participant "
+              + "WHERE p.numero_production = :numeroProduction AND p.flag_actif IS TRUE ")
+  ProductionShort findByIdWithoutArchive(@Param("numeroProduction") Integer numeroProduction);
 
+  @NativeQuery("SELECT DISTINCT " 
+      + "p.numero_production, "
+      + "p.titre, "
+      + "p.nom_archive, "
+      + "'' AS archive "
+      + "FROM vote.productions AS p "
+      + "WHERE p.numero_production = :numeroProduction AND p.flag_actif IS TRUE ")
+  ProductionFile findByIdForUpload(@Param("numeroProduction") Integer numeroProduction);
+
+  
+  
+  
+  
+  @NativeQuery("SELECT DISTINCT " 
+      + "TO_CHAR(p.date_creation, 'DD/MM/YYYY HH24:MI:SS') as date_creation, "
+      + "TO_CHAR(p.date_modification, 'DD/MM/YYYY HH24:MI:SS') as date_modification, "
+      + "p.numero_production, "
+      + "CAST(p.adresse_ip AS VARCHAR) AS adresse_ip, "
+      + "p.type, "
+      + "p.titre, "
+      + "p.auteurs, "
+      + "p.groupes, "
+      + "p.plateforme, "
+      + "p.commentaire, "
+      + "p.informations_privees, "
+      + "p.numero_participant AS numero_gestionnaire, "
+      + "CONCAT(g.pseudonyme, ' = ', g.nom, ' ', g.prenom) AS nom_gestionnaire, "
+      + "p.nom_archive, "
+      + "p.vignette, "
+      + "p.numero_version "
+      + "FROM vote.productions AS p "
+      + "INNER JOIN vote.participants AS g ON p.numero_participant = g.numero_participant "
+      + "WHERE p.numero_participant = :participant AND p.flag_actif IS TRUE ORDER BY p.titre ASC ")
+  List<ProductionShort> findByParticipantWithoutArchive(@Param("participant") Participant participant);
+  
+  
   @Override
   void delete(Production production);
   

+ 10 - 0
src/main/java/fr/triplea/demovote/persistence/dto/ProductionFile.java

@@ -0,0 +1,10 @@
+package fr.triplea.demovote.persistence.dto;
+
+public record ProductionFile
+(
+  int numeroProduction,
+  String titre,
+  String nomArchive,
+  String archive
+) 
+{ }

+ 1 - 1
src/main/java/fr/triplea/demovote/persistence/dto/ProductionDTO.java → src/main/java/fr/triplea/demovote/persistence/dto/ProductionTransfer.java

@@ -1,6 +1,6 @@
 package fr.triplea.demovote.persistence.dto;
 
-public record ProductionDTO
+public record ProductionTransfer
 (
   int numeroProduction,
   String type,

+ 22 - 0
src/main/java/fr/triplea/demovote/persistence/dto/ProductionUpdate.java

@@ -0,0 +1,22 @@
+package fr.triplea.demovote.persistence.dto;
+
+public record ProductionUpdate
+(
+  String dateCreation,
+  String dateModification,
+  int numeroProduction,
+  String adresseIP,
+  String type,
+  String titre,
+  String auteurs,
+  String groupes,
+  String plateforme,
+  String commentaire,
+  String informationsPrivees,
+  int numeroGestionnaire,
+  String nomGestionnaire,
+  String nomArchive,
+  String vignette,
+  int numeroVersion
+) 
+{ }

+ 4 - 0
src/main/java/fr/triplea/demovote/persistence/model/Production.java

@@ -215,7 +215,11 @@ public class Production
   
     try { this.archive = Base64.getDecoder().decode(a); } catch(Exception e) { this.archive = null; }
   }
+  @Transient
+  public void setArchive(byte[] a) { this.archive = (a == null) ? null : a.clone(); }
   public String getArchive() { if (this.archive == null) { return ""; } return "data:application/zip;base64," + Base64.getEncoder().encodeToString(this.archive); }
+  @Transient
+  public byte[] getArchiveAsBinary() { return this.archive; }
   
   public void setVignette(String v) 
   { 

+ 134 - 46
src/main/java/fr/triplea/demovote/web/controller/ProductionController.java

@@ -7,6 +7,9 @@ import java.util.List;
 import java.util.Map;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
 import org.springframework.http.ResponseEntity;
 //import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.CrossOrigin;
@@ -18,12 +21,15 @@ import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestController;
 
 import fr.triplea.demovote.persistence.dao.ParticipantRepository;
 import fr.triplea.demovote.persistence.dao.ProductionRepository;
-import fr.triplea.demovote.persistence.dto.ProductionDTO;
+import fr.triplea.demovote.persistence.dto.ProductionTransfer;
+import fr.triplea.demovote.persistence.dto.ProductionFile;
 import fr.triplea.demovote.persistence.dto.ProductionShort;
+import fr.triplea.demovote.persistence.dto.ProductionUpdate;
 import fr.triplea.demovote.persistence.model.Participant;
 import fr.triplea.demovote.persistence.model.Production;
 import fr.triplea.demovote.persistence.model.ProductionType;
@@ -50,19 +56,51 @@ public class ProductionController
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
   public List<Production> getList(@RequestParam(required = false) String type) 
   { 
-    List<ProductionShort> prods = productionRepository.findAllEnabled();
+    List<ProductionShort> prods = productionRepository.findAllWithoutArchive();
+    
     List<Production> ret = new ArrayList<Production>();
     
     for (ProductionShort prod: prods) { ret.add(prod.toProduction()); }
     
     return ret; 
   }
- 
+
+  @GetMapping(value = "/file/{id}")
+  @ResponseBody
+  public ResponseEntity<Resource> getFile(@PathVariable int id) 
+  {
+    Production p = productionRepository.findById(id);
+    
+    if (p != null) 
+    { 
+      Resource r = new ByteArrayResource(p.getArchiveAsBinary());
+      
+      return ResponseEntity
+              .ok()
+              .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + p.getNomArchive() + "\"")
+              .header(HttpHeaders.CONTENT_TYPE, "application/zip")
+              .body(r); 
+    }
+    
+    return ResponseEntity.notFound().build();
+  }
+
   @GetMapping(value = "/form/{id}")
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
   public ResponseEntity<Production> getForm(@PathVariable int id)
   { 
-    Production p = productionRepository.findById(id);
+    ProductionShort p = productionRepository.findByIdWithoutArchive(id);
+    
+    if (p != null) { return ResponseEntity.ok(p.toProduction()); }
+    
+    return ResponseEntity.notFound().build();
+  }
+
+  @GetMapping(value = "/formfile/{id}")
+  //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
+  public ResponseEntity<ProductionFile> getFormFile(@PathVariable int id)
+  { 
+    ProductionFile p = productionRepository.findByIdForUpload(id);
     
     if (p != null) { return ResponseEntity.ok(p); }
     
@@ -71,38 +109,38 @@ public class ProductionController
 
   @PostMapping(value = "/create")
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
-  public ResponseEntity<Map<String, Boolean>> create(@RequestBody(required = true) ProductionDTO prod_dto, HttpServletRequest request) 
+  public ResponseEntity<Map<String, Boolean>> create(@RequestBody(required = true) ProductionTransfer production, HttpServletRequest request) 
   { 
-    Participant participant = participantRepository.findById(prod_dto.numeroParticipant());
+    Participant participant = participantRepository.findById(production.numeroParticipant());
 
     if (participant != null) 
     {
-      Production prod_new = new Production();
+      Production fresh = new Production();
             
-      prod_new.setNumeroProduction(null);
-      prod_new.setAdresseIP(new Inet(request.getRemoteAddr()));
+      fresh.setNumeroProduction(null);
+      fresh.setAdresseIP(new Inet(request.getRemoteAddr()));
       
-      if(prod_dto.type().equals("EXECUTABLE")) { prod_new.setType(ProductionType.EXECUTABLE); }
-      else if(prod_dto.type().equals("GRAPHE")) { prod_new.setType(ProductionType.GRAPHE); }
-      else if(prod_dto.type().equals("MUSIQUE")) { prod_new.setType(ProductionType.MUSIQUE); }
-      else if(prod_dto.type().equals("VIDEO")) { prod_new.setType(ProductionType.VIDEO); }
-      else if(prod_dto.type().equals("TOPIC")) { prod_new.setType(ProductionType.TOPIC); }
-      else { prod_new.setType(ProductionType.AUTRE); }
+      if(production.type().equals("EXECUTABLE")) { fresh.setType(ProductionType.EXECUTABLE); }
+      else if(production.type().equals("GRAPHE")) { fresh.setType(ProductionType.GRAPHE); }
+      else if(production.type().equals("MUSIQUE")) { fresh.setType(ProductionType.MUSIQUE); }
+      else if(production.type().equals("VIDEO")) { fresh.setType(ProductionType.VIDEO); }
+      else if(production.type().equals("TOPIC")) { fresh.setType(ProductionType.TOPIC); }
+      else { fresh.setType(ProductionType.AUTRE); }
         
-      prod_new.setTitre(prod_dto.titre());
-      prod_new.setAuteurs(prod_dto.auteurs());
-      prod_new.setGroupes(prod_dto.groupes());
-      prod_new.setPlateforme(prod_dto.plateforme());
-      prod_new.setCommentaire(prod_dto.commentaire());
-      prod_new.setInformationsPrivees(prod_dto.informationsPrivees());
-
-      prod_new.setParticipant(participant);
-      prod_new.setNomArchive(prod_dto.nomArchive());
-      prod_new.setArchive(prod_dto.archive());
-      prod_new.setVignette(prod_dto.vignette());
-      prod_new.setNumeroVersion(prod_dto.numeroVersion());
+      fresh.setTitre(production.titre());
+      fresh.setAuteurs(production.auteurs());
+      fresh.setGroupes(production.groupes());
+      fresh.setPlateforme(production.plateforme());
+      fresh.setCommentaire(production.commentaire());
+      fresh.setInformationsPrivees(production.informationsPrivees());
+
+      fresh.setParticipant(participant);
+      fresh.setNomArchive(production.nomArchive());
+      fresh.setArchive(production.archive());
+      fresh.setVignette(production.vignette());
+      fresh.setNumeroVersion(production.numeroVersion());
       
-      productionRepository.save(prod_new);
+      productionRepository.save(fresh);
 
       Map<String, Boolean> response = new HashMap<>();
       response.put("created", Boolean.TRUE);
@@ -115,31 +153,81 @@ public class ProductionController
  
   @PutMapping(value = "/update/{id}")
   //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
-  public ResponseEntity<Production> update(@PathVariable int id, @RequestBody(required = true) Production production) 
+  public ResponseEntity<Map<String, Boolean>> update(@PathVariable int id, @RequestBody(required = true) ProductionUpdate production) 
   { 
     Production found = productionRepository.findById(id);
     
     if (found != null)
     {
-      found.setParticipant(production.getParticipant());
-      found.setEnabled(true);
+      Participant participant = participantRepository.findById(production.numeroGestionnaire());
       
-      found.setAdresseIP(new Inet(request.getRemoteAddr()));
-      found.setType(production.getType()); 
-      found.setTitre(production.getTitre());
-      found.setAuteurs(production.getAuteurs());
-      found.setGroupes(production.getGroupes());
-      found.setPlateforme(production.getPlateforme());
-      found.setCommentaire(production.getCommentaire());
-      found.setInformationsPrivees(production.getInformationsPrivees());
-      found.setNomArchive(production.getNomArchive());
-      found.setArchive(production.getArchive());
-      found.setVignette(production.getVignette());
-      found.setNumeroVersion(found.getNumeroVersion() +1);
-      
-      Production updated = productionRepository.save(found);
+      if (participant != null)
+      {
+        found.setParticipant(participant);
+        found.setEnabled(true);
+        
+        found.setAdresseIP(new Inet(request.getRemoteAddr()));
+        
+        if(production.type().equals("EXECUTABLE")) { found.setType(ProductionType.EXECUTABLE); }
+        else if(production.type().equals("GRAPHE")) { found.setType(ProductionType.GRAPHE); }
+        else if(production.type().equals("MUSIQUE")) { found.setType(ProductionType.MUSIQUE); }
+        else if(production.type().equals("VIDEO")) { found.setType(ProductionType.VIDEO); }
+        else if(production.type().equals("TOPIC")) { found.setType(ProductionType.TOPIC); }
+        else { found.setType(ProductionType.AUTRE); }
+       
+        found.setTitre(production.titre());
+        found.setAuteurs(production.auteurs());
+        found.setGroupes(production.groupes());
+        found.setPlateforme(production.plateforme());
+        found.setCommentaire(production.commentaire());
+        found.setInformationsPrivees(production.informationsPrivees());
+    
+        if (production.vignette() != null) { if (!(production.vignette().isBlank())) { found.setVignette(production.vignette()); } }
+        
+        productionRepository.save(found);
+
+        Map<String, Boolean> response = new HashMap<>();
+        response.put("updated", Boolean.TRUE);
+        
+        return ResponseEntity.ok(response); 
+      }
+    }
+    
+    return ResponseEntity.notFound().build();
+  }
+  
+  @PutMapping(value = "/upload/{id}")
+  //@PreAuthorize("hasAnyRole('LISTE_PRODUCTIONS_ADMIN', 'LISTE_PRODUCTIONS_USER')")
+  public ResponseEntity<Map<String, Boolean>> update(@PathVariable int id, @RequestBody(required = true) ProductionFile production) 
+  { 
+    Production found = productionRepository.findById(id);
     
-      return ResponseEntity.ok(updated);
+    if (found != null)
+    {
+      found.setEnabled(true);
+      
+      if (production.archive() != null)
+      {
+        if (!(production.archive().isBlank()))
+        {
+          if (production.nomArchive() != null)
+          {
+            if (!(production.nomArchive().isBlank()))
+            {
+              found.setNomArchive(production.nomArchive());
+              found.setArchive(production.archive());
+              found.setNumeroVersion(found.getNumeroVersion() + 1);
+              
+              productionRepository.save(found);
+
+              Map<String, Boolean> response = new HashMap<>();
+              response.put("updated", Boolean.TRUE);
+              
+              return ResponseEntity.ok(response); 
+            }
+          }
+        }
+      }
     }
     
     return ResponseEntity.notFound().build();