1 /* 
  2  * Created on Sep 28, 2004 
  3  */ 
  4 package ubc.midp.mobilephoto.core.ui.datamodel; 
  5  
  6 import java.util.Hashtable; 
  7  
  8 import javax.microedition.lcdui.Image; 
  9  
 10 import lancs.midp.mobilephoto.lib.exceptions.ImageNotFoundException;  11 import lancs.midp.mobilephoto.lib.exceptions.InvalidImageDataException; 
 12 import lancs.midp.mobilephoto.lib.exceptions.InvalidPhotoAlbumNameException; 
 13 import lancs.midp.mobilephoto.lib.exceptions.NullAlbumDataReference; 
 14 import lancs.midp.mobilephoto.lib.exceptions.PersistenceMechanismException; 
 15 import lancs.midp.mobilephoto.lib.exceptions.UnavailablePhotoAlbumException; 
 16  
 17 /** 
 18  * @author tyoung 
 19  *  
 20  * This class represents the data model for Photo Albums. A Photo Album object 
 21  * is essentially a list of photos or images, stored in a Hashtable. Due to 
 22  * constraints of the J2ME RecordStore implementation, the class stores a table 
 23  * of the images, indexed by an identifier, and a second table of image metadata 
 24  * (ie. labels, album name etc.) 
 25  *  
 26  * This uses the ImageAccessor class to retrieve the image data from the 
 27  * recordstore (and eventually file system etc.) 
 28  */ 
 29 public class AlbumData { 
 30  
 31   private ImageAccessor imageAccessor; 
 32  
 33   //imageInfo holds image metadata like label, album name and 'foreign key' index to 
 34   // corresponding RMS entry that stores the actual Image object 
 35   protected Hashtable imageInfoTable = new Hashtable(); 
 36  
 37   public boolean existingRecords = false; //If no records exist, try to reset 
 38  
 39   /** 
 40    *  Constructor. Creates a new instance of ImageAccessor 
 41    */ 
 42   public AlbumData() { 
 43     imageAccessor = new ImageAccessor(this); 
 44   } 
 45  
 46   /** 
 47    *  Load any photo albums that are currently defined in the record store 
 48    */ 
 49   public String[] getAlbumNames() { 
 50  
 51     //Shouldn't load all the albums each time 
 52     //Add a check somewhere in ImageAccessor to see if they've been 
 53     //loaded into memory already, and avoid the extra work... 
 54     try { 
 55       imageAccessor.loadAlbums(); 
 56     } catch (InvalidImageDataException e) { 
 57       // TODO Auto-generated catch block 
 58       e.printStackTrace(); 
 59     } catch (PersistenceMechanismException e) { 
 60       // TODO Auto-generated catch block 
 61       e.printStackTrace(); 
 62     } 
 63     return imageAccessor.getAlbumNames(); 
 64   } 
 65  
 66   /** 
 67    *  Get all images for a given Photo Album that exist in the Record Store. 
 68    * @throws UnavailablePhotoAlbumException  
 69    * @throws InvalidImageDataException  
 70    * @throws PersistenceMechanismException  
 71    */ 
 72   public ImageData[] getImages(String recordName) throws UnavailablePhotoAlbumException  { 
 73  
 74     ImageData[] result; 
 75     try { 
 76       result = imageAccessor.loadImageDataFromRMS(recordName); 
 77     } catch (PersistenceMechanismException e) { 
 78       throw new UnavailablePhotoAlbumException(e); 
 79        
 80     } catch (InvalidImageDataException e) { 
 81       throw new UnavailablePhotoAlbumException(e); 
 82     } 
 83  
 84     return result; 
 85  
 86   } 
 87  
 88   /** 
 89    *  Define a new user photo album. This results in the creation of a new 
 90    *  RMS Record store. 
 91    * @throws PersistenceMechanismException  
 92    * @throws InvalidPhotoAlbumNameException  
 93    */ 
 94   public void createNewPhotoAlbum(String albumName) throws PersistenceMechanismException, InvalidPhotoAlbumNameException { 
 95     imageAccessor.createNewPhotoAlbum(albumName); 
 96   } 
 97    
 98   public void deletePhotoAlbum(String albumName) throws PersistenceMechanismException{ 
 99     imageAccessor.deletePhotoAlbum(albumName); 
100   } 
101  
102   /** 
103    *  Get a particular image (by name) from a photo album. The album name corresponds 
104    *  to a record store. 
105    * @throws ImageNotFoundException  
106    * @throws PersistenceMechanismException  
107    */ 
108   public Image getImageFromRecordStore(String recordStore, String imageName) throws ImageNotFoundException, PersistenceMechanismException { 
109  
110     ImageData imageInfo = null; 
111     try { 
112       imageInfo = imageAccessor.getImageInfo(imageName); 
113     } catch (NullAlbumDataReference e) { 
114       imageAccessor = new ImageAccessor(this); 
115     } 
116     //Find the record ID and store name of the image to retrieve 
117     int imageId = imageInfo.getForeignRecordId(); 
118     String album = imageInfo.getParentAlbumName(); 
119     //Now, load the image (on demand) from RMS and cache it in the hashtable 
120     Image imageRec = imageAccessor.loadSingleImageFromRMS(album, imageName, imageId); //rs.getRecord(recordId); 
121     return imageRec; 
122  
123   } 
124   public void addNewPhotoToAlbum(String label, String path, String album) throws InvalidImageDataException, PersistenceMechanismException{ 
125     imageAccessor.addImageData(label, path, album); 
126   } 
127  
128   /** 
129    *  Delete a photo from the photo album. This permanently deletes the image from the record store 
130    * @throws ImageNotFoundException  
131    * @throws PersistenceMechanismException  
132    */ 
133   public void deleteImage(String imageName, String storeName) throws PersistenceMechanismException, ImageNotFoundException { 
134     try { 
135       imageAccessor.deleteSingleImageFromRMS(imageName, storeName); 
136     } 
137     catch (NullAlbumDataReference e) { 
138       imageAccessor = new ImageAccessor(this); 
139       e.printStackTrace(); 
140     }  
141   } 
142    
143   /** 
144    *  Reset the image data for the application. This is a wrapper to the ImageAccessor.resetImageRecordStore 
145    *  method. It is mainly used for testing purposes, to reset device data to the default album and photos. 
146    * @throws PersistenceMechanismException  
147    * @throws InvalidImageDataException  
148    */ 
149   public void resetImageData() throws PersistenceMechanismException { 
150     try { 
151       imageAccessor.resetImageRecordStore(); 
152     } catch (InvalidImageDataException e) { 
153       e.printStackTrace(); 
154     } 
155   } 
156  
157   /** 
158    * Get the hashtable that stores the image metadata in memory. 
159    * @return Returns the imageInfoTable. 
160    */ 
161   public Hashtable getImageInfoTable() { 
162     return imageInfoTable; 
163   } 
164  
165   /** 
166    * Update the hashtable that stores the image metadata in memory 
167    * @param imageInfoTable 
168    *            The imageInfoTable to set. 
169    */ 
170   public void setImageInfoTable(Hashtable imageInfoTable) { 
171     this.imageInfoTable = imageInfoTable; 
172   } 
173  
174   /** 
175    * [EF] Added in order to have access to ImageData 
176    * @param imageAccessor 
177    */ 
178   public void setImageAccessor(ImageAccessor imageAccessor) { 
179     this.imageAccessor = imageAccessor; 
180   } 
181  
182   /** 
183    * [EF] Added in order to have access to ImageData 
184    * @return 
185    */ 
186   public ImageAccessor getImageAccessor() { 
187     return imageAccessor; 
188   } 
189 }    
    |