| Discussion Home | About | Threads By Date | Search |
Posts
Content: Yesterday I posted the Language features of Coldfusion 10. Here is one of the features I tested and it was simply great. CLOSURES. A closure is an inner function. The inner function can access the variables in the outer function. You can access the inner function by accessing the outer function. See the example below. <cfscript> In the above example, the outer function returns a closure. Using the hellourdu variable, the outer function is accessed. It sets the helloWord argument. Using this function pointer, the closure is called. For example, helloInUrdu("Iftikhar"). Observe that even after the execution of outer function, the closure can access the variable sets by the outer function. In this case, using closure, two new functions are created. One adds Assalam-oa-Alykum to the name. And the second one adds مرحبا to the name. helloInUrdu and helloInArabic are closures. They have the same function body; however, store different environments. The inner function is available for execution after the outer function is returned. A closure is formed when the inner function is available for execution. As seen in the example, even after the outer function is returned, the inner function can access the variables in the outer function. Closure retains the reference to the environment at the time it is created. For example, the value of a local variable in the outer function. It makes closure an easy to use and handy feature. File: test.cfm (509 b)
Content: What are Web Services In simple a web page designed to be consumed by software, not by humans. Who remembers WDDX? It Could “serialize” data to Allaire-specified XML and Could output from page request, pass to caller. But it had some Problems. It was Non-standard (though see openwddx.org) and aasn’t a function call/response approach. So technically speaking
So How does web services work ? The basic Web services platform is XML + HTTP. XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions. The HTTP protocol is the most used Internet protocol. Web services platform elements:
But the good news is CFML hides the need for you to understand any of these. Many languages support web services like Java, ASP.net, Perl etc. They often use API which involve lots of coding. Coldfusion support very easy publication and consumption of web services. For this you do not need to know JAVA, SOAP, WSDL,UDDI not even XML. Publishing Your First Web Service You only need to create a CFC function that returns that data, expose function as “remote”. If you’re new to CFCs’, they have many other benefits and features. Their use for web services is actually one of their easier purposes to understand. Building the Component Function <!--- hello.cfc ---> There you go, your first web service. Now any web service client (caller) can invoke this web service (hello.cfc) and its method (GetHello) Calling Your Web Service You Can call from any language/platform that supports web services Simply need to refer to:
For example http://localhost/demo/hello.cfc?wsdl Consuming our Web Service via Invoke To use the web service within CFML, use either CFINVOKE, CFOBJECT, or createObject CFINVOKE: Calls web service (using URL described earlier, with ?wsdl as querystring), names method to execute, and variable to hold returned result <cfinvoke Consuming our Web Service via CFObject/Createobject You can also use CFOBJECT/CreateObject instead. It is Slight difference from CFINVOKE
<cfobject webservice="http://localhost/demo/hello.cfc?wsdl“ name="fromhello"> Or <cfscript> There you go. Your first web service which can be called from any scripting Language
Content: OK, I agree that I should have masterd this long time but it took me time to study this concept as I was not getting out of the SQL Frame . After a long wait I finallay decided to tell some thing about ORM and hence I decided to do a post on the ORM basics. In this post, we will build a simple example to get a taste of ColdFusion ORM (CF-ORM) and during that we will also understand some of the basic concepts. ORM is object relational mapping and in ColdFusion, objects are created using CFC. CFCs that needs to be persisted are called persistent CFC and that is marked by setting ‘persistent’ attribute to true on the component. We also need to define what persistent fields will be there in a persistent CFC and that is defined using ‘cfproperty’. A field/property is marked persistent by setting persistent attribute to true on the cfproperty. By default, if the CFC is persistent, all its properties are considered as persistent unless you mark a property non-persistent. So typically ‘persistent’ attribute on the property is used only when you need to make that property non-persistent. Each persistent CFC in ColdFusion application maps to a table in the database and each property in the persistent CFC maps to a column in the table (not exactly true but we will come to that later.. For the time being lets keep it that way). We will use the cfartgallery datasource for this example which has Artists and Art tables. The first thing you need to do is – enable ORM for the application and define a datasource to be used (What is an ORM without a datasource?). ColdFusion ORM uses Application.cfc to define all the ORM specific settings. (If you haven’t started using Application.cfc for your application, its time to start using it!) Application.cfc Component { this.name="ArtGallery"; this.ormenabled="true"; this.datasource="cfartgallery"; } Note that the datasource setting defined here makes it the default datasource of your application which can be used by tags like cfquery, cfinsert, cfupdate, cfdbinfo. The same default datasource will be used by ORM as well.
Now that the application is configured, let us build the object and define the persistence information on it. To start with, we will first define the Artist.cfc Artists.cfc /** * @persistent */ Component {
File: CF-orm-2010.ppt (5.81 Mb)
Content: In ColdFusion 10, support for creating and publishing REST services has been added. The ColdFusion components can now be made available as REST services and these services can be consumed by various clients. REST stands for Representational State Transfer. It is an architectural style which is based on web standards and HTTP protocol. The idea here is to use HTTP protocol instead of complex mechanisms such as CORBA, RPC or SOAP to connect between the machines. In fact, the World Wide Web which is based on HTTP can be viewed as a REST based architecture. REST Architectural principles:A REST based application follows some architectural principles:
Getting Started with creating REST servicesNew attributes have been added to cfcomponent, cffunction and cfargument to aid in creating a REST service. Once a component is created it has to be registered with the Administrator and then it can be accessed via a HTTP request. <!--- component with attributes rest and restpath --->
<cfcomponent rest="true" restpath="/crudService">
<!--- handle GET request (httpmethod), take argument in restpath(restpath={customerID}), return query data in json format(produces=text/json) ---> <cffunction name="getHandlerJSON" access="remote" httpmethod="GET" restpath="{customerID}" returntype="query" produces="application/json"> <cfargument name="customerID" required="true" restargsource="Path" type="numeric"/>
<cfset myQuery = queryNew("id,name", "Integer,varchar", [[1, "Sagar"], [2, "Ganatra"]])>
<cfquery dbtype="query" name="resultQuery"> select * from myQuery where id = #arguments.customerID# </cfquery>
<cfreturn resultQuery>
</cffunction>
</cfcomponent>
The function ‘getHandlerJSON’ handles a GET request and accepts an argument customerID. Notice the additional attributes in cffunction tag. The attribute ‘httpmethod’ is used to specify the type of request this function would handle (GET,POST etc,.). Attribute ‘restpath’ is used to specify the path that should be used in the URL, here the path is specified in curly braces as {customerID}. When this type of notation is used it indicates that a value is passed in the URL and the same has to be copied to the functions’ argument. Therefore the name of the argument is same as the one specified in restpath. There are different ways to pass arguments to a REST service, I’ll explain this in my next post.
Content: Coldfusion 10 Finally is Public Image:
Content: With Coldfusion 10, we deep dive into the world of HTML5 video support through the CFMEDIAPLAYER tag Then(CF 9) : The cfmediaplayer tag could only support Flash videos,the likes of flv,f4v.
Play the snippet in Firefox or Chrome to view the webm video
The Flash and HTML5 players have been designed to look as identical as possible so as to support seamless fallback.
Now play the snippet in IE9,Firefox,Chrome and Safari . File: sample_webm.webm (9.78 Mb)
Content: CFScript is a language within a language. It is a scripting language that is like JavaScript but is simpler to use. Also, unlike JavaScript, CFScript only runs on the ColdFusion server; it does not run on the client system. CFScript code can use all the ColdFusion functions and expressions, and has access to all ColdFusion variables that are available its scope. CFScript provides a compact and efficient way to write ColdFusion logic. Typical uses of CFScript include the following:
Because you use functions and expressions directly in CFScript, you do not have to surround each assignment or function in a cfset tag. Also, CFScript assignments are often faster than cfset tags. CFScript provides a set of decision and flow-control structures that are more familiar than ColdFusion tags to most programmers. In addition to variable setting, other operations tend to be slightly faster in CFScript than in tags. You can use CFScript to create user-defined functions, or UDFs (also known as custom functions). You call UDFs in the same manner that you call standard ColdFusion functions. UDFs are to ColdFusion built-in functions what custom tags are to ColdFusion built-in tags. Typical uses of UDFs include data manipulation and mathematical calculation routines. You cannot include ColdFusion tags in CFScript. However, some functions and CFScript statements are equivalent to commonly used tags. For more information, see Tag equivalents in CFScript. Comparing tags and CFScriptThe following examples show how you can use CFML tags and CFScript to do the same thing. Each example takes data submitted from a form and places it in a structure; if the form does not have a last name and department field, it displays a message. Code: Using CFML tags cfif IsDefined("Form.submit")> <cfif (Form.lastname NEQ "") AND (Form.department NEQ "")> <cfset employee=structnew()> <cfset employee.firstname=Form.firstname> <cfset employee.lastname=Form.lastname> <cfset employee.email=Form.email> <cfset employee.phone=Form.phone> <cfset employee.department=Form.department> <cfoutput> Adding #Form.firstname# #Form.lastname#<br> </cfoutput> <cfelse> <cfoutput> You must enter a Last Name and Department.<br> </cfoutput> </cfif> </cfif> Using CFScript <cfscript> if (IsDefined("Form.submit")) { if ((Form.lastname NEQ "") AND (Form.department NEQ "")) { employee=StructNew(); employee.firstname=Form.firstname; employee.lastname=Form.lastname; employee.email=Form.email; employee.phone=Form.phone; employee.department=Form.department; WriteOutput("Adding #Form.firstname# #Form.lastname# <br>"); } else WriteOutput("You must enter a Last Name and Department.<br>"); } </cfscript>
Content: We don't often work with XML now, most of the data exchange is done through JSON. Most of the APIs that supported XML and JSON said XML is still a data type that will inevitably be a part of our lives for some time are now dropping XML support. That's why it's actually kind of exciting that ColdFusion 10 now supports XPath 2.0 in the xmlSearch() and xmlTransform() functions. I agree that I am decades behind developers who rule the web, but sill, after some Googling I worked out to put forward a demo of the functionality that is now available in ColdFusion 10. XPath is used to navigate through elements and attributes in an XML document. XPath is a major element in W3C's XSLT standard - and XQuery and XPointer are both built on XPath expressions.
Code: <!--- Create an XML document on which to test new XPath 2.0 functionality support. ---> <cfxml variable="bookData"> <books> <book id="101" rating="4.5"> <title>ZAVIA</title> <author>Ashfaq Ahmad</author> <published>Sep 1, 2004</published> <isbn>9693515870</isbn> </book> <book id="201" rating="4"> <title>Butt Parey</title> <author>Dr. Younis Butt</author> <published>November 1, 2007</published> <isbn>9693510151</isbn> </book> <book id="301" rating="4.5"> <title>KHARMASTIAN</title> <author>DR.MUHAMMAD YOUNAS BUTT</author> <isbn>9693510488</isbn> </book> </books> </cfxml> <!--- Groovy - now let's execute some XML Path queries. ---> <cfscript> // Get all of the ratings that are greater than or equal to 4.5. results = xmlSearch( bookData, "//book/@rating[ number( . ) > 4.0 ]" ); // Get the average rating of the reviews. results = xmlSearch( bookData, "avg( //book/@rating )" ); // Get a compoud result of the Title and Author notdes. Notice // that we can now create divergent results in the SAME path. // We don't need to create two completely different paths. results = xmlSearch( bookData, "//book/( title, author )" ); // Get all of the book's children EXCEPT for the ISBN number. // XPath 2.0 introduces some intesting operators like "except", // "every", "some", etc. results = xmlSearch( bookData, "//book/( * except isbn )" ); // XPath 2.0 now uses sequences instead of node-sets which allow // for more interesting data combinations. This only gets the // nodes from one collection that are NOT in the other collection. // We're using inline branching and merging! results = xmlSearch( bookData, "//book/( (title, published) except (isbn, published) )" ); // Get all of the ISBN numbers that use a 10-digit ISBN. XPath // 2.0 now supports regular exprdssion functions like matches(), // replace(), and tokenize() -- thought it is quicky and a // bit limited in patterns. results = xmlSearch( bookData, "//book/isbn[ matches( text(), '^\d{10}$' ) ]" ); // Iterate over one collection and map it onto the resultant // collection. We can now iterate inline within a path. results = xmlSearch( bookData, "for $b in (//book) return ( $b/published )" ); // We can now pass in params into our xmlSeach() calls. Notice // that the key, "title" is quoted - that is because XPATH is // case-sensitive. results = xmlSearch( bookData, "//book/title[ . = $title ]", { "title": "KHARMASTIAN" } ); // Get the given book, no matter what the casing. FINALLY, we // can case-insensitive searching in XML :) results = xmlSearch( bookData, "//book[ upper-case( title ) = 'Butt Parey' ]" ); // Debug the results. writeDump( results ); </cfscript>
Content: Coldfusion 10 Beta Launched. Language fearutes are as follows Support for for-in construct (for query) Image:
Content: In a recent discussion about an upcoming project, the idea of “reaching beyond the browser” came up. We wanted to provide students (our target audience) with an avenue to provide feedback in addition to the web. After our meeting we had two questions to answer: What was the best way to provide interaction to mobile devices, and what could we use in our current infrastructure that would allow us to talk to mobile devices? The answers were SMS (Short Message Service) and ColdFusion 8. Thanks to the awesome Macromedia Coldufusion MX 7 included a new Event Gateway that allowed a ColdFusion application to send and receive SMS messages. My next step was to set up a sample application, so our team members could see how it would work. I was able to find a good overview of how a CF application and a mobile device communicate using SMS here: http://livedocs.adobe.com/coldfusion/8/UseGateways_1.html What I couldn’t find was a good step-by-step CF SMS example, so I pieced together what I could find and created this one. In this tutorial we will set up a CF gateway and create a sample application that will receive SMS messages as votes for candidates and send a confirmation message to the voter. Step 1 - Locate your sms-test.cfg file in your CF installation directory under gateway\config\. Step 2 - Copy this file and save it in the same directory as vote.cfg or a name of your choosing. If you open this file you will see all of the settings that your Gateway Instance will use to send and receive messages. Since this is a test application, we will use the default settings that point the Gateway Instance to IP 127.0.0.1, port 7901, and phone number 5551212. Step 3 - Create a blank CFC called vote.cfc and save it under your web root in a folder of your choosing. This will be the file that processes all incoming SMS messages. Step 4 - Login to CF Administrator and make sure the SMS Test Server is running under Event Gateways and Settings. Step 5 - Create a new Gateway Instances in the CF Administrator under Event Gateways and Gateway Instances. Gateway ID: SMS Vote - 5551212 Gateway Type: SMS Gateway CFC Path: web root\subfolder\vote.cfc Configuration File: cf rootdir\gateway\config\vote.cfg Startup Mode: Automatic Step 6 - Start the SMS Vote - 5551212 Gateway Instance and make sure it is the only SMS Instance running. Step 7 - Now that your Gateway is up and running we will add our SMS processing code to our vote.cfc. First, we create our component and define our only method with the name onIncomingMessage. This is the method that the gateway will call whenever there is an incoming message. The only argument supplied by the gateway is a structure called CFEvent that contains information about the incoming message. <cfcomponent displayname="SMS Voting Listener" hint="Process SMS messages."> <cffunction output="no"> <cfargument required="yes" /> Next we pull the incoming message and the sender’s phone number out of the structure. <--- Get message data and sender number---> <cfset voterMessage=arguments.CFEvent.data.message /> <cfset voterNumber=arguments.CFEvent.originatorID /> We will also need to create a return structure to pass back to the gateway. The command value is set to submit which will send the return message, the gatewayid is set as the source address for the message, and the destination address is set as the voter’s phone number. <--- Set up return struct with needed arguments---> <cfset retValue=structNew() /> <cfset retValue.command="submit" /> <cfset retValue.sourceAddress=arguments.CFEvent.gatewayid /> <cfset retValue.destAddress=voterNumber /> Next we will use a switch statement to process the incoming message. If the message is “Shahrukh” or “Salman” then we will insert the vote and voter’s phone number into our database. We also set the return message that thanks our user for voting. If the incoming message wasn’t a candidate’s name then we return a “Not a voting option” message.
Note: Be sure to create a database and datasource (called “SMSVotes” in this example) for this code to use.
<--- Add vote to db if incoming msg is a name and set return msg ---> <cfswitch expression="#voterMessage#"> <cfcase value="Shahrukh"> <cfquery datasource="SMSVotes"> INSERT INTO Votes(Phone, Vote) VALUES ('#voterNumber#', 'Shahrukh') </< span>cfquery> <cfset retValue.shortMessage="Thank you for voting for " & voterMessage /> </< span>cfcase> <cfcase value="Salman"> <cfquery datasource="SMSVotes"> INSERT INTO Votes(Phone, Vote) VALUES ('#voterNumber#', 'Salman') </< span>cfquery> <cfset retValue.shortMessage="Thank you for voting for " & voterMessage /> </< span>cfcase> <cfdefaultcase> <cfset retValue.shortMessage="Not a voting option: " & voterMessage /> </< span>cfdefaultcase> </< span>cfswitch>
Lastly, we return our return structure with all the information the gateway needs to send our voter a confirmation message.
<--- Send the return struct to gateway ---> <cfreturn retValue /> </< span>cffunction> </< span>cfcomponent>
Step 8 - Now we need to test our application and to do this we will use a cell phone emulator that is installed with CF 7. Go to Start -> Programs -> Macromedia -> ColdFusion MX 7 -> SMS Client Application. This will launch a command prompt window along with a Java popup that is pre-filled with default connection information, which we will use.
Once you connect, you will see a simulated cell phone interface useful for testing purposes. You can type your message by using the keypad on the cell phone or by selecting the text box and typing with your keyboard. Try voting for Shahrukh, Salman, and a non-candidate to see if your application is working properly.
That’s it! That is all you need to do to get an example SMS application up and running. As laid out in the Macromedia link above, you will need to set up an account with a SMSC (Short Message Service Center) such as AT&T, Verizon, or Sprint to actually be able to send and receive SMS messages. This provider will receive all the incoming SMS messages over their mobile network and then forward that message over TCP/IP to your ColdFusion server. After the SMSC provides you with settings for your vote.cfg file, you will be SMS enabled.
I hope you found this tutorial useful. Now go forth and start texting!
|
Filter by Date
Filter by TagAllannouncement Benefit Flash General General Discussion Question Sticky Change View
Titles Change Sort
Alphabetically |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||