View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   *
25   */
26  package com.gisgraphy.domain.repository;
27  
28  import javax.persistence.PersistenceException;
29  
30  import org.hibernate.Query;
31  import org.hibernate.Session;
32  import org.springframework.orm.hibernate3.HibernateCallback;
33  import org.springframework.stereotype.Repository;
34  
35  import com.gisgraphy.domain.geoloc.entity.Language;
36  
37  /**
38   * Interface of data access object for {@link Language}
39   * 
40   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
41   */
42  @Repository
43  public class LanguageDao extends GenericDao<Language, Long> implements
44  	ILanguageDao {
45  
46      /**
47       * Default constructore
48       */
49      public LanguageDao() {
50  	super(Language.class);
51  
52      }
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see com.gisgraphy.domain.repository.ILanguageDao#findByIso639Alpha2Code(java.lang.String)
58       */
59      public Language getByIso639Alpha2Code(final String iso639Alpha2LanguageCode) {
60  	return (Language) this.getHibernateTemplate().execute(
61  		new HibernateCallback() {
62  
63  		    public Object doInHibernate(Session session)
64  			    throws PersistenceException {
65  			String queryString = "from "
66  				+ Language.class.getSimpleName()
67  				+ " as l where Iso639Alpha2LanguageCode= ?";
68  
69  			Query qry = session.createQuery(queryString);
70  			qry.setCacheable(true);
71  
72  			qry.setParameter(0, iso639Alpha2LanguageCode
73  				.toUpperCase());
74  			Language result = (Language) qry.uniqueResult();
75  
76  			return result;
77  		    }
78  		});
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see com.gisgraphy.domain.repository.ILanguageDao#findByIso639Alpha3Code(java.lang.String)
85       */
86      public Language getByIso639Alpha3Code(final String iso639Alpha3Code) {
87  	return (Language) this.getHibernateTemplate().execute(
88  		new HibernateCallback() {
89  
90  		    public Object doInHibernate(Session session)
91  			    throws PersistenceException {
92  			String queryString = "from "
93  				+ Language.class.getSimpleName()
94  				+ " as l where Iso639Alpha3LanguageCode= ?";
95  
96  			Query qry = session.createQuery(queryString);
97  			qry.setCacheable(true);
98  
99  			qry.setParameter(0, iso639Alpha3Code.toUpperCase());
100 			Language result = (Language) qry.uniqueResult();
101 
102 			return result;
103 		    }
104 		});
105     }
106 
107     /*
108      * (non-Javadoc)
109      * 
110      * @see com.gisgraphy.domain.repository.ILanguageDao#getByIso639Code(java.lang.String)
111      */
112     public Language getByIso639Code(String iso639LanguageCode) {
113 	if (iso639LanguageCode == null) {
114 	    throw new IllegalArgumentException(
115 		    "can not retrieve language with iso639Code="
116 			    + iso639LanguageCode);
117 	} else if (iso639LanguageCode.length() == 2) {
118 	    return getByIso639Alpha2Code(iso639LanguageCode);
119 	} else if (iso639LanguageCode.length() == 3) {
120 	    return getByIso639Alpha3Code(iso639LanguageCode);
121 	} else {
122 	    throw new IllegalArgumentException(
123 		    "can not retrieve language with iso639Code="
124 			    + iso639LanguageCode
125 			    + " : iso639LanguageCode must have a lenght of 2 or 3");
126 	}
127     }
128 
129 }