1 7 8 package org.jboss.media.entity; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.InputStream ; 14 import java.net.URL ; 15 16 import javax.ejb.CreateException ; 17 import javax.ejb.FinderException ; 18 import javax.ejb.SessionBean ; 19 import javax.emb.MediaEntityLocal; 20 import javax.emb.MediaEntityLocalHome; 21 import javax.emb.MediaException; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingException ; 25 26 43 public abstract class MediaEntityManagerBean implements SessionBean 44 { 45 52 public String createMediaEntity() 53 throws MediaException, CreateException , NamingException 54 { 55 MediaEntityLocalHome mediaEntityLocalHome = getMediaEntityLocalHome(); 56 MediaEntityLocal mediaEntityLocal = mediaEntityLocalHome.create(); 57 return (String ) mediaEntityLocal.getPrimaryKey(); 58 } 59 60 public MediaEntityDTO getMediaEntity(String identity) 61 throws MediaException, FinderException , NamingException 62 { 63 MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity); 64 65 MediaEntityDTO mediaEntityDTO = new MediaEntityDTO(); 66 mediaEntityDTO.setContent(mediaEntityLocal.getContent()); 67 mediaEntityDTO.setLocation(mediaEntityLocal.getLocation()); 68 mediaEntityDTO.setDescription(mediaEntityLocal.getDescription()); 69 mediaEntityDTO.setName(mediaEntityLocal.getName()); 70 mediaEntityDTO.setMimeType(mediaEntityLocal.getMimeType()); 71 72 return mediaEntityDTO; 73 } 74 75 87 public String updateMediaEntity( 88 String identity, 89 MediaEntityDTO mediaEntityDTO) 90 throws MediaException, FinderException , NamingException 91 { 92 MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity); 93 94 mediaEntityLocal.setContent(mediaEntityDTO.getContent()); 95 mediaEntityLocal.setLocation(mediaEntityDTO.getLocation()); 96 mediaEntityLocal.setDescription(mediaEntityDTO.getDescription()); 97 mediaEntityLocal.setName(mediaEntityDTO.getName()); 98 mediaEntityLocal.setMimeType(mediaEntityDTO.getMimeType()); 99 100 return (String ) mediaEntityLocal.getPrimaryKey(); 101 } 102 103 125 public Object getMediaEntityProperty(String identity, String property) 126 throws MediaException, FinderException , NamingException 127 { 128 if (identity == null || property == null) 129 { 130 throw new NullPointerException (); 131 } 132 133 MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity); 134 135 if (property.equals("content")) 136 { 137 return mediaEntityLocal.getContent(); 138 } 139 else if (property.equals("location")) 140 { 141 return mediaEntityLocal.getLocation(); 142 } 143 else if (property.equals("description")) 144 { 145 return mediaEntityLocal.getDescription(); 146 } 147 else if (property.equals("name")) 148 { 149 return mediaEntityLocal.getName(); 150 } 151 else if (property.equals("mimeType")) 152 { 153 return mediaEntityLocal.getMimeType(); 154 } 155 else 156 { 157 throw new IllegalArgumentException ( 158 "Invalid property name: " + property); 159 } 160 } 161 162 180 public void setMediaEntityProperty( 181 String identity, 182 String property, 183 Object value) 184 throws MediaException, FinderException , NamingException 185 { 186 if (identity == null || property == null) 187 { 188 throw new NullPointerException (); 189 } 190 191 MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity); 192 193 try 194 { 195 if (property.equals("content")) 196 { 197 mediaEntityLocal.setContent((byte[]) value); 198 } 199 else if (property.equals("location")) 200 { 201 mediaEntityLocal.setLocation((URL ) value); 202 } 203 else if (property.equals("description")) 204 { 205 mediaEntityLocal.setDescription((String ) value); 206 } 207 else if (property.equals("name")) 208 { 209 mediaEntityLocal.setName((String ) value); 210 } 211 else if (property.equals("mimeType")) 212 { 213 mediaEntityLocal.setMimeType((String ) value); 214 } 215 else 216 { 217 throw new IllegalArgumentException ( 218 "Invalid property name: " + property); 219 } 220 } 221 catch (ClassCastException e) 222 { 223 throw new IllegalArgumentException ( 224 "Invalid value for property: " + property); 225 } 226 } 227 228 231 public void setMediaEntityContent(String identity, File contentFile) 232 throws MediaException, FinderException , NamingException , FileNotFoundException 233 { 234 if (identity == null) 235 { 236 throw new NullPointerException (); 237 } 238 239 MediaEntityLocal mediaEntityLocal = this.findMediaEntity(identity); 240 InputStream contentStream = new FileInputStream (contentFile); 241 mediaEntityLocal.setContent(contentStream); 242 } 243 244 246 private MediaEntityLocal findMediaEntity(String identity) 247 throws NamingException , FinderException 248 { 249 MediaEntityLocalHome mediaEntityLocalHome = getMediaEntityLocalHome(); 250 return mediaEntityLocalHome.findByPrimaryKey(identity); 251 } 252 253 private MediaEntityLocalHome getMediaEntityLocalHome() 254 throws NamingException 255 { 256 Context initialContext = new InitialContext (); 257 return (MediaEntityLocalHome) initialContext.lookup(MediaEntityBean.MEDIA_ENTITY_JNDI); 258 } 259 } | Popular Tags |