1 package org.sourceforge.vlibrary.user.dao;
2
3
4 import java.sql.Types;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Locale;
9 import java.util.Map;
10
11 import org.apache.commons.lang.StringUtils;
12 import org.apache.log4j.Logger;
13 import org.sourceforge.vlibrary.Constants;
14 import org.sourceforge.vlibrary.exceptions.LibraryException;
15 import org.sourceforge.vlibrary.user.domain.Author;
16 import org.sourceforge.vlibrary.user.domain.Book;
17 import org.sourceforge.vlibrary.user.domain.Subject;
18 import org.sourceforge.vlibrary.util.Utils;
19 import org.springframework.context.support.ResourceBundleMessageSource;
20 import org.springframework.jdbc.core.JdbcTemplate;
21
22
23
24
25
26
27
28
29 public class BookDAO {
30
31
32 private static Logger logger = Logger.getLogger(
33 BookDAO.class.getName());
34
35 private ResourceBundleMessageSource resourceBundleMessageSource;
36
37 private JdbcTemplate jdbcTemplate;
38 private String insertBookSQL;
39 private String selectBookByISBNSQL;
40 private String countCopiesSQL;
41 private String selectBookByIdSQL;
42 private String updateBookSQL;
43 private String selectBookAuthorByBookIdAuthoridSQL;
44 private String insertBookAuthorSQL;
45 private String selectAuthorsByBookIdSQL;
46 private String selectSubjectBySubjectIdBookIdSQL;
47 private String insertSubjectBookSQL;
48 private String selectSubjectsByBookIdSQL;
49 private String selectAllBooksSQL;
50 private String selectBookByTitleSQL;
51 private String selectBookByOwnerSQL;
52 private String selectBookByAuthorSQL;
53 private String selectSubjectOrSearchMainSQL;
54 private String selectSubjectOrSearchOrderBySQL;
55 private String selectSubjectAndSearchSelectSQL;
56 private String selectSubjectAndSearchWhereSQL;
57 private String selectSubjectAndSearchOrderBySQL;
58 private AuthorDAO authorDAO;
59 private SubjectDAO subjectDAO;
60
61
62
63
64 public BookDAO() {
65 }
66
67
68
69
70
71 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
72 this.jdbcTemplate = jdbcTemplate;
73 }
74
75
76
77
78 public void setResourceBundleMessageSource(
79 ResourceBundleMessageSource resourceBundleMessageSource) {
80 this.resourceBundleMessageSource = resourceBundleMessageSource;
81 }
82
83
84
85
86 public void setSelectSubjectAndSearchSelectSQL(
87 String selectSubjectAndSearchSelectSQL) {
88 this.selectSubjectAndSearchSelectSQL = selectSubjectAndSearchSelectSQL;
89 }
90
91
92
93
94 public void setSelectSubjectAndSearchWhereSQL(
95 String selectSubjectAndSearchWhereSQL) {
96 this.selectSubjectAndSearchWhereSQL = selectSubjectAndSearchWhereSQL;
97 }
98
99
100
101
102 public void setSelectSubjectAndSearchOrderBySQL(
103 String selectSubjectAndSearchOrderBySQL) {
104 this.selectSubjectAndSearchOrderBySQL =
105 selectSubjectAndSearchOrderBySQL;
106 }
107
108
109
110
111 public void setInsertBookSQL( String insertBookSQL) {
112 this.insertBookSQL = insertBookSQL;
113 }
114
115
116
117
118 public void setSelectSubjectOrSearchMainSQL(
119 String selectSubjectOrSearchMainSQL) {
120 this.selectSubjectOrSearchMainSQL = selectSubjectOrSearchMainSQL;
121 }
122
123
124
125
126 public void setSelectSubjectOrSearchOrderBySQL(
127 String selectSubjectOrSearchOrderBySQL) {
128 this.selectSubjectOrSearchOrderBySQL = selectSubjectOrSearchOrderBySQL;
129 }
130
131
132
133
134 public void setSelectBookByISBNSQL( String selectBookByISBNSQL) {
135 this.selectBookByISBNSQL = selectBookByISBNSQL;
136 }
137
138
139
140
141 public void setCountCopiesSQL( String countCopiesSQL) {
142 this.countCopiesSQL = countCopiesSQL;
143 }
144
145
146
147
148 public void setSelectBookByIdSQL( String selectBookByIdSQL) {
149 this.selectBookByIdSQL = selectBookByIdSQL;
150 }
151
152
153
154
155 public void setUpdateBookSQL( String updateBookSQL) {
156 this.updateBookSQL = updateBookSQL;
157 }
158
159
160
161
162 public void setSelectBookAuthorByBookIdAuthoridSQL(
163 String selectBookAuthorByBookIdAuthoridSQL) {
164 this.selectBookAuthorByBookIdAuthoridSQL =
165 selectBookAuthorByBookIdAuthoridSQL;
166 }
167
168
169
170
171 public void setInsertBookAuthorSQL( String insertBookAuthorSQL) {
172 this.insertBookAuthorSQL = insertBookAuthorSQL;
173 }
174
175
176
177
178 public void setSelectAuthorsByBookIdSQL(String selectAuthorsByBookIdSQL) {
179 this.selectAuthorsByBookIdSQL = selectAuthorsByBookIdSQL;
180 }
181
182
183
184
185 public void setSelectSubjectBySubjectIdBookIdSQL(
186 String selectSubjectBySubjectIdBookIdSQL) {
187 this.selectSubjectBySubjectIdBookIdSQL =
188 selectSubjectBySubjectIdBookIdSQL;
189 }
190
191
192
193
194 public void setInsertSubjectBookSQL( String insertSubjectBookSQL) {
195 this.insertSubjectBookSQL = insertSubjectBookSQL;
196 }
197
198
199
200
201 public void setSelectSubjectsByBookIdSQL(
202 String selectSubjectsByBookIdSQL) {
203 this.selectSubjectsByBookIdSQL = selectSubjectsByBookIdSQL;
204 }
205
206
207
208
209 public void setSelectAllBooksSQL( String selectAllBooksSQL) {
210 this.selectAllBooksSQL = selectAllBooksSQL;
211 }
212
213
214
215
216 public void setSelectBookByTitleSQL( String selectBookByTitleSQL) {
217 this.selectBookByTitleSQL = selectBookByTitleSQL;
218 }
219
220
221
222
223 public void setSelectBookByOwnerSQL( String selectBookByOwnerSQL) {
224 this.selectBookByOwnerSQL = selectBookByOwnerSQL;
225 }
226
227
228
229
230 public void setSelectBookByAuthorSQL( String selectBookByAuthorSQL) {
231 this.selectBookByAuthorSQL = selectBookByAuthorSQL;
232 }
233
234
235
236
237 public void setAuthorDAO(AuthorDAO authorDAO) {
238 this.authorDAO = authorDAO;
239 }
240
241
242
243
244 public void setSubjectDAO(SubjectDAO subjectDAO) {
245 this.subjectDAO = subjectDAO;
246 }
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 public Book insert(Book book) throws LibraryException {
262 if (book == null || book.getTitle() == null ) {
263 final String message = resourceBundleMessageSource.getMessage(
264 "error.book.insert.missing.data",
265 new Object[] {book==null?"":book.toString()},
266 Locale.US);
267 logger.error(message);
268 throw new LibraryException(message);
269 }
270
271 if (logger.isDebugEnabled()) {
272 logger.debug(resourceBundleMessageSource.getMessage(
273 "inserting.book",
274 new Object[] {book.toString()},
275 Locale.US));
276 }
277
278 try {
279 jdbcTemplate.update(insertBookSQL,
280 new Object [] {
281 StringUtils.replace
282 (book.getTitle(),Constants.APOSTROPHE,
283 Constants.ESCAPED_APOSTROPHE)
284 ,
285 StringUtils.replace
286 (book.getPublisher(),Constants.APOSTROPHE,
287 Constants.ESCAPED_APOSTROPHE)
288 ,
289 StringUtils.replace
290 (book.getPub_date(),Constants.APOSTROPHE,
291 Constants.ESCAPED_APOSTROPHE)
292 ,
293 new Long(book.getOwner())
294 ,
295 book.normalizeIsbn(book.getIsbn())
296 },
297 new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
298 Types.INTEGER, Types.VARCHAR}
299 );
300 } catch (final Exception se) {
301 final String errString = resourceBundleMessageSource.getMessage(
302 "error.book.insert.sql",
303 new Object[] { book.toString() },
304 Locale.US);
305 logger.error(errString,se);
306 throw new LibraryException(errString,se);
307 }
308
309
310 book = retrieveByIsbn(book.normalizeIsbn(book.getIsbn()));
311
312
313
314 final int count = countCopies(book.getIsbn());
315 if (count > 1) {
316 book.setCopy(count - 1);
317 update(book);
318 }
319
320 if (logger.isDebugEnabled()) {
321 logger.debug( resourceBundleMessageSource.getMessage(
322 "book.insert.successful",
323 new Object[] { book.toString() },
324 Locale.US));
325 }
326
327 return book;
328 }
329
330
331
332
333
334
335
336
337
338
339
340 public void update(Book book) throws LibraryException {
341 if (book == null || book.getTitle() == null ) {
342 final String message = resourceBundleMessageSource.getMessage(
343 "error.book.update.missing.data",
344 new Object[] {book==null?"":book.toString()},
345 Locale.US);
346 logger.error(message);
347 throw new LibraryException(message);
348 }
349
350 if (logger.isDebugEnabled()) {
351 logger.debug( resourceBundleMessageSource.getMessage(
352 "updating.book",
353 new Object[] { book.toString() },
354 Locale.US));
355 }
356
357 try {
358 jdbcTemplate.update(updateBookSQL,
359 new Object [] {
360 StringUtils.replace
361 (book.getTitle(),Constants.APOSTROPHE,
362 Constants.ESCAPED_APOSTROPHE)
363 ,
364 StringUtils.replace
365 (book.getPublisher(),Constants.APOSTROPHE,
366 Constants.ESCAPED_APOSTROPHE)
367 ,
368 StringUtils.replace
369 (book.getPub_date(),Constants.APOSTROPHE,
370 Constants.ESCAPED_APOSTROPHE)
371 ,
372 new Long(book.getOwner())
373 ,
374 book.normalizeIsbn(book.getIsbn())
375 ,
376 new Integer(book.getCopy())
377 ,
378 new Long(book.getId())
379 },
380 new int[] {Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
381 Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.INTEGER}
382 );
383
384 } catch (final Exception se) {
385 final String errString = resourceBundleMessageSource.getMessage(
386 "error.book.update",
387 new Object[] { book.toString() },
388 Locale.US);
389 logger.error(errString,se);
390 throw new LibraryException(errString,se);
391 }
392
393 if (logger.isDebugEnabled()) {
394 logger.debug( resourceBundleMessageSource.getMessage(
395 "book.update.successful",
396 new Object[] { book.toString() },
397 Locale.US));
398 }
399 }
400
401
402
403
404
405
406
407
408
409
410
411
412
413 public Book retrieveByGenericCriteria(String sqlString,
414 Object[] criteria) throws LibraryException {
415 if (logger.isDebugEnabled()) {
416 logger.debug( resourceBundleMessageSource.getMessage(
417 "retrieving.book",
418 criteria,
419 Locale.US));
420 }
421
422 Book book = null;
423
424 try {
425 final List rows = jdbcTemplate.queryForList(sqlString, criteria);
426 book = loadBookProperties(rows);
427 } catch (final Exception se) {
428 final String errString = resourceBundleMessageSource.getMessage(
429 "error.book.retrieve",
430 criteria,
431 Locale.US);
432 logger.error(errString,se);
433 throw new LibraryException(errString,se);
434 }
435
436 if (logger.isDebugEnabled()) {
437 logger.debug(resourceBundleMessageSource.getMessage(
438 "book.retrieve.successful",
439 new Object[] {book==null?null:book.toString() },
440 Locale.US));
441 }
442
443 return book;
444 }
445
446
447
448
449
450
451
452
453 private Book loadBookProperties(List rs)
454 throws LibraryException {
455 if (rs == null) {
456 final String errString = resourceBundleMessageSource.getMessage(
457 "error.book.retrieve",
458 new Object[] { rs },
459 Locale.US);
460 logger.error(errString);
461 throw new LibraryException(errString);
462 }
463
464 Book book = null;
465 final Iterator it = rs.iterator();
466 if (it.hasNext()) {
467 final Map record = (Map)it.next();
468 book = loadBookPropertiesfromRecord(record);
469 }
470
471 return book;
472 }
473
474
475
476
477
478
479
480
481 private Book loadBookPropertiesfromRecord(Map<String, Object> record)
482 throws LibraryException {
483 final Book book = new Book();
484 book.setId(new Long(((Integer) Utils.getIgnoreCase(
485 record, "ID")).longValue()));
486 book.setCopy(((Integer) Utils.getIgnoreCase(record, "COPY")).intValue());
487 book.setTitle((String) Utils.getIgnoreCase(record, "TITLE"));
488 book.setPublisher((String) Utils.getIgnoreCase(record, "PUBLISHER"));
489 book.setIsbn((String) Utils.getIgnoreCase(record, "ISBN"));
490 book.setPub_date((String) Utils.getIgnoreCase(record, "PUB_DATE"));
491 book.setOwner(new Long(((Integer) Utils.getIgnoreCase(
492 record, "OWNER")).longValue()));
493
494 return book;
495 }
496
497
498
499
500
501
502
503
504
505 public String getIsbn(long bookID) throws LibraryException {
506 final Book book = retrieve(bookID);
507 if (null == book) {
508 return null;
509 } else {
510 return retrieve(bookID).getIsbn();
511 }
512 }
513
514
515
516
517
518
519
520
521
522 public Book retrieve(long bookID) throws LibraryException {
523 return retrieve(Long.toString( bookID));
524 }
525
526
527
528
529
530
531
532
533
534 public Book retrieve(String bookID) throws LibraryException {
535 return retrieveByGenericCriteria(selectBookByIdSQL,
536 new Object[] { bookID } );
537 }
538
539
540
541
542
543
544
545
546
547 public Book retrieveByIsbn( String isbn ) throws LibraryException {
548 return retrieveByGenericCriteria(selectBookByISBNSQL,
549 new Object[] {isbn});
550 }
551
552
553
554
555
556
557
558
559 public int countCopies(String isbn) throws LibraryException {
560 int count;
561 final Object[] criteria = new Object[] {isbn};
562 if (logger.isDebugEnabled()) {
563 logger.debug(resourceBundleMessageSource.getMessage(
564 "counting.book.copies",
565 criteria,
566 Locale.US));
567 }
568 try {
569 count = jdbcTemplate.queryForInt(countCopiesSQL, criteria);
570 } catch (final Exception se) {
571 final String errString = resourceBundleMessageSource.getMessage(
572 "error.book.counting",
573 criteria,
574 Locale.US);
575 logger.error(errString,se);
576 throw new LibraryException(errString,se);
577 }
578
579 if (logger.isDebugEnabled()) {
580 logger.debug(resourceBundleMessageSource.getMessage(
581 "book.count.successful",
582 new Object[] {Integer.valueOf(count)},
583 Locale.US));
584 }
585 return count;
586 }
587
588
589
590
591
592
593
594
595
596
597
598 public void addAuthor(Book book, Author author) throws
599 LibraryException {
600 if (logger.isDebugEnabled()) {
601 logger.debug( resourceBundleMessageSource.getMessage(
602 "inserting.book.author",
603 new Object[] {
604 new Long(author.getId()),
605 new Long(book.getId())
606 }
607 ,
608 Locale.US));
609 }
610
611 authorDAO.insert( author );
612
613 try {
614
615 List rows;
616
617 rows = jdbcTemplate.queryForList(
618 selectBookAuthorByBookIdAuthoridSQL,
619 new Object[] {
620 new Long(author.getId()),
621 new Long(book.getId())
622 }
623 );
624
625 final Iterator it = rows.iterator();
626
627 if ( it.hasNext()) {
628 return;
629 }
630
631
632 if (authorDAO.findAuthorByFirstLastName(author) == null) {
633 authorDAO.insert(author);
634 }
635
636 jdbcTemplate.update( insertBookAuthorSQL,
637 new Object [] {
638 new Long(author.getId()),
639 new Long(book.getId())
640 },
641 new int[] {Types.INTEGER, Types.INTEGER}
642 );
643 } catch (final Exception se) {
644 final String errString = resourceBundleMessageSource.getMessage(
645 "error.inserting.book.author",
646 new Object[] {
647 new Long(author.getId()),
648 new Long(book.getId())
649 },
650 Locale.US);
651 logger.error(errString,se);
652 throw new LibraryException(errString,se);
653 }
654
655 if (logger.isDebugEnabled()) {
656 logger.debug( resourceBundleMessageSource.getMessage(
657 "inserting.book.author.successful",
658 new Object[] {
659 new Long(author.getId()),
660 new Long(book.getId())
661 }
662 ,
663 Locale.US));
664 }
665 }
666
667
668
669
670
671
672
673
674 public ArrayList<Author> getAuthors(Book book) throws LibraryException {
675 if (book == null || book.getTitle() == null ) {
676 final String message = resourceBundleMessageSource.getMessage(
677 "error.book.retrieve.missing.data",
678 new Object[] { book==null?"":book.toString() },
679 Locale.US);
680 logger.error(message);
681 throw new LibraryException( message );
682 }
683
684 if (logger.isDebugEnabled()) {
685 logger.debug( resourceBundleMessageSource.getMessage(
686 "retrieving.book.authors",
687 new Object[] { book.toString() },
688 Locale.US));
689 }
690
691 final ArrayList<Author> outList = new ArrayList<Author>();
692
693 try {
694 List rows;
695
696 rows = jdbcTemplate.queryForList(selectAuthorsByBookIdSQL,
697 new Object[] {new Long(book.getId()) }
698 );
699
700 final Iterator<Map<String, Object>> it = rows.iterator();
701
702 while(it.hasNext()) {
703 final Map<String, Object> record = it.next();
704 final Author au = new Author(
705 (String) Utils.getIgnoreCase(record, "FIRSTNAME"),
706 (String) Utils.getIgnoreCase(record, "LASTNAME"));
707 au.setId(new Long(((Integer) Utils.getIgnoreCase(
708 record, "ID")).longValue()));
709 outList.add(au);
710 }
711
712 } catch (final Exception se) {
713 final String errString = resourceBundleMessageSource.getMessage(
714 "error.book.retrieve.sql",
715 new Object[] { book.toString() },
716 Locale.US);
717 logger.error(errString,se);
718 throw new LibraryException(errString,se);
719 }
720
721 if (logger.isDebugEnabled()) {
722 logger.debug( resourceBundleMessageSource.getMessage(
723 "book.author.retrieve.successful",
724 new Object[] { outList },
725 Locale.US));
726 }
727
728 return outList;
729 }
730
731
732
733
734
735
736
737
738
739
740
741
742 public void addSubject(Book book, Subject subject) throws LibraryException {
743 if (logger.isDebugEnabled()) {
744 logger.debug( resourceBundleMessageSource.getMessage(
745 "updating.book.subjects",
746 new Object[] { book.toString() + subject.toString()},
747 Locale.US));
748 }
749
750 if (book.getId() > 0) {
751
752 try {
753 List rows;
754
755 rows = jdbcTemplate.queryForList(
756 selectSubjectBySubjectIdBookIdSQL,
757 new Object[] {
758 new Long(subject.getId()),
759 new Long(book.getId())
760 }
761 );
762
763 final Iterator it = rows.iterator();
764
765 if (it.hasNext()) {
766 return;
767 }
768 } catch (final Exception se) {
769 final String errString = resourceBundleMessageSource.getMessage(
770 "error.book.retrieve.sql",
771 new Object[] { book.toString() + subject.toString()},
772 Locale.US);
773 logger.error(errString,se);
774 throw new LibraryException(errString,se);
775 }
776
777
778 final Long id = subjectDAO.getSubjectIdByDescription(subject);
779
780 if ( id == null ) {
781 subject = subjectDAO.insert(subject);
782 } else {
783 subject.setId(id.longValue());
784 }
785
786 try {
787 jdbcTemplate.update(insertSubjectBookSQL,
788 new Object [] {
789 new Long(subject.getId()),
790 new Long(book.getId())
791 },
792 new int[] {Types.INTEGER, Types.INTEGER}
793 );
794 } catch (final Exception se) {
795 final String errString = resourceBundleMessageSource.getMessage(
796 "error.book.update.sql",
797 new Object[] { book.toString() + subject.toString()},
798 Locale.US);
799 logger.error(errString,se);
800 throw new LibraryException(errString,se);
801 }
802 }
803
804 if (logger.isDebugEnabled()) {
805 logger.debug( resourceBundleMessageSource.getMessage(
806 "book.subjects.update.successful",
807 new Object[] { book.toString() },
808 Locale.US));
809 }
810 }
811
812
813
814
815
816
817
818
819
820 public ArrayList<Subject> getSubjects(Book book) throws LibraryException {
821 if (logger.isDebugEnabled()) {
822 logger.debug( resourceBundleMessageSource.getMessage(
823 "retrieving.book.subjects",
824 new Object[] { book.toString() },
825 Locale.US));
826 }
827 final ArrayList<Subject> outList = new ArrayList<Subject>();
828 try {
829 List rows;
830
831 rows = jdbcTemplate.queryForList(
832 selectSubjectsByBookIdSQL,
833 new Object[] {
834 new Long(book.getId())
835 }
836 );
837
838 final Iterator<Map<String, Object>> it = rows.iterator();
839
840 while (it.hasNext()) {
841 final Map<String, Object> record = it.next();
842 final Subject su = new Subject(
843 (String) Utils.getIgnoreCase(record, "DESCRIPTION"));
844 su.setId(((Integer) Utils.getIgnoreCase(record, "ID")).longValue());
845 outList.add(su);
846 }
847
848 } catch (final Exception se) {
849 final String errString = resourceBundleMessageSource.getMessage(
850 "error.book.retrieve.sql",
851 new Object[] { book.toString() },
852 Locale.US);
853 logger.error(errString,se);
854 throw new LibraryException(errString,se);
855 }
856
857 if (logger.isDebugEnabled()) {
858 logger.debug( resourceBundleMessageSource.getMessage(
859 "book.subject.retrieve.successful",
860 new Object[] { outList },
861 Locale.US));
862 }
863
864 return outList;
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878 public void setAuthors(Book book, ArrayList<Author> authors)
879 throws LibraryException {
880 for (int i=0;i<authors.size();i++) {
881 final Author author = authors.get(i);
882 addAuthor(book,author );
883 }
884 }
885
886
887
888
889
890
891
892
893
894
895
896
897 public void setSubjects(Book book, ArrayList<Subject> subjects)
898 throws LibraryException {
899 for( int i=0; i<subjects.size(); i++) {
900 final Subject su = subjects.get(i);
901
902 addSubject(book, su);
903 }
904 }
905
906
907
908
909
910
911
912 public ArrayList dump()
913 throws LibraryException {
914 return executeQuery(selectAllBooksSQL, null);
915 }
916
917
918
919
920
921
922
923
924
925 public ArrayList titleSearch(String title)
926 throws LibraryException {
927 final Object[] criteria = new Object[] {
928 "%" +
929 StringUtils.replace(title, Constants.APOSTROPHE,
930 Constants.ESCAPED_APOSTROPHE) +
931 "%"};
932
933 return executeQuery(selectBookByTitleSQL, criteria);
934 }
935
936
937
938
939
940
941
942
943
944
945 public ArrayList ownerSearch(long owner)
946 throws LibraryException {
947 return executeQuery( selectBookByOwnerSQL,
948 new Object[] { new Long(owner) } );
949 }
950
951
952
953
954
955
956
957
958
959
960
961
962
963 public ArrayList<Book> subjectAndSearch(ArrayList subjects)
964 throws LibraryException {
965 final StringBuffer buf = new StringBuffer(selectSubjectAndSearchSelectSQL);
966
967 for (int i=0;i<subjects.size();i++) {
968 final Subject su = (Subject)subjects.get(i);
969
970 buf.append(selectSubjectAndSearchWhereSQL);
971
972 buf.append("'%");
973 buf.append(StringUtils.replace
974 (stripEndLineChar(su.getDescription()),
975 Constants.APOSTROPHE, Constants.ESCAPED_APOSTROPHE));
976 buf.append("%'))");
977
978 if (i<subjects.size()-1) {
979 buf.append(" AND ");
980 }
981 }
982
983 buf.append( selectSubjectAndSearchOrderBySQL);
984 return executeQuery(buf.toString(), null);
985 }
986
987
988
989
990
991 private String stripEndLineChar(String input) {
992 String output = input;
993
994 if (input.substring(input.length() -1).equalsIgnoreCase("\r" )) {
995 output = input.substring(0, input.length() -1);
996 }
997
998 return output;
999 }
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 public ArrayList<Book> subjectOrSearch(ArrayList<Subject> subjects)
1013 throws LibraryException {
1014
1015 final StringBuffer inBuff = new StringBuffer("");
1016
1017 for (int i=0;i<subjects.size();i++) {
1018 final Subject su = subjects.get(i);
1019
1020 inBuff.append((new Long(su.getId())).toString());
1021
1022 if (i<subjects.size()-1) {
1023 inBuff.append(",");
1024 }
1025 }
1026
1027 return executeQuery(
1028 selectSubjectOrSearchMainSQL + inBuff.toString() +
1029 selectSubjectOrSearchOrderBySQL, null);
1030 }
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041 public ArrayList<Book> authorSearch(String authorLastName)
1042 throws LibraryException {
1043 final Object[] criteria = new Object[] {
1044 StringUtils.replace(authorLastName,Constants.APOSTROPHE,
1045 Constants.ESCAPED_APOSTROPHE)};
1046
1047 return executeQuery(selectBookByAuthorSQL, criteria );
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 private ArrayList<Book> executeQuery(String query, Object[] criteria )
1061 throws LibraryException {
1062 if (logger.isDebugEnabled()) {
1063 logger.debug( resourceBundleMessageSource.getMessage(
1064 "book.search.executing",
1065 new Object[] { query },
1066 Locale.US));
1067 }
1068
1069 final ArrayList<Book> outList = new ArrayList();
1070
1071 try {
1072 List rows;
1073 if ( criteria != null ) {
1074 rows = jdbcTemplate.queryForList(query, criteria);
1075 } else {
1076 rows = jdbcTemplate.queryForList(query);
1077 }
1078
1079 final Iterator<Map<String, Object>> it = rows.iterator();
1080
1081 while (it.hasNext()) {
1082 final Map<String, Object> record = it.next();
1083 final Book bk = loadBookPropertiesfromRecord( record );
1084 outList.add(bk);
1085 }
1086 } catch (final Exception se) {
1087 final String errString = resourceBundleMessageSource.getMessage(
1088 "error.book.search.sql",
1089 new Object[] { query },
1090 Locale.US);
1091 logger.error(errString,se);
1092 throw new LibraryException(errString,se);
1093 }
1094
1095 if (logger.isDebugEnabled()) {
1096 logger.debug( resourceBundleMessageSource.getMessage(
1097 "book.search.successful",
1098 new Object[] { outList },
1099 Locale.US));
1100 }
1101 return outList;
1102 }
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112 public boolean bookExists(long book)
1113 throws LibraryException {
1114 if (retrieve( book ) != null) {
1115 return true;
1116 } else {
1117 return false;
1118 }
1119 }
1120 }