1 | package org.medias.eccad.persistance.jdbc; |
---|
2 | |
---|
3 | import java.sql.Connection; |
---|
4 | import java.sql.ResultSet; |
---|
5 | import java.sql.SQLException; |
---|
6 | import java.sql.Statement; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | |
---|
11 | import org.medias.eccad.helpers.CharactereHelper; |
---|
12 | import org.medias.eccad.helpers.LoggerPerso; |
---|
13 | import org.medias.eccad.modele.Produit; |
---|
14 | import org.medias.eccad.persistance.jdbc.SGBD_jdbc; |
---|
15 | import org.medias.eccad.persistance.dao.ProduitDAO; |
---|
16 | import org.medias.eccad.persistance.exception.PersistanceException; |
---|
17 | |
---|
18 | public class ProduitDAOjdbc extends GeneriqueDAOjdbc implements ProduitDAO{ |
---|
19 | |
---|
20 | public ProduitDAOjdbc(SGBD_jdbc sgbd) { |
---|
21 | super(sgbd); |
---|
22 | } |
---|
23 | |
---|
24 | public ProduitDAOjdbc() { |
---|
25 | super(); |
---|
26 | } |
---|
27 | |
---|
28 | public List<Produit> getListProductByParam(long id_param) throws PersistanceException { |
---|
29 | Statement requete; |
---|
30 | String sql; |
---|
31 | List<Produit> liste_param = new LinkedList<Produit>(); |
---|
32 | Connection conn = getConnection(); |
---|
33 | |
---|
34 | try { |
---|
35 | requete = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
---|
36 | sql = "select distinct id_produit, title_produit, begintime_produit, endtime_produit, header_produit from produit join grille using (id_produit) where id_param = " + id_param + ";"; |
---|
37 | |
---|
38 | ResultSet resultat = requete.executeQuery(sql); |
---|
39 | |
---|
40 | while (resultat.next()) { |
---|
41 | liste_param.add(new Produit(resultat.getLong("id_produit"), |
---|
42 | CharactereHelper.getEncodage(resultat.getString("title_produit"), "ISO-8859-1"), |
---|
43 | resultat.getDate("begintime_produit"), |
---|
44 | resultat.getDate("endtime_produit"), |
---|
45 | CharactereHelper.getEncodage(resultat.getString("header_produit"), "ISO-8859-1"))); |
---|
46 | } |
---|
47 | |
---|
48 | } |
---|
49 | catch (SQLException sqle) { |
---|
50 | throw new PersistanceException(sqle, "getListProductByParam"); |
---|
51 | } |
---|
52 | |
---|
53 | finally { |
---|
54 | closeConnection(); |
---|
55 | } |
---|
56 | |
---|
57 | LoggerPerso.log(SGBD_jdbc.class, LoggerPerso.DEBUG, "getListProduit::taille::"+liste_param.size()+"\nSQL::" + sql); |
---|
58 | return liste_param; |
---|
59 | } |
---|
60 | |
---|
61 | public Produit getProduit(String sql, Connection conn) throws SQLException { |
---|
62 | Statement requete; |
---|
63 | Produit produit = null; |
---|
64 | |
---|
65 | requete = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
---|
66 | |
---|
67 | ResultSet resultat = requete.executeQuery(sql); |
---|
68 | |
---|
69 | if (resultat.next()) { |
---|
70 | produit = new Produit(resultat.getInt("id_produit"), |
---|
71 | resultat.getString("title_produit"), |
---|
72 | resultat.getDate("begintime_produit"), |
---|
73 | resultat.getDate("endtime_produit")); |
---|
74 | produit.setHeader(CharactereHelper.getEncodage(resultat.getString("header_produit"), "ISO-8859-1")); |
---|
75 | produit.setTemporalres(CharactereHelper.getEncodage(resultat.getString("temporalRes_produit"), "ISO-8859-1")); |
---|
76 | //produit.setType(CharactereHelper.getEncodage(resultat.getString("name_typeproduit"), "ISO-8859-1")); |
---|
77 | } |
---|
78 | |
---|
79 | return produit; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | public Produit getProduitById(long id_produit) throws PersistanceException { |
---|
84 | Statement requete; |
---|
85 | String sql; |
---|
86 | Produit produit = null; |
---|
87 | Connection conn = getConnection(); |
---|
88 | |
---|
89 | try { |
---|
90 | requete = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
---|
91 | sql = "select distinct id_produit, title_produit, begintime_produit, endtime_produit, header_produit, temporalRes_produit, name_typeproduit from produit join typeproduit using (id_typeproduit) where id_produit = " + id_produit + ";"; |
---|
92 | |
---|
93 | ResultSet resultat = requete.executeQuery(sql); |
---|
94 | |
---|
95 | if (resultat.next()) { |
---|
96 | produit = new Produit(resultat.getInt("id_produit"), |
---|
97 | resultat.getString("title_produit"), |
---|
98 | resultat.getDate("begintime_produit"), |
---|
99 | resultat.getDate("endtime_produit")); |
---|
100 | produit.setHeader(CharactereHelper.getEncodage(resultat.getString("header_produit"), "ISO-8859-1")); |
---|
101 | produit.setTemporalres(CharactereHelper.getEncodage(resultat.getString("temporalRes_produit"), "ISO-8859-1")); |
---|
102 | produit.setType(CharactereHelper.getEncodage(resultat.getString("name_typeproduit"), "ISO-8859-1")); |
---|
103 | } |
---|
104 | } |
---|
105 | catch (SQLException sqle) { |
---|
106 | throw new PersistanceException(sqle, "getListProductByParam"); |
---|
107 | } |
---|
108 | finally { |
---|
109 | closeConnection(); |
---|
110 | } |
---|
111 | LoggerPerso.log(SGBD_jdbc.class, LoggerPerso.DEBUG, "getProduitById::nom::"+ produit.getNom() + "::date::" + produit.getBeginDate()); |
---|
112 | |
---|
113 | return produit; |
---|
114 | } |
---|
115 | |
---|
116 | public List<Produit> getProduitByLink(Long id_produit, Long id_facteur) throws PersistanceException { |
---|
117 | Statement requete; |
---|
118 | String sql; |
---|
119 | List<Produit> liste_produit = new LinkedList<Produit>(); |
---|
120 | Connection conn = getConnection(); |
---|
121 | |
---|
122 | try { |
---|
123 | requete = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
---|
124 | sql = "select distinct id_produit, title_produit, begintime_produit, endtime_produit, header_produit from produit join lienproduit using (id_produit) "; |
---|
125 | |
---|
126 | if (id_produit != null) { |
---|
127 | sql += " where id_produit = " + id_produit +";"; |
---|
128 | } |
---|
129 | else if (id_facteur != null) { |
---|
130 | sql += " where id_facteur = " + id_facteur +";"; |
---|
131 | } |
---|
132 | |
---|
133 | ResultSet resultat = requete.executeQuery(sql); |
---|
134 | |
---|
135 | while (resultat.next()) { |
---|
136 | liste_produit.add(new Produit(resultat.getLong("id_produit"), |
---|
137 | CharactereHelper.getEncodage(resultat.getString("title_produit"), "ISO-8859-1"), |
---|
138 | resultat.getDate("begintime_produit"), |
---|
139 | resultat.getDate("endtime_produit"), |
---|
140 | CharactereHelper.getEncodage(resultat.getString("header_produit"), "ISO-8859-1"))); |
---|
141 | } |
---|
142 | } |
---|
143 | catch (SQLException sqle) { |
---|
144 | throw new PersistanceException(sqle, "getListProductByLink"); |
---|
145 | } |
---|
146 | |
---|
147 | finally { |
---|
148 | closeConnection(); |
---|
149 | } |
---|
150 | |
---|
151 | LoggerPerso.log(SGBD_jdbc.class, LoggerPerso.DEBUG, "getListProductByLink::taille::"+liste_produit.size()); |
---|
152 | return liste_produit; |
---|
153 | } |
---|
154 | |
---|
155 | public Produit getProduitByName(String name) throws PersistanceException { |
---|
156 | Produit produit = null; |
---|
157 | Connection conn = getConnection(); |
---|
158 | String sql = "select distinct * from produit where title_produit ilike '" + name + "';"; |
---|
159 | |
---|
160 | try { |
---|
161 | produit = getProduit(sql, conn); |
---|
162 | } catch (SQLException e) { |
---|
163 | throw new PersistanceException(e, "getProduitByName " + sql ); |
---|
164 | } |
---|
165 | finally { |
---|
166 | closeConnection(); |
---|
167 | } |
---|
168 | |
---|
169 | return produit; |
---|
170 | } |
---|
171 | |
---|
172 | } |
---|