TreeMap of Integers in ColdFusion - coldfusion

I need to efficiently search ranges of integers in ColdFusion. These ranges have different lengths and they can have "holes" (they are not continuous).
I thought that a Java TreeMap called from ColdFusion would be a efficient solution. However, I sadly discovered that the TreeMap is alphabetically ordered and I need numeric order. I tried to apply the JavaCast function to the key of the map but it didn't work. Apparently, ColdFusion was expecting the key to be a String and not an Int.
Here is the sample code (without the JavaCast attempt) to illustrate my situation:
<!--- Create the TreeMap --->
<cfset map = createObject("java", "java.util.TreeMap").init()>
<!--- Add Ranges to it --->
<!--- From 1 to 3 return "Range 1". --->
<cfset t = StructNew()>
<cfset t.finish = JavaCast("int", 3)>
<cfset t.value = "Range 1">
<cfset map.put(1, t)><!--- This 1 is the one I tried to JavaCast --->
<!--- From 4 to 10 return "Range 2". --->
<cfset t = StructNew()>
<cfset t.finish = JavaCast("int", 10)>
<cfset t.value = "Range 2">
<cfset map.put(4, t)>
<!--- From 20 to 50 return "Range 3". --->
<cfset t = StructNew()>
<cfset t.finish = JavaCast("int", 50)>
<cfset t.value = "Range 3">
<cfset map.put(20, t)>
<!--- From 75 to 80 return "Range 4". --->
<cfset t = StructNew()>
<cfset t.finish = JavaCast("int", 80)>
<cfset t.value = "Range 4">
<cfset map.put(75, t)>
<!--- Debug. Output the Map content --->
<cfoutput>Debug - Ordered map: #map.toString()#</cfoutput><br />
<!--- Search number --->
<cfset key = 9>
<cfset entry = map.floorEntry(key)>
<!--- Debug. Output entry found --->
<cfoutput>Debug - Entry found: #entry#</cfoutput><br />
<!--- Debug. Output result --->
<cfif isNull(entry)>
<cfoutput>Too small</cfoutput>
<cfelseif key lte entry.getValue().finish>
<cfoutput>Debug - Result: #entry.getValue().value#</cfoutput>
<cfelse>
<cfoutput>Too large</cfoutput>
</cfif>
And this is the output generated by the code above:
Debug - Ordered map: {1={FINISH={3},VALUE={Range 1}}, 20={FINISH={50},VALUE={Range 3}}, 4={FINISH={10},VALUE={Range 2}}, 75={FINISH={80},VALUE={Range 4}}}
Debug - Entry found: 75={FINISH={80},VALUE={Range 4}}
Debug - Result: Range 4
When I was expecting it to be:
Debug - Ordered map: {1={FINISH={3},VALUE={Range 1}}, 4={FINISH={10},VALUE={Range 2}}, 20={FINISH={50},VALUE={Range 3}}, 75={FINISH={80},VALUE={Range 4}}}
Debug - Entry found: 4={FINISH={10},VALUE={Range 2}}
Debug - Result: Range 2
So my question here is, what do I have to do to get the TreeMap working as I want, which is, with numeric order?
By the way, I'm using ColdFusion 9.
Thank you very much!

(From comments)
Very first thing that comes to mind is you are not casting all of the key values as integers, so they are treated as strings when the TreeMap performs its comparisons. That would explain why floorEntry returns the wrong results. It is comparing strings, not integers. Try casting all of the keys as integers, and it should work as expected.
<cfset map.put( javacast("int", 1), t)>
...
<cfset map.put( javacast("int", 75), t)>
<cfset key = 9>
<cfset entry = map.floorEntry( javacast("int", key) )>

Related

Split and Sort an Array by date, getting blank array places

I am just confusing myself with this one. I have a data log that is saved on the database as something like this:
Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
<!--- all other tracking info --->
I am trying to pull from this log only the discharges and sort/filter them by date. This log is saved in the database as one column varchar(MAX). I need to sort this data and only pull the discharge dates / reason using regex and then apply a filter. The end product should end up with me putting that array in a table with PATIENT | DISCHARGE DATE | DISCHARGE REASON My code right now is giving me some errors as it is leaving me with some empty array values. My question is that I need to remove those blank values and sort by the date, but I am unsure where to begin.
<cfquery name="getDischarge" datasource="#this.dsn#">
select PatientsName, LogData from Patient
</cfquery>
<cfoutput query="getDischarge" group="PatientsName">
<cfif LogData neq "">
<cfsavecontent variable="str">
#LogData#
</cfsavecontent>
<cfset possibilities = reMatch("Discharge Date?:(\d\d?/\d\d?/\d{4})\s*Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<cfset dateArray = ArrayNew(2)>
<cfloop index="w"
array="#possibilities#">
<cfif w NEQ "">
<!--- create 1 dimensional temp array to hold DATE | REASON --->
<cfset tempArray = ArrayNew(1)>
<!--- trim our regex to have only the date & then DateFormat --->
<cfset theDate = #Mid(w, 16, 11)#>
<cfset formatDate = #dateformat('#thedate#','mm-dd-yyyy')#>
<!--- use our regex to find the reason for discharge --->
<cfset theReason = reMatch("Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<!--- append our DATE | REASON to 1d temp array --->
<cfset ArrayAppend(tempArray, '#formatDate#')>
<cfset ArrayAppend(tempArray, '#theReason#')>
<!--- append our 1d array to our 2d array to output matching DATE | REASON --->
<cfset #ArrayAppend(dateArray, '#tempArray#')#>
</cfif>
</cfloop>
<cfdump var="#dateArray#">
</cfif> <!--- logdata neq "" --->
</cfoutput>
To me, logically, this should work and omit blank values, but this is what I get when I dump this data:
Looks like you are struggling with the Jakarta ORO regular expression engine that is used in ColdFusion. reMatch/reMatchNoCase are terrible when it comes to capturing. Java on the other hand offers the POSIX regular expression engine.
<cfset data = [
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
More stuff...",
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/16/2012
Reason for Discharge:lorem ipsum."
]>
<cfset result = queryNew("DISCHARGE_DATE,DISCHARGE_REASON", "VARCHAR,VARCHAR")>
<cfloop array="#data#" index="line">
<cfset dischargeDate = reMatchGroupNoCase("Discharge Date:([0-9]{2}/[0-9]{2}/[0-9]{4})", line)>
<cfset dischargeReason = reMatchGroupNoCase("Reason for Discharge:([^\n]*)", line)>
<cfset hasDate = (arrayLen(dischargeDate) eq 1)>
<cfset hasReason = (arrayLen(dischargeReason) eq 1)>
<cfif hasDate or hasReason>
<cfset rowIndex = queryAddRow(result, 1)>
<cfif hasDate and isDate(dischargeDate[1])>
<cfset querySetCell(result, "DISCHARGE_DATE", dateFormat(dischargeDate[1], "yyyy-mm-dd"), rowIndex)>
</cfif>
<cfif hasReason>
<cfset querySetCell(result, "DISCHARGE_REASON", dischargeReason[1], rowIndex)>
</cfif>
</cfif>
</cfloop>
<cfquery name="orderedResult" dbType="query">
SELECT
*
FROM
[result]
ORDER BY
[DISCHARGE_DATE] ASC
</cfquery>
<cfdump var="#orderedResult#">
And here is the function you need:
<cffunction name="reMatchGroupNoCase" access="public" output="false" returnType="array">
<cfargument name="regex" type="string" required="true">
<cfargument name="value" type="string" required="true">
<cfset LOCAL.result = []>
<cfset LOCAL.Pattern = createObject("java", "java.util.regex.Pattern")>
<cfset ARGUMENTS.regex = LOCAL.Pattern.compile(ARGUMENTS.regex, bitOr(LOCAL.Pattern["CASE_INSENSITIVE"], LOCAL.Pattern["UNICODE_CASE"]))>
<cfset LOCAL.buffer = ARGUMENTS.regex.matcher(toString(ARGUMENTS.value))>
<cfset LOCAL.length = LOCAL.buffer.groupCount()>
<cfloop condition="LOCAL.buffer.find()">
<cfloop from="1" to="#LOCAL.length#" index="LOCAL.i">
<cfset LOCAL.value = LOCAL.buffer.group(
javaCast("int", LOCAL.i)
)>
<cfif isNull(LOCAL.value)>
<cfcontinue>
</cfif>
<cfset LOCAL.result.add(LOCAL.value)>
</cfloop>
</cfloop>
<cfreturn LOCAL.result>
</cffunction>
I recommend this approach:
with q1 as (select 'Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge). ' logData
)
select substring(logData, patindex('%Admission Date: %', logdata) + 16
, 10) admitDate
from q1
where logData like '%Discharge Date:%'
That returns 08/01/2012. You may have some complications for a variety of reasons, but the general idea should work.

Coldfusion 10 cfloop errors

I am getting an error after upgrade from coldfusionOX to coldfusion 10.
Error Occurred While Processing Request Complex object types cannot be
converted to simple values.
The expression has requested a variable or an intermediate expression
result as a simple value. However, the result cannot be converted to a
simple value. Simple values are strings, numbers, boolean values, and
date/time values. Queries, arrays, and COM objects are examples of
complex values. The most likely cause of the error is that you tried
to use a complex value as a simple one. For example, you tried to use
a query variable in a cfif tag.
It occurs at line " cfloop index="local.thisRight" list="#rights#" ". It seems like ColdFusion does not like the "rights" here.
Anyone can give me some help? Thanks so much.
<cfif local.profile.rights.profile.self is not "">
<cfquery name="local.getAffiliations" datasource="#Request.readerDSN#">
SELECT tblPersonsToAffiliations.affiliationID, tblPersonsToAffiliations.rights, tblAffiliations.affiliationType, tblPersonsToAffiliations.relationshipType
FROM tblPersonsToAffiliations INNER JOIN tblAffiliations
ON tblPersonsToAffiliations.affiliationID = tblAffiliations.affiliationID
WHERE tblPersonsToAffiliations.personID IN (#local.profile.rights.profile.self#)
AND (tblPersonsToAffiliations.archived IS NULL
OR tblPersonsToAffiliations.archived = '')
</cfquery>
<cfif local.getAffiliations.recordCount is not 0>
<cfloop query="local.getAffiliations">
<cfif local.getAffiliations.relationshipType is "interested">
<cfset local.thisRelationshipType = "provisionalMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][thisRelationshipType] = affiliationID>
</cfif>
<cfelse>
<cfset local.thisRelationshipType = "fullMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = affiliationID>
</cfif>
<cfif isNull(rights)>
<cfelse>
<cfloop index="local.thisRight" list="#rights#" >
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRight#")>
<cfset local.profile.rights[affiliationType][local.thisRight] = ListAppend(local.profile.rights[affiliationType][local.thisRight], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRight] = affiliationID>
</cfif>
</cfloop>
</cfif>
</cfif>
</cfloop>
</cfif>
</cfif>
A bit earlier in your code you do this:
<cfif local.getAffiliations.relationshipType is "interested">
I think you need the same query name prefix in front of "rights" that is used when evaluating "relationshipType".
Try this:
#local.getAffiliations.rights#
HTH!
I am betting it is failing on this line:
<cfloop index="local.thisRight" list="rights" >
You are attempting to use the string "rights" as a list. My first reaction would be that you need to make that:
<cfloop index="local.thisRight" list="#rights#" >

Checking for key existence in structure

I have a variable named #cfData# which contains an array of structures. As it's clear from the image, for 1st structure array there are 6 keys and for 2nd, only two keys,viz date and open.
If I run a common loop, to go through each and every key, I will get an error at second array element. So the following only works when all the keys are present in the structure:
<cfset blockedtotal = 0 />
<cfset bouncetotal = 0 />
<cfset blocked = 0/>
<cfset datetotal = 0 />
<cfloop array = #cfData# index = "i">
<cfset blockedtotal += i.blocked />
<cfset bouncetotal += i.bounce />
</cfloop>
After reading online, I got an idea of using StructKeyExists where I think I can proceed in the following way:
<cfif structKeyExists(cfData,"bounce")>
<cfoutput>Bounce:#cfData.bounce#"/></cfoutput>
<cfelse>
<cfoutput> Bounce : [none]<br/></cfoutput>
</cfif>
But I am wondering, where exactly should I insert the above code inside the cfloop? Please advise if my approach is wrong.
Update:
Thanks guys. I got it running by using the following code based on the answers and it's running fine:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "date")>
<cfset counter++>
<cfoutput>#counter#</cfoutput> Date is: <cfoutput> #i.date#</cfoutput> <br/>
</cfif>
</cfloop>
you don't need a "common loop". You can loop through each struct with
<cfloop array="#cfData#" index="i">
<cfloop collection="#i#" item="key">
struct with key '#key#' has data: #i[key]#
</cfloop>
</cfloop>
Of, if you need to decide if the struct has certain key, do something:
<cfloop array="#cfData#" index="i">
<cfif structKeyExists(i, "someKey")>
<cfset counter++>
</cfif>
</cfloop>

ColdFusion 9 - Error: Invalid list index 0

I thought I had this one licked...
But I can't seem to get it tracked down...
Funny thing is - the list delete function works... And performs the desired task...
But I still get this error...
Invalid list index 0. In function ListDeleteAt(list, index [, delimiters]), the value of index, 0, is not a valid as the first argument (this list has 12 elements). Valid indexes are in the range 1 through the number of elements in the list. The error occurred on line 735.
Code below
<cfset pwlist = "#add.pwlist#">
<cfset curlist = "#add.pwlist#">
<cfset ud = "#session.demshinuser_id#">
<cfoutput>
#curlist#
<br>
<br>
#pwlist#
<br><br>
#ud#<br>
<cfset newlist = ListDeleteAt(curlist, ListFind(pwlist,ud,","), ",")> <-- Error Here
#newlist#
</cfoutput>
<cfquery name=Update DATASOURCE="#ds#">
update shindates
set
pwlist = '#newlist#'
where shinid = '#shinid#'
</cfquery>
<cfif src is "cpwupc">
<cflocation url="upc.cfm" addToken="no">
</cfif>
<cfif src is "hcpw">
<cflocation url="list.cfm?typeid=#add.typeid#" addToken="no">
</cfif>
Found on Goog - trying this too to check if it a bad list thing... and ud isn't in list in first place...
<cfset pos = listfind(pwlist,ud)>
<cfif pos>
do list delete
<cfelse>
error. element not found. do something else
</cfif>

ColdFusion ImageWrite to Amazon S3

This is a strange one - I have 'virtually' identical code on 2 pages, it works on one but not the other. The incredibly unhelpful error message, "S3 Error Message." doesn't shed any light on where I'm going wrong with this.
The code below is identical on both pages - the only (very slight) difference between the 2 pages is the way imgLink is generated - on the working page it is obtained from a single source (an XML feed) and cfset, on the none-working page imgLink is initially set as 'none' and then a number of sources are checked until it finds one - it's still cfset in the same way, and I have a cfif to make sure it's valid before processing. HOWEVER - I have also tried hardcoding the source (i.e., pasting in the value that would usually be in the imgLink cfset) and it still fails.
I've debugged this in every way I can possibly think in the last day, without success. So, I guess I'm looking for pointers as to what else I should look at that could be causing it to fail.
The full error returned is -
An error occurred when performing a file operation create on file s3://mybucket/1577-67BC4EF7-1B21-866F-32E95DF67F3336C6-f.jpg.
The cause of this exception was: org.apache.commons.vfs.FileSystemException: Unknown message with code "S3 Error Message."..
And my code is;
<cfscript>
this.name ="Object Operations";
this.s3.accessKeyId = "accessKey";
this.s3.awsSecretKey = "secretKey";
this.s3.defaultLocation="EU";
</cfscript>
<!--- CFImage Stuff --->
<!--- S3 Permissions --->
<cfset perms = [{group="all", permission="read"}]>
<!--- Create the Images ---->
<cfset imageuuid = '#CreateUUID()#'>
<cfset imagefull = '#getid.id#-#imageuuid#-f.jpg'>
<cfset imagemain = '#getid.id#-#imageuuid#-m.jpg'>
<cfset imagethumb = '#getid.id#-#imageuuid#-t.jpg'>
<cfimage action="read" name="img1" source="#imgLink#">
<!--- Create the full size image 505 x N --->
<cfif img1.height GT img1.width>
<cfif img1.width GTE 505>
<cfset ImageResize(img1,'505','')>
</cfif>
<cfelseif img1.width GT img1.height>
<cfif img1.width GTE 505>
<cfset ImageResize(img1, '505','')>
</cfif>
</cfif>
<cfset ImageWrite(img1, "s3://mybucket/#imagefull#")>
<cfset StoreSetACL("s3://mybucket/#imagefull#","#perms#")>
<!--- Create the main size image 251 x N --->
<cfif img1.height GT img1.width>
<cfif img1.width GTE 251>
<cfset ImageResize(img1,'251','')>
</cfif>
<cfelseif img1.width GT img1.height>
<cfif img1.width GTE 251>
<cfset ImageResize(img1, '251','')>
</cfif>
</cfif>
<cfset ImageWrite(img1, "s3://mybucket/#imagemain#")>
<cfset StoreSetACL("s3://mybucket/#imagemain#","#perms#")>
<!--- Create the thumbnail 52 x 52 --->
<!--- resize image to 52 pixels tall if width is greater then height --->
<cfif img1.height GT img1.width>
<cfset ImageResize(img1,'52','')>
<cfset fromX = img1.Height / 2 - 26>
<cfset ImageCrop(img1,0,fromX,52,52)>
<!--- resize image to 75 pixels wide if height is greater then width --->
<cfelseif img1.width GT img1.height>
<cfset ImageResize(img1,'','52')>
<cfset fromY = img1.Width / 2 - 26>
<cfset ImageCrop(img1,fromY,0,52,52)>
<cfelse>
<cfset ImageResize(img1,'','52')>
<cfset ImageCrop(img1,0,0,52,52)>
</cfif>
<cfset ImageWrite(img1, "s3://mybucket/#imagethumb#")>
<cfset StoreSetACL("s3://mybucket/#imagethumb#","#perms#")>
Just realised I hadn't added my solution to this, so here it is- The folder in which the 'non-working' code was in had it's own Application.cfc, which didn't include the S3 elements in the code posted above. The 'working' code did have that in the corresponding Application.cfc.
Not quite sure why that had to be in Application.cfc when it was at the
The this object in application.cfc is an application component and this on ColdFusion page is just a structure variable. Put <cfdump var=#this#> in both places, application.cfc and yourfile.cfm, to see the difference.