1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
package nf;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import ui.BoiteDialogUI;
import ui.FenetreUI;
public class SauvegardeFichiers {
private File directory;
private String code;
private String annee;
private String increment;
private FenetreUI fenetre;
public SauvegardeFichiers(File directory, String code, String annee, String increment) {
this.directory = directory;
this.code = code;
this.annee = annee;
this.increment = increment;
}
public void sauvegarde(File qc14File, File rapportFile, File annexeFile) {
try {
if(code.isEmpty()){
String message ="Vous devez insérer le code projet.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);}
if (annee.isEmpty()){
String message ="Vous devez insérer l'année.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);
}
if (increment.isEmpty()){
String message ="Vous devez insérer l'increment.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);
}
if(qc14File.exists()){
String message ="Vous devez insérer une demande.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);
}
if(!rapportFile.exists()){
String message ="Vous devez insérer un rapport.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);
}
if(!annexeFile.exists()){
String message ="Vous devez insérer un annexe.";
BoiteDialogUI.afficherErreur(fenetre, "Erreur", message);
}
String rapportFileEx = "";
String baseDir = directory.toPath().toString() + File.separator + code + "_" + annee + "_" + increment;
int idxRapportFileEx = rapportFile.getName().indexOf('.');
if(idxRapportFileEx != -1) {
rapportFileEx = "." + rapportFile.getName().substring(idxRapportFileEx+1);
}
String newRapportPath = baseDir + File.separator + code + "_" + annee + "_" + increment + rapportFileEx;
File newRapportFile = new File(newRapportPath);
newRapportFile.mkdirs();
Files.copy(rapportFile.toPath(), newRapportFile.toPath(), StandardCopyOption.REPLACE_EXISTING).toFile();
String qc14FileExtension = "";
int idxQc14Ext = qc14File.getName().indexOf('.');
if(idxQc14Ext != -1) {
qc14FileExtension = "." + qc14File.getName().substring(idxQc14Ext+1);
}
String newQc14Path = baseDir + File.separator + "QC14_" + code + "_" + annee + "_" + increment + qc14FileExtension;
File newQc14File = new File(newQc14Path);
newQc14File.mkdirs();
Files.copy(qc14File.toPath(), newQc14File.toPath(), StandardCopyOption.REPLACE_EXISTING).toFile();
String newAnnexePath = baseDir + File.separator + "Annexes";
File newAnnexeFile = new File(newAnnexePath);
newAnnexeFile.mkdirs();
Files.copy(annexeFile.toPath(), newAnnexeFile.toPath(), StandardCopyOption.REPLACE_EXISTING).toFile();
File source = new File(annexeFile.getAbsolutePath());
File dest = new File(newAnnexePath);
try {
newAnnexeFile.copyDirectory(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
String message ="Sauvegarde effectue.";
BoiteDialogUI.afficherInformation(fenetre, "Sauvegarde", message);
}
/**for (int i=0; i<annexeFile.length(); i++) {
Path dest = newAnnexeFile.resolve(newAnnexeFile.length([i]).getFileName());
//this.code.replace(this.code,"");
//this.annee.replace(this.annee,"");
//this.increment.replace(this.increment,"");
}**/
catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager