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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| import static com.theleme.importCCAM.importCCAM.log;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.stream.Stream;
import static javax.swing.Action.ACCELERATOR_KEY;
import static javax.swing.Action.SHORT_DESCRIPTION;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JTextPane;
import javax.swing.filechooser.*;
import org.apache.log4j.LogManager;
public class MainFrame extends JFrame {
public JFileChooser boite = new JFileChooser(".");
public File fichierNX = null;
static int cpt = 0;
private JMenuBar menu = new JMenuBar();
private JMenu menuFichier = new JMenu("Fichier");
private Actions actionOuvrir = new Actions("Ouvrir", 'O', "Ouvrir le fichier texte");
private Actions actionImporter = new Actions("Importer", 'I', "Importer le fichier de la CCAM");
private Actions actionRestaurer = new Actions("Restaurer", 'R', "Restaurer le fichier sauvegarder");
private Actions actionCopier = new Actions("Copier", 'C', "Copie le texte selectionné");
private Actions actionVider = new Actions("Vider", 'V', "Vide les logs");
private Actions actionQuitter = new Actions("Quitter", 'W', "quitte l'application");
private JMenu menuEdition = new JMenu("Edition");
JTextPane logs = new JTextPane();
Charset charset = Charset.forName("ISO-8859-1");
OracleJDBC oracleJDBC = new OracleJDBC();
public MainFrame() throws SQLException {
super("Import CCAM 0.1");
logs.setEditable(false);
setJMenuBar(menu);
menu.add(menuFichier);
menuFichier.setMnemonic('F');
menu.add(menuEdition);
menuEdition.setMnemonic('E');
menuFichier.add(actionOuvrir);
menuFichier.add(actionImporter);
menuFichier.add(actionRestaurer);
menuFichier.addSeparator();
menuFichier.add(actionQuitter).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent fin) {
System.exit(0);
}
});
menuEdition.add(actionCopier).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
logs.copy();
}
});
menuEdition.add(actionVider);/*.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
logs.clean();}}); */
add(new JScrollPane(logs));
setSize(1000, 700);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
TextPaneAppender appender = new TextPaneAppender();
appender.setTextPane(logs);
LogManager.getRootLogger().addAppender(appender);
}
public class FiltreSimple extends FileFilter {
private String description;
private String extension;
public FiltreSimple(String description, String extension) {
if (description == null || extension == null) {
throw new NullPointerException("La description (ou extension) ne peut être null.");
}
this.description = description;
this.extension = extension;
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
String nomFichier = file.getName().toLowerCase();
return nomFichier.endsWith(extension);
}
public String getDescription() {
return description;
}
}
private class Actions extends AbstractAction {
private String methode;
public Actions(String libellé, char raccourci, String description) {
super(libellé, new ImageIcon(libellé.toLowerCase() + ".gif"));
putValue(SHORT_DESCRIPTION, description);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control " + raccourci));
methode = libellé.toLowerCase();
}
public void actionPerformed(ActionEvent e) {
try {
this.getClass().getDeclaredMethod(methode).invoke(this);
} catch (Exception ex) {
MainFrame.this.setTitle("probleme");
System.out.println(ex.getMessage());
}
}
private void ouvrir() throws IOException {
try {
ArrayList<String> tables = new ArrayList<String>();
tables.add("Ligne de fin");
FileFilter texte = new FiltreSimple("Fichiers Texte", ".txt");
FileFilter compressé = new FiltreSimple("Fichiers compressés", ".zip");
boite.addChoosableFileFilter(texte);
boite.addChoosableFileFilter(compressé);
boite.setAcceptAllFileFilterUsed(false);
if (boite.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
fichierNX = boite.getSelectedFile();
MainFrame.this.setTitle(fichierNX.getName() + " - Import CCAM 1.0 ");
String test = fichierNX.getAbsolutePath();
String line = "";
try (Stream<String> lines = Files.lines(Paths.get(fichierNX.getAbsolutePath()), charset)) {
line = lines.skip(0).findFirst().get();
try {
log.info("Version de la CCAM : " + line.substring(95, 97) + ":" + line.substring(97, 99));
if (line.isEmpty()) {
log.error("Version CCAM inconnue");
}
} catch (Exception e) {
log.error("impossible de " + e.getMessage());
}
try {
log.info("Date de la CCAM : " + line.substring(52, 56) + "/" + line.substring(56, 58) + "/" + line.substring(58, 60));
if (line.isEmpty()) {
log.error("Date de la CCAM inconnue");
}
} catch (Exception e) {
log.error("Impossible de " + e.getMessage());
}
}
try (Stream<String> lines = Files.lines(Paths.get(fichierNX.getAbsolutePath()), charset)) {
line = lines.findFirst().get();
tables.stream()
.filter(Line -> Line.startsWith("999"))
.forEach(Line -> log.info(Line));
try {
log.info("Vérification du fichier CCAM terminé ");
} catch (Exception e) {
log.error("Echec de la verification " + e.getMessage());
}
}
}
} catch (Exception ex) {
log.error("Echec de l'ouverture " + ex.toString());
}
}
private void Importer() throws SQLException, ClassNotFoundException {
try (Stream<String> lines = Files.lines(Paths.get(fichierNX.getAbsolutePath()), charset)) {
oracleJDBC.driver();
oracleJDBC.open();
lines.forEach(s -> {
try {
oracleJDBC.insertRow(s);
} catch (SQLException ex) {
log.error("Echec de l'import de la commande SQL " + ex.getMessage());
} catch (ParseException ex) {
log.error("Echec du parsage " + ex.getMessage());
}
});
} catch (IOException ex) {
log.error("Echec de l'import " + ex.getMessage());
}
}
}
} |
Partager