<< Adobe Pakistan User Group

Discussion Area

ask questions, discuss topics, solve problems

Discussion Home | About | Threads By Date | Search

Posts

1-10 of 16 | Next> | Last>>

Language Features - Using closures 1
Thread posted on Feb 25 by Tayyab Hussain

Title: Language Features - Using closures
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>
function helloTranslator(String helloWord)
    {
        return function(String name)
            {
                return "#helloWord#, #name#";
             };
    }
helloInUrdu=helloTranslator("Assalam-oa-Alykum");
helloInArabic=helloTranslator("مرحبا");
writeoutput(helloInUrdu("Iftikhar"));
//closure is formed.
//Prints Assalam-oa-Alykum, Iftikhar.
writeoutput("<br>");
writeoutput(helloInArabic("Hamdan"));
//Prints مرحبا, Hamdan.
</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)

Creating and Consuming Web Services with CFML
Thread posted on Apr 08 by Tayyab Hussain

Title: Creating and Consuming Web Services with CFML
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 

  • Web services are application components
  • Web services communicate using open protocols
  • Web services are self-contained and self-describing
  • Web services can be discovered using UDDI
  • Web services can be used by other applications
  • XML is the basis for Web services

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:

  • SOAP (Simple Object Access Protocol)
  • UDDI (Universal Description, Discovery and Integration)
  • WSDL (Web Services Description Language)

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 --->
<CFCOMPONENT>
<CFFUNCTION NAME="GetHello" ACCESS="REMOTE"
RETURNTYPE="String">
<CFRETURN "Hello World">
</CFFUNCTION>
</CFCOMPONENT>

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:

  • Domain name (and port, if needed) that’s
  • used to access code on CFMX
  • Directory in which CFC is located
  • CFC name
  • ?WSDL indicator

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
webservice="http://localhost/demo/hello.cfc?wsdl"
returnvariable="fromhello" method="GetHello">
<cfoutput>#fromhello#</cfoutput>

Consuming our Web Service via CFObject/Createobject

You can also use CFOBJECT/CreateObject instead. It is  Slight difference from CFINVOKE

  • They return an object representing the web service
  • Then can invoke method in CFML as with other objects

 <cfobject webservice="http://localhost/demo/hello.cfc?wsdl“ name="fromhello">
<cfoutput>#fromhello.GetHello()#</cfoutput>

Or

<cfscript>
fromhello=createobject("webservice","http://localhost/demo/hello.cfc?wsdl");
writeoutput(fromhello.GetHello());
</cfscript>

There you go. Your first web service which can be called from any scripting Language


ColdFusion ORM : The basics
Thread posted on May 19 by Tayyab Hussain

Title: ColdFusion ORM : The basics
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 {
    property name="artistid" generator="increment";
    property firstname;
    property lastname;   
    property address;
    property city;
    property state;
    property postalcode;
    property email;
    property phone;         
    property fax;
    property thepassword;
}

This is the most simplistic definition of the component where we have defined only the component and its properties names. Since the table for this CFC already exists in the database, we have not added any table specific information in this and we will let ORM infer all the information from the database. The only additional setting that we have added here is the ‘generator’ attribute which is used to auto-generate the primary key.

After the components are defined, the first request to this application (i.e a page in this application) will make CF-ORM do all the setup necessary (basically generation of hibernate configuration, mapping files, building the session factory etc). Once the setup is done, you are all set to work with the entities.

We will first list all the artists and here is what you need to do for that

listAll.cfm
<cfscript>
   artists = EntityLoad("Artists");
   writedump(artists);
</cfscript>

To load a particular Artists with its ID, here is what you do

 list.cfm

<cfscript>
   artist = EntityLoadByPK("Artists", 1);
   writedump(artist);
</cfscript>

There are several flavors of EntityLoad functions details of which can be read here

Let us now see how to perform insert and update on it.

save.cfm

<cfscript><br>   // Insert a new artist
   artist = new Artists();
   artist.setfirstname("Leonardo");
   artist.setlastname("Da vinci");
   artist.setcity("Paris");
   EntitySave(artist);
 
   writeOutput(artist.getartistid());// Update an artist
   artist = EntityLoadByPK("Artists", 2);
   artist.setcity("NewYork"); // artist is automatically updated.
</cfscript>

As we see in the above example, EntitySave is used to insert/update an object in the table. There are some important things to note here

  • EntitySave is an intelligent function which automatically finds if a new row needs to be inserted for the given object or whether an eisting row needs to be updated.
  • We called EntitySave for the insert here but not for update but even then artist ‘2′ gets updated. So how did that happen? Actually what happens here is when you load an artist object, it becomes associated with the hibernate session which keeps track of any changes in the object and automatically saves it when the session is flushed. We will talk about more about hibernate session in a later post. For the time being lets just say that Hibernate Session is a short-lived object that represents a conversation between the application and the persistence layer and also acts as the first level of cache.
  • We did not write any setter or getter method for artist’s properties in Artists.cfc but we are calling them here. That works because ColdFusion 9 automatically generates accessor methods for any property written in a CFC. More details on generated methods in a later post.
  • At line no 6, we called entitySave, but if you check the database, the row is not inserted yet. So when does that happen? Hibernate batches up all the operations till the end of the request or to be exact till hibernate session is flushed. ColdFusion ORM starts up a session when the first ORM method is called in the request and is automatically flushed when the request ends. The batching is done for performance reason so that hibernate executes the sql with the final state of the objects. It will be a huge performance bottleneck if ORM keeps executing sql for each changes in the object.

To delete an Artist, you need to call EntityDelete() passing the object to be deleted.

delete.cfm
<cfscript>
    artist = EntityLoadByPK("Artists", 15, true);
    EntityDelete(artist);
</cfscript>

Relationship

So far we have seen how to perform CRUD for a single entity. But in any application, there will be entities which are associated and ORM must load the associated object as well when loading a particular entity. For our example, an Art will have an Artist and when loading the art object, it should also load the associated artist. So lets build the model first after which we will see how to work with the association.

In cfartgallery, the table Artists has a one-to-many relationship with Art table, which are joined using the foreign key column ARTISTSID. This means that each artist has created multiple arts and each art is created by one artist. To represent this in the object model, each ARTIST object would contain an array of ART objects. Each ART object will also contain a reference to its ARTIST object thereby forming a bidirectional relation.

To achieve this, we will need to add an extra property ‘arts’ to ‘Artists’ that contains an array of ART objects for that Artist. The modified Artists.cfc would look like

/**
 * @persistent
 */
Component {
    property name="artistid" generator="increment";
    property firstname;
    property lastname;   
    property address;
    property city;
    property state;
    property postalcode;
    property email;
    property phone;         
    property fax;
    property thepassword;
    property name="arts" fieldtype="one-to-many" fkcolumn="artistid" cfc="Art" cascade="all" inverse="true";
}
Here is the Art.cfc
/**
 * @persistent
 */
Component {
    property name="artid" generator="increment";
    property artname;
    property price;
    property largeimage;
    property mediaid;
    property issold;
    property name="artist" fieldtype="many-to-one" fkcolumn="artistid" cfc="Artists" ;
}

Notice the artist property above which is of many-to-one type. Also notice that both the property use the same value for fkcolumn attribute i.e ‘artistid’ of Art table that references artistID pk of Artist table.

Since we have added a new persistent CFC (Art.cfc) after the application was loaded, we need to re-initialize the ORM for this application so that mappings for Art.cfc also gets generated. This can be done by calling ORMReload() method. There are some nice ways to do this but for the time being lets keep it simple by putting this in a separate page which we will call to reload ORM.

initializeORM.cfm

<cfset ormReload()>

If you load and dump Artist (using listAll.cfm), you should also see the associated art objects for artists.

Now let us create a new Art and associate it with an existing Artist.

artCreate.cfm

<cfscript>
    artist = EntityLoad("artists", 1 ,"true");
    art = new Art();
    art.setartname("landscape");
    art.setPrice(1500);
    art.setissold(false);
    art.setartist(artist);
    artist.addArts(art);
    EntitySave(art);
</cfscript>
If you notice line 7-8 above, we associate artist to art by calling art.setArtist(artist) as well as art to artist by calling artist.addArts(art). Hibernate needs us to do this in order to set up the bidirectional relation properly. Since it is a bidirectional relation, you must also decide which side will set the relation in the database. i.e which side of the relation will set the fkcolumn in the table. This is controlled by the “inverse” attribute of proeprty, which if set to true, tells hibernate that this is a inverse of the other relation and this side of relation should be ignored for persistance. If you don’t set inverse to true, Hibernate will unnecessarily fire two sqls for the same association.
So there you have it. We have seen how you can use ORM to perform the basic CRUD operations on entities. For more details, you can refer to the ORM doc and Hibernate docs. Please also go through the attached Power Point Slide

Note: EntityLoadByPK(”artists”, 1) & EntityLoad(”artists”, 1 ,”true”) are the same 
This method was added because it always gives the object right back and you don’t have to pass the boolean parameter. however it was not documented, Just went through an article and noticed.




File: CF-orm-2010.ppt (5.81 Mb)

ColdFusion 10: RESTful WebServices in ColdFusion - Introduction
Thread posted on May 18 by Tayyab Hussain

Title: ColdFusion 10: RESTful WebServices in ColdFusion - Introduction
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:

  1. Resource Identification: In a REST based architecture everything is a resource. Each of these resources should be identified with a URI.  In ColdFusion, the functions are identified as resources, each of these resources can then be invoked using an URI.
  2. Uniform and constrained interface: Every resource in a RESTful application should support HTTP common operations. The resources are manipulated with the HTTP protocol methods – GET, PUT, POST, DELETE.  In ColdFusion, the resources (functions) can be accessed via HTTP and each of these resource support the HTTP verbs. Depending on the verb specified in the request, the corresponding resource would be invoked.
  3. Representation oriented: REST allows resources to have different representation – plain, html, xml, json etc. A client can request a specific representation via the HTTP protocol. HTTP provides a simple content-type negotiation protocol between the client and the server. For example, an AJAX application may need data in JSON format, where as a Java application may need it in XML format.
  4. Stateless communication: In REST, the server doesn’t store the client session data. However, many techniques exist to exchange state information such as URI rewriting, cookies and hidden form fields. Also, for encryption REST can be used on top of HTTPS.

Getting Started with creating REST services

New 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.

Creating a RESTful Web Service

cfcomponent:

As mentioned above, functions in a CFC are the resources and are invoked using a HTTP request. To mark a component as a REST service, attributes rest and restpath are provided. The attribute rest is a boolean attribute, if true the component is enabled as a REST service. This is an optional attribute if restpath is specified in cfcomponent tag. The restpath attribute is used to specify the path using which the REST service would be accessed. If restpath is not specified then the path to the CFC would be used in the URI to invoke the service.

<!--- 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.

Complex data types in ColdFusion such as Array, Query, Struct etc, will be serialized to either JSON or XML format. The produces attribute specifies the mime type to which the data returned by the function will be serialized to. There is also an attribute ‘consumes’ that specifies the mime type of data present in the request body.

Coming to the function arguments, the new attribute restargsource specifies the way in which arguments are passed to a rest service. In this example it is path which indicates the argument will passed in the URL path itself. The other possible values include Matrix, Header, Cookie, Form and Query.

Registering a REST Service

There are two ways in which the REST services can be registered with the Administrator. You can either use the Admin API (functions defined in extensions.cfc) or use the function restinitApplicaiton. The former method requires a Administrator privileges and therefore would be available only to selected users. The function restInitApplication can be used to register a REST service and doesn’t require Administrator privileges.

restInitApplication(‘Absolute_path’,[‘Service mapping’])

The directory containing CFC files with with either rest=’true’ or with a restpath in the cfcomponent tag should be specified as the first argument. To identify the set of CFCs in a directory the Service mapping has to be provided. This is an optional fied, if there is an Applicaiton.cfc file present in the directory. In which case the Application name would be used to identify the REST services in the directory.

If any changes are made to any of the components in the registered directory then the corresponding service has to be refreshed. Again this can be done in the Administrator or a call to restInitApplication would refresh the registered service.

Accessing a REST service via HTTP

A RESTful Web service can be accessed either by using HTTP or HTTPS.  The URL to access a REST service in ColdFusion follows this format:

http://servername:port/context_path_j2ee/ rest/rest_application_name_or_serverMapping/restpath_or_componentPath/additionalrestPaths/params_ifany/

Here, the context_path_j2ee is applicable only if you have installed ColdFusion as J2EE on any of the Application servers. In which the context path would be cfusion (by default). However, this can be omitted on Standalone installation.

The string ‘rest’ in URL specifies that the request is for a REST service. ColdFusion has a servlet mapping for the same and would direct the request to the servlet that handles REST service. If there is a directory in the server webroot with the same name, then the servlet mapping has to be updated in web.xml file inside wwwroot\WEB-INF directory.

As mentioned earlier, while registering a REST service the application name specified in Applicaiton.cfc file would be used in the URL to access the REST service. If there is no Applicaiton.cfc then the specified service mapping would be used. To access a service specified in the component the restpath for the component has to be specified. However, if the restpath is not specified in the cfcomponent tag then the path to the CFC would be used in URL. Also, the functions defined in component can also have restpath defined and this has to be specified in the URL. If arguments are to be passed in the URL path then the same can also be specified.

In this example, the function ‘getHandlerJSON’, can be accessed using cfhttp:

<cfhttp url="http://localhost:8500/rest/restapp/crudService/1" result="restResult" method="GET"/>

Here restapp is the application name defined in Applicaiton.cfc file. /crudService is the restpath specified in the cfcomponent tag and the numeric 1 is the argument to the REST service. There are various ways in which arguments can be passed to a REST service, I’ll explain this in my next post.


Coldfusion 10
Thread posted on May 16 by Tayyab Hussain

Title: Coldfusion 10
Content:

Coldfusion 10 Finally is Public


Image:

CF10-HTML5 Video
Thread posted on Apr 09 by Tayyab Hussain

Title: CF10-HTML5 Video
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.

Now (CF 10):  The cfmediaplayer can now support HTML 5 videos , the likes of mp4,ogv,webm ; formats conditioned for video playback over the internet.These require no additional plugins. You can skin/style the player,use Javascript functions ,set event handlers and even set a customised title !

More on all that later!!

Snippet #1:  Just specify your HTML5 video in the source attribute of the cfmediaplayer tag

<cfmediaplayer name="player1" source="sample_webm.webm" />

Play the snippet in Firefox or Chrome to view the webm video

Alternately , you can use ColdFusion.MediaPlayer.setSource() method and add to your existing code.

Snippet #2 : Use the all new "type" attribute to tell your browser whether to play the same video as flash or HTML. This is pretty useful when you want to enforce a particular type of content to be sent to the client.
MP4 videos can play as flash and HTML videos. This essentially means that if you try to play an MP4 video with flash enabled,you see a flash player loaded (right click on the player and observe the "About Flash Player" info ) and when flash is disabled the HTML% player is loaded

Try this in Chrome to see the difference:

 
<cfmediaplayer name="player2" source="sample_mp4.mp4" type="html" />
  
<cfmediaplayer name="player3" source="sample_mp4.mp4" type="flash" />

The Flash and HTML5 players have been designed to look as identical as possible so as to support seamless fallback.

Snippet #3: Use the HTML source tag to set multiple videos as sources to the cfmediaplayer tag and let ColdFusion handle the video playback depending on video support by the browser

<cfmediaplayer name="player4" source="sample_webm.webm" type="html">
<source src="sample_mp4.mp4" type="video/mp4" />
</cfmediaplayer>

Now play the snippet in IE9,Firefox,Chrome and Safari .

You will see :
IE9 and Safari5 : playing sample_mp4.mp4 in HTML because it currently doesnt support WEBM
Firefox and Chrome : playing sample_webm.webm

A few things to consider :

1)Use the <!DOCTYPE html > and the <head/> <body> tags to write your cfm page for best results. A few browsers will not render the player properly otherwise!
2)Can't play your video ?  Are you using a web server ? Then make sure the right mime-types are added so that content can be served to the client.
3)Video playback is subject to browser support for formats,codecs and rendering.


File: sample_webm.webm (9.78 Mb)

About CFScript
Thread posted on Apr 03 by Tayyab Hussain

Title: About CFScript
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:

  • Simplifying and speeding variable setting

  • Building compact JavaScript-like flow control structures

  • Creating user-defined functions

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 CFScript

The 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>

CF10- XMLsearch() &amp; XMLTransform() now support Xpath 2
Thread posted on Mar 08 by Tayyab Hussain

Title: CF10- XMLsearch() & XMLTransform() now support Xpath 2
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>

Coldfusion 10 Beta - Language enhancements
Thread posted on Feb 24 by Tayyab Hussain

Title: Coldfusion 10 Beta - Language enhancements
Content:

Coldfusion 10 Beta Launched. Language fearutes are as follows

Support for for-in construct (for query)
Provide file content in the tag body
Callstack for ColdFusion functions
Function to get application metadata
Function to get disk space details
Application-specific In-memory file system
Secure file upload by verifying the MIME type
Implicit constructor for CFC
Method chaining for CFC methods
CFC Implicit notation
New function ArraySlice
New parameter merge supports arrayAppend
New parameter format added to LSParseDateTime function
New attribute runOnce added to cfinclude
New attribute timeout in cfstoredproc
New attribute maxLength in cfparam
New functions dateTimeFormat and lsDateTimeFormat
New function reEscape
The function replaceList takes delimiters
Modifications to the functions arraySort, listSort, and structSort
Implicit struct now supports use of : (colon) separator
The attribute output is ignored in the interface signature
FUNCTION is now a ColdFusion datatype
Dynamic references supported in query looping
New function invoke
New attribute secure in cfpop
New attribute group in cfloop
For-in constructs now support Java arrays
Enhancements to queryAddRow and queryNew functions
New function listRemoveDuplicates
Support for XPath 2.0 and XSLT 2.0 syntax


Image:

SMS Using Coldfusion Event Gateways
Thread posted on Feb 17 by Tayyab Hussain

Title: SMS Using Coldfusion Event Gateways
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

<< May 2013 >>
Su Mo Tu We Th Fr Sa
      01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Filter by Tag

All
announcement
Benefit
Flash
General
General Discussion
Question
Sticky

Change View

Titles
Summaries
Table
Clip
Clip & Comments
Full

Change Sort

Alphabetically
By Last Activity
By Date Posted
By Number of Comments
By Number of Views
By Author