Tip storing and managing images
Managing images is very simple with Version 7. Two points have to be considered:
* The storage of the images in the database.
* The display of the images on a page.
Storage in the database
Two ways are possible to handle the database storage:
- The simplest way is to let the supervisor work. This supposes that the used data type is a Rich media type with the Supervisor management check-box set. In this case, the image must be stored in a dedicated table: the table, the column that stores the image, and the key components values that perform the join to get the image are simply entered in the class dictionary.
- If the data type has the Supervisor management check-box not ticked, the developer must manage by himself the assignment of the image field values. This is done by using the media management events. Naturally, it requires writing code.
Manage the image in the user interface
Two choices for the UI behavior
Managing an image can be done in two ways in the user interface:
- The first way is to display the picture directly at a given place on the page. For this, you have to use a data type that has a content type equal to image. The standard data type ABIMG is dedicated to this (the storage management is done by the supervisor).
- The second way is to display a link that triggers the display of the picture upon click. To do this, you must use a data type associated to the content type application/x-document. This is for example the case of the datatype 'AB0' where a link appears. But if you want, instead of this link, to have an icon that represents the type of file, you need to assign dynamically the content type.
The second way where the type is constant
An example of the second way is given here. The best way to do it is on the 'AREAD_AFTER' event. This will handle the '$detail' facet. But if the icon must also appear on a query facet, you must call it in the 'AQUERY_TRANS_AFTER' event.
Let's imagine that we have a 'MYPICTURE' property in the representation, that is always in **jpeg** format. The code to be written will then be the following:$METHODS
Case ACTION
When "AREAD_AFTER","AQUERY_TRANS_AFTER" : Gosub AREAD_AFTER
...
Endcase
Return
$AREAD_AFTER
[L]ASTATUS = Fmet this.ASETATTRIBUTE("MYPICTURE","$contentType","image")
Return
How the supervisor handles variable types
What happens if the document type is variable, and if you want to display the right icon in the browser and open the most relevant widget to display the document when clicking on it?
You will need to set the relevant $contentType
attribute. This can of course be done with a most complex piece of code that will test the type of document to apply the right content type. To avoid to have to write this kind of code, the supervisor provides a very simple feature that is based on the following principles:
- In the data type dictionary, a blob or clob file is associated to a content type.
- The content type dictionary has a list of the most common content types. When the specifiable check-box is set on a content type, the 'ASETATTRIBUTES' can automatically be handled by the supervisor.
- To do this, the document must be managed by the supervisor: you have filled the table and the column that store the document in the class. An additional optional column in the class, called Content Type, corresponds to a column of the blob table that stores the content type code. The data type of this column must be 'ATYP'.
- The system will then handle automatically the data types:
- assign the right content type when downloading a document and save it in the blob table.
- send the right attribute that displays the right icon.
Let's give an example:
- The table 'MYBLOB' has 3 columns : a KEY column (type A, 10 characters), a 'MYDOC' column (Blbfile type), and a 'DOCTYP' column (type ATYP).
- The main entity 'SALESORDER' has an alphanumeric key of 10 characters. A document of variable type can be attached to every order.
To handle this variable type of document, in the 'SALESORDER' class:
- a property 'MYDOCUMENT' is present.
- Its data type is 'AB0'. This type is managed by the supervisor.
- The 'AB0' datatype has a content type called 'ATYPE9', that is "specifiable" and associated to the "application/x-document" content type.
- In the class definition, the property 'MYDOCUMENT' has a blob table called 'MYBLOB', a blob property 'MYDOC', and a data type property called 'DOCTYP'.
An that's all !
Of course, you will find in the content type a huge number of standard data ready to be used. But you can also add your own content types in this table to handle dedicated formats of document, if they can be for instance recognized by a browser thanks to additional viewer plug-ins.
The code you have avoided to write
Let's imagine the supervisor function is not used. This might be because you don't have a column with the right data type or because you want to manage the data type determination with another algorithm.
Let's see an example where a class stores a document that might have various formats in 'MYDOCUMENT' property. Let's suppose a 'DOCTYPE' char value defines the most frequent formats by their extension : pdf, doc, docx, xls, xlsx, ppt, txt, htm...
This could be handled that way:
$METHODS
Case ACTION
When "AREAD_AFTER" : Gosub AREAD_AFTER
...
Endcase
Return
$AREAD_AFTER
Case this.DOCTYPE
When "pdf" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","application/pdf")
When "doc" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","application/msword")
When "docx" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","application/vnd.ms-word.document.12")
When "xls" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","application/vnd.ms-excel")
When "xlsx" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","application/vnd.ms-excel.12")
When "jpg","jpeg" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","image/jpeg")
When "htm","html" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","text/html")
When "txt" : [L]ASTATUS = Fmet this.ASETATTRIBUTE("MYDOCUMENT","$contentType","text/plain")
....
Endcase
Return
This last example is a little bit complicated... You probably don't know all the content types that might exist, and by the way, if you check your document type for "xlsx", the official one is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet". So, using the supervisor management is much simpler.