This blog article is part 1 of a series of posts showing how to deal with images or files in an ADF application. Each of the techniques to do this are described in other blog posts or documents, but I couldn’t find a sample doing it all together in on sample application.
The goal of this series is to provide a sample showing how to upload a file from a client to the server, store the data on the server, make it available for later retrieval and show the data in the user interface (form and table).
Part 1 gives an overview of the sample application I’m going to build and how to set it up
Part 2 shows how to upload a file, store it and download it back to the client
Part 3 implements two techniques to show the data (image) on the user interface
Part 4 backport of the sample to JDeveloper 11gR1
Part 5 implements a technique to show the uploaded file right after upload without the need to commit first
Overview of the sample application
The sample allows to create and manage catalogs. A catalog has a unique id, a name and may contain files and images which the user can upload into a catalog and download from it. All this is implemented in a simple way, so no fancy layout, only bare functionality. Here is a screen shot of the sample application after part 2 is finished:
As you see the UI is nothing I would use in a real world application, but for this sample it does the trick.
To create a new catalog you click the ‘New Catalog’ button and can fill in a name for the new catalog. The id is automatically assigned via a groovy expression which calls a sequence defined in the db. In the catalog screen you see the catalog together with all images added to this catalog. Here you can remove the whole catalog. The image data is deleted too in this case.
Once you have a catalog created you can add images or other files to it by using the ‘New Image’ button.
When adding a new image to a catalog you can specify a name for the image, the content type which will be read from the file once you hit the upload button. The image id is assigned by a groovy expression, the catalog id is populated by the master record, the catalog. As there is no visible image of the data in this version, an output text shows you if data has already been uploaded (Image Data available/not available).
This concludes the short run through the sample application.
The following db diagram shows the two tables involved (CATALOG and IMAGES) and their 1:* relationship. For reference I added the two sequences which generate the primary key for the tables.
Next is the DML to generate the two tables and the sequences. You can add the tables to an existing DB or create a new schema and add them their. If you later download the source code you’ notice, that I added the DML to the well known HR schema. If you use an other schema, you have to change the db connection accordingly.
CREATE TABLE CATALOG ( CATALOG_ID NUMBER(12) NOT NULL , CATALOG_NAME VARCHAR2(200 CHAR) NOT NULL , CONSTRAINT CATALOG_PK PRIMARY KEY ( CATALOG_ID ) ENABLE ); CREATE UNIQUE INDEX CATALOG_PK ON CATALOG (null ASC); CREATE SEQUENCE CATALOG_SEQ INCREMENT BY 1 START WITH 100 NOCACHE; CREATE TABLE IMAGES ( IMAGE_ID NUMBER(12) NOT NULL , IMAGE_NAME VARCHAR2(200 CHAR) NOT NULL , CONTENT_TYPE VARCHAR2(50 CHAR) , IMAGE_DATA BLOB , CATALOG_ID NUMBER(12) NOT NULL , CONSTRAINT IMAGES_PK PRIMARY KEY ( IMAGE_ID ) ENABLE ); CREATE UNIQUE INDEX IMAGES_PK ON IMAGES (null ASC); ALTER TABLE IMAGES ADD CONSTRAINT IMAGES_CATALOG_FK FOREIGN KEY ( CATALOG_ID ) REFERENCES CATALOG ( CATALOG_ID ) ENABLE; CREATE SEQUENCE IMAGE_SEQ INCREMENT BY 1 START WITH 100 NOCACHE;
Finally here are the task flows which build the first part of the sample application:
The start page ‘Catalog’ contains a region (catalog-task-flow-description.xml) which is used to add or edit catalogs and to add or edit images for a catalog.
This concludes part 1. In part two I describe in detail how to implement the file upload and download feature and store the data in a blob column in the db.
To be continued…