- Timestamp:
- Oct 16, 2010, 1:08:28 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/dao/ImageDao.java
r7070 r7221 6 6 import java.util.HashMap; 7 7 import java.util.List; 8 9 8 import org.jdom.Document; 10 9 import org.jdom.Element; 11 12 10 import sun.misc.BASE64Encoder; 13 11 import fr.mael.jiwigo.Main; … … 18 16 import fr.mael.jiwigo.transverse.util.Outil; 19 17 import fr.mael.jiwigo.transverse.util.preferences.PreferencesManagement; 18 20 19 21 20 /** … … 50 49 */ 51 50 public class ImageDao { 52 /** 53 * Logger 54 */ 55 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 56 .getLog(ImageDao.class); 57 58 /** 59 * Singleton 60 */ 61 private static ImageDao instance; 62 63 /** 64 * cache to avoid downloading image for each access 65 */ 66 private HashMap<Integer, List<Image>> cache; 67 68 /** 69 * 70 */ 71 private Integer firstCatInCache; 72 73 /** 74 * Private singleton, to use a singleton 75 */ 76 private ImageDao() { 77 cache = new HashMap<Integer, List<Image>>(); 78 } 79 80 /** 81 * @return le singleton 82 */ 83 public static ImageDao getInstance() { 84 if (instance == null) { 85 instance = new ImageDao(); 86 } 87 return instance; 88 } 89 90 /** 91 * Lists all images 92 * @return the list of images 93 * @throws IOException 94 */ 95 public List<Image> lister(boolean rafraichir) throws IOException { 96 return listerParCategory(null, rafraichir); 97 } 98 99 /** 100 * Listing of the images for a category 101 * @param categoryId the id of the category 102 * @return the list of images 103 * @throws IOException 104 */ 105 public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException { 106 if (rafraichir || cache.get(categoryId) == null) { 107 Document doc = null; 108 if (categoryId != null) { 109 doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", String 110 .valueOf(categoryId)); 111 } else { 112 doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); 113 } 114 Element element = doc.getRootElement().getChild("images"); 115 List<Image> images = getImagesFromElement(element); 116 cache.remove(categoryId); 117 cache.put(categoryId, images); 118 if (firstCatInCache == null) { 119 firstCatInCache = categoryId; 120 } 121 return images; 122 } else { 123 return cache.get(categoryId); 124 } 125 } 126 127 /** 128 * Creation of an image<br/> 129 * Sequence : <br/> 130 * <li> 131 * <ul>sending of the thumbnail in base64, thanks to the method addchunk.</ul> 132 * <ul>sending of the image in base64, thanks to the method addchunk</ul> 133 * <ul>using of the add method to add the image to the database<ul> 134 * </li> 135 * Finally, the response of the webservice is checked 136 * 137 * @param the image to create 138 * @return true if the creation of the image was the successful 139 * @throws Exception 140 */ 141 //TODO ne pas continuer si une des réponses précédentes est négative 142 public boolean creer(Image image) throws Exception { 143 //thumbnail converted to base64 144 BASE64Encoder base64 = new BASE64Encoder(); 145 146 String thumbnailBase64 = base64.encode(Outil.getBytesFromFile(image.getThumbnail())); 147 //sends the thumbnail and gets the result 148 Document reponseThumb = (Main.sessionManager.executerReturnDocument("pwg.images.addChunk", "data", 149 thumbnailBase64, "type", "thumb", "position", "1", "original_sum", Outil.getMD5Checksum(image 150 .getOriginale().getAbsolutePath()))); 151 152 //begin feature:0001827 153 Double doubleChunk = Double.parseDouble(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE.getLabel())) * 1000 * 1024; 154 int chunk = doubleChunk.intValue(); 155 ArrayList<File> fichiersAEnvoyer = Outil.splitFile(image.getOriginale(), chunk); 156 boolean echec = false; 157 for (int i = 0; i < fichiersAEnvoyer.size(); i++) { 158 File fichierAEnvoyer = fichiersAEnvoyer.get(i); 159 String originaleBase64 = base64.encode(Outil.getBytesFromFile(fichierAEnvoyer)); 160 Document reponseOriginale = (Main.sessionManager.executerReturnDocument("pwg.images.addChunk", "data", 161 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", Outil 162 .getMD5Checksum(image.getOriginale().getAbsolutePath()))); 163 if (!Outil.checkOk(reponseOriginale)) { 164 echec = true; 165 break; 166 } 167 } 168 //end 169 170 //add the image in the database and get the result of the webservice 171 Document reponseAjout = (Main.sessionManager.executerReturnDocument("pwg.images.add", "file_sum", Outil 172 .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Outil.getMD5Checksum(image 173 .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Outil.getMD5Checksum(image 174 .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 175 .getName(), "author", Main.sessionManager.getLogin(), "level", String 176 .valueOf(ImagesManagement.privacyLevel))); 177 LOG.debug("Response add : " + Outil.documentToString(reponseAjout)); 178 // System.out.println(Main.sessionManager.executerReturnString("pwg.images.add", "file_sum", Outil 179 // .getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", Outil.getMD5Checksum(image 180 // .getThumbnail().getCanonicalPath()), "position", "1", "original_sum", Outil.getMD5Checksum(image 181 // .getOriginale().getAbsolutePath()), "categories", String.valueOf(image.getIdCategory()), "name", image 182 // .getName(), "author", Main.sessionManager.getLogin())); 183 // Document reponsePrivacy = null; 184 // if (Outil.checkOk(reponseAjout)) { 185 // reponsePrivacy = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_PRIVACY_LEVEL.getLabel()); 186 // } 187 boolean reussite = true; 188 if (!Outil.checkOk(reponseThumb) || echec || !Outil.checkOk(reponseAjout)) { 189 reussite = false; 190 } 191 suppressionFichierTemporaires(); 192 return reussite; 193 194 } 195 196 /** 197 * Add tags to an image 198 * @param imageId id of the image 199 * @param tagId ids of the tags 200 * @throws IOException 201 */ 202 public boolean addTags(Integer imageId, String tagId) throws IOException { 203 Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", String 204 .valueOf(imageId), "tag_ids", tagId); 205 return Outil.checkOk(doc); 206 207 } 208 209 /** 210 * parse an element to find images 211 * @param element the element to parse 212 * @return the list of images 213 */ 214 private List<Image> getImagesFromElement(Element element) { 215 List<Element> listElement = (List<Element>) element.getChildren("image"); 216 ArrayList<Image> images = new ArrayList<Image>(); 217 for (Element im : listElement) { 218 Image myImage = new Image(); 219 myImage.setMiniature(im.getAttributeValue("tn_url")); 220 myImage.setUrl(im.getAttributeValue("element_url")); 221 myImage.setWidth(Integer.valueOf(im.getAttributeValue("width"))); 222 myImage.setHeight(Integer.valueOf(im.getAttributeValue("height"))); 223 myImage.setFile(im.getAttributeValue("file")); 224 myImage.setVue(Integer.valueOf(im.getAttributeValue("hit"))); 225 myImage.setIdentifiant(Integer.valueOf(im.getAttributeValue("id"))); 226 myImage.setName(im.getChildText("name")); 227 if (myImage.getName() == null) { 228 myImage.setName(myImage.getFile()); 229 } 230 images.add(myImage); 231 232 } 233 return images; 234 } 235 236 /** 237 * Search images 238 * @param searchString the string to search 239 * @return the list of images matching the string 240 * @throws IOException 241 */ 242 public List<Image> search(String searchString) throws IOException { 243 Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); 244 LOG.debug(doc); 245 Element element = doc.getRootElement().getChild("images"); 246 return getImagesFromElement(element); 247 248 } 249 250 private void suppressionFichierTemporaires() { 251 File file = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 252 file.delete(); 253 file = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 254 file.delete(); 255 256 } 51 52 /** 53 * Logger 54 */ 55 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(ImageDao.class); 56 57 /** 58 * Singleton 59 */ 60 private static ImageDao instance; 61 62 /** 63 * cache to avoid downloading image for each access 64 */ 65 private HashMap<Integer, List<Image>> cache; 66 67 /** 68 * 69 */ 70 private Integer firstCatInCache; 71 72 73 /** 74 * Private singleton, to use a singleton 75 */ 76 private ImageDao() { 77 cache = new HashMap<Integer, List<Image>>(); 78 } 79 80 81 /** 82 * @return le singleton 83 */ 84 public static ImageDao getInstance() { 85 if (instance == null) { 86 instance = new ImageDao(); 87 } 88 return instance; 89 } 90 91 92 /** 93 * Lists all images 94 * @return the list of images 95 * @throws IOException 96 */ 97 public List<Image> lister(boolean rafraichir) throws IOException { 98 return listerParCategory(null, rafraichir); 99 } 100 101 102 /** 103 * Listing of the images for a category 104 * @param categoryId the id of the category 105 * @return the list of images 106 * @throws IOException 107 */ 108 public List<Image> listerParCategory(Integer categoryId, boolean rafraichir) throws IOException { 109 if (rafraichir || cache.get(categoryId) == null) { 110 Document doc = null; 111 112 if (categoryId != null) { 113 doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel(), "cat_id", 114 String.valueOf(categoryId)); 115 } else { 116 doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_IMAGES.getLabel()); 117 } 118 Element element = doc.getRootElement().getChild("images"); 119 List<Image> images = getImagesFromElement(element); 120 cache.remove(categoryId); 121 cache.put(categoryId, images); 122 if (firstCatInCache == null) { 123 firstCatInCache = categoryId; 124 } 125 return images; 126 } else { 127 return cache.get(categoryId); 128 } 129 } 130 131 132 /** 133 * Creation of an image<br/> 134 * Sequence : <br/> 135 * <li> 136 * <ul>sending of the thumbnail in base64, thanks to the method addchunk.</ul> 137 * <ul>sending of the image in base64, thanks to the method addchunk</ul> 138 * <ul>using of the add method to add the image to the database<ul> 139 * </li> 140 * Finally, the response of the webservice is checked 141 * 142 * @param the image to create 143 * @return true if the creation of the image was the successful 144 * @throws Exception 145 */ 146 //TODO ne pas continuer si une des réponses précédentes est négative 147 public boolean creer(Image image) throws Exception { 148 //thumbnail converted to base64 149 BASE64Encoder base64 = new BASE64Encoder(); 150 151 String thumbnailBase64 = base64.encode(Outil.getBytesFromFile(image.getThumbnail())); 152 //sends the thumbnail and gets the result 153 Document reponseThumb = (Main.sessionManager.executerReturnDocument("pwg.images.addChunk", "data", thumbnailBase64, 154 "type", "thumb", "position", "1", "original_sum", Outil.getMD5Checksum(image.getOriginale().getAbsolutePath()))); 155 156 //begin feature:0001827 157 Double doubleChunk = Double.parseDouble(PreferencesManagement.getValue(PreferencesEnum.CHUNK_SIZE.getLabel())) * 1000 * 1024; 158 int chunk = doubleChunk.intValue(); 159 ArrayList<File> fichiersAEnvoyer = Outil.splitFile(image.getOriginale(), chunk); 160 boolean echec = false; 161 for (int i = 0; i < fichiersAEnvoyer.size(); i++) { 162 File fichierAEnvoyer = fichiersAEnvoyer.get(i); 163 String originaleBase64 = base64.encode(Outil.getBytesFromFile(fichierAEnvoyer)); 164 Document reponseOriginale = (Main.sessionManager.executerReturnDocument("pwg.images.addChunk", "data", 165 originaleBase64, "type", "file", "position", String.valueOf(i), "original_sum", 166 Outil.getMD5Checksum(image.getOriginale().getAbsolutePath()))); 167 if (!Outil.checkOk(reponseOriginale)) { 168 echec = true; 169 break; 170 } 171 } 172 //end 173 174 //add the image in the database and get the result of the webservice 175 Document reponseAjout = (Main.sessionManager.executerReturnDocument("pwg.images.add", "file_sum", 176 Outil.getMD5Checksum(image.getOriginale().getAbsolutePath()), "thumbnail_sum", 177 Outil.getMD5Checksum(image.getThumbnail().getCanonicalPath()), "position", "1", "original_sum", 178 Outil.getMD5Checksum(image.getOriginale().getAbsolutePath()), "categories", 179 String.valueOf(image.getIdCategory()), "name", image.getName(), "author", Main.sessionManager.getLogin(), 180 "level", String.valueOf(ImagesManagement.privacyLevel))); 181 LOG.debug("Response add : " + Outil.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 (!Outil.checkOk(reponseThumb) || echec || !Outil.checkOk(reponseAjout)) { 193 reussite = false; 194 } 195 suppressionFichierTemporaires(); 196 return reussite; 197 198 } 199 200 201 /** 202 * Add tags to an image 203 * @param imageId id of the image 204 * @param tagId ids of the tags 205 * @throws IOException 206 */ 207 public boolean addTags(Integer imageId, String tagId) throws IOException { 208 Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.SET_INFO.getLabel(), "image_id", 209 String.valueOf(imageId), "tag_ids", tagId); 210 return Outil.checkOk(doc); 211 212 } 213 214 215 /** 216 * parse an element to find images 217 * @param element the element to parse 218 * @return the list of images 219 */ 220 private List<Image> getImagesFromElement(Element element) { 221 List<Element> listElement = (List<Element>) element.getChildren("image"); 222 ArrayList<Image> images = new ArrayList<Image>(); 223 for (Element im : listElement) { 224 Image myImage = new Image(); 225 myImage.setMiniature(im.getAttributeValue("tn_url")); 226 myImage.setUrl(im.getAttributeValue("element_url")); 227 myImage.setWidth(Integer.valueOf(im.getAttributeValue("width"))); 228 myImage.setHeight(Integer.valueOf(im.getAttributeValue("height"))); 229 myImage.setFile(im.getAttributeValue("file")); 230 myImage.setVue(Integer.valueOf(im.getAttributeValue("hit"))); 231 myImage.setIdentifiant(Integer.valueOf(im.getAttributeValue("id"))); 232 myImage.setName(im.getChildText("name")); 233 if (myImage.getName() == null) { 234 myImage.setName(myImage.getFile()); 235 } 236 images.add(myImage); 237 238 } 239 return images; 240 } 241 242 243 /** 244 * Search images 245 * @param searchString the string to search 246 * @return the list of images matching the string 247 * @throws IOException 248 */ 249 public List<Image> search(String searchString) throws IOException { 250 Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.SEARCH.getLabel(), "query", searchString); 251 LOG.debug(doc); 252 Element element = doc.getRootElement().getChild("images"); 253 return getImagesFromElement(element); 254 255 } 256 257 258 private void suppressionFichierTemporaires() { 259 File file = new File(System.getProperty("java.io.tmpdir") + "/originale.jpg"); 260 file.delete(); 261 file = new File(System.getProperty("java.io.tmpdir") + "/thumb.jpg"); 262 file.delete(); 263 264 } 257 265 258 266 }
Note: See TracChangeset
for help on using the changeset viewer.