Things I've Learned
My technical blog. Hard won knowledge, Useful info, and even occasional Wit and Wisdom. If it takes me a while to figure something out, I will usually post it here and (hopefully) save someone else the trouble. -Neil Moncur
Wednesday, February 29, 2012
Identifying data type in literals without casting
• M (deciMal)
• D (Double)
• F (Float)
•L (Long)
So, for example, if you do the following:
decimal salesTaxRate = 6.5;
The compiler will interpret the literal "6.5" as a double, and you're going to have problems. But, just append a"D" and you're good. Like so:
decimal salesTaxRate = 6.5M;
Now the compiler interprets 6.5 as a fixed-point decimal, instead of a floating point double.
Why the compiler can't just assume that the double literal needs to be cast to a decimal, I can't say. Maybe one of the C# experts out there can say?
Friday, July 15, 2011
Saving XML to Database By Way of HTTP Post
- I am processing information from a third party. They hit a specific ColdFusion cfm page with some HTTP Post parameters.
- One of those Post parameters contains some XML. The XML contains the data that I am interested in.
- I get the XML from the HTTP Post parameters using GetHTTPRequestData(). This takes the information from the Post, and puts it into a struct.
- <cfset variables.parameter_raw="getHTTPRequestData()">
- I take the XML and pass it into xmlParse(). This parses the XML, and dumps the XML into a very usable struct.
- <cfset variables.parameter="xmlParse(variables.parameter_raw)">
- Then, before doing anything else, I dump the XML into a log file. This way, even if everything else goes wrong, I still have a copy of the XML for debugging or whatever.
- <cflog file="foo" text="#serializeJSON(variables.parameter)#">
- Then I extract the variables I need from the XML, and pass them into a CFC. This is the tricky part. For some reason, if you try to pass the parameter into the CFC only by name, it will add a bunch of invisible XML stuff to it, making it too long for the database column I want to save it to. So, in order to just get the value, without all the other wacky XML added on, add ".xmlText" to the end. See example below:
- <cfset data_access_object.save_data(data_id="variables.parameter.data_idb.xmlText,
data_value=variables.parameter.data_value.xmlText, data_code=variables.parameter.data_code.xmlText)>
- That should do it. That ".xmlText" is really hard to debug in this context if you don't know about it. Hopefully this will help somebody else out there.
Sunday, October 03, 2010
Simple Java to make sure a directory exists before doing a cffile upload
Simple Java to make sure that a directory exists before doing a cffile upload:
<cfset CreateObject("java", "java.io.File").init("C:\Inetpub\wwwroot\myNewDirectory\images").mkdirs() />
So, for example, you might do something like this:
<cfset CreateObject("java", java.io.File").init("c:\uploads\#dateformat(now(), 'yyyymmdd')#") />
You can just do the above before every upload. The first time it will create the directory, and every time after that it will just do nothing.
Thanks to cfgears for reminding me how to do this:
http://www.cfgears.com/index.cfm/2009/5/19/Tapping-in-to-ColdFusions-underlying-Java-capabilities
Thursday, September 30, 2010
listFind vs. listContains
listContainsNoCase(list, substring)
This function just finds the substring, and tells you which item contains that sub string. It is important to understand that this item is not searching for a list item, just for a sub string. Consider the following example:
listContainsNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Text")=5Notice how it found that item number 5 has the word "Text" in it, even though "Text" is not the complete item.
listFindNoCase(list, value)
This function finds a value in a list, and tells you which item matches the value. It compares the value to the list item, and only accepts complete matches. So, consider the following examples:
listFindNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Text")=0Notice how in the first example above, no item was found. This is because there is no single item with value = "Text". However, it did find "Simple Text" in the second example above.
listFindNoCase("Date,Decimal,Image,Integer,Long Text,Rich Text,Simple Text", "Simple Text")=7
Of course the same is true for listFind, and listContains, but those are case-sensitive.
Incidentally, I put this one in the "duh" category, because I think that the names are reversed. The first usage should be named listFind, and the second should be named listContains.
Hope this helps.
Monday, September 13, 2010
Resolving Samsung Captivate Windows 7 (64 bit) connection problem
------ NOTE ------
See the updates at the end of this post. Although many people have reported in comments that this fix worked for them, it actually stopped working for me after a few days. Consequently I have actually returned my Captivate in favor of a different phone that actually works.
------ END NOTE ------
On Friday I purchased a new Samsung Captivate from Costco. Before I get into the USB connectivity issues, let me just say that this is a great phone. Great speaker, great microphone, great ambient-noise management, great processor speed, great camera, outstanding screen, tons of memory, the list goes on. However, I encountered some serious problems getting it to connect to my computer. It took two days to get the problems resolved. Amazingly, neither this problem nor the solution is documented online. So, here is what I learned. I don't make any recommendations that you should try this, or any guarantees whatsoever that any of this will work for you, or that it will not damage your phone, etc, etc. Try it at your own risk, you have been warned. However, I did eventually get it to work for me, and am now much happier with my phone. Here's what I did.
First, the computer is a newish, capable, reliable laptop running Windows 7 Home Premium (64 bit). It is a pretty new install of Windows, and there are no known hardware problems. When I complained to the guy at the AT&T store, he admitted that other customers have complained about the same problem. So I don't think that the problem was with my computer.
When I first attempted to connect my phone to my computer, Windows quickly reported that the driver install failed. I made several attempts, all of which failed. Next I tried on a Vista (32 bit) box. The driver install seemed to work OK, but it kept reinstalling over, and over, and over again. Like every 3 minutes, over and over again, times infinity. Weird.
So, after two days of research, and trial and error, and visiting 2 AT&T stores, here is what I learned.
Samsung Captivate (and I believe other Galaxy class phones) use a software package called Kies to manage your phone's connection to your computer. By default, when you connect the Captivate (and probably most Galaxy class phones) to a USB port on Windows, it is only willing to talk to Kies. If you don't have Kies on the computer, then the connection will fail.
The bad news is that Kies is not available in the U.S. (!?). I had to download it from Samsung's Singapore website. Worse, Kies is kind of lame. It looks nice, is user friendly, and is a good effort, but isn't very useful and just got kind of got in the way.
The good news is that installing Kies also installs the drivers. After installing Kies, Windows immediately recognized my phone. So that's good.
The better news is that it turns out you may not actually have to install Kies at all. You can change the default USB setting on the device (which I have done). If you go to settings > Applications > USB settings, you will see that the default USB behavior is set to "Samsung Kies". I changed mine to "Mass Storage". Now when I connect my Captivate to my computer, it doesn't try to talk to Kies. Instead, it just pretends to be an "External Mass Storage" device which Windows has no problem recognizing (think external hard drive, or thumb drive).
Next, when I connect my Captivate to the computer, Windows makes that happy little two-tone chime that means something has successfully connected. Then on the Captivate, I just have to tell it to mount on my computer. There is a little status bar along the top of the screen. If you touch that bar and drag it down, there are some useful toggle buttons and a section called "Ongoing". Under ongoing there is a "USB connected" entry. When I click "USB connected", it asks me if I want to mount my phone on Windows. If I click "Mount", then I can access my phone just like an external hard-drive. I can copy music or pictures directly onto or off of the phone. Easy-peasy.
So, at least now I have a way to get stuff onto and off of my phone, and I don't have to use Kies. This is good. Also, I reformatted the internal SD card (read somewhere online that this helps).
Again, I don't promise or guarantee or warranty or say that any of this will work for you. Just that it worked for me. Good luck!
Now I would just like to take a moment to talk directly to Samsung.
Samsung, listen, you are a big company. You are a respected brand. You make good products. You spend millions of dollars advertising them. But even though your phone is widely available for purchase across the U.S., it cannot connect to Windows 7 without your users going to extensive lengths to research and troubleshoot, and the software for your phone isn't even available in the U.S. This is a problem. It's not like this is a special advance order phone direct from Japan. This is a widely advertised, commercially available phone that consumers can buy at any AT&T store, Costco, Sam's Club, Best But, etc. Yet they cannot connect their phone to their computers, unless they download software from Singapore, or change unfamiliar settings.
Please fix this. Either release your software in the U.S., or include instructions in the packaging or on your website on how to change the USB settings. Better yet, on your phones sold in the U.S., just default them to "Mass Storage" mode, and include a quick-start paper on how to mount the phone as an external hard drive.
Ok, that's all for now. Will report if the situation changes.
------ UPDATE 10/3/2010 ------
Unfortunately, the fix described below stopped working for me a few days ago. That was the last straw. Although I liked the Samsung Captivate, I decided that I wasn't going to keep a phone that lacks basic driver support and can't even figure out how to connect to a computer. So, I returned my Captivate to Costco, and bought an iPhone instead.
------ UPDATE 10/4/2010 ------
On 10/2/2010 Anonymous commented: "I cannot believe this phone has been out for now over 3 months and no fix. Shame on Samsung; this is why people turn to macs and iPhones, lazy companies."
I agree. Putting the time, effort, and R&D money into building a fabulous phone, and then failing to deliver basic USB drivers (even just simple Mass Storage drivers) is absurd in the extreme. That's like spending months building a fabulous house, and then failing to include something simple like a door. Eventually no matter how great the house is, the owner will get tired of climbing into the house through a window, and just buy a house--any house--with a door.
------ END UPDATES ------
Thursday, September 09, 2010
ORM is not configured for the current application
ORM is not configured for the current application
Very mysterious, and took a while to troubleshoot. In my Application.cfc, in the onRequestStart method, there was a <cfset ormreload()> . I had put it there thinking that while I was doing some development it would be nice to have the ORM reloading all of time as I make changes. However, it looks like it was interfering with the ORM's sequencing, and on Ajax page loads it was causing ColdFusion to erroneously report back that the ORM was not loaded. I simply moved the ormReload to onApplicationStart, and the problem seems fixed.
Wednesday, March 24, 2010
Knowing What To Do In An Emergency
As the various nominees were talking about their background and qualifications, a man in the audience near me suddenly collapsed. His knees buckled and he simply sprawled backwards (luckily a half filled box of papers cushioned his fall). He was completely inert and unresponsive.
This was so unexpected. Most of us in the room knew the man from our neighborhood, one of nature's authentic nice guys. There was a collective gasp from the 30-40 people in the room. Everyone just stood there for a beat or two; it took a few moments to compute what was happening.
I asked his wife if he is diabetic, she said no. I got out my phone, dialed 911, and asked if he was breathing. He wasn't, so I hit the call button. As I started talking to the 911 dispatcher, somebody began administering chest compressions.
Fortunately, a friend had been talking recently about what to say when you call 911. As soon as the dispatcher answered, I knew exactly what to say:
Me: I am at ----- ----- Junior High School in [city]. A 50 year old man has just collapsed and is not breathing. He is not diabetic. Someone has started administering CPR.
Dispatcher: You say he is not breathing. Is he having a seizure?
Me: He does not appear to be having a seizure, although his eyes appear to have rolled up into his head.
Dispatcher: Stay on the line. I am going to put you on hold while I get an ambulance. Could you have someone go to the front of the school to meet the ambulance?
The good news is the man started to revive while I was on hold. He seemed surprised to be on the floor, and was totally unaware of the drama that had occured around him. He assured everyone that he was alright, and left the room to get some fresh air. His wife declined the ambulance (they are expensive) and everyone went home safely.
Some takeaways from last night's drama:
1) When you call 911, stay calm. Clearly express where you are (give the address if you know it, especially if you are on a cell phone), what happened, how old the person is, and any important / relevant details (e.g. "They are performing CPR", or "The person is bleeding a lot from [this] area"). Try to give correct information. Answer questions clearly.
2) When the person started administering CPR, he didn't seem like he was sure what to do. I don't know how they determined that the man wasn't breathing. I didn't see anyone feel under his nose, so I am guessing that they just looked at his chest. Nobody felt for a pulse. Nobody tried slapping him on the cheek or speaking loudly into his ear. Someone probably should have done these things before starting CPR.
(Disclaimer: I am not a doctor of medical expert in any way. This is simply my untrained opinion. Any EMTs out there, please feel free to correct me if I am wrong)
It is my understanding that administering CPR to someone whose heart is still beating can cause harm. Don't get me wrong; I am glad that somebody was willing to try to step up and help, and I know that hindsight is 20/20. So, this leads to my third point:
3) Know what to do in an emergency. Even basic knowledge of what to do in an emergency will save lives. Take basic CPR / First Aid classes. The American Red Cross offers them on a regular basis for a moderate fee. Hospitals, colleges, and many large workplaces will occasionally offer the classes for free or for a small fee. Take advantage of these opportunities to learn. My workplace is going to offer a 3 hour CPR-AED-First Aid class in a couple of weeks. I plan on being there.
Although it can seem unlikely that you will ever need to know this stuff, imagine what it would be like to watch somebody die because you don't know basic stuff that you could have learned at a couple of evening classes. Now imaging taking a couple of 2-3 hour classes, and knowing what to do in an emergency, and saving someone's life. Less than 10 years ago, I had to give my infant daughter mouth-to-mouth resuscitation after she stopped breathing. Because I had taken first aid as a 14 year old Boy Scout, I knew how to resuscitate an infant (you cover mouth and nose with your mouth), and possibly saved her life. I am sure glad that I had a basic understanding of what to do--at least enough to keep her alive until the ambulance got there. Sign up for a class. Give up an evening or afternoon (or two) to learn these basic skills. You may just save a life. And it may be someone you love.