Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/MapLayerMetadataServiceBean.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 /* | |
| 2 * To change this license header, choose License Headers in Project Properties. | |
| 3 * To change this template file, choose Tools | Templates | |
| 4 * and open the template in the editor. | |
| 5 */ | |
| 6 | |
| 7 package edu.harvard.iq.dataverse; | |
| 8 | |
| 9 import edu.harvard.iq.dataverse.authorization.Permission; | |
| 10 import edu.harvard.iq.dataverse.authorization.users.User; | |
| 11 import java.io.File; | |
| 12 import java.io.FileOutputStream; | |
| 13 import java.io.IOException; | |
| 14 import java.io.InputStream; | |
| 15 import java.io.OutputStream; | |
| 16 import java.net.URL; | |
| 17 import java.nio.file.Path; | |
| 18 import java.util.Arrays; | |
| 19 import java.util.List; | |
| 20 import java.util.logging.Logger; | |
| 21 import javax.ejb.EJB; | |
| 22 import javax.ejb.Stateless; | |
| 23 import javax.inject.Named; | |
| 24 import javax.persistence.EntityManager; | |
| 25 import javax.persistence.NoResultException; | |
| 26 import javax.persistence.PersistenceContext; | |
| 27 import javax.persistence.Query; | |
| 28 | |
| 29 /** | |
| 30 * | |
| 31 * @author raprasad | |
| 32 */ | |
| 33 @Stateless | |
| 34 @Named | |
| 35 public class MapLayerMetadataServiceBean { | |
| 36 | |
| 37 | |
| 38 @PersistenceContext(unitName = "VDCNet-ejbPU") | |
| 39 private EntityManager em; | |
| 40 | |
| 41 @EJB | |
| 42 PermissionServiceBean permissionService; | |
| 43 | |
| 44 private static final Logger logger = Logger.getLogger(MapLayerMetadataServiceBean.class.getCanonicalName()); | |
| 45 | |
| 46 | |
| 47 public MapLayerMetadata find(Object pk) { | |
| 48 if (pk==null){ | |
| 49 return null; | |
| 50 } | |
| 51 return (MapLayerMetadata) em.find(MapLayerMetadata.class, pk); | |
| 52 } | |
| 53 | |
| 54 public MapLayerMetadata save( MapLayerMetadata layer_metadata) { | |
| 55 if (layer_metadata==null){ | |
| 56 return null; | |
| 57 } | |
| 58 if ( layer_metadata.getId() == null ) { | |
| 59 em.persist(layer_metadata); | |
| 60 return layer_metadata; | |
| 61 } else { | |
| 62 return em.merge( layer_metadata ); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 | |
| 67 | |
| 68 /* | |
| 69 Given a datafile id, return the associated MapLayerMetadata object | |
| 70 | |
| 71 */ | |
| 72 public MapLayerMetadata findMetadataByDatafile(DataFile datafile){ | |
| 73 | |
| 74 if (datafile == null){ | |
| 75 return null; | |
| 76 } | |
| 77 | |
| 78 try{ | |
| 79 // String sqlStatement = | |
| 80 Query query = em.createQuery("select m from MapLayerMetadata m WHERE m.dataFile=:datafile", MapLayerMetadata.class); | |
| 81 query.setParameter("datafile", datafile); | |
| 82 query.setMaxResults(1); | |
| 83 //entityManager.createQuery(SQL_QUERY).setParameter(arg0,arg1).setMaxResults(10).getResultList(); | |
| 84 return (MapLayerMetadata) query.getSingleResult(); | |
| 85 } catch ( NoResultException nre ) { | |
| 86 return null; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 | |
| 91 /* | |
| 92 Delete a mapLayerMetadata object. | |
| 93 | |
| 94 First check if the given user has permission to edit this data. | |
| 95 | |
| 96 */ | |
| 97 public boolean deleteMapLayerMetadataObject(MapLayerMetadata mapLayerMetadata, User user){ | |
| 98 logger.info("deleteMapLayerMetadataObject"); | |
| 99 | |
| 100 if ((mapLayerMetadata == null)||(user==null)){ | |
| 101 return false; | |
| 102 } | |
| 103 | |
| 104 if (permissionService.userOn(user, mapLayerMetadata.getDataFile().getOwner()).has(Permission.EditDataset)) { | |
| 105 em.remove(em.merge(mapLayerMetadata)); | |
| 106 | |
| 107 this.deleteOlderMapThumbnails(mapLayerMetadata); | |
| 108 return true; | |
| 109 } | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 | |
| 114 public MapLayerMetadata findMetadataByLayerNameAndDatafile(String layer_name){//, DataFile datafile) { | |
| 115 if ((layer_name == null)){//||(datafile==null)){ | |
| 116 return null; | |
| 117 } | |
| 118 //Query query = em.createQuery("select o.id from MapLayerMetadta as o where o.layer_name =:layerName and o.datafile_id =:datafileID;"); | |
| 119 //Query query = em.createQuery("select m from MapLayerMetadata m where m.layer_name =:layerName ;"); | |
| 120 try{ | |
| 121 return em.createQuery("select m from MapLayerMetadata m WHERE m.layerName=:layerName", MapLayerMetadata.class) | |
| 122 .setParameter("layerName", layer_name) | |
| 123 .getSingleResult(); | |
| 124 } catch ( NoResultException nre ) { | |
| 125 return null; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 | |
| 130 | |
| 131 public List<MapLayerMetadata> getMapLayerMetadataForDataset(Dataset dataset){ | |
| 132 if (dataset == null){ | |
| 133 return null; | |
| 134 } | |
| 135 Query query = em.createQuery("select object(o) from MapLayerMetadata as o where o.dataset=:dataset");// order by o.name"); | |
| 136 query.setParameter("dataset", dataset); | |
| 137 return query.getResultList(); | |
| 138 } | |
| 139 | |
| 140 | |
| 141 /** | |
| 142 * Before downloading a file for map icons (see "retrieveMapImageForIcon" below), | |
| 143 * first remove any existing .img and .img.* files | |
| 144 * | |
| 145 * e.g. delete all that start with (DataFile name) + ".img" | |
| 146 * | |
| 147 * @param mapLayerMetadata | |
| 148 * @return | |
| 149 * @throws IOException | |
| 150 */ | |
| 151 private boolean deleteOlderMapThumbnails(MapLayerMetadata mapLayerMetadata) { | |
| 152 if (mapLayerMetadata==null){ | |
| 153 logger.warning("mapLayerMetadata is null"); | |
| 154 return false; | |
| 155 } | |
| 156 | |
| 157 // Retrieve the data file | |
| 158 // | |
| 159 DataFile df = mapLayerMetadata.getDataFile(); | |
| 160 | |
| 161 // Get the parent directory | |
| 162 // | |
| 163 Path fileDirname = df.getFileSystemLocation().getParent(); | |
| 164 if (fileDirname == null){ | |
| 165 logger.warning("DataFile directory has null path. Directory path: " + df.getFileSystemLocation().toString()); | |
| 166 return false; | |
| 167 } | |
| 168 | |
| 169 // Verify that the directory exists | |
| 170 // | |
| 171 File fileDirectory = new File(fileDirname.normalize().toString()); | |
| 172 if (!(fileDirectory.isDirectory())){ | |
| 173 logger.warning("DataFile directory is not actuall a directory. Directory path: " + fileDirectory.toString()); | |
| 174 return false; | |
| 175 } | |
| 176 | |
| 177 /* Iterate through directory and delete any ".img" files for this DataFile | |
| 178 | |
| 179 Example: | |
| 180 Datafile name: 14a5e4abf7d-e7eebfb6474d | |
| 181 Types of files that would be deleted (if they exist): | |
| 182 14a5e4abf7d-e7eebfb6474d.img | |
| 183 14a5e4abf7d-e7eebfb6474d.img.thumb64 | |
| 184 14a5e4abf7d-e7eebfb6474d.img.thumb400 | |
| 185 */ | |
| 186 String iconBaseFilename = df.getFileSystemLocation().toString() + ".img"; | |
| 187 | |
| 188 for (File singleFile : fileDirectory.listFiles()) { | |
| 189 if (singleFile.toString().startsWith(iconBaseFilename)) { | |
| 190 //logger.info("file found: " + singleFile.toString()); | |
| 191 singleFile.delete(); | |
| 192 //results.add(file.getName()); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 return true; | |
| 197 } | |
| 198 | |
| 199 /** | |
| 200 * Use the mapLayerMetadata.mapImageLink to retrieve a PNG file directly from WorldMap | |
| 201 * | |
| 202 * Next step: Save this image as the default icon | |
| 203 * | |
| 204 * Example mapImageLink: http://worldmap.harvard.edu/download/wms/14708/png?layers=geonode:power_plants_enipedia_jan_2014_kvg&width=948&bbox=76.04800165,18.31860358,132.0322222,50.78441&service=WMS&format=image/png&srs=EPSG:4326&request=GetMap&height=550 | |
| 205 * | |
| 206 * Parameter by parameter (note width/height): | |
| 207 * http://worldmap.harvard.edu/download/wms/14708/png? | |
| 208 * layers=geonode:power_plants_enipedia_jan_2014_kvg | |
| 209 * width=948 | |
| 210 * bbox=76.04800165,18.31860358,132.0322222,50.78441 | |
| 211 * service=WMS | |
| 212 * format=image/png | |
| 213 * srs=EPSG:4326 | |
| 214 * request=GetMap | |
| 215 * height=550 | |
| 216 * | |
| 217 * @param mapLayerMetadata | |
| 218 * @return boolean | |
| 219 * @throws IOException | |
| 220 */ | |
| 221 public boolean retrieveMapImageForIcon(MapLayerMetadata mapLayerMetadata) throws IOException { | |
| 222 if (mapLayerMetadata==null){ | |
| 223 logger.warning("mapLayerMetadata is null"); | |
| 224 return false; | |
| 225 } | |
| 226 | |
| 227 this.deleteOlderMapThumbnails(mapLayerMetadata); | |
| 228 if (true){ | |
| 229 // debug check | |
| 230 // return false; | |
| 231 } | |
| 232 if ((mapLayerMetadata.getMapImageLink()==null)||mapLayerMetadata.getMapImageLink().isEmpty()){ | |
| 233 logger.warning("mapLayerMetadata does not have a 'map_image_link' attribute"); | |
| 234 return false; | |
| 235 } | |
| 236 | |
| 237 String imageUrl = mapLayerMetadata.getMapImageLink(); | |
| 238 imageUrl = imageUrl.replace("https:", "http:"); | |
| 239 logger.info("Attempt to retrieve map image: " + imageUrl); | |
| 240 | |
| 241 String destinationFile = mapLayerMetadata.getDataFile().getFileSystemLocation().toString() + ".img"; | |
| 242 logger.info("destinationFile: getFileSystemLocation()" + mapLayerMetadata.getDataFile().getFileSystemLocation()); | |
| 243 logger.info("destinationFile: " + destinationFile); | |
| 244 | |
| 245 URL url = new URL(imageUrl); | |
| 246 logger.info("retrieve url : " + imageUrl); | |
| 247 | |
| 248 logger.info("try to open InputStream"); | |
| 249 InputStream is = null; | |
| 250 | |
| 251 try{ | |
| 252 is = url.openStream(); | |
| 253 }catch(IOException exio){ | |
| 254 logger.warning("Error when retrieving map icon image. Exception: " + exio.getMessage()); | |
| 255 if (is!=null){ | |
| 256 try { is.close(); } catch (IOException ignore) {} | |
| 257 } | |
| 258 return false; | |
| 259 | |
| 260 } | |
| 261 | |
| 262 OutputStream os = null; | |
| 263 | |
| 264 try{ | |
| 265 logger.info("try to start OutputStream"); | |
| 266 os = new FileOutputStream(destinationFile); | |
| 267 } catch (Exception ex){ | |
| 268 logger.warning("Error when retrieving map icon image. Exception: " + ex.getMessage()); | |
| 269 if (os!=null){ | |
| 270 try { os.close(); } catch (IOException ignore) {} | |
| 271 } | |
| 272 return false; | |
| 273 } | |
| 274 | |
| 275 byte[] b = new byte[2048]; | |
| 276 int length; | |
| 277 | |
| 278 logger.info("Writing file..."); | |
| 279 while ((length = is.read(b)) != -1) { | |
| 280 os.write(b, 0, length); | |
| 281 } | |
| 282 | |
| 283 logger.info("Closing streams..."); | |
| 284 is.close(); | |
| 285 os.close(); | |
| 286 | |
| 287 logger.info("Done"); | |
| 288 return true; | |
| 289 } | |
| 290 } | |
| 291 |
