The amazing adventures of Doug Hughes

Archive for September, 2009

Intro to Flex CSS Styling

Black ButtonI’m going to start in on series of blog posts discussing using CSS to style your Flex applications. I’ll start with simply styling a button then move on to a more complex Flex component like the datagrid, then styling a custom component and finally I’d like to take a look at some of the changes coming to using CSS in Flex 4.

Styling your component inline
Often when you see people post a quick snippet of code online they will simply style the component inline.

(frequently found on quick example blog posts)

For example:

Here we assign the font color and font size of our button inline with our declaration of our button instance. The advantage of adding a style inline with the button is it’s somewhat easy to find the exact instance of the button that has a unique style and if your building something quickly its nice to add in the style on the fly as you code.

The disadvantage of styling inline is that the unique button style that you create isn’t all that easy to reuse. Sure you can copy and paste that code into each button declaration but if the client wants neon pink text instead of that killer green color that you picked out you’ll need to go back into each of your button instances and make the change. For that reason this is generally considered a bad practice and the chances you’ll get beat up at the Flex playground are relatively high at 9/10.

Declaring your Style in a Script Block
You’ll often see people declare all of their styles in a script block at the top of the page that they are working on. (Also frequently found on quick example blog posts)

For Example:


Button {
    color: #060;
    font-size: 14;
}
.neonPinkButtonStyle  {
    borderColor: #93f;
    color: #fff;
    cornerRadius: 6;
    fillAlphas: 1, 1, 1, 1;
    fillColors: #939, #939, #936, #936;
    textRollOverColor: #fff;
    themeColor: #90f;
}

Here we’re adding a style block that allows us to declare styles and we can reuse these styles. The advantage to this is that it’s fairly easy to find the style that your looking to edit (people generally add this style block at the top of the file). Secondly it’s more reusable than our inline code. If this page has 50 buttons with these two styles we should be able to quickly make changes to this one block of code and see the updated styles on all 50 buttons.
The disadvantage of adding a script block at the top of each mxml file is that your Flex application is mostly likely made up of many small mxml files and you’ll want to reuse the styles across the entire application. For that reason this is also generally considered a bad practice and the chances you’ll get beat up at the Flex playground are relatively high at 7/10.

Also of note is the way we declared styles for these two buttons. When we declare

‘Button’ (no dot in front) we are referring to all Flex components of type Button. When we declare the button instance we don’t have to specify a style name. When we set up a specific style name like ‘.neonPinkButtonStyle’ we have to reference that name when we declare the instance of the button as we did on our second button with the code styleName="neonPinkButtonStyle" (no dot needed)

Referencing a style from an external style sheet
More often than not, when you work on a project you’ll want to reference an external css style sheet.
For example:

< mx: Style source = "com/alagad/assets/style.css" / >

You ‘ll most often add the reference to your external style sheet in the main application file of your project. To make changes or add styles to your entire Flex application just open up your style sheet and make the needed changes.
Now if we have these styles posted somewhere in our css style sheet:

 Button
  {
  font-size:14;
  color: #006600;
  }
  .neonPinkButtonStyle
  {
  cornerRadius: 6;
  fillAlphas: 1, 1, 1, 1;
  fillColors: #993399, #993399, #993366, #993366;
  color: #ffffff;
  textRollOverColor: #FFFFFF;
  borderColor: #9933ff;
  themeColor: #9900ff;
  }

And we have two Buttons hidden somewhere deep in our Flex application, we will get the correct styling applied to our button instances and we can quickly make changes to all our button styles anywhere in our application by changing the reference to our one Button style in our style.css file. We can also change buttons that are specifically styled with the neonPinkButtonStyle, assuming they have been propertly referenced on the button instance by adding the styleName=&quot;neonPinkButtonStyle&quot; tag to the button instance. (note that the more specific style reference &quot;.neonPinkButtonStyle&quot; takes precedence over the more general ‘Button’ applied to all buttons in the application) This is generally considered a ‘good practice’ and the chances you’ll get beat up at the Flex playground are relatively low at only 3/10.
Lastly, I use the term ‘best practices’ and ‘bad practice’ very loosely. It’s always more importand to get figure out the workflow that works best for you. Maybe you style inline with the intentions of coming back and refactoring later. Or maybe you just leave it and move on to more pressing issues. If your boss only gives you two weeks to finish a project, just try to finish your project before the deadline, and then go out and get some sun. Chances are you won’t find any Flex bullies outside anyway.
This Style Explorer has been around for a while and I’m sure it’s help thousands of developers learn more about what attributes are easy to style in the basic Flex component library.

ColdFusion 9 ORM on Tomcat Gotcha

I am sitting here working through an application that makes use of the new ColdFusion 9 ORM capabilities. I have a simple business object that I created using EntityNew(), then populated it, and attempted to save it using EntitySave(). Nothing fancy and everything straight out of the docs.
The problem was, when I ran this code I would just get a blank screen no errors, nothing. I started up my application server Tomcat in the console and checked the output as I ran the script. Sure enough, there was a slew of Java errors being displayed with the first being

java.lang.NoClassDefFound: javax/transaction/Synchronization

With a bit of googling, I came across a blog article by Rupesh Kumar which mentioned this issue in the comments. I had heard that Tomcat was not officially supported by ColdFusion which did not make a lot of sense, but apparently this just came back and bit me.

Somewhere in the Hibernate integration with ColdFusion 9, it is expecting a library called jta.jar (part of the Java Transaction support) to be included with the application server. This library is not included by default with Tomcat and thus the error I was seeing, or not seeing.
I did some searching and could not find the jta.jar library to download, but I did find a zip download of all of the classes available on the Java site at http://java.sun.com/javaee/technologies/jta/index.jsp. So, I simply downloaded this and renamed it to jta.jar, then dropped it into my Tomcat /lib folder. With that in place, I restarted Tomcat, fired up my CF template again and everything worked like it should.

I cant fault Adobe since they dont support Tomcat. I am curious as to why they dont support it since it used to be supported, but hopefully this will save somebody a couple of hours digging around when their script fails to show anything.

MSN bot was smashing Trac server, here is how I blocked it

Just wanted to put a quick post out there. I host about 50 trac environments on an EC2 server, several rather large ones of which are public and pretty high traffic. Yesterday the trac server started having issues, and most of the logged errors had a seemingly random type of python trace data, followed by:

IOError: failed to write data

I dug around online for a bit but did not see much in the way of help for this issue, then I noticed that nearly all the errors were showing from a client that started with 65.55.X.X. Flipping through other logs, it appeared to be an MSN bot agent hitting Trac pages, and in some cases even hitting an infinite recursion (somehow linking to /report2/report2/report2/report2/……). The quick and dirty fix to solve this issue was to block the entire subnet from accessing my vhosts, which was done with this little gem inside of each vhost &lt;directory&gt; tag in Apache:

Order Allow,Deny
Allow from all
Deny from 65.55.0.0/16

Granted this is not a perfect solution, but keeping the site up for real users was more important at the moment than allowing a rampant recursive spider to keep slamming the site as soon as I brought it back up (for nearly 2 days straight now).
Happy Friday!

Hibernate & ColdFusion 9 – What is ORM?

Before we dig into Hibernate, I wanted to take a quick textbook look at what ORM is since Hibernate is an example of an ORM framework. ORM spelled out is object/relational mapping. One text I read said that the slash between object and relational is supposed to emphasize the mismatch problem that occurs when the object oriented world meets the relational world.
The way the book Java Persistence with Hibernate defines ORM is:

.. the automated (and transparent) persistence of objects in a Java application to the tables in a relational database, using metadata that describes the mapping between the objects and the database.

This sounds pretty simple so basically an ORM knows enough about the object model in your application to know how to take a Person object and its related Address etc. instances and transform that data into your relational database structure and vice versa.
Again, according to Java Persistence with Hibernate, an ORM solution is made up of the following 4 components:

  • An API for performing basic CRUD expressions think SQL
  • A language or API for creating queries that refer to object and their properties instead of tables and column
  • Capabilities for describing mappings between objects
  • Capabilities for functionality including dirty checking, lazy fetching, and other optimization

There were a lot of buzz words in that last point, but the optimization reference is one I am interested in. ORM solutions in the past have always seemed to just add an extra layer of overhead to an application and thus suffer a performance penalty. If through Hibernate we can optimize the queries that are run and the way data is returned, that could make a big difference. On the flip side, there are quite a few benefits to a properly implemented ORM solution like Hibernate.
Productivity
As much as the DBAs out there love to write and optimize SQL queries, like any other coding, it still takes time to write and time to debug. In addition, you may have to deal with cross platform issues in dealing with different relational database systems. An ORM framework such as Hibernate can drop right into place and with a few tweaks to the object model, handle all of this persistence layer for us. I expect there will be some optimization work and a few queries that still need to be written, but for the most part, all of that redundant CRUD work is taken care of.
Maintenance
Maintenance goes along with productivity fewer lines of code to be written in the first place means fewer lines of code to maintain down the road. In the case of Hibernate, you are also dealing with an open source, battle tested application rather than starting from scratch with your own persistence layer thus hopefully reducing the bugs you have to deal with.
Performance
This one is where I have the most questions. With hand coded SQL queries, you can always tweak the query to get just the data you need. You can cache those query results and more to get every little bit of performance you can. With an ORM solution, it is argued in the book that via Hibernate, these optimizations are even easier to achieve and faster to implement. There is also the argument that with an open source project like Hibernate, there were many more minds looking at the code and had more time to investigate optimizations than you have on any given project. This argument I can see and agree with.
Vendor Independence
This benefit can be argued as with most custom applications I have ever worked on, you very rarely see a database platform change. However, if you were developing software that was sold commercially and had to support multiple database platforms, I could see this being a big benefit. In order to support multiple platforms without an ORM, you would either be limited to very generic SQL or you would have built multiple persistence layer implementations which would just add to the maintenance overhead. With a vendor independent solution such as Hibernate, this dependency is taken care of for you.
That is enough for today. Next up, the requisite Hello World project what book would be complete without one? The book obviously sets this up in Java, but my plan is to duplicate it on ColdFusion 9 using the new Hibernate integration.

A New Series: Hibernate & ColdFusion 9

  Recently I have been working on a project using Groovy and Hibernate amongst other technologies and while I have definitely missed ColdFusion, it is good to get out of your comfort zone on occasion. The problem with using new technologies in a project setting is that most of your focus is on delivering the project and not on really learning the ins and outs of the technology. So, with a few minutes of breathing room allotted before the next project, I wanted to delve a bit deeper into Hibernate, especially with its integration into the next major release of ColdFusion.

  To do this, my plan is to work through the book Java Persistence with Hibernate which was recommended to me and not only look at some of the capabilities behind Hibernate, but also see how Hibernates capabilities are presented within ColdFusion 9. This series should also serve as a bit of a review for the book and whether I would really recommend it for the ColdFusion development community. To answer one question before it comes up, my point here is not to say that Hibernate will solve the worlds problems and that every developer should use it. My goal is to simply take a closer look at Hibernate and the tools that we will have at our disposal with the next release of ColdFusion. It is up to you to decide if these capabilities are worth your time and effort to learn more about.

  Now, on to the book. In going through some of the introduction and preface, I came across this definition of what Hibernate is:

Hibernate is an ambitious project that aims to be a complete solution to the problem of managing persistent data It mediates the applications interaction with a relational database, leaving the developer free to concentrate on the business problem at hand.

  At first glance, this sounds great for a developer I get to focus on the application and its architecture while ignoring the whole concept of data persistence within the application. Reality is jumping up and down in the back of my head though saying this is too good to be true, so we will find out.
The first chapter of the book talks in general about persistence, how it fits into our software model and how it works in an object oriented paradigm. I know my very first web application dealt with storing and retrieving data from a little Microsoft Access database, so the concept of data persistence is nothing new to me and I assume it is not new to most developers. The chapter talks a bit about relational databases databases like Microsoft SQL Server or MySQL if you are not familiar with the term as well as briefly discussing SQL in terms of the language for writing queries and communicating with the database. While it is a well written intro, none of this should really be new for the average developer.

  The twist with data persistence comes into play with what the books calls the paradigm mismatch. In other words, what happens when you start developing applications in an object oriented fashion and have to deal with persistence. With an object oriented application, you have abstract concepts like a Person and an Address. However, a relational database does not understand the concept of a Person or an Address. It understands a simple string like a first name or a street name for example. So, how do you go about saving and retrieving a Person instance to a relational database? While you can obviously introduce a data access layer to your application that handles this conversion( this is discussed in the persistence layers section ) , this is the problem that Hibernate is attempting to solve amongst others.

  The sub-section entitled The Paradigm Mismatch is very good in explaining the different issues in mapping objects to a relational database including granularity, inheritance, polymorphism, and identities. The only thing here is that you have to have some knowledge of object oriented development and terminology, but I think that would be expected when reading a book on Hibernate which is all about persistence of object models.
I will cut this off here although I have only touched on half of the first chapter. The sections on persistence layers are equally as good and as such, I will leave something for you to read about yourself. I will look a bit more at the section that provides an overview of object/relational mapping (ORM) in the next post in this series.

Tag Cloud