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