2023-04-01 09:54:28 +02:00
package baseEvaluations ;
2024-04-08 10:29:34 +02:00
import java.io.BufferedReader ;
2024-04-08 20:46:40 +02:00
import java.io.BufferedWriter ;
2024-04-08 10:29:34 +02:00
import java.io.File ;
import java.io.FileInputStream ;
2023-04-01 09:54:28 +02:00
import java.io.IOException ;
2024-04-08 10:29:34 +02:00
import java.io.InputStreamReader ;
2024-04-08 20:46:40 +02:00
import java.nio.charset.StandardCharsets ;
import java.nio.file.Files ;
import java.nio.file.Path ;
2023-04-01 09:54:28 +02:00
import java.nio.file.Paths ;
2024-04-08 10:29:34 +02:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2023-04-01 09:54:28 +02:00
2024-04-10 12:29:17 +02:00
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JOptionPane ;
2023-04-01 09:54:28 +02:00
import MEPTL.commandes ;
import cXML.Run ;
2024-04-08 20:46:40 +02:00
import cXML.node ;
2023-04-01 09:54:28 +02:00
2024-04-10 12:29:17 +02:00
/ * *
* Ecriture d ' une évaluation .
* @author pabr6
*
* /
2023-04-01 09:54:28 +02:00
public class ecritureBaseEvaluation {
2024-04-10 12:29:17 +02:00
/ * *
* Ecriture d ' une évaluation
* @param evaluation
* /
2024-04-08 20:46:40 +02:00
public ecritureBaseEvaluation ( node evaluation ) {
2023-04-01 09:54:28 +02:00
String directoryName = " " ;
2024-04-08 10:29:34 +02:00
File file = null ;
2023-04-01 09:54:28 +02:00
if ( commandes . PathBaseEvaluationDefaut ) {
directoryName = Paths . get ( " " ) . toAbsolutePath ( ) . toString ( ) ;
} else {
directoryName = commandes . PathBaseEvaluations ;
}
2024-04-08 10:29:34 +02:00
if ( commandes . os . contains ( " Win " ) ) {
file = new File ( directoryName + " \\ " + commandes . NameBaseEvaluations ) ;
} else {
file = new File ( directoryName + " / " + commandes . NameBaseEvaluations ) ;
}
2024-04-09 14:50:03 +02:00
String debut = " " ;
String fin = " " ;
String XMLBase = " " ;
2024-04-08 10:29:34 +02:00
if ( file ! = null ) {
2024-04-09 14:50:03 +02:00
//Lecture de la base de données
2024-04-08 10:29:34 +02:00
BufferedReader br ;
try {
br = new BufferedReader (
new InputStreamReader (
new FileInputStream ( file . getAbsoluteFile ( ) ) , " UTF-8 " ) ) ;
String line ;
StringBuilder targetString = new StringBuilder ( ) ;
while ( ( line = br . readLine ( ) ) ! = null ) {
targetString . append ( line ) ;
}
String baseToString = targetString . toString ( ) . replace ( " \ t " , " " ) . replace ( " \ r " , " " ) . replace ( " \ n " , " " ) . replaceAll ( " >/{1,}< " , " >< " ) ;
2024-04-08 20:46:40 +02:00
Pattern p = Pattern . compile ( " < \\ bevaluation \\ b.{1,500} \\ bdate \\ b= \\ \" " + evaluation . getAttributs ( ) . get ( " date " ) + " \\ \" .*?< \\ / \\ bevaluation \\ b> " ) ;
2024-04-09 14:50:03 +02:00
Matcher m = p . matcher ( baseToString ) ;
2024-04-08 10:29:34 +02:00
2024-04-09 14:50:03 +02:00
2024-04-08 20:46:40 +02:00
2024-04-08 10:29:34 +02:00
if ( m . find ( ) ) {
2024-04-09 14:50:03 +02:00
//Supprime le node évluation pour le remplacer par le nouveau.
2024-04-08 20:46:40 +02:00
debut = baseToString . substring ( 0 , m . start ( ) ) ;
fin = baseToString . substring ( m . end ( ) ) ;
String codeXMLEvaluation = Run . ecritureNode ( evaluation , 0 ) . toString ( ) . replace ( " \ t " , " " ) . replace ( " \ r " , " " ) . replace ( " \ n " , " " ) . replaceAll ( " >/{1,}< " , " >< " ) ;
XMLBase = debut + codeXMLEvaluation + fin ;
2024-04-09 14:50:03 +02:00
} else {
//Ajoute une nouvelle évaluation au début de la base
p = Pattern . compile ( " <evaluations.*?> " ) ;
m = p . matcher ( baseToString ) ;
if ( m . find ( ) ) {
debut = baseToString . substring ( 0 , m . end ( ) ) ;
fin = baseToString . substring ( m . end ( ) ) ;
String codeXMLEvaluation = Run . ecritureNode ( evaluation , 0 ) . toString ( ) . replace ( " \ t " , " " ) . replace ( " \ r " , " " ) . replace ( " \ n " , " " ) . replaceAll ( " >/{1,}< " , " >< " ) ;
XMLBase = debut + codeXMLEvaluation + fin ;
}
2024-04-08 10:29:34 +02:00
}
2024-04-08 20:46:40 +02:00
try {
// écriture du node
2024-04-10 12:29:17 +02:00
Path outputFilePath = file . toPath ( ) ;
2024-04-08 20:46:40 +02:00
BufferedWriter fichier = Files . newBufferedWriter ( outputFilePath , StandardCharsets . UTF_8 ) ;
fichier . write ( XMLBase ) ;
2024-04-10 12:29:17 +02:00
fichier . close ( ) ;
JFrame frame = new JFrame ( ) ;
JLabel texte = new JLabel ( " <html><h1>Enregistrement réussi</h1><p>L'évaluation <b> " + evaluation . getAttributs ( ) . get ( " name " ) + " </b> a été enregistrée dans la base de données.</p> "
+ " <p> " + file . getAbsolutePath ( ) + " </p></html> " ) ;
JOptionPane . showMessageDialog ( frame , texte ) ;
2024-04-08 20:46:40 +02:00
} catch ( IOException e ) {
// TODO Auto-generated catch block
e . printStackTrace ( ) ;
}
2024-04-08 10:29:34 +02:00
} catch ( Exception e ) {
}
2023-04-01 09:54:28 +02:00
}
2024-04-08 10:29:34 +02:00
// try {
// Run.ecritureNodeEnXML(commandes.evaluationsBase, commandes.NameBaseEvaluations, directoryName, Run.TypeFile.Evaluation);
// } catch (IOException e) {
// e.printStackTrace();
// }
2023-04-01 09:54:28 +02:00
}
}