Home / Articles / Adobe ColdFusion / Developing a ColdFusion/J2EE Solution

Developing a ColdFusion/J2EE Solution

Chapter Description

This article kicks off in the midst of fictional Sonic Systems' web project. It shows how the aim in creating a project blueprint was to eliminate the possibility for changes in the architecture during development, and to help development go as smoothly as possible.

Editing Template Development

The final step in developing the Sonic application was to create the portlets that would be used to perform any tasks within the portal application, including customizing the portlet setup, logging into applications, and administering users within the application. The administrative interface was set up first to edit the database and users and groups within the system.

The Administrative Interface

To start, an administrative controller was set up. By appending the URL variable admin=true, the admin controller overrides the tab controller. This is done in the index.cfm by adding a <CFIF> statement:

<CFIF IsDefined("URL.Admin")>

The AdminIndex.cfm controller template takes a URL parameter called action. Within a <CFSWITCH> statement, a number of <CFCASE> statements include the appropriate file based on what is passed in the action URL parameter. Each of these included files is protected using Project Omega's bit-based permission template.

This security framework is a Project Omega tool that the team had used for many of their projects. Basically, every permission level is assigned a number that is a power of 2. These are the bit values: 1, 2, 4, 8, 16, etc. When a user logs in, the user's permissions are added together, and the resulting value is set to the SESSION.Permissions.userPermission variable. This value can then be tested to see if it contains the relevant bit value to access a certain template. A <CF_SECURE> tag can be used to perform such tests.


The REQUIREDPERMISSION attribute contains the bit value necessary to access the specified resource. These permission levels can be looked up in the database, as well as added to and edited. The USERPERMISSIONS attribute contains the user's permission level, and the FAILUREREDIRECT specifies where to redirect the user if the permissions are not adequate.

The existing administrative templates needed very minimal adaptation in order to work with the portal application. The admin=true URL variable just needed to be appended, and the existing design elements stripped out. This included editing of users, groups, and permissions. The only additional editing required was editing affiliations, the groups that contain portal design information.

For the affiliations add and edit page, Project Omega used <CFDIRECTORY> to obtain the possible headers, footers, stylesheets and portlet registries, and then <CFSELECT> to display the possibilities:

                      MESSAGE="You must select a header.">
     <CFOUTPUT QUERY="headerList">
          <OPTION<CFIF name EQ getAffiliation.Header>

The IncludeDirectory contains the location of the possible includes: where to find headers and footers. There is a similar variable for both registries and stylesheets. The FILTER attribute filters for all files containing "head" (footers filter by "foot" and stylesheets for ".css") in the filename. This will be important for designers to know when they add new headers and footers. Finally, the values returned are looped through to output the options, and a <CFIF> statement will select the option that is currently entered in the database; this effectively defaults the existing option.

Portlet Configurator

The portlet configurator allows users to edit how their portal is displayed—their PSML file, basically. This feature would require the creation of two templates: a PSML writer component and the DefinePortlets.cfm portlet. Together these templates will use Cold-Fusion's XML functions to edit an XML document object representation of the user's PSML file and save any changes to the actual PSML file and reset the user's preferences.

The PSML writer component will contain the following methods:

  • addPortlet(). Adds new tab to a user's PSML document object.

  • deletePortlet(). Removes a tab from the user's PSML document object.

  • writeNewPortlet(). Writes changes to a user's PSML document object.

  • savePSML(). Saves the user's PSML document object to the actual PSML file.

In all PSML editing functions (but not when saving the PSML file), the getUserPSML() method from the PSML reader component is invoked to return the ColdFusion XML document object of the user's PSML file. Within each, the appropriate ColdFusion functions are used to edit the document object.

The addPortlet() function first uses the ArrayAppend() and XmlElem-New() function to add a new <portlets> element, which will contain the tab information.

ArrayAppend(userPSML.portlets.XmlChildren, XmlElemNew(userPSML,"portlets")); 

XML child nodes are accessed in ColdFusion as an array. The <portlets> child nodes are what store the tabs, so adding a new element to this array with ArrayAppend() will be the same as adding a new element to the XML document object. The XmlElemNew() is used to create new XML elements—here you're actually creating the empty <portlets> element. These same basic techniques are used to create the title of the tab, the controller and insert portlets based on the TABTITLE, CONTROLLER and PORTLETSLIST (a comma-delimited list of portlets) arguments passed to it. The same arguments, USER (required), MEDIA, and LANGUAGE are also needed to retrieve the user's PSML file.

The writeNewPortlet() function saves information to an existing port-let. In addition to the USER, MEDIA, LANGUAGE, TABTITLE, CONTROLLER, and PORTLETSLIST arguments, this function also takes TAB, the index number of the <portlets> element being edited; AUTHARRAY, a two-dimensional array containing portlet log-in information; and AUTHKEY, an encryption key for encoding the user names and passwords.

The deletePortlet() function will remove a tab. This is done fairly simply by using ArrayDeleteAt() to remove the corresponding <portlets> element. The required argument, Tab, will specify the index of the tab to be deleted:


In all of these functions, the user's PSML is returned as a document object. This is done so that it can be further edited or saved by the calling file. If it is to be saved, the user's PSML is passed to the savePSML() function.

<!--- find location of user's PSML file --->
<CFINVOKE COMPONENT="portal.components.userinfo"
   <CFSET Success = TRUE>
        <CFSET Success = FALSE>
<CFRETURN Success>

Here, the PSML passed in the PSML argument is converted back from its ColdFusion XML document object representation into a string using the ToString() function. The PSML location is retrieved using the now familiar getUserPSML() function of the userinfo component. Finally, this method tries to save the file to this location using <CFFILE>. Depending on whether it succeeds or fails, a Boolean value is set to the Success variable, which is then returned.

Once this component was completed, the actual editing template could be created. To start, no anonymous use should ever be able to access this template; anonymous users get only what is set up as the default. A

<CFIF SESSION.User EQ "anon">
   You must log in to access this feature.

The first part of this template to be developed is the form. This form edits one tab at a time, and the first tab is the default. For each tab, the form will have a pull-down of controllers (from the controller registry), a tab name, and a list of all the portlets in the user's portlet registry—along with a check box, illustrating whether the user wants the given portlet on that tab and text fields to enter a user name and password if necessary. Figure I-3.8 shows what this form looks like.

Figure 3-8 Figure I-3.8 Each tab can be edited individually for each user.

In addition, this page will have a button to delete tabs, a link to create a new tab, and a pull-down to change the current tab for editing. Once this was all laid out and made dynamic, the actually processing information was entered at the top of the page.

A key aspect of developing portlets is to make sure that the form is submitted to the same page. This can be done by always linking to index.cfm in <A> and <FORM> tags, or else by using the CGI.SCRIPT_NAME variable. This would always resolve to the current page. This was important, because if a link went to another page, the user would leave the portal. However, by linking to the same page, the user would reload the portal page and include the portlet.

In order to do this, the form-processing information is included at the top of the page. For example, the tab-deleting feature is enclosed within a

<CFIF IsDefined("FORM.DPDeleteTab")>
   <!--- if last tab, don't allow --->
<CFIF ArrayLen(SESSION.UserPrefs.Tabs) LTE 1>
    You cannot delete your last tab.
<!--- Delete the friggin' tab already --->
<CFINVOKE COMPONENT="portal.components.PSMLWriter"
          <!--- save new PSML document for user --->
      <CFINVOKE COMPONENT="portal.components.PSMLWriter"
      <!--- if file was saved, re-write user's session --->
      <CFIF PSMLSaved>
         <!--- reset admin tab --->
         <CFSET SESSION.AdminTab = 1>
         <!--- send to home page
               (in case user is on tab just deleted) --->
         <CFLOCATION URL="#script_name#?CurrentTab=1">
         <P>There was a problem trying to delete this

The FORM.DPDeleteTab variable will only exist if the user has posted this page to the portlet. If so, this block will execute, first disallowing deletion of the last tab, and then invoking the PSML writer component to delete the portlet and save the PSML. If the PSML save is successful, the user's preferences are rewritten using the custom tags, and the user is redirected to the main portal page.

instant message

Note that the name of the form variable checked is unique: DPDeleteTab. This is to prevent other portlets from executing similar <CFIF> blocks. For example, if the name was just Delete, another portlet that deletes something different may be looking for that variable and actually execute. This was a problem Project Omega had learned about the hard way. The add and edit <CFIF> blocks were finally added, and the template was finished.


Remember that when portlets are loaded, there are many to a page. When passing FORM and URL variables, try to make them unique so as not to cause errors in other concurrently loading portlets.

Now that this was completed, the administrator needed to be able to link to these pages. To do so, a few steps were taken. First, two levels of permissions were added to the database: one for non-anonymous portal users and another for administrators. After that, the header template was edited. A series of <CFCASE> statements check the user's permissions. Depending on the result, a log-in file, a portal user navigation bar, or an administrative user navigation bar is included. Figure I-3.9 shows the three possibilities that will be included in the upper-right section of the page.

The final portlet Omega had to develop was the log-in one. This was a simple portlet that used the PSML reader components to retrieve all of the portlets a user had with log-in information. The user name and password were then output to hidden form fields and a button drawn for each portlet. By reading the portlet registry, the page to post log-in information could be obtained. Each button would then post this location with the TARGET attribute set to _blank, opening a new window.

Figure 3-9 Figure I-3.9 Depending on the type of user, the upper-right part of the portal screen will contain one of three navigation elements.

13. Creating a Real User and Testing | Next Section Previous Section