Posts

How to convert a string to a SQL date

Image
  Steps SimpleDateFormat  - create a class that encapsulates the format of the date you are creating.  Parse the date sent using the method to ensure it meets date formatting requirements. Throw a parse exception if the date is invalid. Create a sqlDate from the parsed date. Use the number of milliseconds from  January 1, 1970, 00:00:00 GMT as an input into the method. This ensures the accuracy of the formed SQL date. Return the sqlDate to the method caller. 

The CSV Editor - The Beginning

Image
What am I doing..?  What began as a process to learn JavaFX  has turned into a lengthy, mistake-driven, process. I have learnt a lot and gained an appreciation for creating something which has value and at the same time provides a pathway for learning. CSV Editor - the starting point It seemed a simple idea. Build a CSV Editor in JavaFX.  I started by looking for an example and soon found this remarkable free tool CSVed . This editor did everything and much more. There isn't a feature that isn't covered by this tool and its long revision history ( dating back to 2008 !!! ) is a testament to software endurance. I decided I wouldn't be as bold.  I would play it safe, set the features small, and climb the software ladder one step at a time. I had years of Java experience but my JavaFX experience was minimal.  The Features So, using my loved Modelling tool, Sparx Enterprise Architect from Sparx Systems , I mapped out my main features. And so began a journey of rab...

Vaadin JPA Container Example

Simple example showing how to use a Vaadin JPA Container The scenario is a Customer entity in a database. You have a use case which requires the retrieval of a customer record by Name and By Number. 1) Set up a Entity Manager private final EntityManager em; 2) Create the Entity Manager from the persistence unit em = JPAContainerFactory.createEntityManagerForPersistenceUnit(< name of persistence unit >); 3) Get Customer By Number - returns a JPA Container which is useful for Vaadin Tables public JPAContainer getCustomerByNumber(String aCustomerNumber) { JPAContainer customer = JPAContainerFactory.make(Customer.class, em); Container.Filter filter = new Compare.Equal("number", aCustomerNumber); customer.addContainerFilter(filter); return customer; } 4) Get Customer By Name - uses JPA named query public Customer getCustomerByName(String aCustomerName) { Query queryCustomerByName = em.createN...

Have Software Components finally arrived

Image
Introduction In April 2013 Dr Dobbs editorial piece summoned a long held vision for software components and reuse. It proclaimed : “After 40 years in the desert of incompatibility and limited reuse, we have crossed the river Jordan.” It was a romantic notion and it provides a sign post for a vision about software components that stretches back to 1968. 1968 Software Engineering NATO Conference Components became part of the software engineering discipline in the very first Software Engineering conference in 1968. Sponsored by NATO, the conference brought together the leading researchers at the time. In a presentation provided by M. D. McIlroy, titled,  “Mass Produced Software Components”, McIlory set out the fundamental principles by which a industry based on the production of software components could be founded. “My thesis is that the software industry is weakly founded, and that one aspect of this weakness is the absence of a software components subindu...

Vaadin - GRID Component

Image
Introduction Vaadin 7.4 was recently released and it includes a new component called the GRID . It represents a 'Spreadsheet" type model from a UI perspective, however I think it lacks a lot of features you would expect from this type of component. Maybe this component will evolve over time and it will begin to have the functions that you expect from a spreadsheet. ( note: there is a commercial add on called the Spreadsheet from Vaadin ) Use Case The use case I am demonstrating below is a simple grid showing projected sales of Widgets over a five year period. Creation Creating a Grid is very simple and you have numerous options for sizing on the page. In the example below I setting it across the page. The Grid rows can be selected for editing, you can select single or multiple rows in a single action. myGrid = new Grid(); myGrid.setSizeFull(); myGrid.setSelectionMode(SelectionMode.SINGLE); Container Backing Similar to many other Vaadin componen...

Google Guava HashBasedTable Example

Image
A simple example showing how to establish a Google Guava Hash Based Table. Notes A class called Board represents a Sudoku Board Create a Hash Based Table which contains a Cell Class Create the Hash Based Table in the constructor Load the board using a int array Print a simple representation of the board Cell class below shows a simple way to represent a cell on a Sudoku Board

Jersey Rest Basics

Image
I use the Jersey Rest API  and I always deploy to Tomcat. There are some basic things you need to do to make this work. There are few examples around the internet which are very good, but they seem to be focused on some very old versions of the framework. Number 1, make sure your web xml contains this configuration. Number 2, I use Maven , so this is my Pom File, this example includes the dependency for using JSON processing. Number 3, write some code. That's for another blog entry.

Web Cam and Vaadin

A very interesting Web Cam Add On is available from the Vaadin Add On site . Here is a simple recipe for making it work. Create a split panel and add the Web Cam to the left hand side of the panel. splitPanel = new HorizontalSplitPanel(); splitPanel.setSizeFull(); VerticalLayout detailsLayout = new VerticalLayout(); detailsLayout.setSpacing(true); detailsLayout.setSizeFull(); // Create the webcam and assign a receiver. final Webcam webcam = new Webcam(); webcam.setWidth("600px"); detailsLayout.addComponent(webcam); detailsLayout.setComponentAlignment(webcam, Alignment.TOP_LEFT); Create a receiver which creates a temporary file with a jpeg extension. webcam.setReceiver(new Upload.Receiver() { @Override public OutputStream receiveUpload(String filename, String mimeType) { try { targetFile = File.createTemp...

Java and KML Files

What is a KML file.? A Keyhole Markup Language File (KML) is a markup language to describe geographic data. How does it relate to Java.? An API exists ( which appears quite old ) which can process KML files. Either producing them or turning the KML files into Java objects. ( Marshalling or Unmarshalling ) Problem ? I recently downloaded some geographic data from a public dataset which was expressed as a KML file. Its my intention to use that data in a sample application. However I had a huge problem getting the API to un-marshall the data. It had a major problem with the namespace in the dataset. This namespace causes the API major problems. <kml xmlns="http://earth.google.com/kml/2.1"> After numerous hours I found this worked. ( so I replaced the above with the below ) <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom=...

Vaadin and Google Maps

Vaadin and a Google Map Widget One of my major irritations with Vaadin has been its lack of support for Google Maps as a standard widget. The reality is that many commercial applications require some form of mapping feature and the lack of support for a standard Google Map widget has been a major problem. However this has changed with Version 7.x of Vaadin, a new Google Map Addon has appeared and it fills a very important gap in the Vaadin feature set. How to Use It. If you use Maven in your development process then using the Addon in your Vaadin project is very easy. Just place this dependency in your POM file and make sure you have the correct repository established for the Vaadin Add On's. <dependency>    <groupId>com.vaadin.tapio</groupId>    <artifactId>googlemaps</artifactId>    <version>0.6.2</version> </dependency> Here is some simple code to get you started. It creates a Googl...

No Machine stopped after launching EC2 instance into new VPC - solved

The Error If you get this error from your No Machine Client Desktop NX> 500 Authentication failed NX> 500 Remote host identification has changed NX> 500 Offending key in /usr/NX/home/nx/.ssh/known_hosts NX> 999 Bye. NX> 280 Exiting on signal: 15 Solution I solved this by doing: Find file usr/NX/home/nx/.ssh/known_hosts which includes old keys used by your ssh daemon. Remove your old key(s) by commenting out the lines. Create new connection into host using No Machine Wizard. Thanks to this post

AWS Samples Behind a Corporate Firewall

Image
The AWS Java SDK includes some interesting examples of how to use the API for a range of different use cases ( S3, SQS, SNS etc ) These work well if they are installed on a EC2 instance. However, what if you are behind a Corporate Firewall and you want the example to access the AWS services on the internet.? The Amazon S3 Client Object contains a constructor which receives a Client Configuration Object. AmazonS3Client s3Client = new AmazonS3Client(AWSCredentials creds, ClientConfiguration config) The Client Configuration object has methods to establish a proxy configuration so that the client can connect into the internet via the proxy. ClientConfiguration config = new ClientConfiguration(); config.setProxyHost("ProxyHostName"); config.setProxyUsername("ProxyUserName"); config.setProxyPassword("ProxyPassword"); If you add this type of code into a AWS Sample then you should be able to punch your way through the Corporate Fir...

Dealing with Amazon Web Service Access Keys

Image
Amazon Web Services ( AWS ) access keys are required to make API calls for the range of AWS services. These keys can be troublesome, as the tendency is to hard code the values in your software ( which many internet examples do ) or to use configuration files which hold them in plain text. In many ways this is bad. The secret key once displayed to you ( as the user ) is never returned again, hence its importance can not be understated. However there is a way around this problem and its associated with AWS Identity Access Managemen t ( IAM ) roles and AWS EC2 Instance s. In the digram above an application hosted on an EC2 instance requires access to S3 Objects. If you use the AWS Java SDK you can create a S3 Client Object which has a constructor which receives the private and access keys. AmazonS3Client s3Client = new AmazonS3Client(AWSCredentials creds); The constructor for this client receives a AWSCredentials object which is built using the secret and access key. ...

Vaadin 7 - because I have to

I have been resisting Vaadin 7 for a long time. Trapped in version 6, at times I have created an illusion that version 7 didn't exist. "I will get to it, soon, soon", I would say to myself. But, its time. I have bought a book , downloaded the book code and cracked open the first example. My initial impressions, Vaadin 7 is different from version 6, not just a "little different", its different by a wide margin. This journey is going to be all up hill.

A View Manager in Vaadin

One of the problems I faced early on in developing a Vaadin Application was how to move from one screen to the next. I managed to find a example pattern which is based on a "View Manager". This object is responsible for maintaining the various screens within the application. At any point you can ask the "Manager" for a previous screen and you can display its contents. The example I found that uses this pattern is called the " Gas Diary ". The View Manager class itself contains a HaspMap and a Stack. The HashMap contains a string for the name of the view and a Layout. The Stack contains a Layout. In the Gas Diary Main Application the Login Screen is "switched" to via this process. setMainWindow(mainWindow); viewManager = new ViewManager(mainWindow); // Create the login screen viewManager.switchScreen(LoginScreen.class.getName(), new LoginScreen()); This method has worked for me...

Open Search Server - A Discovery.

Search. The missing app in organisations. In my opinion poor search is the root of "knowledge management" issues. In fact, I think a poor search platform is to blame for huge duplication within organisations. In reality, no one can find anything in your average company. Open Search Server is an interesting open source solution in this space.

Google Drive API Example - What worked for me

Image
An interesting Java example exists for the Google Drive API on the Google Developers Site . It is located in the Quick Start menu. It is intended to provide developers with a simple way to start using the Google Drive API. While this is a welcome extension provided by Google, I ran into problems making this example work. The first problem I encountered was enabling the Drive API. The instructions indicated that you are required to "enable the Drive API". I did this, but it appears you also need to enable the Drive SDK as well. If you fail to enable both you receive an "access not configured" error. The 2nd problem that caused me a few hours of frustration was the inability to build the example using the suggested Maven configuration. After numerous attempts I was able to settle on this Maven configuration which appeared to work. <dependency>       <groupId>com.google.apis</groupId>       <artifactId>goog...

Mounting a windows folder from a Linux Host

I use VMWare Player to run Linux VM's on a Windows Host Machine. I use the Linux VM's as a development environment. To mount a windows folder in my Linux VM I use this command. sudo mount -t vmhgfs .host:/ /home/user/shares This gives me a folder ( shares ) in my Linux host which points to the windows folders I have made available from the Host operating system ( in my case Windows 7 ).

Oracle - FIX JAVA !!!!

I have been a loyal Java Developer since 1995.... Thousands of lines of Java have flowed from my fingers. At times, my fingers just seem to "know" what next statement to write. I don't have to think about it. But, my patience has run out. Another Java Zero Day exploit has hit the "fan". Oracle, its time. Its time, to use your billions to fix Java. The future of Java is at stake and the clock is ticking....

Vaadin ThreadLocal

I use the Vaadin Thread Local pattern in my Vaadin applications as a means to move the Application object around the various components and to implement a authentication process. The structure of an application which uses this pattern is as follows: public class Vaadin Application extends Application implements HttpServletRequestListener {      private static ThreadLocal <Vaadin Application > instance = new ThreadLocal <Vaadin Application >() public Vaadin Application () {         instance . set ( this );     } @Override     public void onRequestStart ( HttpServletRequest request , HttpServletResponse response ) {         instance . set ( this );     }     @Override     public void onRequestEnd ( HttpServletRequest request , HttpServletResponse response ) {         instance . remove ();   ...