1
2
3
4
5 package org.sourceforge.vlibrary.user.logic;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Locale;
11 import java.util.Map;
12
13 import org.apache.log4j.Logger;
14 import org.sourceforge.vlibrary.Constants;
15 import org.sourceforge.vlibrary.exceptions.LibraryException;
16 import org.sourceforge.vlibrary.user.dao.AuthorDAO;
17 import org.sourceforge.vlibrary.user.dao.BookDAO;
18 import org.sourceforge.vlibrary.user.dao.LibraryTransactionDAO;
19 import org.sourceforge.vlibrary.user.dao.LocationDAO;
20 import org.sourceforge.vlibrary.user.dao.ReaderDAO;
21 import org.sourceforge.vlibrary.user.dao.SubjectDAO;
22 import org.sourceforge.vlibrary.user.domain.Author;
23 import org.sourceforge.vlibrary.user.domain.Book;
24 import org.sourceforge.vlibrary.user.domain.Location;
25 import org.sourceforge.vlibrary.user.domain.Reader;
26 import org.sourceforge.vlibrary.user.domain.Subject;
27 import org.sourceforge.vlibrary.user.exceptions.BookAwaitingPickupException;
28 import org.sourceforge.vlibrary.user.exceptions.BookNotFoundException;
29 import org.sourceforge.vlibrary.user.exceptions.DuplicateRequestException;
30 import org.sourceforge.vlibrary.user.exceptions.ReaderNotFoundException;
31 import org.sourceforge.vlibrary.user.valuebeans.BookMoveTransaction;
32 import org.sourceforge.vlibrary.user.valuebeans.LibraryTransaction;
33 import org.sourceforge.vlibrary.user.workers.MailWorker;
34 import org.sourceforge.vlibrary.util.Crypto;
35 import org.sourceforge.vlibrary.util.SruSrwClientInterface;
36 import org.springframework.context.support.ResourceBundleMessageSource;
37
38
39
40
41
42
43
44
45
46 public class LibraryManager {
47 private ResourceBundleMessageSource resourceBundleMessageSource;
48
49
50 private static Logger logger =
51 Logger.getLogger(LibraryManager.class.getName());
52
53
54 private AuthorDAO authorDAO;
55 private BookDAO bookDAO;
56 private ReaderDAO readerDAO;
57 private SubjectDAO subjectDAO;
58 private LocationDAO locationDAO;
59 private LibraryTransactionDAO libraryTransactionDAO;
60 private MailWorker mailWorker;
61 private Crypto crypto;
62 private SruSrwClientInterface sruClient;
63
64
65
66
67
68
69
70
71
72 public void setResourceBundleMessageSource(
73 ResourceBundleMessageSource resourceBundleMessageSource) {
74 this.resourceBundleMessageSource = resourceBundleMessageSource;
75 }
76
77
78
79
80 public void setMailWorker(MailWorker mailWorker) {
81 this.mailWorker = mailWorker;
82 }
83
84
85
86
87 public void setAuthorDAO(AuthorDAO authorDAO) {
88 this.authorDAO = authorDAO;
89 }
90
91
92
93
94 public void setBookDAO(BookDAO bookDAO) {
95 this.bookDAO = bookDAO;
96 }
97
98
99
100
101 public void setReaderDAO(ReaderDAO readerDAO) {
102 this.readerDAO = readerDAO;
103 }
104
105 public ReaderDAO getReaderDAO() {
106 return this.readerDAO;
107 }
108
109
110
111
112 public void setSubjectDAO(SubjectDAO subjectDAO) {
113 this.subjectDAO = subjectDAO;
114 }
115
116
117
118
119 public void setLibraryTransactionDAO(
120 LibraryTransactionDAO libraryTransactionDAO) {
121 this.libraryTransactionDAO = libraryTransactionDAO;
122 }
123
124
125
126
127 public void setCrypto( Crypto crypto) {
128 this.crypto = crypto;
129 }
130
131
132
133
134 public void setLocationDAO(LocationDAO locationDAO) {
135 this.locationDAO = locationDAO;
136 }
137
138
139
140
141 public void setSruSrwClientInterface( SruSrwClientInterface sruClient) {
142 this.sruClient = sruClient;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public ArrayList<Subject> getSubjects() throws LibraryException {
158 if (logger.isDebugEnabled()) {
159 logger.debug("");
160 }
161
162 return subjectDAO.getSubjects();
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public void createSubjectIds(ArrayList<Subject> subjects) throws LibraryException {
176 if (logger.isDebugEnabled()) {
177 logger.debug(subjects);
178 }
179
180 subjectDAO.setSubjectIds(subjects);
181 }
182
183
184
185
186
187
188
189
190 public ArrayList<Location> getLocations() throws LibraryException {
191 if (logger.isDebugEnabled()) {
192 final String message = resourceBundleMessageSource.getMessage("retrieving.locations",
193 new Object[] { new String("") }, Locale.US);
194 logger.debug(message);
195 }
196 try {
197 return locationDAO.getLocations();
198 } catch (final Exception ex) {
199 final String errString = resourceBundleMessageSource.getMessage("error.retrieving.locations",
200 new Object[] { new String("") }, Locale.US);
201 logger.error(errString, ex);
202 throw new LibraryException(errString);
203 }
204
205 }
206
207
208
209
210
211
212
213
214
215
216 public boolean isUserValidByPhone(String phoneNo) throws LibraryException {
217 if (logger.isDebugEnabled()) {
218 logger.debug("phoneNo="+phoneNo);
219 }
220
221 final Reader rd = readerDAO.retrieveByPhone(phoneNo);
222
223 if (rd == null) {
224 return false;
225 } else {
226 return true;
227 }
228 }
229
230
231
232
233
234
235
236
237
238
239
240 public long retrieveIDByPhone(String phoneNo) throws LibraryException {
241 if (logger.isDebugEnabled()) {
242 logger.debug("phoneNo=" + phoneNo);
243 }
244
245 Reader rd = new Reader();
246
247 rd = readerDAO.retrieveByPhone(phoneNo);
248
249 if ( rd == null) {
250 return 0;
251 }
252
253 return rd.getId();
254 }
255
256
257
258
259
260
261
262
263 public boolean isUserIDValid(long id) throws LibraryException {
264 if (logger.isDebugEnabled()) {
265 logger.debug("userId" + id);
266 }
267
268 return readerDAO.readerExists(id);
269 }
270
271
272
273
274
275
276
277
278
279 public boolean isUserIDValid(String UID)
280 throws LibraryException {
281 if (logger.isDebugEnabled()) {
282 logger.debug("userId" + UID);
283 }
284
285 return readerDAO.uidExists(UID);
286 }
287
288
289
290
291
292
293
294
295
296
297
298 public boolean readerExists(String firstName, String lastName)
299 throws LibraryException {
300 if (logger.isDebugEnabled()) {
301 logger.debug(
302 "firstName="+firstName + ", lastName=" + lastName);
303 }
304
305 return readerDAO.readerExists( firstName, lastName);
306 }
307
308
309
310
311
312
313
314
315
316 public boolean readerExists(long id) throws LibraryException {
317 if (logger.isDebugEnabled()) {
318 logger.debug("reader="+id);
319 }
320
321 return readerDAO.readerExists(id);
322 }
323
324
325
326
327
328
329
330
331
332
333 public boolean deskPhoneExists(String deskPhone) throws LibraryException {
334 if (logger.isDebugEnabled()) {
335 logger.debug("deskPhone="+deskPhone);
336 }
337
338 return readerDAO.deskPhoneExists(deskPhone);
339 }
340
341
342
343
344
345
346
347
348 public ArrayList<Reader> getReaders() throws LibraryException {
349 if (logger.isDebugEnabled()) {
350 logger.debug("");
351 }
352
353 return readerDAO.getReaders();
354 }
355
356
357
358
359
360
361
362
363 public LibraryTransaction retrieveTransaction(long id)
364 throws LibraryException {
365 if (logger.isDebugEnabled()) {
366 logger.debug("tranId="+id);
367 }
368
369 return libraryTransactionDAO.retrieve(id );
370 }
371
372
373
374
375
376
377
378
379
380 public ArrayList<Book> authorSearch(String authorLastName)
381 throws LibraryException {
382 if (logger.isDebugEnabled()) {
383 logger.debug("authorLastName="+authorLastName);
384 }
385
386 return bookDAO.authorSearch( authorLastName );
387 }
388
389
390
391
392
393
394
395
396
397 public ArrayList<Book> subjectOrSearch(ArrayList<Subject> subjects)
398 throws LibraryException {
399 if (logger.isDebugEnabled()) {
400 logger.debug(subjects);
401 }
402
403 return bookDAO.subjectOrSearch( subjects );
404 }
405
406
407
408
409
410
411
412
413
414
415 public ArrayList<Book> subjectAndSearch(ArrayList subjects)
416 throws LibraryException {
417 if (logger.isDebugEnabled()) {
418 logger.debug(subjects);
419 }
420
421 return bookDAO.subjectAndSearch(subjects);
422 }
423
424
425
426
427
428
429
430
431
432 public ArrayList<Book> ownerSearch(long owner)
433 throws LibraryException {
434 if (logger.isDebugEnabled()) {
435 logger.debug("owner="+owner);
436 }
437
438 return bookDAO.ownerSearch( owner );
439 }
440
441
442
443
444
445
446
447
448
449 public ArrayList<Book> titleSearch(String title)
450 throws LibraryException {
451 if (logger.isDebugEnabled()) {
452 logger.debug("bookTitle="+title);
453 }
454
455 return bookDAO.titleSearch( title );
456 }
457
458
459
460
461
462
463
464
465 public ArrayList<Book> dump()
466 throws LibraryException {
467 if (logger.isDebugEnabled()) {
468 logger.debug("");
469 }
470
471 return bookDAO.dump();
472 }
473
474
475
476
477
478
479
480
481
482
483
484
485 public Reader retrieveByUid(String uid) throws LibraryException {
486 if (logger.isDebugEnabled()) {
487 logger.debug("readerUid="+uid);
488 }
489
490 return readerDAO.retrieveByUid( uid );
491 }
492
493
494
495
496
497
498
499
500
501 public ArrayList<Author> getAuthors(Book book)
502 throws LibraryException {
503 if (logger.isDebugEnabled()) {
504 logger.debug(book.toString());
505 }
506
507 return bookDAO.getAuthors(book);
508 }
509
510
511
512
513
514
515
516
517
518 public ArrayList<Subject> getSubjects(Book book)
519 throws LibraryException {
520 if (logger.isDebugEnabled()) {
521 logger.debug(book.toString());
522 }
523
524 return bookDAO.getSubjects(book);
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549 public BookMoveTransaction processExchange(long reader, long book, String transactionType, long location)
550 throws LibraryException {
551 if (logger.isDebugEnabled()) {
552 String message;
553 if (transactionType.equals(Constants.CHECKIN)) {
554 message = resourceBundleMessageSource.getMessage("trans.checking.in", new Object[] {
555 (new Long(reader)).toString(), (new Long(book)).toString(), Long.toString(location) },
556 Locale.US);
557 } else {
558 message = resourceBundleMessageSource.getMessage("trans.checking.out", new Object[] {
559 (new Long(reader)).toString(), (new Long(book)).toString(), Long.toString(location) },
560 Locale.US);
561 }
562 logger.debug(message);
563 }
564
565 Book bk = new Book();
566 bk.setId(book);
567
568 try {
569 bk = bookDAO.retrieve(book);
570 if (bk == null) {
571 throw new Exception();
572 }
573 } catch (final Throwable e) {
574 final String errString = resourceBundleMessageSource.getMessage("error.book.retrieve",
575 new Object[] { bk == null ? "" : bk.toString() }, Locale.US);
576 logger.error(errString, e);
577 throw new LibraryException(errString, e);
578 }
579
580 Reader rd = new Reader();
581 try {
582 rd = readerDAO.retrieveById(reader);
583 if (rd == null) {
584 throw new Exception();
585 }
586 } catch (final Throwable e) {
587 final String errString = resourceBundleMessageSource.getMessage("error.reader.retrieve",
588 new Object[] { rd == null ? "" : rd.toString() }, Locale.US);
589 logger.error(errString, e);
590 throw new LibraryException(errString, e);
591 }
592
593
594 try {
595 if (transactionType.equals(Constants.CHECKIN)) {
596 processCheckin(reader, book, location);
597 } else if (transactionType.equals(Constants.CHECKOUT)) {
598 processCheckout(reader, book, location);
599 } else {
600 final String errString = resourceBundleMessageSource.getMessage("error.trans.request.badrequest",
601 new Object[] { (new Long(reader)).toString(), (new Long(book)).toString() }, Locale.US);
602 logger.error(errString);
603 throw new LibraryException(errString);
604 }
605 } catch (final Throwable e) {
606 final String errString = resourceBundleMessageSource.getMessage("error.trans.request.badrequest",
607 new Object[] { (new Long(reader)).toString(), (new Long(book)).toString() }, Locale.US);
608 logger.error(errString, e);
609 throw new LibraryException(e.getMessage(), e);
610 }
611
612
613 final BookMoveTransaction t = new BookMoveTransaction();
614 t.setReaderEmail(rd.getEmail());
615 t.setReaderName(rd.getFirstName() + " " + rd.getLastName());
616 t.setBookTitle(bk.getTitle());
617 t.setTransaction(transactionType);
618 t.setLocation(location);
619
620 if (logger.isDebugEnabled()) {
621 final String message = resourceBundleMessageSource.getMessage("trans.checkin.successful", new Object[] {
622 (new Long(reader)).toString(), (new Long(book)).toString(), Long.toString(location) }, Locale.US);
623 logger.debug(message);
624 }
625
626 return t;
627 }
628
629
630
631
632
633
634
635 public boolean bookExists(long book)
636 throws LibraryException {
637 if (logger.isDebugEnabled()) {
638 logger.debug("book="+book);
639 }
640
641 return bookDAO.bookExists(book);
642 }
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660 public long getReaderID(String firstName,String lastName)
661 throws LibraryException {
662 if (logger.isDebugEnabled()) {
663 logger.debug(
664 "firstname=" +firstName + ", lastName=" + lastName);
665 }
666
667 return readerDAO.getReaderID( firstName, lastName );
668 }
669
670
671
672
673
674
675
676
677
678
679 public void createAuthorIds(ArrayList<Author> authors) throws LibraryException {
680 if (logger.isDebugEnabled()) {
681 logger.debug(authors);
682 }
683
684 authorDAO.setAuthorIds(authors);
685 }
686
687
688
689
690
691
692
693
694
695 public Book retrieveBook(long bookID) throws LibraryException {
696 if (logger.isDebugEnabled()) {
697 logger.debug("book=" + bookID);
698 }
699
700 return bookDAO.retrieve(bookID);
701 }
702
703
704
705
706
707
708
709
710
711
712 public Reader retrieveReader( long id ) throws LibraryException {
713 if (logger.isDebugEnabled()) {
714 logger.debug("reader=" + id);
715 }
716
717 return readerDAO.retrieveById( id );
718 }
719
720
721
722
723
724
725
726
727
728
729
730
731 public void createBookAuthors(Book book, ArrayList<Author> authors)
732 throws LibraryException {
733 if (logger.isDebugEnabled()) {
734 logger.debug(book.toString() + authors);
735 }
736
737 bookDAO.setAuthors( book, authors );
738 }
739
740
741
742
743
744
745
746
747
748
749
750
751 public void createBookSubjects(Book book, ArrayList<Subject> subjects)
752 throws LibraryException {
753 if (logger.isDebugEnabled()) {
754 logger.debug(book.toString() + subjects);
755 }
756
757 bookDAO.setSubjects( book, subjects );
758 }
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779 public Book createBook(Book book, ArrayList<Subject> subjects, ArrayList<Author> authors)
780 throws LibraryException {
781 if (logger.isDebugEnabled()) {
782 logger.debug(book.toString() + subjects + authors);
783 }
784
785 book = bookDAO.insert(book);
786
787 bookDAO.setAuthors(book, authors);
788 bookDAO.setSubjects(book, subjects);
789 return book;
790 }
791
792
793
794
795
796
797
798
799
800
801
802 public void updateBook(Book book) throws
803 LibraryException {
804 if (logger.isDebugEnabled()) {
805 logger.debug(book.toString());
806 }
807
808 bookDAO.update( book );
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824 public void insertReader(Reader reader)
825 throws LibraryException {
826 if (logger.isDebugEnabled()) {
827 logger.debug( reader.toString() );
828 }
829
830 readerDAO.insert(reader);
831 }
832
833
834
835
836
837
838
839
840
841
842
843 public void updateReader(Reader reader)
844 throws LibraryException {
845 if (logger.isDebugEnabled()) {
846 logger.debug(reader.toString());
847 }
848
849 readerDAO.update(reader);
850 }
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867 public void processCheckin(long reader, long book, long location) throws LibraryException {
868 if (logger.isInfoEnabled()) {
869 final String message = resourceBundleMessageSource.getMessage("trans.checking.in", new Object[] {
870 (new Long(reader)).toString(), (new Long(book)).toString(), Long.toString(location) }, Locale.US);
871 logger.info(message);
872 }
873 libraryTransactionDAO.processCheckin(reader, book, location);
874
875
876 Book bk = new Book();
877 bk.setId(book);
878 bk = retrieveBook(book);
879 final ArrayList<Reader> requestors = getRequestors(book, location);
880
881 for (int i = 0; i < requestors.size(); i++) {
882 final Reader rd = requestors.get(i);
883 try {
884 mailWorker.sendReturnNotification(rd, bk, locationDAO.getLocationDescription(location));
885 } catch (final Exception ex) {
886 final String errString = resourceBundleMessageSource.getMessage("error.trans.checkin",
887 new Object[] { (new Long(reader)).toString(), (new Long(book)).toString() }, Locale.US);
888
889 logger.error(errString, ex);
890 }
891 }
892
893
894 if (logger.isInfoEnabled()) {
895 logger.info(resourceBundleMessageSource.getMessage("trans.checkin.successful",
896 new Object[] { Long.toString(reader), Long.toString(book), Long.toString(location) }, Locale.US));
897 }
898 }
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914 public void processCheckout(long reader, long book, long location)
915 throws LibraryException {
916
917
918 if (logger.isDebugEnabled()) {
919 logger.info( resourceBundleMessageSource.getMessage(
920 "trans.checking.out",
921 new Object[] {(new Long(reader)).toString(),
922 (new Long(book)).toString()},
923 Locale.US));
924 }
925
926
927 if (!readerDAO.readerExists(reader)) {
928 final String errString = resourceBundleMessageSource.getMessage(
929 "error.nonexistent.reader",
930 new Object[] { new Long(reader) },
931 Locale.US);
932 logger.error(errString);
933 throw new ReaderNotFoundException(errString);
934 }
935
936 if (!bookExists(book)) {
937 final String errString = resourceBundleMessageSource.getMessage(
938 "error.nonexistent.book",
939 new Object[] { new Long(book) },
940 Locale.US);
941 logger.error(errString);
942 throw new BookNotFoundException(errString);
943 }
944
945
946 Reader newPossessor = new Reader();
947 newPossessor.setId(reader);
948 newPossessor = readerDAO.retrieve(newPossessor);
949
950
951 libraryTransactionDAO.processCheckout(reader, book, bookDAO.getIsbn(book), location);
952
953
954 final Book bk = bookDAO.retrieve(book);
955 final ArrayList<Reader> requestors = getRequestors(book, location);
956 for (int i=0; i<requestors.size(); i++) {
957 final Reader requestor = requestors.get(i);
958 try {
959 mailWorker.sendCheckoutNotification(requestor,newPossessor, bk,
960 locationDAO.getLocationDescription(location));
961 } catch( final Exception ex) {
962 final String errString =
963 resourceBundleMessageSource.getMessage(
964 "error.trans.checkout",
965 new Object[] {(new Long(reader)).toString(),
966 (new Long(book)).toString()},
967 Locale.US);
968
969 logger.error(errString, ex);
970 }
971 }
972
973
974 if (logger.isDebugEnabled()) {
975 logger.info(resourceBundleMessageSource
976 .getMessage("trans.checkout.successful",
977 new Object[] {
978 Long.toString(reader), Long.toString(book),
979 Long.toString(location)
980 }, Locale.US));
981 }
982 }
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997 public long getPossessor(long book)
998 throws LibraryException {
999 if (logger.isDebugEnabled()) {
1000 logger.info( resourceBundleMessageSource.getMessage(
1001 "retrieving.book.possesor",
1002 new Object[] {(new Long(book)).toString()},
1003 Locale.US));
1004 }
1005
1006 if (!bookExists(book)) {
1007 final String errString = resourceBundleMessageSource.getMessage(
1008 "error.nonexistent.book",
1009 new Object[] { new Long(book) },
1010 Locale.US);
1011 logger.error(errString);
1012 throw new BookNotFoundException(errString);
1013 }
1014
1015 final long result = libraryTransactionDAO.getPossessor(book);
1016
1017 if (result == Constants.CHECKED_IN || readerExists(result)) {
1018 return result;
1019 }
1020
1021
1022 Book bk = new Book();
1023 bk.setId(book);
1024 bk = bookDAO.retrieve(book);
1025 if (logger.isDebugEnabled()) {
1026 logger.info( resourceBundleMessageSource.getMessage(
1027 "retrieving.book.possesor.successful",
1028 new Object[] {(new Long(book)).toString()},
1029 Locale.US));
1030 }
1031 return bk.getOwner();
1032 }
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058 public void processRequest(long reader, long book, long location)
1059 throws Exception {
1060 if (logger.isDebugEnabled()) {
1061 logger.info( resourceBundleMessageSource.getMessage(
1062 "processing.request",
1063 new Object[] {(new Long(reader)).toString(),
1064 (new Long(book)).toString()},
1065 Locale.US));
1066 }
1067
1068
1069 if (!readerExists(reader)) {
1070 final String errString = resourceBundleMessageSource.getMessage(
1071 "error.nonexistent.reader",
1072 new Object[] { new Long(reader) },
1073 Locale.US);
1074 logger.error(errString);
1075 throw new ReaderNotFoundException(errString);
1076 }
1077
1078 if (!bookExists(book)) {
1079 final String errString = resourceBundleMessageSource.getMessage(
1080 "error.nonexistent.book",
1081 new Object[] { new Long(book) },
1082 Locale.US);
1083 logger.error(errString);
1084 throw new BookNotFoundException(errString);
1085 }
1086
1087 final String isbn = bookDAO.retrieve(book).getIsbn();
1088
1089 final long[] copies = libraryTransactionDAO.getCopies(isbn, location);
1090 if (copies.length == 0) {
1091
1092 libraryTransactionDAO.processRequest(reader, book, location);
1093 Reader requestor = new Reader();
1094 Book bk = new Book();
1095 bk.setId(book);
1096 bk = bookDAO.retrieve(book);
1097 requestor.setId(reader);
1098 requestor = readerDAO.retrieve(requestor);
1099
1100 final String locationString = locationDAO.getLocationDescription(location);
1101 mailWorker.sendAcquisitionNotification(requestor, bk, locationString);
1102
1103 mailWorker.sendAcquisitionRequestConfirmation(requestor, bk, locationString);
1104 }
1105
1106
1107 for (int i = 0; i < copies.length; i++) {
1108 if (requestPending(reader, copies[i])) {
1109 final String warning = resourceBundleMessageSource.getMessage(
1110 "error.trans.duplicate.request",
1111 new Object[] { (new Long(reader)).toString(),
1112 Long.valueOf(copies[i]).toString()},
1113 Locale.US);
1114 logger.warn(warning);
1115 throw new DuplicateRequestException(warning);
1116 }
1117 final long possessorId = getPossessor(copies[i]);
1118 if (possessorId == Constants.CHECKED_IN) {
1119 throw new BookAwaitingPickupException();
1120 }
1121 libraryTransactionDAO.processRequest(reader, copies[i], location);
1122 Reader possessor = new Reader();
1123 Reader requestor = new Reader();
1124 Book bk = new Book();
1125 bk.setId(copies[i]);
1126 bk = bookDAO.retrieve(copies[i]);
1127 possessor.setId(possessorId);
1128 possessor = readerDAO.retrieve(possessor);
1129 requestor.setId(reader);
1130 requestor = readerDAO.retrieve(requestor);
1131 mailWorker.sendRequestNotification(possessor, requestor, bk,
1132 locationDAO.getLocationDescription(location));
1133 mailWorker.sendRequestConfirmation(possessor, requestor, bk,
1134 locationDAO.getLocationDescription(location));
1135 }
1136 if (logger.isDebugEnabled()) {
1137 logger.info( resourceBundleMessageSource.getMessage(
1138 "request.processed",
1139 new Object[] {(new Long(reader)).toString(),
1140 (new Long(book)).toString()},
1141 Locale.US));
1142 }
1143 }
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 private ArrayList<Reader> getRequestors(long book, long location)
1154 throws LibraryException {
1155 if (logger.isDebugEnabled()) {
1156 logger.info( resourceBundleMessageSource.getMessage(
1157 "retrieving.requesters",
1158 new Object[] {Long.toString(book), Long.toBinaryString(location)},
1159 Locale.US));
1160 }
1161
1162 final Book bk = retrieveBook(book);
1163 final List<Long> readerIdList = libraryTransactionDAO.getRequestors(bk.getIsbn(), location);
1164 final ArrayList<Reader> outList = new ArrayList<Reader>();
1165 final Iterator<Long> it = readerIdList.iterator();
1166
1167 while(it.hasNext()) {
1168 final Long readerId = it.next();
1169 Reader rd = new Reader();
1170 rd.setId(readerId);
1171 rd = readerDAO.retrieve(rd);
1172 if ( rd != null ) {
1173 outList.add(rd);
1174 }
1175 }
1176
1177 if (logger.isDebugEnabled()) {
1178 logger.info( resourceBundleMessageSource.getMessage(
1179 "requesters.retrieved",
1180 new Object[] {Long.toString(book), Long.toString(location), Integer.toString(outList.size())},
1181 Locale.US));
1182 }
1183
1184 return outList;
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197 public boolean requestPending(long reader,long book)
1198 throws LibraryException {
1199 if (logger.isDebugEnabled()) {
1200 logger.debug(
1201 "reader=" + reader+ ", book=" + book);
1202 }
1203
1204 return libraryTransactionDAO.requestPending( reader, book );
1205 }
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216 public void cancelRequest(long reader, long book)
1217 throws LibraryException {
1218 if (logger.isDebugEnabled()) {
1219 logger.info( resourceBundleMessageSource.getMessage(
1220 "cancelling.request",
1221 new Object[] {(new Long(reader)).toString(),
1222 (new Long(book)).toString()},
1223 Locale.US));
1224 }
1225
1226
1227 if (!readerDAO.readerExists(reader)) {
1228 final String errString = resourceBundleMessageSource.getMessage(
1229 "error.nonexistent.reader",
1230 new Object[] { new Long(reader) },
1231 Locale.US);
1232 logger.error(errString);
1233 throw new ReaderNotFoundException(errString);
1234 }
1235
1236 if (!bookExists(book)) {
1237 final String errString = resourceBundleMessageSource.getMessage(
1238 "error.nonexistent.book",
1239 new Object[] { new Long(book) },
1240 Locale.US);
1241 logger.error(errString);
1242 throw new BookNotFoundException(errString);
1243 }
1244
1245 libraryTransactionDAO.cancelRequest(reader, retrieveBook(book).getIsbn());
1246
1247
1248 final Book bk = bookDAO.retrieve(book);
1249 Reader rd = new Reader();
1250 rd.setId(reader);
1251 rd = readerDAO.retrieve(rd);
1252 try {
1253 mailWorker.sendDeleteConfirmation(rd,bk);
1254 } catch( final Exception ex) {
1255 logger.error(ex);
1256 }
1257
1258 if (logger.isDebugEnabled()) {
1259 logger.info( resourceBundleMessageSource.getMessage(
1260 "request.cancelled",
1261 new Object[] {(new Long(reader)).toString(),
1262 (new Long(book)).toString()},
1263 Locale.US));
1264 }
1265 }
1266
1267
1268
1269
1270
1271
1272
1273
1274 public List<LibraryTransaction> getTransactions(long book)
1275 throws LibraryException {
1276 if (logger.isDebugEnabled()) {
1277 logger.info( resourceBundleMessageSource.getMessage(
1278 "retrieving.transactions",
1279 new Object[] {(new Long(book)).toString()},
1280 Locale.US));
1281 }
1282
1283 if (!bookExists(book)) {
1284 final String errString = resourceBundleMessageSource.getMessage(
1285 "error.nonexistent.book",
1286 new Object[] {new Long(book)},
1287 Locale.US);
1288 logger.error(errString);
1289 throw new BookNotFoundException(errString);
1290 }
1291
1292
1293 final Book bk = bookDAO.retrieve(book);
1294 final LibraryTransaction trans = new LibraryTransaction();
1295 trans.setBook(book);
1296 trans.setBookTitle(bk.getTitle());
1297 final long reader = bk.getOwner();
1298 trans.setReader(reader);
1299 Reader rd = new Reader();
1300 rd.setId(reader);
1301 rd = readerDAO.retrieve(rd);
1302 if (rd != null) {
1303 trans.setReaderName(rd.getFirstName() + " " + rd.getLastName());
1304 trans.setReaderPhone(rd.getDeskPhone());
1305 trans.setTimeString(bk.getCreated());
1306 trans.setAction(Constants.ADD_ACTION);
1307 final String action = resourceBundleMessageSource.getMessage(
1308 "trans.add.label",
1309 null,
1310 Locale.US);
1311 trans.setActionString( action );
1312 }
1313
1314
1315 final ArrayList<LibraryTransaction> result = new ArrayList<LibraryTransaction>();
1316 result.add(trans);
1317
1318
1319 result.addAll(libraryTransactionDAO.getTransactions(book));
1320
1321 if (logger.isDebugEnabled()) {
1322 logger.info( resourceBundleMessageSource.getMessage(
1323 "transactions.retrieved",
1324 new Object[] {(new Long(book)).toString()},
1325 Locale.US));
1326 }
1327
1328 return result;
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 public void resetPassword(String user, String newPwd)
1341 throws LibraryException {
1342 if (logger.isDebugEnabled()) {
1343 logger.debug(
1344 "user=" + user + ", newPwd=" + newPwd);
1345 }
1346
1347 readerDAO.resetPassword(user, newPwd);
1348 }
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361 public void forgotPassword(String uid, String email)
1362 throws LibraryException {
1363 if (logger.isDebugEnabled()) {
1364 logger.info( resourceBundleMessageSource.getMessage(
1365 "forgotPassword.processing",
1366 new Object[] { uid, email },
1367 Locale.US));
1368 }
1369
1370 final Reader rd = retrieveByUid(uid);
1371
1372 if (rd == null || !rd.getEmail().equalsIgnoreCase(email) ) {
1373 final String errString = resourceBundleMessageSource.getMessage(
1374 "error.nonexistent.reader",
1375 new Object[] { uid + ", " + email },
1376 Locale.US);
1377 logger.error(errString);
1378 throw new LibraryException(errString);
1379 }
1380
1381
1382 final byte[] bytes = new byte[8];
1383 new java.util.Random().nextBytes( bytes );
1384 final String newPassword = crypto.convert( bytes );
1385
1386 try {
1387
1388 resetPassword(uid, newPassword);
1389
1390
1391 mailWorker.sendNewPasswordConfirmation( rd, newPassword );
1392 } catch( final Exception ex) {
1393 final String errString = resourceBundleMessageSource.getMessage(
1394 "error.trans.request.badrequest",
1395 new Object[] { uid, email },
1396 Locale.US);
1397 logger.error(errString);
1398 throw new LibraryException(errString);
1399 }
1400
1401 if (logger.isDebugEnabled()) {
1402 logger.info( resourceBundleMessageSource.getMessage(
1403 "forgotPassword.processed.successfully",
1404 new Object[] {uid, email},
1405 Locale.US));
1406 }
1407 }
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427 public Map lookupBook(String isbn)
1428 throws LibraryException {
1429 if (logger.isDebugEnabled()) {
1430 logger.debug(
1431 "isbn=" + isbn);
1432 }
1433
1434 try {
1435 return sruClient.callSruSrwProvider(isbn);
1436 } catch(final Throwable ex) {
1437 if (logger.isDebugEnabled()) {
1438 logger.debug( ex.getCause() );
1439 }
1440
1441 throw new LibraryException(ex.getMessage(), ex);
1442 }
1443 }
1444 }