Friday, November 18, 2011

Working with Class in Coldfusion

It has been like months I've abandoned this blog, seems it dusts and rusts the every pages. *taking broom and start cleaning*

The day has come for every programmer in the world, for those who haven't, to catch up with new (not that really new, at least been a decade) and popular concept of programming : OOP.

Why do I think so ? well to me, first and all . it's COOL.

I don't say it's better, faster, reliable, or anything sounds other than COOL. read this blog before you decide to join the club http://www.codinghorror.com/blog/2007/03/your-code-oop-or-poo.html.

Regardless what they say, lets us just start having fun with Class and the OOP Concept, and how do we gonna apply that in real using ColdFusion.

I assume you've already understand about the concept of OOP and what characteristic it has. so here I will go with the basic : How to define the Class in ColdFusion.

in Coldfusion, we defined the class using a file with extension .cfc, e.g. classname.cfc. We can put the file under the webroot together with the the other coldfusion files or any specific folder as we wish as long as we setting it up in the coldfusion administrator. (see the adobe coldfusion website for detail)

Inside the file, we define the class with tag . and inside the we can defined the functions related to this class using tag. here's the sample.



....

we don't have to name the component as we do in Java. In Coldfusion, the name of class is same with the name of the file. let say if we have a coldfusion cfc with name employee.cfc, the object name when once we want to initiate will be "employee". and to invoke the function inside the class, we just simply call it like employeeclass.functionthatdosomethinghere(param1,param2,...).

alright, we have through the very basic, can wait to go for some real action ? I will explain you in more detail about making a simple coldfusion class.

But before we do that, think as if we are a programmer who been asked by our boss to work on a certain problem. the problem is that you are now work in Employee System project, where it maintains employee data.
Boss asks you to make a class to manage employee data, add new employee and edit their data. (lets start with this simple first)

How we gonna do that ?

start with a problem solving design about our plan to make it work. I usually grab a sheet of paper and put some a scratch to figure it out. I figure the employee, the functions it has, the parameters and... um.. I think the plan is seems like this image below.


can it it work like that ? ........

I'm totally kidding, a bad scratch anyway.

there're certain kind of methodology to define a relation between one class to another class, the parameters that are used to be transferred from one to another. But, I found it's too difficult.
besides, I'm a kind of person who better scratch badly instead of creating a visually beautiful template to define a relation of classes. Read some article relate to Class definition in web.
but we just gonna do it simple now.

go back to the problem, thinking OOP is a matter of understanding the meaning of object you want to create. The problem is boss asked us to make an object to maintain employee data, that able to input, and edit. I came up with idea to make a class with name employee, where there's 2 function there, get employee data and set employee data.

Class name : employee.cfc
Function getEmployee(emp_id,field_name)
Function setEmployee(emp_id,field_name,field_value)

Then, I will start to begin with the programming. first, I will create the basic structure in the file.










Well, we've got the scratch now. next we will going into the detail. but firstly, assume there is a database that keep the employee data inside. I assume you've already familiar how create database and setting the Datasource in Coldfusion. if you're not, check this web site http://www.quackit.com/coldfusion/tutorial/coldfusion_datasource.cfm.

let say we set the datasource name to "mydb" in Coldfusion administrator and we have table temployee, with following data structure.
Table Name : temployee
Fields :
emp_id varchar(20)
emp_name varchar(100)
emp_address varchar(255)
birth_date datetime
emp_email varchar(255)

Getting the employee data in the database is fairly simple using tag, please see Coldfusion documentation for detail.

so lets go back to the class problem. the scratch of employee.cfc is ready, the database is ready as well. we have also understand about what we need to do. so we can now "upgrade" each getemployee () and setemployee() function to what it should be.

BUT.... I will continue my writing later, coz I need to do something, BRB and stay tuned.

Friday, February 4, 2011

ColdFusion Structure and Array

In this Post, I would to show you how to use Array and Structure. let make example, we want to keep employee data containing employee Id, Name, birth date and Address. here is how we can define the structure variable.

<cfset personaldata = StructNew()>
<cfset personaldata.EmployeeId = "1">
<cfset personaldata.EmployeeName = "Fernando Torres">
<cfset personaldata.BirthDate = "1 Jan 1985">
<cfset personaldata.Address = "Jakarta">



then, how bout if we want to keep more than one employee ? we can use the combined structure and array like this



<cfset employees = ArrayNew(1)>

<cfset employees[1].personaldata = StructNew()>
<cfset employees[1].personaldata.EmployeeId = "1">
<cfset employees[1].personaldata.EmployeeName = "Theemployee A">
<cfset employees[1].personaldata.BirthDate = "1 Jan 1985">
<cfset employees[1].personaldata.Address = "Jakarta">

<cfset employees[2].personaldata = StructNew()>
<cfset employees[2].personaldata.EmployeeId = "2">
<cfset employees[2].personaldata.EmployeeName = "Theemployee B">
<cfset employees[2].personaldata.BirthDate = "23 mar 1985">
<cfset employees[2].personaldata.Address = "Surabaya">



simple isn’t it ? I think that’s all for this post. Cheers !

Sunday, January 30, 2011

Date Add in Javascript (Tips & Trick)

Hi Guys, Good day to you ! allow me repost a programming tips that I copied from my another blog. enjoy !


I came across from a problem where I found a little difficulty using the existing Javascript standard date functions. especially when I need to 'add' the date forward or backward. let say, for example, I want to know what is the date one month after 'now', or what is the date 7 days from now ?

I'm so familiar with Coldfusion where it provide dateAdd() function where you can use to add a date with a certain unit of time. Unfortunately, there's none as easy as this in Javascript.

Therefore I created my own Javascript functions that works similar with it. here is the code.

 //Simple Date Add Function
function dateAdd(datepart,number,objDate){
y = objDate.getFullYear();
m = objDate.getMonth()+1;
d = objDate.getDate();
hr = objDate.getHours();
mn = objDate.getMinutes();
sc = objDate.getSeconds();
newY=y;
newM=m;
newD=d;
if (datepart== 'y'){
newY = parseInt(y) + parseInt(number);
} else if (datepart== 'm') {
newM = parseInt(m) + parseInt(number);
} else {
newD = parseInt(d) + parseInt(number);
}
objNewDate = new Date(newY,newM,newD,hr,mn,sc);
return objNewDate;
}


This function took 3 parameters,

- Datepart. the option is "y" for year, "m" for month and "d" for day. currently this is the only options provided ^^, sure you can expand the usability on your own if you would so.


- Number. Number of units of datepart to add to date (positive, to get dates in the future; negative, to get dates in the past). Number must be an integer.


- objDate. Javascript Date/time object



Example :



function dateAddSample() {
example1 = dateAdd("m",3,new Date()); //add current date 3 months forward
example2 = dateAdd("m",-6,new Date()); //add current date 6 months backward
example3 = dateAdd("d",7,new Date()); //add current date 7 days forward
alert(example1);
alert(example2);
alert(example3);
}

That's all the code, hope it helpful. cheers !

Easy Block Facebook

Probably some of you have same problem with me, got a lot of important , but you are person who easily drown in distraction surfing in web sites such as facebook, twitter, youtube or else.

I got some tips you can use if you want to block those websites so you will unable to access it before you finish those important work. this tips can works in Windows without you need any additional application.

well, It simple as you edit a file named ‘hosts’ which placed in folder C:\Windows\System32\drivers\etc. here you go.

1. Open that hosts file using notepad, make sure you run the notepad as administrator. in Win7 you can right-click the Notepad’s icon in your start menu and click the run as administrator option

2. add the list of websites that you want to block. here is mine.

 
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 www.facebook.com
127.0.0.1 www.detik.com
127.0.0.1 www.twitter.com



3. Save it and done !.



it is easy right ?



This things works by redirect any of the listed domains and IPs to localhost 127.0.0.1. For example, if I access my Facebook using the browser, then my computer will do is automatically process those request and redirect me to specified IP which in this case is my localhost’s IP. this is very simple so that you don’t need any additional application to block or filter the websites.



That’s the tips from me, hope useful. keep productive !

Thursday, December 30, 2010

Happy New Year 2011

Helloo Guys !

It almost new year already, and it has been months I abandoned this blog. sorry that I’ve been really busy.

I’m not thinking about something End-year special coding or tips to post at now, I just would to wish you belated

Merry Christmas 2010

and wish you with upcoming

Happy New Year 2011

I hope I can do better next year, I hope I can bring more to you guys a more new knowledge.

Thank you for reading ^^

Monday, October 11, 2010

Windows Control Panel Run Command

I’ve just found my friend working very well with keyboard, minimize the usages of mouse.

then below is keyboard shortcut to access the control panel for Windows, to make you looks more cool on working :)

 

Control panel tool Command
-----------------------------------------------------------------
Accessibility Options control access.cpl
Add New Hardware control sysdm.cpl add new hardware
Add/Remove Programs control appwiz.cpl
Date/Time Properties control timedate.cpl
Display Properties control desk.cpl
FindFast control findfast.cpl
Fonts Folder control fonts
Internet Properties control inetcpl.cpl
Joystick Properties control joy.cpl
Keyboard Properties control main.cpl keyboard
Microsoft Exchange control mlcfg32.cpl
(or Windows Messaging)
Microsoft Mail Post Office control wgpocpl.cpl
Modem Properties control modem.cpl
Mouse Properties control main.cpl
Multimedia Properties control mmsys.cpl
Network Properties control netcpl.cpl
NOTE: In Windows NT 4.0, Network
properties is Ncpa.cpl, not Netcpl.cpl
Password Properties control password.cpl
PC Card control main.cpl pc card (PCMCIA)
Power Management (Windows 95) control main.cpl power
Power Management (Windows 98) control powercfg.cpl
Printers Folder control printers
Regional Settings control intl.cpl
Scanners and Cameras control sticpl.cpl
Sound Properties control mmsys.cpl sounds
System Properties control sysdm.cpl

Thursday, September 16, 2010

Update Collation SQL Server

Still talking about implementing foreign characters to your application. in SQL Server database we know what so-called Collation. you need to set the collation of the database and the table’s column to collation type that support to keep the foreign characters into the table. what I’m going to show here, is a solution for a problem that you may have when you want to alter table collation in a situation where there’s numerous number of tables to alter. hope this below SQL script might help you

 declare   
@NewCollation varchar(255)
,@Stmt nvarchar(4000)
,@DBName sysname
set @NewCollation = 'Thai_CI_AS' -- change this to the collation that you want to use
set @DBName = DB_NAME()
declare
@CName varchar(255)
,@TName sysname
,@OName sysname
,@Sql varchar(8000)
,@Size int
,@Status tinyint
,@Colorder int
declare curcolumns cursor read_only forward_only local
for select
QUOTENAME(C.Name)
,T.Name
,QUOTENAME(U.Name) + '.' +QUOTENAME(O.Name)
,C.Prec
,C.isnullable
,C.colorder
from syscolumns C
inner join systypes T on C.xtype=T.xtype
inner join sysobjects O on C.ID=O.ID
inner join sysusers u on O.uid = u.uid
where T.Name in ('varchar', 'char', 'text', 'nchar', 'nvarchar', 'ntext')
and O.xtype in ('U')
and C.collation != @NewCollation
and objectProperty(O.ID, 'ismsshipped')=0
order by 3, 1
open curcolumns
SET XACT_ABORT ON
fetch curcolumns into @CName, @TName, @OName, @Size, @Status, @Colorder
while @@FETCH_STATUS =0
begin
set @Sql='ALTER TABLE '+@OName+' ALTER COLUMN '+@CName+' '+@TName+ isnull ('('
+convert(varchar,@Size)+')', '') +' COLLATE '+ @NewCollation
+' '+ case when @Status=1 then 'NULL' else 'NOT NULL' end
begin try
exec(@Sql)
end try
begin catch
--error catch
set
print error_message()
end catch
fetch curcolumns into @CName, @TName, @OName, @Size, @Status, @Colorder
end
close curcolumns
deallocate curcolumns


you can run the script in SQL server 2005 or above, well, that’s a little trick for me, hope can help.



Thanks to Kim that had helped me on this :)