[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Converting an Application.cfm to Application.CFC

Posted on 2008-11-10
1
Medium Priority
?
1,638 Views
Last Modified: 2012-05-05
I'm having trouble converting 2 application.cfm files to an application.cfc.  I have put comments in my code snippet to help in asking the questions.

The first application.cfm file is the main root of my web application and the 2nd one has to do with the login directory.  

I'm also having trouble setting up an error handling framework using the code already created (FYI - I didn't help create it)

I'll attach the first application.cfm file on the snippet in this post and I'll post the 2nd application.cfm file right after it so you'll be able to tell which ones are which

My goal is to set up the error handling framework as well as have my web app work the way its supposed to when a user logs in.

Can someone please point me in the right direction I'm having a little trouble.  I know what to do regarding the onApplicationStart but its the other functions (ie onSession and onRequest) that I'm having trouble with
<!--- HERE IS THE FIRST AND MAIN APPLICATION.CFM FILE -------------->
<!--- go get the global vars PRETTY CERTAIN THIS GOES IN THE OnApplicationStart Function--->
<cfset request.MY_Datasource = "MY_Datasource">
	<cfset request.MY_Datasource2 = "MY_Datasource2">
	<cfset request.MY_Datasource3 = "MY_Datasource3">
	<cfset request.MY_Datasource4 = "MY_Datasource4">
	<cfset request.MY_Datasource5 = "MY_Datasource5">
	<cfset request.time=20>
         <cfset request.default_email=myEmailAddress
<!--- is the DB alive? This checks to make sure the database is working correctly however I'm not sure if it works anymore --->
<cfif not( CGI.SERVER_NAME EQ "MyIP" ) and request.debug EQ 0>
	<cfmodule template="includes/customtags/dbcheck.cfm">
</cfif>
 
<!--- setup the session vars via an application instance --->
<!--- Should make note to users, cookies are required --->
<cfapplication name="MyApp" 
			sessionmanagement="Yes"
            sessiontimeout="#createtimespan(0,0,request.time,0)#">
 
<!--- this is supposed to make the cfid/cftoken cookie expire when the browser closes--->
<cfset Variables.cfid_local = Cookie.CFID>
<cfset Variables.cftoken_local = Cookie.CFTOKEN>
<cfcookie name="CFID" value="#Variables.cfid_local#">
<cfcookie name="CFTOKEN" value="#Variables.cftoken_local#">
 
<!--- Move Session to Request FOR READS ONLY!!!--->
<cflock timeout="30" throwontimeout="No" type="READONLY" scope="SESSION">
	<cfset structappend(request,Duplicate(session))>
</cflock>
 
 
<cfparam name="request.show_js_message" default=true>
<cfif NOT isdefined("request.login")>
	<cflocation url="#request.root#/login/index.cfm" addtoken="no">
<cfelseif request.login NEQ "yes">
	<cflocation url="#request.root#/login/index.cfm" addtoken="no">
<cfelseif request.show_js_message eq true>
	<!---script>This isn't used anymore');</script>
	<cfset request.show_js_message = false--->
</cfif>
 
<cfset curr_time = timeformat( now(), "HHmmss" )>
<cfset start_down_time = "220000">
<cfset end_down_time = "070000">
<!---Not sure what this really does but left in code to make sure it doesn't mess anything up--->
<cfquery name="status" datasource="#request.DATASOURCE#">
	select * from table where commrun = (select max(commrun) from commruns)
</cfquery> 
 
<!--- if the time is during the down time or cycles are running, display the message --->
<cfif (((curr_time GT start_down_time or curr_time LT end_down_time)) or (commrunstatus.status NEQ "C" and commrunstatus.status NEQ "Z")) and FindNoCase('OCS/',cgi.PATH_INFO,1) and cgi.SERVER_NAME neq 'liwzs31b'>
	<cflocation url="#request.root#/dsp_ocsdown.cfm" addtoken="no">
	<cfabort>
</cfif>
 
<!--- This is an error template.  I don't think it works anymore Is there a way to make it work? --->
<cfif request.debug NEQ 1>
	<cferror type="EXCEPTION" template="#request.root#/includes/customtags/errortemplate_excp.cfm" mailto="#request.default_email#">
	<cferror type="REQUEST" template="#request.root#/includes/customtags/errortemplate.cfm" mailto="#request.default_email#">
</cfif>
<!---  global container for oft used queries/SQL --->
<!--- store the cycles query in the request scope...wchich goes into the session scope too --->
<cfif NOT IsDefined("request.cycledates")>
	<cfquery name="GETCYCLEdata" datasource="#request.DATASOURCE#" cachedwithin="#CreateTimeSpan(0,1,0,0)#">
		SELECT		*
		FROM		table
		ORDER BY 	cyclecode, cd DESC
	</cfquery>
	<cfset request.cycledates = GETCYCLEdata>
</cfif>
 
<!--- store the cycles query in the request scope...wchich goes into the session scope too --->
<cfif NOT IsDefined("request.markets")>
	<cfquery name="GetData" datasource="#request.DATASOURCE#" cachedwithin="#CreateTimeSpan(0,1,0,0)#">
		SELECT		*
		FROM		table
		ORDER BY 	COLUMN
	</cfquery>
	<cfset request.markets = GetData>
</cfif>
<!--- People in particular groups get a longer session --->
<CFQUERY name="get_normal_Groups" datasource="#request.DATASOURCE#" cachedwithin="#CreateTimeSpan(0,15,0,0)#">
	select * from table
	where groupid  in (myGroupIDs) and groupname = '#request.groupname#'
</CFQUERY>
<!--- People in particular groups get a longer session --->
<CFQUERY name="get_Special_Groups" datasource="#request.DATASOURCE#" cachedwithin="#CreateTimeSpan(0,15,0,0)#">
	select * from table
	where groupid  in (myGroupIDs) and groupname = '#request.groupname#'
</CFQUERY>
<!--- Determines which users receive which session time out limits --->
<CFIF get_normal_Groups.recordcount GTE 1>
	<CFSET request.time = 240>
<CFELSEIF get_Special_Groups.recordcount GTE 1>
	<CFSET request.time = 60>
<cfelseif FindNoCase('something',cgi.path_info )>
	<CFSET request.time = 60>
<cfelse>
	<CFSET request.time = 20>
</CFIF>
 
<cfapplication name="MyApp" 
			sessionmanagement="Yes"
            sessiontimeout="#createtimespan(0,0,request.time,0)#">
 
<!---HERE IS THE 2ND APPLICATION.CFM FILE THAT HANDLES I BELIEVE THE LOGIN PROCESS I'M WONDERING IF I CAN JUST EXTEND THIS FROM THE MAIN APPLICATION.CFM FILE WHEN I CONVERT IT TO AN APPLICATION.CFC-------->
 
 
<!----------------------------------------------------------------------->
<!--- I BELIEVE THIS CAN BE EXTENDED FROM THE ORIGINAL APPLICATION.CFC
 
Purpose:  This is the application.cfm file for the login dir.  Do not do a 
		login check in here.  This dir will not be secure, so don't put 
			anything good in it.
--->
<!----------------------------------------------------------------------->
<!--- go get the global vars These is used on the main application.cfm file so do I even need this here --->
<cfinclude template="GlobalVars.cfm">
 
<!---End of Setting global variables--->
 
<!--- is the DB alive? --->
<cfif not( CGI.SERVER_NAME EQ 'DEV_ServerName' OR CGI.SERVER_NAME EQ "Dev_IP" ) and request.debug EQ 0>
	<cfmodule template="../includes/customtags/dbcheck.cfm">
</cfif>
 
<!--- setup the session vars via an application instance --->
<!--- Should make note to users, cookies are required --->
<cfapplication name="MY_APP"
               sessionmanagement="Yes"
               setclientcookies="Yes"
               sessiontimeout="#createtimespan(0,0,request.time,0)#">
 
<!--- this is supposed to make the cfid/cftoken cookie expire when the browser closes--->
<cfset Variables.cfid_local = Cookie.CFID>
<cfset Variables.cftoken_local = Cookie.CFTOKEN>
<cfcookie name="CFID" value="#Variables.cfid_local#">
<cfcookie name="CFTOKEN" value="#Variables.cftoken_local#">
 
<!--- These are used on act_login.cfm to allow admins to log in as anyone --->
<cfif CGI.SERVER_NAME EQ 'DEV_ServerName' OR CGI.SERVER_NAME EQ "Dev_IP">
	
	<CFSET admin_ssn = "USER_SSN">
	<CFSET admin_pin = "USER_pin">	
</CFIF>
			   
<!--- Move Session to Request Not sure what to do with this--->
<cflock timeout="30" throwontimeout="No" type="READONLY" scope="SESSION">
	<cfset structappend(request,Duplicate(session))>
</cflock>
 
<!--- This is another error handling template How should I handle this if I want to convert these 2 Application.cfc to a CFC
 
If you notice the mailto values equal variables from the main application.cfm file.  Do I really need them here as well--->
<cfif request.debug NEQ 1>
	<cferror type="EXCEPTION" template="#request.root#/includes/customtags/errortemplate_excp.cfm" mailto="#request.default_email#">
	<cferror type="REQUEST" template="#request.root#/includes/customtags/errortemplate.cfm" mailto="#request.default_email#">
</cfif>

Open in new window

0
Comment
Question by:rudodoo
1 Comment
 
LVL 27

Accepted Solution

by:
azadisaryev earned 2000 total points
ID: 22927472
there're too many unknowns in your code to even try to re-write it into an Application.cfc: the code in all the custom tags, cfincludes, etc used in your code above will determine where that code should go in your Application.cfc file... without knowing what is in those files it is not possible to tell you where they should go.

but, in a nutshell:

1) application name & session management settings go in a pseudo-constructor in Application.cfc:
<cfcomponent>
<cfscript>
THIS.name = "MyApp";
THIS.sessionmanagement = "yes";
THIS.sessiontimeout = createtimespan(0,0,30,0);
</cfscript>

<!--- other functions go below the above constructor --->
</cfcomponent>

2) any variables that do not change from request to request and do not depend on user's session should be put into application scope and will thus be defined in onApplicationStart method.
in your case at least dsn names, default email, start_down_time and end_down_time will be defined here.
onApplicationStart method is executed only once per application - when a first request to an application page is received after application has timed out or server has been restarted. if you make changes to application-scope variables you will need to either restart the server or call onApplicationStart() fro your code to re-init your application.

3) session initialization and session-specific vars are defined in onSessionStart method. i.e. if you are storing some user data in session scope, that structure should be initialized here.

4) request-specific variables are defined in onRequestStart method. these can include user login status checks, request timeout setting, redirects, checking if request falls on server downtime, etc. if needed, you can assign previously defined application and session scope vars to request-scope vars here.

5) use onError and onMissingTemplate methods to define your error handling. this is too big a topic to put in a reply here - there are huge chapters in cf books devoted to this - , so google around and check out big cf name's blogs for examples and tips.

ray camden at coldfusionjedi.com and ben nadel at bennadel com have lots of blog posts on Application.cfc-related topics - you may want to check these out.

hth
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question