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
| public class ControllerAdmin extends Component implements ActionListener {
private Vue_administrateur vue_admin;
private Reservation vue_resa;
public ControllerAdmin(Vue_administrateur vue_admin) {
this.vue_admin = vue_admin;
}
public ControllerAdmin(Reservation vue_resa) {
this.vue_resa = vue_resa;
}
/**
* @param event administrateur controller
* différents events quand on avance de page en page lors de la création d'un film, d'une salle ou de l'affichage de la iste des users inscrits
*/
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
// Controller pour la création d'une salle en XML
if (source == vue_admin.ajout_salle) {
String salle = vue_admin.salle.getText();
Integer row = (Integer) vue_admin.row.getValue();
Integer col = (Integer) vue_admin.col.getValue();
Integer handi = (Integer) vue_admin.handi.getValue();
Integer totalPlaces;
// calcul nb total de places
totalPlaces = row * col;
try {
int i;
i = Integer.decode(salle);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Le numéro de salle doit être un chiffre compris entre 1 et 3", "Attention", JOptionPane.INFORMATION_MESSAGE);
}
// On caste le jtextfield en integer pour vérifier SI ce n'est pas compris entre 1 et 3, SINON on exécute le parse xml
int salleInt = Integer.parseInt(salle);
if (salleInt > 3) {
JOptionPane.showMessageDialog(this, "Il n'y a que 3 salles dans ce cinéma. Si vous voulez reconstruire une salle, choisissez 1, 2 ou 3 et nos architectes se mettront au boulot !", "Attention", JOptionPane.INFORMATION_MESSAGE);
} else if (salleInt < 1) {
JOptionPane.showMessageDialog(this, "Il n'y a que 3 salles dans ce cinéma. Si vous voulez reconstruire une salle, choisissez 1, 2 ou 3 et nos architectes se mettront au boulot !", "Attention", JOptionPane.INFORMATION_MESSAGE);
} else {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document xml = builder.newDocument();
Element root = xml.createElement("salle");
Element trunk1 = xml.createElement("numSalle");
Element trunk2 = xml.createElement("nbCol");
Element trunk3 = xml.createElement("nbRow");
Element trunk4 = xml.createElement("nbHandi");
root.appendChild(trunk1);
root.appendChild(trunk2);
root.appendChild(trunk3);
root.appendChild(trunk4);
trunk1.setTextContent(salle);
trunk2.setTextContent(String.valueOf(col));
trunk3.setTextContent(String.valueOf(row));
trunk4.setTextContent(String.valueOf(handi));
// On crée un fichier xml correspondant au résultat
// construire la transformation inactive
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
String resultFile = "salles/Salle " + salle + ".xml";
StreamResult XML = new StreamResult(resultFile);
t.transform(new DOMSource(root), XML);
System.out.println("Le fichier " + resultFile + ".xml a été créé.");
String salle_nom = "Salle " + salle;
String querySalle = "UPDATE salle SET nb_place = '" + totalPlaces + "' WHERE salle_nom = '" + salle_nom + "'";
String querySalleHandi = "UPDATE salle SET nb_place_handi = '" + handi + "' WHERE salle_nom = '" + salle_nom + "'";
try {
PreparedStatement state = BD_Access.getInstance().prepareStatement(querySalle);
// java.sql.Statement state = BD_Access.getInstance().createStatement();
state.executeUpdate();
state.close();
PreparedStatement stateBis = BD_Access.getInstance().prepareStatement(querySalleHandi);
// java.sql.Statement stateBis = BD_Access.getInstance().createStatement();
stateBis.executeUpdate();
stateBis.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
vue_admin.setEnd("Votre salle a bien été ajoutée.");
vue_admin.endingCreate();
}
if (source == vue_admin.ajout_film) {
String film = vue_admin.textFilm.getText();
if (film == null || film.length() == 0) {
JOptionPane.showMessageDialog(null,
"Vous devez rentrer un titre de film", "Champs vide",
JOptionPane.ERROR_MESSAGE);
} else {
String apiKey = "c5f1c364694d8b4dcee999aee55a";
film = (film.replace(' ', '+'));
System.out.println("film choisi : " + film);
String url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&language=fr&®ion=FR&query=" + film;
/* On rempli le json à l'aide d'input stream*/
InputStream input = null;
OutputStream output = null;
try {
input = new URL(url).openStream();
output = new FileOutputStream("salles/films.json");
byte[] buffer = new byte[1024];
for (int length; (length = input.read(buffer)) > 0; ) {
output.write(buffer, 0, length);
}
System.out.println("Fichier rempli : output.json");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null)
try {
output.close();
} catch (IOException logOrIgnore) {
}
if (input != null)
try {
input.close();
} catch (IOException logOrIgnore) {
}
}
try {
JSONParser parser = new JSONParser();
Object object = parser.parse("salles/films.json");
// convert Object to JSONObject
JSONObject jsonObject = (JSONObject) object;
// Reading the array
JSONArray results = (JSONArray) jsonObject.get("results");
// on affiche la nouvelle page
vue_admin.choseFilm(results);
} catch (Exception e) {
e.printStackTrace();
}
}
} |
Partager