Source code: list/ListTitles.java
1 package list;
2
3 import datas.Item;
4 import datas.Loan;
5 import datas.Reservation;
6 import datas.Title;
7
8 import java.io.BufferedReader;
9 import java.io.File;
10 import java.io.FileOutputStream;
11 import java.io.FileReader;
12 import java.io.IOException;
13 import java.util.Enumeration;
14 import java.util.NoSuchElementException;
15 import java.util.Vector;
16
17 /**
18 * Class ListTitles
19 */
20 public class ListTitles {
21
22 private final String FICHIER = "listTitles.data";
23 private static ListTitles myListTitles;
24 private Vector theTitles;
25
26 /**
27 * Create the list
28 * @see Object#Object()
29 */
30 private ListTitles() {
31 theTitles = new Vector();
32 }
33
34 /**
35 * Method getHandle return the unique object listTitles.
36 * @return ListTitles the unique ListTitles object
37 */
38 public static ListTitles getHandle() {
39 if (myListTitles == null) {
40 myListTitles = new ListTitles();
41 }
42 return myListTitles;
43 }
44
45 /**
46 * Gets the vector of all the titles.
47 * @return Returns a Vector of all the title
48 */
49 public Vector getTheTitles() {
50 return theTitles;
51 }
52
53 /**
54 * Method findTheTitle with his name or his author or his isbn<br>.
55 * usage : <br><li>
56 * <ol>to find with his name : findTheTitle(name,null,null)
57 * <ol>to find with his author : findTheTitle(null,author,null)
58 * <ol>to find with his isbn : findTheTitle(null,null,isbn)
59 * </li>
60 * @param nameTitle the name of the title
61 * @param author the name of the author
62 * @param isbn the isbn of the title
63 * @return Title the title found or null if no title is found
64 *
65 * @pre (nameTitle!=null)||(author!=null)||(isbn!=null) //One of the argument must not be null
66 */
67 public Title findTheTitle(String nameTitle, String author, String isbn) {
68 Title ret = null;
69 Title temp;
70 //Recherche suivant le nom
71 if (nameTitle != null) {
72 //On récupère la liste des titles
73 Enumeration elements = theTitles.elements();
74 while (elements.hasMoreElements()) {
75 temp = (Title) elements.nextElement();
76 //On regarde si il correspond au nom fourni
77 if (temp.getName().equalsIgnoreCase(nameTitle)) {
78 //Si oui on le renvoie
79 ret = temp;
80 }
81 }
82 //Recherche selon l'auteur
83 } else if (author != null) {
84 Enumeration elements = theTitles.elements();
85 while (elements.hasMoreElements()) {
86 temp = (Title) elements.nextElement();
87 if (temp.getAuthor().equalsIgnoreCase(author)) {
88 ret = temp;
89 }
90 }
91 //Recherche selon l'isbn
92 } else if (isbn != null) {
93 Enumeration elements = theTitles.elements();
94 while (elements.hasMoreElements()) {
95 temp = (Title) elements.nextElement();
96 if (temp.getIsbn().equalsIgnoreCase(isbn)) {
97 ret = temp;
98 }
99 }
100 }
101 return ret;
102 }
103
104 /**
105 * Method addNewTitle add a new title to the list.
106 * @param theTitle the title to add
107 * @return boolean true if the title have been added
108 *
109 * @pre theTitle!=null //The title must be set
110 */
111 public boolean addNewTitle(Title theTitle) {
112 boolean ret = false;
113 //On vérifie que le titre n'existe pas déjà
114 if (theTitles.indexOf(theTitle) == -1) {
115 //On l'ajoute au vector
116 ret = theTitles.add(theTitle);
117 }
118 return ret;
119 }
120
121 /**
122 * Method deleteTitle delete the title.
123 * @param theTitle the title that need to be deleted
124 * @return boolean true if the title have been deleted
125 */
126 public boolean deleteTitle(Title theTitle) {
127 boolean ret = false;
128 boolean itemRemoved = true;
129 boolean reservationRemoved = true;
130 Reservation reservation;
131
132 //Si il y a des item associé au title
133 if (ListItems.getHandle().findTheItemCopyOfTitle(theTitle).size() != 0) {
134 //Récupère tous les items associé au title
135 Enumeration enumeration =
136 ListItems.getHandle().findTheItemCopyOfTitle(theTitle).elements();
137 //Tant qu'il y a encore des item et que les précédents ont bien été effacé
138 while (enumeration.hasMoreElements() && itemRemoved) {
139 Item element = (Item) enumeration.nextElement();
140 //Vérifie que l'item à bien pu être effacé
141 itemRemoved = ListItems.getHandle().deleteItem(element);
142 }
143 }
144
145 //Si tous les item correspondant au title ont bien été effacé
146 if (itemRemoved) {
147 //Recherche la réservation associée au title
148 reservation = ListReservations.getHandle().findTheReservation(theTitle);
149 //Si elle existe
150 if (reservation != null) {
151 //On essaye de l'effacer
152 reservationRemoved =
153 ListReservations.getHandle().deleteTheReservation(reservation);
154 }
155 //Si il n'y a plus ni réservation ni item lié au title
156 if (reservationRemoved) {
157 //On l'efface de la liste
158 ret = theTitles.remove(theTitle);
159 }
160 }
161 return ret;
162 }
163
164 /**
165 * Method updateTitle update the title.
166 * @param oldName old name of the title
167 * @param newName new name of the title
168 * @param author new author of the title
169 * @param isbn new isbn of the title
170 * @param book true if it was a book
171 * @return boolean true if the title have been updated
172 *
173 * @pre ((oldName != null) && (oldName.length() != 0)) // oldName must be set
174 * @pre ((newName != null) && (newName.length() != 0)) // newName must be set
175 * @pre ((author != null) && (author.length() != 0)) // author must be set
176 * @pre ((isbn != null) && (isbn.length() != 0)) // isbn must be set
177 */
178 public boolean updateTitle(
179 String oldName,
180 String newName,
181 String author,
182 String isbn,
183 boolean book) {
184
185 boolean ret = true;
186 //On recherche le title
187 Title title = this.findTheTitle(oldName,null,null);
188
189 //Si on l'a trouvé
190 if (title != null) {
191 //On stocke le vieux title
192 Title oldTitle = title;
193 //On met à jour le title
194 title.setAuthor(author);
195 title.setName(newName);
196 title.setIsbn(isbn);
197 title.setBook(book);
198
199 //On met à jour toutes les réservations correspondant au title
200 ListReservations listReservations = ListReservations.getHandle();
201 Reservation reservation = listReservations.findTheReservation(oldTitle);
202 if (reservation != null) {
203 ret = ret || reservation.setTitle(title);
204 }
205
206 //On met à jour tous les loan correspondant au title
207 if (!ListItems.getHandle().findTheItemCopyOfTitle(oldTitle).isEmpty()) {
208 Enumeration enumeration =
209 ListItems.getHandle().findTheItemCopyOfTitle(oldTitle).elements();
210 while (enumeration.hasMoreElements()) {
211 Item item = (Item) enumeration.nextElement();
212 ListLoans listLoans = ListLoans.getHandle();
213 Loan loan = listLoans.findTheLoan(item);
214 item.setTitle(title);
215 if (loan != null) {
216 ret = ret || loan.setItem(item);
217 }
218 }
219 }
220
221 //On met à jour tous les item correspondant au title
222 if (!ListItems.getHandle().findTheItemCopyOfTitle(oldTitle).isEmpty()) {
223 Enumeration enumeration =
224 ListItems.getHandle().findTheItemCopyOfTitle(oldTitle).elements();
225 while (enumeration.hasMoreElements()) {
226 Item item = (Item) enumeration.nextElement();
227 ret = ret || item.setTitle(title);
228 }
229 }
230 }
231 return ret;
232 }
233
234 /**
235 * Method readListFromDisk read the data of the listTitles from the disk.
236 * @return boolean true if the data have been read
237 */
238 public boolean readListFromDisk() {
239 BufferedReader in = null;
240 //Vide le vecteur pour éviter la duplication des informations
241 this.theTitles = new Vector();
242 //On récupère le répertoire courant de l'utilisateur
243 String filePath = System.getProperty("user.dir");
244
245 boolean ret = true;
246 try {
247 String line;
248 //On ouvre le fichier en lecture
249 in = new BufferedReader(new FileReader(filePath + "/" + FICHIER));
250 while ((line = in.readLine()) != null) {
251 //Pour chaque ligne on ajoute le title correspondant
252 this.addNewTitle(new Title(line));
253 }
254 } catch (IOException e) {
255 ret = false;
256 } catch (NoSuchElementException e) {
257 ret = false;
258 } finally {
259 try {
260 if (in != null) {
261 //On ferme le fichier
262 in.close();
263 }
264 } catch (IOException e) {
265 ret = false;
266 }
267 }
268 return ret;
269 }
270
271
272 /**
273 * Method writeListToDisk write the data of the listTitles to the disk.
274 * @return boolean true if and only if the data have been write
275 */
276 public boolean writeListToDisk() {
277 boolean ret = true;
278 int theTitlesLenght = this.theTitles.size();
279 FileOutputStream out = null;
280 //On récupère le répertoire courant de l'utilisateur
281 String filePath = System.getProperty("user.dir");
282
283 try {
284 //On ouvre le fichier en écriture
285 out = new FileOutputStream(new File(filePath + "/" + FICHIER));
286 for (int i = 0; i < theTitlesLenght; i++) {
287 //On passe par un string buffer pour des raisons de performance
288 StringBuffer sb = new StringBuffer();
289 //On écrit le titre correspondant
290 sb.append(((Title) this.theTitles.get(i)).toString());
291 sb.append('\n');
292 //On écrit les données
293 out.write(sb.toString().getBytes());
294 }
295 //On vide le tampon
296 out.flush();
297 } catch (IOException e) {
298 //Si quelque chose ne se passe pas bien on l'affiche
299 System.out.println(e.getLocalizedMessage());
300 ret = false;
301 } finally {
302 try {
303 if (out != null) {
304 //On ferme le fichier
305 out.close();
306 }
307 } catch (IOException e) {
308 ret = false;
309 }
310 }
311 return ret;
312 }
313 }