I have this requirement to be able to upload photos that will be tied to a particular product. I've taken the filesystem approach in which the images are stored in a filesystem rather than in a database. Here's my algorithm for saving a product and saving its associated images to the filesystem and its reference to the database.
1. Create new Product instance (web tier)
2. Read uploaded images (web tier)
3. For each uploaded images, create an Image instance and assign a URL referencing the uploaded image (web tier)
4. Save uploaded images in the filesystem using the assigned URL (web tier)
5. Call an EJB method passing the new Product instance, and associated Image instances with assigned URLs referencing the uploaded images saved in the filesystem (EJB)
5.1. EJB then persists the Product instance and associates the Image instances. (EJB)
My question is what is the better approach in deleting the Product and its associated Image instances? I mean, how am I supposed to delete the image files being referenced by the Image instances from the filesystem if we're not supposed to create, modify, or delete files from a filesystem within EJB method?
The first thing that occurred to me is to return the deleted Image instances to the web tier and let the web tier delete the image files it is referencing from the filesystem. I just thought that it looks ugly. Is there any better approach than this? Most of my delete operations do not return the instances they've deleted.