How to pass a structure to CFComponent - coldfusion

This is how I am passing a string parameter to a component in ColdFusion 10:
<cfset DeptObj = New CompEmp('#Trim(DeptId)#','#Trim(DeptSecId)#')>
<cfset EmpStatus = DeptObj.GetEmp('NewHire')> ----> passing a string
My question is, how do I pass a ColdFusion structure to a function in a component instead of a string?
For example, if I have the following structure:
<cfset str_MyBioInfo = {myFName="#GetBio.FName#", myLName="#GetBio.LName#",
myBday="#GetBio.BDate#"}
And I want to pass str_MyBioInfo to a component, do I do it this way?
<cfset BioObj = New BioInfo()>
<cfset BioInfoDetail = BioObj.GetBio(str_MyBioInfo)> ----> ?
And in the <cffunction> I have MybioInfo structure as an argument like this:
<cffunction name="GetBio">
<cfargument name="str_MyBioInfo" type="Struct"> ---> ?
</cffunction>

I think you are looking for how to pass a collection of arguments to a function:
<cfset str_MyBioInfo = { FName="firstName"
, LName="LastName"
, BDate="YourdateOfBirth"
} />
<cfset BioObj = createObject("component", "BioObj")>
<cfset BioInfoDetail = BioObj.GetBio(argumentCollection = str_MyBioInfo)>
<cffunction name="GetBio">
<cfargument name="FName" type="String" required="true" >
<cfargument name="LName" type="String" required="true" >
<cfargument name="BDate" type="String" required="true" >
</cffunction>

Related

Coldfusion and Related Selects

For several years, I have used dropdowns to determine the requirements for a SQL query and have never needed to use dropdowns with ‘related selects’ until now. I found a sample on how to do this and I can create related selects (with static data) which works well, see below using my data relating to vehicles and makes.
However, when I come to apply this to my environment, and use dynamic SQL query data, I cannot get it to work. I am confident that all the pieces of code are working together (as it works perfectly for the static data), but if I try and build the data automatically and then create the string to pass to the queryNew function, it fails.
This is the working Content.cfc file with STATIC query strings/data array
<cfcomponent>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}])>
<cfset tblModel = queryNew("name,code,continent_id", "varchar,varchar,varchar", [{name:"TSERIES",code:"TSERIES",continent_id:"RENAULT"},{name:"MASTER",code:"MASTER",continent_id:"RENAULT"}])>
<cfset tblVoltage = queryNew("name,code", "varchar,varchar", [{name:"24 volt",code:"MASTER"},{name:"12 volt",code:"TSERIES"}])>
<cffunction name="getContent" access="remote" returntype="query" output="true">
<cfargument name="strTableName" type="string" required="true">
<cfargument name="strID" type="string" required="true">
<cfargument name="strName" type="string" required="true">
<cfargument name="intDistinct" type="numeric" required="false" default="0">
<cfargument name="selectedCol" type="string" required="false" default="0">
<cfargument name="selectedID" type="string" required="false" default="0">
<cfquery name="qryContent" dbtype="query">
select
<cfif arguments.intDistinct eq 1>distinct</cfif>
#arguments.strID# as theID,
#arguments.strName# as theValue
from #arguments.strTableName#
<cfif arguments.selectedID neq 0>
where #arguments.selectedCol# = '#arguments.selectedID#'
</cfif>
order by #arguments.strName#
</cfquery>
<cfreturn qryContent />
</cffunction>
</cfcomponent>
When I try and create the query using this method, nothing appears to work. I am looping through the database (from a query) and then creating a string using the required structure and the passing it to the function.
<cfcomponent>
<cfset XSTATIC = ""/>
<cfquery name="NOXVehicleModels" datasource="EBSNOX" >
SELECT DISTINCT VehicleMake
FROM [dbo].[NOX-Master]
WHERE VehicleMake IS NOT NULL
</cfquery>
<cfloop query="NOXVehicleModels">
<cfset XSTATIC = XSTATIC & "{name:'" & #Trim(NOXVehicleModels.VehicleMake)# & "',id:'"& #Trim(NOXVehicleModels.VehicleMake)# & "'},"/>
</cfloop>
<cfset XLEN=LEN(#XSTATIC#)/>
<cfset XSTATIC = MID(XSTATIC,1,XLEN-1)/>
<cfoutput>#XSTATIC#</cfoutput>
<cfset tblMake = queryNew("name,id", "varchar,varchar", [#XSTATIC#])>
I have created a separate cfoutput to test the string for structure etc and it appears to be correct, but it is just not passing to the querynew function. This is what the output looks like:-
{name:'CUMMINS',id:'CUMMINS'},{name:'DAF',id:'DAF'},{name:'IVECO',id:'IVECO'},{name:'MAN',id:'MAN'},{name:'MERCEDES',id:'MERCEDES'},{name:'RENAULT',id:'RENAULT'},{name:'SCANIA',id:'SCANIA'},{name:'VOLVO',id:'VOLVO'}
{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'},{name:'RENAULT',id:'RENAULT'}
What I have done so far:-
I have checked that the quotes are not important i.e. single/double.
I have tried to build the query outside of the tag.
Again, all appears (?) to be in order and I cannot understand that could be the problem. Possibly the structure is missing something before parsed to the function?
Any help would be greatly appreciated.
Thanks,
Jack

issues with sears api using coldfusion

I am trying to work with sears api and my process is failing due to my lack of understanding the documentation provided by sears
this is the sears url
https://www.searscommerceservices.com/question/seller-facing-api-changes-0216/
based upon what they have given as to how to generate the signature and authenticate i am trying something like this but i am getting an error:
Request Timed out
Here is my Try in CF
<cfscript>
function ISODateFormat(datetime) {
return '#DateFormat(datetime, "yyyy-mm-dd")#T#TimeFormat(datetime, "HH:mm:ss")#Z';
}
</cfscript>
<cfset variables.sellerid = '1234'>
<cfset variables.emailaddress = 'abc#domain.com'>
<cfset variables.secretkey = 'xxxx='>
<cffunction name="auth" access="public" returntype="any">
<cfset var myResult="">
<cfset sDate = ISODateFormat(now())>
<cfset StrtoSig = "#variables.sellerid#:#variables.emailaddress#:#sDate#">
<cfset secretKey = variables.secretkey>
<cfset x = hmac("#StrtoSig#","#secretKey#","HMACSHA256","utf-8")>
<cfreturn x>
</cffunction>
<cffunction access="public" name="getitems" returntype="any">
<cfset surl = 'https://seller.marketplace.sears.com/SellerPortal/api/attributes/v4?itemClassId=1234&sellerId=1234'>
<cfset getauth = auth()>
<cfset sheader = "authorization:HMAC-SHA256 emailaddress=#variables.emailaddress#,timestamp=#sDate#,signature=#getauth#">
<cfhttp url="#surl#" result="results">
<cfhttpparam name="authorization" type="HEADER" value="#sheader#">
</cfhttp>
<cfdump var="#results#">
</cffunction>

Extract 7z file in coldfusion

Can anyone help me by suggesting a function to extract a .7z file in ColdFusion? I use ColdFusion 10 and cfscript based code. Indeed we have the cfzip tag, but it only extracts .zip and .jar files.
You can use cfexecute, which unfortunately is not availble in cfscript, to execute the 7z extractor on your server and pass through the various commands to extract the file to a place of your choosing.
Luckily for you, it seems Raymond Camden has gone into it in some detail:
http://www.raymondcamden.com/index.cfm/2011/2/21/Working-with-RARs-in-ColdFusion
Function to unrar .rar file in given destination.. use cfexecute tag to run rar exe in command line
<cffunction name="Unrar" access="public" returnType="boolean" output="false">
<cfargument name="archivefile" type="string" required="true">
<cfargument name="destination" type="string" required="true">
<cfset var exeName = "">
<cfset var result = "">
<cfset var errorresult = "">
<cfif not fileExists(arguments.archivefile)>
<cfthrow message="Unable to work with #arguments.arvhiefile#, it does not exist.">
</cfif>
<cfif findnocase(".rar",arguments.archivefile)>
<cfset var exeName = expandpath("WinRAR\rar.exe")>
<cfset var args = []>
<cfif directoryExists(#arguments.destination#)>
<cfset args[1] = "x +o">
<cfelse>
<cfset directoryCreate(#arguments.destination#)>
<cfset args[1] = "x">
</cfif>
<cfset args[2] = arguments.archivefile>
<cfset args[3] = "#arguments.destination#">
</cfif>
<cfexecute name="#exeName#" arguments="#args#" variable="result" errorvariable="errorresult" timeout="99" />
<cfif findNoCase("OK All OK", result)>
<cfreturn true>
<cfelse>
<cfreturn false>
</cfif>
</cffunction>

Access function arguments from another function

I am trying to create a custom debug tool and I need to use a component with two separate functions in it. The first function (startTimer) has some arguments such as startCount and the other one (endTimer) has endCount. What I am trying to accomplish is something like the following code:
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="string" required="no">
</cffunction>
<cffunction name="endTimer" returntype="void" access="public">
<cfargument name="endActionTime" type="string" required="no">
<cfset finalTime = endActionTime - startTimer.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
</cffunction>
And this is how I am calling the function
<cfscript>
location = CreateObject("component","timer");
loc =location.startTimer(
startActionTime = getTickCount()
);
end = location.endTimer(
endActionTime = getTickCount()
);
</cfscript>
I guess I am having scope issues because when I am trying to run the code I am getting an undefined error on startTimer.startActionTime. What is the correct way to do something like this?
You can use the variables scope like so:
<cfcomponent>
<cfset variables.startActionTime = 0>
<cffunction name="startTimer" access="public" returntype="void">
<cfargument name="startActionTime" type="numeric" required="no">
<cfset variables.startActionTime = arguments.startActionTime>
</cffunction>
<cffunction name="endTimer" returntype="string" access="public">
<cfargument name="endActionTime" type="numeric" required="no">
<cfset finalTime = endActionTime - variables.startActionTime>
<!---Some SQL will go here to record the data in a db table --->
<Cfreturn finaltime>
</cffunction>
</cfcomponent>
From the Adobe Docs: Variables scope variables created in a CFC are available only to the component and its functions, and not to the page that instantiates the component or calls its functions.

parse url and title from string of multiple href tags in coldfusion

i need to parse the url and title from multiple href tags in a string regex...
i need to get each url and title into a variable
eg.
<DT>NJ Party Boat - Sea Devil of Point Pleasant Beach, NJ
<DT>test parse
<DT>google
Ok, if I understand correctly, I would do something like this:
<cffunction name="reMatchGroups" access="public" returntype="array" output="false">
<cfargument name="text" type="string" required="true" />
<cfargument name="pattern" type="string" required="true" />
<cfargument name="scope" type="string" required="false" default="all" />
<cfscript>
l = {};
l.results = [];
l.pattern = createObject("java", "java.util.regex.Pattern").compile(javacast("string", arguments.pattern));
l.matcher = l.pattern.matcher(javacast("string", arguments.text));
while(l.matcher.find()) {
l.groups = {};
for(l.i = 1; l.i <= l.matcher.groupCount(); l.i++) {
l.groups[l.i] = l.matcher.group(javacast("int", l.i));
}
arrayAppend(l.results, l.groups);
if(arguments.scope == "one")
break;
}
return l.results;
</cfscript>
</cffunction>
The above function returns groups for each regex pattern match.
You could use it like this:
<cfset a = reMatchGroups("This is a link", "href=[""']([^""|']*)[""'][^>]*>([^<]*)", "all") />
Which will give you an array of structs with the key-value pairs for each back reference in the regex. In this case the href and node text.