View Javadoc

1   package org.sourceforge.vlibrary.user.dao;
2   
3   
4   import java.util.ArrayList;
5   
6   import org.apache.log4j.Logger;
7   
8   import org.sourceforge.vlibrary.Constants;
9   import org.sourceforge.vlibrary.exceptions.LibraryException;
10  import org.sourceforge.vlibrary.user.domain.Author;
11  import org.sourceforge.vlibrary.util.Utils;
12  import org.apache.commons.lang.StringUtils;
13  import java.util.List;
14  import java.util.Iterator;
15  import java.util.Map;
16  import java.util.Locale;
17  
18  import java.sql.Types;
19  
20  import org.springframework.context.support.ResourceBundleMessageSource;
21  import org.springframework.jdbc.core.JdbcTemplate;
22  
23  /**
24   * Implementation of author data access interface.
25   * 
26   * @version $Revision$ $Date$
27   *
28   */
29  
30  public class AuthorDAOSpringJDBCImpl implements AuthorDAO {
31      
32      /** log4j Logger */
33      private static Logger logger =
34       Logger.getLogger(AuthorDAOSpringJDBCImpl.class.getName());
35      
36      private ResourceBundleMessageSource resourceBundleMessageSource;
37      
38      private JdbcTemplate jdbcTemplate;
39      
40      private String selectAuthorByFirstLastNameSQL;
41      
42      private String insertAuthorByFirstlastNameSQL;
43      
44      /**
45       * Used for Spring Dependency Injection
46       */
47      public void setSelectAuthorByFirstLastNameSQL(
48              String selectAuthorByFirstLastNameSQL) {
49          this.selectAuthorByFirstLastNameSQL = selectAuthorByFirstLastNameSQL;
50      }
51      
52      /**
53       * Used for Spring Dependency Injection
54       */
55      public void setInsertAuthorByFirstlastNameSQL(
56              String insertAuthorByFirstlastNameSQL) {
57          this.insertAuthorByFirstlastNameSQL = insertAuthorByFirstlastNameSQL;
58      }
59      
60      /**
61       * Used for Spring Dependency Injection
62       * @param jdbcTemplate The jdbcTemplate to set.
63       */
64      public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
65          this.jdbcTemplate = jdbcTemplate;
66      }
67      
68      /**
69       * Used for Spring Dependency Injection
70       */
71      public void setResourceBundleMessageSource(
72              ResourceBundleMessageSource resourceBundleMessageSource) {
73          this.resourceBundleMessageSource = resourceBundleMessageSource;
74      }
75      
76      public void insert(Author author) throws LibraryException {
77          if (author == null) {
78              String message = resourceBundleMessageSource.getMessage(
79               "error.inserting.author",
80               new Object[] { author==null?"":author.toString()},
81               Locale.US);
82              logger.error(message);
83              throw new LibraryException( message );
84          }
85          
86          if (logger.isDebugEnabled()) {
87              logger.debug( resourceBundleMessageSource.getMessage(
88               "inserting.author",
89               new Object[] { author.toString() },
90               Locale.US));
91          }
92          
93          Long id = findAuthorByFirstLastName(author);
94          if (id != null) { 
95              return;
96          }
97          
98          insertNewAuthor(author);
99          
100         id = findAuthorByFirstLastName(author);
101         if (id == null ) {
102             String message = resourceBundleMessageSource.getMessage(
103             "error.inserting.author",
104              new Object[] { author},
105              Locale.US);
106             logger.error(message);
107             throw new LibraryException( message );
108         }
109         
110         if (logger.isDebugEnabled()) {
111             logger.debug( resourceBundleMessageSource.getMessage(
112             "author.insert.successful",
113              new Object[] { author},
114              Locale.US));
115         }
116     }
117     
118     public Long findAuthorByFirstLastName(Author author)
119     throws LibraryException {
120         if ( author == null ) {
121             String message = resourceBundleMessageSource.getMessage(
122             "error.retrieving.author",
123              new Object[] { ""},
124              Locale.US);
125             logger.error(message);
126             throw new LibraryException( message );
127         }
128                
129         if (logger.isDebugEnabled()) {
130             logger.debug( resourceBundleMessageSource.getMessage(
131             "retrieving.author",
132              new Object[] { author.toString() },
133              Locale.US));  
134         }
135   
136         Long id = null;
137         List rows;
138         try {
139             rows = jdbcTemplate.queryForList(
140             selectAuthorByFirstLastNameSQL,
141              new Object[] {
142                 StringUtils.replace(author.getFirstName().trim(),
143                  Constants.APOSTROPHE,
144                  Constants.ESCAPED_APOSTROPHE
145                  ) 
146                  ,
147                 StringUtils.replace(author.getLastName().trim(),
148                  Constants.APOSTROPHE,
149                  Constants.ESCAPED_APOSTROPHE
150                  )
151               }
152             );
153         } catch (Exception se) {
154             String errString = resourceBundleMessageSource.getMessage(
155              "error.retrieving.author",
156              new Object[] { author.toString() },
157              Locale.US);
158             logger.error(errString,se);
159             throw new LibraryException(errString,se);
160         }
161        
162         Iterator it = rows.iterator();
163         if (it.hasNext()) {
164             Map record = (Map)it.next();
165             id = new Long(((Integer)
166                     Utils.getIgnoreCase(record, "ID")).longValue());
167             author.setId(id);
168         }
169         
170         if (logger.isDebugEnabled() && id != null) {
171             logger.debug( resourceBundleMessageSource.getMessage(
172             "author.retrieve.successful",
173              new Object[] { author},
174              Locale.US));
175         }
176    
177         return id;
178     }
179     
180     /**
181      * Insert a new Author into the DB.
182      *
183      * @param author author To be inserted in the DB
184      * @throws LibraryException
185      */
186     public void insertNewAuthor(Author author) throws LibraryException {
187         if (author == null) {
188             String message = resourceBundleMessageSource.getMessage(
189                     "error.inserting.new.author",
190              new Object[] {""},
191              Locale.US);
192             logger.error(message);
193             throw new LibraryException(message);
194         }
195         
196         if (logger.isDebugEnabled()) {
197             logger.debug( resourceBundleMessageSource.getMessage(
198                     "inserting.new.author",
199              new Object[] { author.toString() },
200              Locale.US));   
201         }
202         
203         try {
204             author.setFirstName(author.getFirstName().trim());
205             author.setLastName(author.getLastName().trim());
206             
207             jdbcTemplate.update(insertAuthorByFirstlastNameSQL,
208              new Object[] {
209                 StringUtils.replace(  author.getFirstName(),
210                  Constants.APOSTROPHE,
211                  Constants.ESCAPED_APOSTROPHE
212                  )
213                  ,
214                 StringUtils.replace(  author.getLastName(),
215                  Constants.APOSTROPHE,
216                  Constants.ESCAPED_APOSTROPHE
217                  )
218              },
219              new int[] {Types.VARCHAR, Types.VARCHAR}        
220             );
221         } catch (Exception se) {
222             String errString = resourceBundleMessageSource.getMessage(
223             "error.inserting.new.author",
224              new Object[] { author.toString() },
225              Locale.US);
226             logger.error(errString,se);
227             throw new LibraryException(errString,se);
228         }
229         
230         if (logger.isDebugEnabled()) {
231             logger.debug( resourceBundleMessageSource.getMessage(
232              "new.author.insert.successful",
233              new Object[] { author},
234              Locale.US));
235         }
236     }
237     
238     public void setAuthorIds(ArrayList authors)
239     throws LibraryException {
240         if (authors == null) {
241             return;
242         }
243         
244         Author curAuth = null;
245         for (int i=0;i<authors.size();i++) {
246             Author au = new Author();
247             curAuth = (Author)authors.get(i);
248             au.setFirstName(curAuth.getFirstName());
249             au.setLastName(curAuth.getLastName());
250             insert( au ); //Will just update id if author already exists
251             curAuth.setId(au.getId());
252             au = null;
253         }
254     }
255     
256     public AuthorDAOSpringJDBCImpl() {
257     }
258 }