1 | package fr.mael.jiwigo.dao; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.security.NoSuchAlgorithmException; |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.List; |
---|
9 | |
---|
10 | import org.w3c.dom.Document; |
---|
11 | import org.w3c.dom.Element; |
---|
12 | import org.w3c.dom.Node; |
---|
13 | import org.w3c.dom.NodeList; |
---|
14 | |
---|
15 | import sun.misc.BASE64Encoder; |
---|
16 | import fr.mael.jiwigo.om.Image; |
---|
17 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
18 | import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; |
---|
19 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
20 | import fr.mael.jiwigo.transverse.exception.WrongChunkSizeException; |
---|
21 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
22 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
23 | |
---|
24 | /* |
---|
25 | * jiwigo-ws-api Piwigo webservice access Api |
---|
26 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
27 | * All Rights Reserved |
---|
28 | * |
---|
29 | * This library is free software. It comes without any warranty, to |
---|
30 | * the extent permitted by applicable law. You can redistribute it |
---|
31 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
32 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
33 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
34 | */ |
---|
35 | /** |
---|
36 | |
---|
37 | * Dao of the images |
---|
38 | * @author mael |
---|
39 | * |
---|
40 | */ |
---|
41 | public class ImageDao { |
---|
42 | |
---|
43 | /** |
---|
44 | * Logger |
---|
45 | */ |
---|
46 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
47 | .getLog(ImageDao.class); |
---|
48 | |
---|
49 | /** |
---|
50 | * Singleton |
---|
51 | */ |
---|
52 | private static ImageDao instance; |
---|
53 | |
---|
54 | /** |
---|
55 | * cache to avoid downloading image for each access |
---|
56 | */ |
---|
57 | private HashMap<Integer, List<Image>> cache; |
---|
58 | |
---|
59 | /** |
---|
60 | * |
---|
61 | */ |
---|
62 | private Integer firstCatInCache; |
---|
63 | |
---|
64 | private SessionManager sessionManager; |
---|
65 | |
---|
66 | private ArrayList<File> filesToSend; |
---|
67 | |
---|
68 | /** |
---|
69 | * Private singleton, to use a singleton |
---|
70 | */ |
---|
71 | private ImageDao(SessionManager sessionManager) { |
---|
72 | cache = new HashMap<Integer, List<Image>>(); |
---|
73 | this.sessionManager = sessionManager; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * @return le singleton |
---|
78 | */ |
---|
79 | public static ImageDao getInstance(SessionManager sessionManager) { |
---|
80 | if (instance == null) { |
---|
81 | instance = new ImageDao(sessionManager); |
---|
82 | } |
---|
83 | return instance; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Lists all images |
---|
88 | * @return the list of images |
---|
89 | * @throws IOException |
---|
90 | * @throws ProxyAuthenticationException |
---|
91 | */ |
---|
92 | public List<Image> list(boolean refresh) throws IOException, ProxyAuthenticationException { |
---|
93 | return listByCategory(null, refresh); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Listing of the images for a category |
---|
98 | * @param categoryId the id of the category |
---|
99 | * @return the list of images |
---|
100 | * @throws IOException |
---|
101 | * @throws ProxyAuthenticationException |
---|
102 | */ |
---|
103 | public List<Image> listByCategory(Integer categoryId, boolean refresh) throws IOException, |
---|
104 | ProxyAuthenticationException { |
---|
105 | if (refresh || cache.get(categoryId) == null) { |
---|
106 | Document doc = null; |
---|
107 | if (categoryId != null) { |
---|
108 | doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String |
---|
109 | .valueOf(categoryId)); |
---|
110 | } else { |
---|
111 | doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); |
---|
112 | } |
---|
113 | Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); |
---|
114 | List<Image> images = getImagesFromElement(element); |
---|
115 | cache.remove(categoryId); |
---|
116 | cache.put(categoryId, images); |
---|
117 | if (firstCatInCache == null) { |
---|
118 | firstCatInCache = categoryId; |
---|
119 | } |
---|
120 | return images; |
---|
121 | } else { |
---|
122 | return cache.get(categoryId); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | * Creation of an image<br/> |
---|
128 | * Sequence : <br/> |
---|
129 | * <li> |
---|
130 | * <ul>sending of the thumbnail in base64, thanks to the method addchunk.</ul> |
---|
131 | * <ul>sending of the image in base64, thanks to the method addchunk</ul> |
---|
132 | * <ul>using of the add method to add the image to the database<ul> |
---|
133 | * </li> |
---|
134 | * Finally, the response of the webservice is checked |
---|
135 | * |
---|
136 | * @param image the image to create |
---|
137 | * @return true if the creation of the image was the successful |
---|
138 | * @throws IOException |
---|
139 | * @throws NoSuchAlgorithmException |
---|
140 | * @throws WrongChunkSizeException |
---|
141 | * @throws Exception |
---|
142 | */ |
---|
143 | //TODO ne pas continuer si une des reponses precedentes est negative |
---|
144 | public boolean create(Image image, Double chunkSize) throws FileAlreadyExistsException, IOException, |
---|
145 | ProxyAuthenticationException, NoSuchAlgorithmException, WrongChunkSizeException { |
---|
146 | //thumbnail converted to base64 |
---|
147 | BASE64Encoder base64 = new BASE64Encoder(); |
---|
148 | |
---|
149 | String thumbnailBase64 = base64.encode(Tools.getBytesFromFile(image.getThumbnail())); |
---|
150 | //sends the thumbnail and gets the result |
---|
151 | Document reponseThumb = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", thumbnailBase64, |
---|
152 | "type", "thumb", "position", "1", "original_sum", Tools.getMD5Checksum(image.getOriginale() |
---|
153 | .getAbsolutePath()))); |
---|
154 | |
---|
155 | //begin feature:0001827 |
---|
156 | int chunk = chunkSize.intValue(); |
---|
157 | if (chunk == 0) { |
---|
158 | throw new WrongChunkSizeException("Error : the chunk size cannot be 0"); |
---|
159 | } |
---|
160 | filesToSend = Tools.splitFile(image.getOriginale(), chunk); |
---|
161 | boolean echec = false; |
---|
162 | for (int i = 0; i < filesToSend.size(); i++) { |
---|
163 | File fichierAEnvoyer = filesToSend.get(i); |
---|
164 | String originaleBase64 = base64.encode(Tools.getBytesFromFile(fichierAEnvoyer)); |
---|
165 | Document reponseOriginale = (sessionManager.executeReturnDocument("pwg.images.addChunk", "data", |
---|
166 | originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Tools |
---|
167 | .getMD5Checksum(image.getOriginale().getAbsolutePath()))); |
---|
168 | if (!Tools.checkOk(reponseOriginale)) { |
---|
169 | echec = true; |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | //end |
---|
174 | |
---|
175 | //add the image in the database and get the result of the webservice |
---|
176 | Document reponseAjout = (sessionManager.executeReturnDocument("pwg.images.add", "file_sum", Tools |
---|
177 | .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Tools.getMD5Checksum(image |
---|
178 | .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Tools.getMD5Checksum(image |
---|
179 | .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image |
---|
180 | .getName(), "author", sessionManager.getLogin(), "level", image.getPrivacyLevel())); |
---|
181 | LOG.debug("Response add : " + Tools.documentToString(reponseAjout)); |
---|
182 | // System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil |
---|
183 | // .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Outil.getMD5Checksum(image |
---|
184 | // .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Outil.getMD5Checksum(image |
---|
185 | // .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image |
---|
186 | // .getName(), "author", Main.sessionManager.getLogin())); |
---|
187 | // Document reponsePrivacy = null; |
---|
188 | // if (Outil.checkOk(reponseAjout)) { |
---|
189 | // reponsePrivacy = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_PRIVACY_LEVEL.getLabel()); |
---|
190 | // } |
---|
191 | boolean reussite = true; |
---|
192 | if (!Tools.checkOk(reponseThumb) || echec || !Tools.checkOk(reponseAjout)) { |
---|
193 | reussite = false; |
---|
194 | } |
---|
195 | deleteTempFiles(); |
---|
196 | return reussite; |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Add tags to an image |
---|
202 | * @param imageId id of the image |
---|
203 | * @param tagId ids of the tags |
---|
204 | * @throws IOException |
---|
205 | * @throws ProxyAuthenticationException |
---|
206 | */ |
---|
207 | public boolean addTags(Integer imageId, String tagId) throws IOException, ProxyAuthenticationException { |
---|
208 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String |
---|
209 | .valueOf(imageId), "tag_ids", tagId); |
---|
210 | try { |
---|
211 | return Tools.checkOk(doc); |
---|
212 | } catch (FileAlreadyExistsException e) { |
---|
213 | LOG.error(Tools.getStackTrace(e)); |
---|
214 | return false; |
---|
215 | } |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * parse an element to find images |
---|
221 | * @param element the element to parse |
---|
222 | * @return the list of images |
---|
223 | */ |
---|
224 | private List<Image> getImagesFromElement(Element element) { |
---|
225 | // List<Element> listElement = (List<Element>) element.getChildren("image"); |
---|
226 | NodeList listImages = element.getElementsByTagName("image"); |
---|
227 | ArrayList<Image> images = new ArrayList<Image>(); |
---|
228 | for (int i = 0; i < listImages.getLength(); i++) { |
---|
229 | Node nodeImage = listImages.item(i); |
---|
230 | if (nodeImage.getNodeType() == Node.ELEMENT_NODE) { |
---|
231 | Element im = (Element) nodeImage; |
---|
232 | Image myImage = new Image(); |
---|
233 | myImage.setThumbnailUrl(im.getAttribute("tn_url")); |
---|
234 | myImage.setUrl(im.getAttribute("element_url")); |
---|
235 | myImage.setWidth(Integer.valueOf(im.getAttribute("width"))); |
---|
236 | myImage.setHeight(Integer.valueOf(im.getAttribute("height"))); |
---|
237 | myImage.setFile(im.getAttribute("file")); |
---|
238 | myImage.setSeen(Integer.valueOf(im.getAttribute("hit"))); |
---|
239 | myImage.setIdentifier(Integer.valueOf(im.getAttribute("id"))); |
---|
240 | myImage.setName(Tools.getStringValueDom(im, "name")); |
---|
241 | Element elementCategories = (Element) im.getElementsByTagName("categories").item(0); |
---|
242 | Element elementCategory = (Element) elementCategories.getElementsByTagName("category").item(0); |
---|
243 | myImage.setIdCategory(Integer.valueOf(elementCategory.getAttribute("id"))); |
---|
244 | if (myImage.getName() == null) { |
---|
245 | myImage.setName(myImage.getFile()); |
---|
246 | } |
---|
247 | images.add(myImage); |
---|
248 | } |
---|
249 | } |
---|
250 | return images; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * Search images |
---|
255 | * @param searchString the string to search |
---|
256 | * @return the list of images matching the string |
---|
257 | * @throws IOException |
---|
258 | * @throws ProxyAuthenticationException |
---|
259 | */ |
---|
260 | public List<Image> search(String searchString) throws IOException, ProxyAuthenticationException { |
---|
261 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); |
---|
262 | LOG.debug(doc); |
---|
263 | Element element = (Element) doc.getDocumentElement().getElementsByTagName("images").item(0); |
---|
264 | return getImagesFromElement(element); |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | private void deleteTempFiles() { |
---|
269 | File file = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); |
---|
270 | file.delete(); |
---|
271 | file = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); |
---|
272 | file.delete(); |
---|
273 | for (File tempCut : filesToSend) { |
---|
274 | tempCut.delete(); |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | |
---|
279 | } |
---|