<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;C0EFSHc4fCp7ImA9WhRRGEk.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375</id><updated>2011-12-02T17:00:19.934+01:00</updated><category term="Scala swing" /><category term="i18n" /><category term="Netbeans 6.8" /><category term="reports" /><category term="programming" /><category term="tutorial" /><category term="glassfish" /><category term="sbt" /><category term="Java" /><category term="Web Service" /><category term="Ajax" /><category term="pdf" /><category term="SSC (Scala Stock Charts)" /><category term="EJB" /><category term="grails" /><category term="Scala" /><category term="gradle" /><category term="stocks" /><category term="JSF2" /><category term="JSF" /><category term="SSL" /><category term="web-fragments" /><category term="Glassfish v3" /><category term="japser" /><category term="Servlet" /><category term="database" /><title>Ironic programmer</title><subtitle type="html">Programming in (mostly) Java, Grails, Ruby and Rails. Sharing thoughts, solution ideas and tricks to work around. Maybe also giving .NET and Microsoft some ironic comments. And ironic, yes often if it is called for.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://ironicprogrammer.blogspot.com/" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>23</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/IronicProgrammer" /><feedburner:info uri="ironicprogrammer" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;D0cMR3c5fCp7ImA9WhdaF0w.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-1543112594158550116</id><published>2011-10-19T11:02:00.001+02:00</published><updated>2011-10-27T13:31:26.924+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-27T13:31:26.924+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><category scheme="http://www.blogger.com/atom/ns#" term="stocks" /><category scheme="http://www.blogger.com/atom/ns#" term="Scala swing" /><title>Scala Stock Charts - part 6 some GUI things</title><content type="html">I got a bit caught up in all the programming and finished up a few diagrams, then some refactoring and was able to remove a couple of hundred lines of code. The program now looks like this:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-4_bk3_dBlwA/Tp3ShE5WywI/AAAAAAAABes/ZssOErtpAnU/s1600/Screen+Shot+2011-10-18+at+9.24.18+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="172" src="http://1.bp.blogspot.com/-4_bk3_dBlwA/Tp3ShE5WywI/AAAAAAAABes/ZssOErtpAnU/s320/Screen+Shot+2011-10-18+at+9.24.18+PM.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see a lot have happened since the last pictures of the application. So here is some things.&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;









Main part&lt;/h2&gt;
The applications is build up around a main frame that contains the bar chart and some overlays. As you can see there is also some text fields and buttons. The upper part is to search stocks/indexes and also handle the overlays in the bar char. These fields and buttons are:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Search field to enter a search criteria to filter the instruments to search for.&lt;/li&gt;
&lt;li&gt;Search button, to apply the search&lt;/li&gt;
&lt;li&gt;A combo box, to show the filtered search result, and also choose the stock to view. This combo box listen to changes and updates the bar chart with the selected stock&lt;/li&gt;
&lt;li&gt;Next there is a box to limit the range of the data. I have not introduced recalculation for splits and emissions so to have a possibility to handle to not show before these events, I added a box to filter away prices before this date part.&lt;/li&gt;
&lt;li&gt;Next there is the intervals for the moving averages in the chart, default they are set to 5,20,50 but can be changed but have as most 3 different, but also as few as one. Default they are Simple Moving Average (SMA),&lt;/li&gt;
&lt;li&gt;to change to Exponential Moving Average (EMA) there is a checkbox&lt;/li&gt;
&lt;li&gt;to update changed settings for Moving averages there is a button "Update averages"&lt;/li&gt;
&lt;li&gt;finally on the top there is a check box to switch on/off viewing of "Bollinger Bands"&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
In the bottom part there is buttons for additional chart types. these are the ones I picked as most interesting for me and they show some different aspect of the prices.&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Stocastics (show how a stocks price is doing in relation to past movements)&lt;/li&gt;
&lt;li&gt;RSI (shows how strong a stock is moving in the current direction)&lt;/li&gt;
&lt;li&gt;Aroon (shows if a stock is trending or oscillating)&lt;/li&gt;
&lt;li&gt;MACD (and also MACD-histogram) (a momentum oscillator)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;br /&gt;
As I mentioned the main part is a Bar chart diagram. It contains some overlays like volume, moving average and optional Bollinger bands. These help to get a basic overview of a stocks movement and trend. It is made up of a specialized panel for the bar chart to handle the drawing of the chart.&lt;br /&gt;
&lt;br /&gt;
The main application implements a method named top that is used in wrapped Swing features used in Scala. I have an object representing my starting point of the application, the first lines looks like:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;object ScaBorsApp extends SimpleSwingApplication with Logging{
    def top = new MainFrame {
      title = "Technical Analysis for Stocks"
      val COL_GREEN = new Color(0,150,0)
      val searchField = new TextField {
          text = "a"
          columns = 10
        }
&lt;/pre&gt;
As you can see I provide a title and also here is the search field where to put a search string. The most important thing to note here is the inheritance of SimpleSwingApplication which makes this object a Swing application&lt;br /&gt;
&lt;br /&gt;
The next part looks like this&lt;br /&gt;
&lt;pre class="codebox-content"&gt;/*
       * Search button, will get the pricelist and do filter and sort to present in combo box
       */
      val searchBtn = new Button("Search") {
          action = Action("Search"){ 
            val pricelists = PriceService.getPriceLists map { kv =&amp;gt; kv.instruments } flatMap { 
                case i: Instrument =&amp;gt; i 
                case x =&amp;gt; x
              } filter {
                i: Instrument =&amp;gt; i.name.toLowerCase().startsWith(searchField.text.toLowerCase)
              }
            
            val items = pricelists.toList sortWith {(i1,i2) =&amp;gt; i1.name.toLowerCase &amp;lt; i2.name.toLowerCase}
            combo.peer.setModel(
              ComboBox.newConstantModel(
                  for{p &amp;lt;- items} yield p.ticker + ": " + p.name
              )
            )
          }
        }
        
      /*
       * ComboBox to present filtered search for stocks, see searchButton action code how search is performed
       */
      import event._
      val combo = new ComboBox(List("-- Search to be able to select --")) {
        preferredSize = new Dimension(300,20)
        listenTo(selection)
        reactions += {
          case SelectionChanged(c) =&amp;gt; 
            curInstrument = new Instrument(selection.item.split(":")(0),selection.item.split(":")(1).trim,"")
            curPriceList = PriceService.getInstrumentData(curInstrument).filter(p =&amp;gt; p.date.substring(0,fromTextBox.text.length).toInt &amp;gt;= fromTextBox.text.toInt)
            barchartPanel.updateList(curPriceList)
          case e =&amp;gt; info(e.toString)
        }
      }
...
val chkBollinger = new CheckBox {
        text = "Bollinger bands"
        reactions += {
          case x =&amp;gt; barchartPanel.setBollinger(this.selected) 
        }
      }
&lt;/pre&gt;
Here you can see some on the fly declaration and reactions to events on the components. &amp;nbsp;The following code shows how to add these to the contents of other panels. you can also see the creation at the click of the button, creation of a generic diagram for "stochastics" mixed in with a DrawListener to make it drawable. Also how the series of stochastics is added to a generic diagram can be seen here.&lt;br /&gt;
&lt;pre class="codebox-content"&gt; 
     /*
       * Top menu panel.
       */
      val menu = new FlowPanel {
        contents += new Label("Stock")
        contents += searchField
        contents += searchBtn
        contents += combo
        contents += fromTextBox
        contents += avgSettings
        contents += chkExpAverage
        contents += btnUpdateAvg
        contents += chkBollinger
      }
....
val bottomPanel = new FlowPanel{
        contents += new Button(){
          text = "Stochastics"
          reactions += {
            case bc: ButtonClicked =&amp;gt; val stD = new Frame {
                title="Stochastics " + curInstrument.name
              }
              stD.open()
              val panel = new GenericDiagram with DrawListener
              val scrPan = new ScrollPane{
                preferredSize = new Dimension(1180,220)
                contents = panel
              }
              stD.contents = scrPan
              val stoList = PriceUtil.stochastics(curPriceList, 14)
              panel.addSerie(ChartSerie(stoList,Color.blue))
              panel.addSerie(ChartSerie(PriceUtil.slidingAvg(stoList, 3),Color.red))
              panel.addDateSerie(curPriceList.map{p=&amp;gt;p.date})
              panel.setLabelLevels(List(20d,50d,80d))
              // need to force set max, it is not always 100 ??
              panel.maxVal = 100d
          }
        }
....
contents = new BorderPanel() {
        add(menu, BorderPanel.Position.North) 
        add(scrollPane, BorderPanel.Position.Center)
        add(bottomPanel, BorderPanel.Position.South)
      }
&lt;/pre&gt;
This last part of the code adds the actual panels containing the fields, bar chart and buttons to the Swing application.&lt;br /&gt;
&lt;h2&gt;





Additional panels&lt;/h2&gt;
To add some extra check to the main part there is some diagrams that first was their own specialized panel, but after some refactoring they now all except MACD uses a generalized panel called &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;GenericDiagram&lt;/span&gt; and here is a part of that code:&lt;br /&gt;
&lt;br /&gt;
And here is addition of some of the buttons into menu panel (top panel) followed by some example of the bottom panel with addition of button and reactions to its event. You see here that here is where the panel frame is set up inside the reaction to show the GenericDiagram with "stochastic" data.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;    val chkBollinger = new CheckBox {
        text = "Bollinger bands"
        reactions += {
          case x =&amp;gt; barchartPanel.setBollinger(this.selected) 
        }
      }

...

trait BasicChartProps {
  val PAD_TOP,PAD_BOTTOM = 20
  val PREF_H = 200
  val PREF_W = 1180
  var maxVal = Double.MinValue
  var minVal = Double.MaxValue
  var startX = 50
}
/**
 * Generic panel for simple series
 */
class GenericDiagram extends Panel with BasicChartProps with Logging{
  private var series = List[ChartSerie]()
  private var dateSerie = List[String]()
  
...
&lt;/pre&gt;
Not so much to mention really except that as you see I extend with a trait with basic chart properties. this is intended to have common properties for this and other diagrams. The var's is to be able to add series and date series to the diagram. So this panel is only responsible to draw the things that are sent to it.



&lt;br /&gt;
&lt;h2&gt;








Ability to draw&lt;/h2&gt;
No bar chart is complete if it is not possible to draw some help lines. This is managed by adding a trait for this, that can be mixed in wherever I want it. The code for that trais is:

&lt;br /&gt;
&lt;pre class="codebox-content"&gt;...
/**
 * Trait to use for Panels that should have line drawin possibilities
 */
trait DrawListener extends Panel {
  val pointZero = new Point(0,0)
  
  var curStart = pointZero//new Point(0,0)
  var curEnd = new Point(0,0)
  listenTo(mouse.clicks, mouse.moves, keys)
  reactions += {
    case e: MousePressed  =&amp;gt; 
                curStart = e.point
         //moveTo(e.point)
         requestFocusInWindow()
       case e: MouseDragged  =&amp;gt; curEnd = e.point; repaint() //lineTo(e.point)
       case e: MouseReleased =&amp;gt; moveTo(curStart);lineTo(e.point);curStart=pointZero;curEnd=pointZero
       case KeyTyped(_,'c',_,_) =&amp;gt; 
         path = new geom.GeneralPath
         repaint()
       case _ =&amp;gt; repaint()
  }
  /* records the dragging */
  var path = new geom.GeneralPath

  def lineTo(p: Point) { path.lineTo(p.x, p.y); repaint() }
  def moveTo(p: Point) { path.moveTo(p.x, p.y); repaint() }
  
  override def paintComponent(g: Graphics2D) = {
    super.paintComponent(g)
    g.setColor(Color.black)
    if(curStart.x != 0 &amp;amp;&amp;amp; curEnd.y != 0){
      g.drawLine(curStart.x,curStart.y,curEnd.x,curEnd.y)
    }
    g.draw(path)
  }
}
...
&lt;/pre&gt;
An example of this functionality is to draw help lines in a MACD diagram. (or any really)



&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-qtPWme9pApA/Tp3b0yooEgI/AAAAAAAABe0/29AEEfl02mM/s1600/Screen+Shot+2011-10-18+at+10.04.13+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="64" src="http://4.bp.blogspot.com/-qtPWme9pApA/Tp3b0yooEgI/AAAAAAAABe0/29AEEfl02mM/s320/Screen+Shot+2011-10-18+at+10.04.13+PM.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
This can be mixed in like in this case:


&lt;br /&gt;
&lt;pre class="codebox-content"&gt;val panel = new MACDPanel(Nil) with DrawListener
&lt;/pre&gt;
And then it is possible to draw on that panel.
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-1543112594158550116?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/do1vGgU6RU4o5iJugG5_TEyeHyc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/do1vGgU6RU4o5iJugG5_TEyeHyc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/do1vGgU6RU4o5iJugG5_TEyeHyc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/do1vGgU6RU4o5iJugG5_TEyeHyc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/xjiwgNCNlfY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/1543112594158550116/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/10/scala-stock-charts-part-6-some-gui.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1543112594158550116?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1543112594158550116?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/xjiwgNCNlfY/scala-stock-charts-part-6-some-gui.html" title="Scala Stock Charts - part 6 some GUI things" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-4_bk3_dBlwA/Tp3ShE5WywI/AAAAAAAABes/ZssOErtpAnU/s72-c/Screen+Shot+2011-10-18+at+9.24.18+PM.png" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/10/scala-stock-charts-part-6-some-gui.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ECQnc_cSp7ImA9WhdaEE0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-1163694067784890491</id><published>2011-10-01T18:39:00.000+02:00</published><updated>2011-10-19T09:34:23.949+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T09:34:23.949+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="stocks" /><category scheme="http://www.blogger.com/atom/ns#" term="Scala swing" /><title>Scala stocks part 5 - Refactoring, big time</title><content type="html">There is soooo much stuff that I would like to look in to. Alfresco, Liferay, GWT, Spring, Scrum, Agile etc. Right now though, I continue with my Scala project doing a Technical analysis tool for stocks using a Swing gui, also that with Scalas Swing libraries.&lt;br /&gt;
&lt;br /&gt;
After working with the GUI, I started to get annoyed on myself mixing things up here and there, so I decided to refactor a bit. Actually, since I wrote the last post, I got a bit further then just plain refactoring. Simultaneously with the refactoring I worked on the GUI and also restructured code to finish up the loading of prices, enhancement of that loading and in that work I also managed to introduce some "Actors".&lt;br /&gt;
&lt;br /&gt;
I will now also start to explain a little bit more what the code does.&lt;br /&gt;
&lt;br /&gt;
I will split the refactoring and explanation in more than this post, otherwise I may get too long.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Refactoring of the FileHandler.&lt;/b&gt;&lt;br /&gt;
The new file is now much shorter, and includes only stuff to handle files. The extraction and transformation to the model is removed from the file handling. that messy code can be seen here&amp;nbsp;&lt;a href="http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-3-saving-quotes.html"&gt;http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-3-saving-quotes.html&lt;/a&gt;&amp;nbsp;if you have not already read it.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="codebox-header"&gt;
FileHandler.scala
&lt;/div&gt;
&lt;pre class="codebox-content"&gt;/*
 @author Peter Johansson / Ironic programmer 
 */

package me.ironic.scabors.file
import me.ironic.scabors.model._
import me.ironic.scabors.util.Logging

object FileHandler{
  def apply() = { new FileHandler() }
}

class FileHandler extends Logging {
  import java.io.{File,BufferedWriter,FileWriter,IOException}
  import scala.collection.mutable.{ HashMap =&amp;gt; MutableHashMap}
  import scala.collection.immutable.{ HashMap }
  import scala.io.Source
  
  val rootDir = { scala.util.Properties.userDir }
  val filesDir = { rootDir + "/files"}
  val logFile = { rootDir + "/" + "sBors.log"}
  val pricelistsFile = { rootDir + "/files/pricelists.txt"}
      
  def checkDir(dirName: String){
     val f = new File(dirName)
     if(!f.exists){
       f.mkdirs
       info("!! Created dir at : " + dirName)
     }
  }
  
  def writeToFile(fileName: String, append: Boolean, rows: List[String]){
    // Check that dir exista or creat if not
    checkDir(filesDir)
    
    val bw = new BufferedWriter(new FileWriter(fileName,append))
    
    rows foreach { row =&amp;gt;
      bw.write(row + "\n")
    }
    
    bw.close()
  }
  
  def priceFile(ticker: String) = {
    rootDir + "/files/" + ticker.replace(" ","_") + ".txt"
  }
  
  def loadFile(fileName: String)  = {
    val fc = Source.fromFile(fileName)
    fc.getLines.toList
  }
  
}
&lt;/pre&gt;
&lt;br /&gt;
In the start of the file, you see:
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;object FileHandler{
  def apply() = { new FileHandler() }
}
&lt;/pre&gt;
This is a &lt;i&gt;companion object&lt;/i&gt; I use it to have a factory method. Maybe not really nescessary but at least I get away with not having to write&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;new FileHandler()&lt;/span&gt;&lt;br /&gt;
every time I need it and can use the shorter version&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;FileHandler()&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The rest of the FileHandler should be quite easy to figure out. I have only one write-method where I send in the filename, if I want to append and a list of strings to write to the file. With this I can do all the saving I need.&lt;br /&gt;
&lt;br /&gt;
There is also only one method for getting the file content (loadFile), this returns a list of strings which I later can traverse and to all the logic with.&lt;br /&gt;
&lt;br /&gt;
I also have a method to have a format for how a price file is named. I do not want any blanks in the filename so this method is replacing that with "_" instead.&lt;br /&gt;
&lt;br /&gt;
As you can see the FileHandler extends &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Logging&lt;/span&gt;. I do not need that here since no logging is going on in this class as it is now, but I can add it if I want. More about the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Logging&lt;/span&gt; later.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Introducing PriceDAO.&lt;/b&gt;&lt;br /&gt;
To hide FileHandler from the rest of the system, and also to the mapping between files and model that was before done in the FileHandler, I introduced a DAO object to do the work. Doing this I can always change the way I persist later without too much impact on the code.&lt;br /&gt;
&lt;br /&gt;
There is still some refactoring to do in this one since I have not changed the fact that I use mutable lists.&lt;br /&gt;
&lt;div class="codebox-header"&gt;
PriceDAO.scala&lt;/div&gt;
&lt;pre class="codebox-content"&gt;/*
 @author Peter Johansson / Ironic programmer 
 */
package me.ironic.scabors.dao

/**
 * Provides service methods and communicate with underlaying storage
 */
import me.ironic.scabors.util.Logging
import me.ironic.scabors.model._
import me.ironic.scabors.file.FileHandler
import scala.collection.mutable.{ HashMap =&amp;gt; MutableHashMap}
import scala.collection.immutable.{ HashMap }
  
object PriceDAO extends Logging{
  
  def saveDailyPrices(prices: List[PriceItem]) = {
    info("## Saving daily prices")
       
    var lists = new MutableHashMap[String,List[Price]]
    var curList = "?"
    
    prices foreach { price =&amp;gt;
      price match { 
        case q: Price =&amp;gt; { 
                            savePriceToFile(q);
                            lists(curList) = List(q) ::: lists(curList)
                          }
        case qList: PriceList =&amp;gt; {
                                    curList = qList.name;
                                    lists += qList.name -&amp;gt; List()
                                 }
        case _ =&amp;gt; warn(price.toString)
      }
    }
    
    // Save current lists to file...
    saveStockPriceList(lists)
  }
  
  /**
   * Method for saving a price to file
   */
  protected def savePriceToFile(price: Price) = {
    val fileHandler = FileHandler()
    val fName = fileHandler.priceFile(price.ticker)
    fileHandler.writeToFile(fName, true, List(price.toString))
  }
  
  /**
   * Method for saving an updated pricelist map to file.
   */
  protected def saveStockPriceList(pricelist: MutableHashMap[String,List[Price]]){
    info("## Saving pricelists")
    
    val sb = new StringBuffer
    sb.append("{ \"pricelists\": [\n")
    for{ (listname,prices) &amp;lt;- pricelist } {
      sb.append("\t{ \n\t\t\"listname\": \"" + listname + "\",\n")
      sb.append("\t\t\"stocks\": [\n")
      for{ price &amp;lt;- prices } {
        sb.append("\t\t\t{ \"ticker\": \""+price.ticker + "\", \"aktie\": \"" + price.aktie + "\"},\n")
      }
      sb.append("\t\t],\n")
      sb.append("\t},\n")
    }
    sb.append("}\n")
    
    val fileHandler = FileHandler()
    val fName = fileHandler.pricelistsFile
    fileHandler.writeToFile(fName, false, List(sb.toString))
  }
  
  /**
   * Method for loading price lists to a HashMap, 
   * each entry in the map is a list of Instruments
   */
  def loadPriceLists  : MutableHashMap[String, List[Instrument]] = {
    info("## Loading pricelists")
    var pricelists = new MutableHashMap[String, List[Instrument]]
    val ListExtractorRE = """.*"listname": "(.+)",.*\s*""".r
    val InstExtractorRE = """.*"ticker": "(.+)", "aktie": "(.+)".*\s*""".r
   
    /*
     * When hitting a PriceList, a new list begins
     */
    var curList = "?"
    val fileHandler = FileHandler()
    val fName = fileHandler.pricelistsFile
    val rows = fileHandler.loadFile(fName)
    rows foreach { line =&amp;gt;
      line match {
        case ListExtractorRE(listname) =&amp;gt; val pList = new PriceList(listname)
          curList = pList.name
          pricelists += pList.name -&amp;gt; List()
        case InstExtractorRE(ticker, aktie) =&amp;gt; val inst = new Instrument(ticker, aktie) 
          pricelists(curList) = List(inst) ::: pricelists(curList)
        case _ =&amp;gt; None
      }
      
    } 
    pricelists
  }
  
  /**
   * Method for loading all prices for a specific instrument.
   */
  def loadInstrumentData(inst: Instrument) : List[Price] = {
    val fileHandler = FileHandler()
    info("Loading instrument " + inst)
    val fName = fileHandler.priceFile(inst.ticker)
    
    val PriceExtractorRE = """Price\(([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*).*\)""".r
    val pricelines = fileHandler.loadFile(fName)
    val prices = pricelines map { line =&amp;gt;
      line match {
        case PriceExtractorRE(date,ticker,aktie,diffKr,diffPerc,buy,sell,last,high,low,returnNo,returnKr) =&amp;gt; 
          Price(date,ticker,aktie,diffKr.toDouble,diffPerc.toDouble,buy.toDouble,sell.toDouble,last.toDouble,high.toDouble,low.toDouble,returnNo.toDouble,returnKr.toDouble)
        
      } 
    } 
    prices.toList
  }
  
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Some explanation maybe?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Well, I am not going into every line. I choose some parts that I think is interesting parts of Scala.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;   
def saveDailyPrices(prices: List[PriceItem]) = {
/*....*/
     var lists = new MutableHashMap[String,List[Price]]
    var curList = "?"

      prices foreach { price =&amp;gt;
      price match { 
        case q: Price =&amp;gt; { 
                            savePriceToFile(q);
                            lists(curList) = List(q) ::: lists(curList)
                          }
        case qList: PriceList =&amp;gt; {
                                    curList = qList.name;
                                    lists += qList.name -&amp;gt; List()
                                 }
        case _ =&amp;gt; warn(price.toString)
      }
    }
&lt;/pre&gt;
&lt;br /&gt;
This code it possibly a good&amp;nbsp;candidate to do some further refactoring on to try to make it immutable, but this is what is going on.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;lists&lt;/span&gt; is mutable, because I need to add things in it, and I assume that I do not know what lists are coming in. (Even if they are pretty known from the file)&lt;/li&gt;
&lt;li&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;prices&lt;/span&gt; is a list of type &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;List[PriceItem]&lt;/span&gt;, this means that everything in that list is of something under type &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PriceItem&lt;/span&gt; but right now I am only interested in the types &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Price&lt;/span&gt; and &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PriceList&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;If it is a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;PriceList&lt;/span&gt; I know that a new price list is starting, I add this as a map key with an empty list as the map value.&lt;/li&gt;
&lt;li&gt;The flow goes on and for every &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Price&lt;/span&gt; that comes up this is added in the current list.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
The next chunk of interest...&lt;br /&gt;
&lt;pre class="codebox-content"&gt;    val ListExtractorRE = """.*"listname": "(.+)",.*\s*""".r
    val InstExtractorRE = """.*"ticker": "(.+)", "aktie": "(.+)".*\s*""".r
    /*....*/
    val rows = fileHandler.loadFile(fName)
    rows foreach { line =&amp;gt;
      line match {
        case ListExtractorRE(listname) =&amp;gt; val pList = new PriceList(listname)
          curList = pList.name
          pricelists += pList.name -&amp;gt; List()
        case InstExtractorRE(ticker, aktie) =&amp;gt; val inst = new Instrument(ticker, aktie) 
          pricelists(curList) = List(inst) ::: pricelists(curList)
        case _ =&amp;gt; None
      }
      
    } 
&lt;/pre&gt;
&lt;br /&gt;
Here is the loading of the price lists. I will sow you part of the file so you know what we are dealing with.
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;{ "pricelists": [
        { 
                "listname": "OMX Stockholm Mid Cap",
                "stocks": [
                        { "ticker": "ORES", "aktie": "&lt;d6&gt;resund"},
                        { "ticker": "AF B", "aktie": "&lt;c5&gt;F B"},
                        { "ticker": "VNIL SDB", "aktie": "Vostok Nafta Investment"},
                        { "ticker": "WIHL", "aktie": "Wihlborgs"},
                        { "ticker": "WALL B", "aktie": "Wallenstam B"},
&lt;/c5&gt;&lt;/d6&gt;&lt;/pre&gt;
&lt;/div&gt;
That is how it is saved. So seeinging that makes it easier to explain what the above code does.
&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;first there is 2 "extractors", these are regular expressions. In Scala you can use the 3-qouted strings to make a regular expression just by putting &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;.r&lt;/span&gt; after it. This expressions can then be used as types cases in pattern matching and is very powerful way to extract thing.&lt;/li&gt;
&lt;li&gt;(&lt;span class="Apple-style-span" style="white-space: pre;"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;""".*"listname": "(.+)",.*\s*""".r&lt;/span&gt;&lt;/span&gt;) is for extracting the name of a list. This matches on the line in the file beginning with any character and then "listname":&lt;/li&gt;
&lt;li&gt;the next "extractor" is for matching an instrument. for&amp;nbsp;convenience I write the file with a "ticker"&amp;nbsp;on a new line to avoid complex regexps. So I only need to parse the file and match on these.&lt;/li&gt;
&lt;li&gt;When matching a new list I put a new map entry with an empty list, and when hitting a "ticker" I just add that instrument in the current list. Similar to what is going on when saving them.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Major restruction of PriceLoad.&lt;/b&gt;&lt;/div&gt;
I more or less totally reworked the loading of the prices from the site they are available from&lt;br /&gt;
&lt;br /&gt;
One thing I wanted to do was to use actors. Here is were I got loose on doing that. Well, thats a bit exaggerating since there is only one Actor involved here (and another one in the Logging) so not so big fuzz. Here is the code, and I get into some details after it.&lt;br /&gt;
&lt;div class="codebox-header"&gt;
PriceLoad.scala&lt;/div&gt;
&lt;pre class="codebox-content"&gt;/*
 @author Peter Johansson / Ironic programmer 
 */

package me.ironic.scabors.dao
import me.ironic.scabors.model._
import me.ironic.scabors.util._
// Singleton to handle loading of Prices
object PriceFetcher extends Logging {
  import java.util.Calendar
  
  import java.text.SimpleDateFormat
    val fmt = new SimpleDateFormat("yyyyMMdd")
      
    def stringDateList(c: Calendar,bDate:String) : List[String] ={
      if(fmt.format(c.getTime()) == bDate) {
        List(fmt.format(c.getTime()))
      } else {
        val c2 = c.clone().asInstanceOf[Calendar]
        c2.add(Calendar.DATE,1)
        fmt.format(c.getTime()) :: stringDateList(c2,bDate)
      }
    }
      
   def main(args: Array[String]) = {
      if(args.length &amp;lt; 1  || args.length &amp;gt; 2){
        println("Usage: PriceFetcher YYYYMMDD # for single day")
        println("Usage: PriceFetcher YYYYMMDD YYYYMMDD # for batch mode")
        sys.exit(1)
      }
          
      val list = {
        if(args.length == 1){
          List(args(0))
        } else {
          require(args(1).compareTo(args(0)) &amp;gt; 0)
          val dFrom = fmt.parse(args(0))
          val cal = Calendar.getInstance()
          cal.setTime(dFrom)
          stringDateList(cal, args(1))
        }
      }
      
      info("Will run fetching for list " + list)
      val fetcher = new PriceFetchActor
      fetcher.start
      list foreach { s =&amp;gt;
        fetcher ! Daily(s)
      }
      fetcher ! Stop
      // Need to stop the logger, otherwise there is a hanging thread
      import scala.actors.Actor.State
      while(fetcher.getState != State.Terminated){
        // to wait until all work is done
      }
      stopLogger
      sys.exit(0)    
   }
}

import scala.actors.Actor
import scala.actors.Actor._
import me.ironic.scabors.util.Logging
class PriceFetchActor extends Actor with Logging{
  import scala.io.Source
      
  val baseUrl = "http://thesite/"
  def act(){
    loop{
      react {
        case d: Daily =&amp;gt; 
          info("Fetching prices for " + d)
          val uri = baseUrl + d.date + "k.txt"
          try{
            val lines = readUrl(uri)
            val handledLines = processLines(lines,d.date)
            info("Did lines : " + handledLines.size)
            PriceDAO.saveDailyPrices(handledLines)
          } catch {
            case x =&amp;gt; warn(x.toString)
          }
        case Stop =&amp;gt; 
          info("Stopping PriceFetchActor")
          exit()
        case _ =&amp;gt;
      }
    }
  }
  
  def readUrl(url: String) : List[String] = {
    val fc = Source.fromURL(url).getLines().toList 
    fc
  }
  
  val PriceExtractorRE = """([[^Ticker]^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t.*""".r
  val ListExtractorRE = """(OMX[^\t]+)\t+""".r
  
  // Should go through the lines from the file and make a list with jSon-like format. 
  def processLines(lines: List[String],date: String) : List[PriceItem] = {
    info("-&amp;gt; Processing lines")
    val instruments = lines takeWhile(s=&amp;gt; ! s.startsWith("Externa listan")) 
    instruments map { inst =&amp;gt;  
       inst match {
        case ListExtractorRE(list) =&amp;gt; PriceList(list)//"{\"instrumentList\": \"" + list + "\"}"
        case PriceExtractorRE(ticker,aktie,diffKr,diffPerc,buy,sell,last,high,low,returnNo,returnKr) 
          =&amp;gt; Price(date,ticker,aktie,diffKr.replace(",",".").toDouble,diffPerc.replace(",",".").toDouble,buy.replace(",",".").toDouble,sell.replace(",",".").toDouble,last.replace(",",".").toDouble,high.replace(",",".").toDouble,low.replace(",",".").toDouble,returnNo.replace(",",".").toDouble,returnKr.replace(",",".").toDouble) 
        case _ =&amp;gt; InvalidPrice(date,inst.replace("\t","[tab]"))
      } 
    }
  } 
}&lt;/pre&gt;
&lt;br /&gt;
This is an application object with a main method so it is available to run from the command line. What it does is to check if you enter with one or two arguments. Entering with one argument will load for one day. Entering with two arguments will run for a date span. I do not really car if there is days that does not have any prices, the only thing that will happen is that a FileNotFoundException will be logged.&lt;br /&gt;
&lt;br /&gt;
The date span wil be&amp;nbsp;recursively&amp;nbsp;calculated to a list of strings with the method stringDateList.&lt;br /&gt;
&lt;br /&gt;
After getting the list with dates, whether it is one item or more, I am traversing that list and sending a Daily object to an actor to do the loading and saving of that day. When all dates are handled I send a Stop message so the actor can exit itself.&lt;br /&gt;
&lt;br /&gt;
Since I am sending to actors the flow of the program just passes to the end of the main method so I have to check the state of the actor before I do the exit. (There is a hang otherwise, because there is a thread that is still running somewhere.) and I do not want to do a sys.exit() before since that kills the thread that is loading.&lt;br /&gt;
&lt;br /&gt;
The messages that is sent is case object/class that is defined like:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;case object Stop
case class Daily(date: String)
&lt;/pre&gt;
&lt;br /&gt;
Other than the actor logic, the load and save is more or less the same as before. Just that now it is threaded and runs by an actor by sending messages to it.&lt;br /&gt;
&lt;br /&gt;
Well, that was enough for this time. The GUI is more evolved also, but that is another story, another day. But a teaser to show how it looks so far:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-w9VnkgAspuc/TodCDxeRCII/AAAAAAAABdY/mxYosWetrLo/s1600/Screen+Shot+2011-10-01+at+6.38.02+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="206" src="http://2.bp.blogspot.com/-w9VnkgAspuc/TodCDxeRCII/AAAAAAAABdY/mxYosWetrLo/s320/Screen+Shot+2011-10-01+at+6.38.02+PM.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-1163694067784890491?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/WZF3pIQ2hMw2bQtZzadLT3uF7GE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/WZF3pIQ2hMw2bQtZzadLT3uF7GE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/WZF3pIQ2hMw2bQtZzadLT3uF7GE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/WZF3pIQ2hMw2bQtZzadLT3uF7GE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/WUe7cCUw6wg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/1163694067784890491/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/10/scala-stocks-part-5-refactoring-big.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1163694067784890491?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1163694067784890491?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/WUe7cCUw6wg/scala-stocks-part-5-refactoring-big.html" title="Scala stocks part 5 - Refactoring, big time" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-w9VnkgAspuc/TodCDxeRCII/AAAAAAAABdY/mxYosWetrLo/s72-c/Screen+Shot+2011-10-01+at+6.38.02+PM.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/10/scala-stocks-part-5-refactoring-big.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ECQnc-fSp7ImA9WhdaEE0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-5326722610858241792</id><published>2011-09-24T23:49:00.003+02:00</published><updated>2011-10-19T09:34:23.955+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T09:34:23.955+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><category scheme="http://www.blogger.com/atom/ns#" term="gradle" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="stocks" /><category scheme="http://www.blogger.com/atom/ns#" term="Scala swing" /><title>Scala stock charts, part 4 - Woking with the GUI - 1</title><content type="html">I have been busy the last couple of weeks, or better saying, too lazy to sit home with my Scala Technical Analysis tool. Anyway, this weekend I decided to continue and this time to start from the GUI and start using the functionality done so far from a GUI.&lt;br /&gt;
&lt;br /&gt;
I have also done som restructuring in the packages which now is:&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;package me.ironic.scabors.app&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;package me.ironic.scabors.service&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;package me.ironic.scabors.model&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;package me.ironic.scabors.file&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
even if I have not reworked the messy file-package. I will do that later. however, I finished up with loading the price list, and that code now look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;def loadPriceLists : MutableHashMap[String, List[Instrument]] = {
    log("## Loading pricelists")
    var pricelists = new MutableHashMap[String, List[Instrument]]
    val fc = Source.fromFile(pricelistsFile)
    val ListExtractorRE = """.*"listname": "(.+)",.*\s*""".r
    val InstExtractorRE = """.*"ticker": "(.+)", "aktie": "(.+)".*\s*""".r
   
    /*
     * When hitting a PriceList, a new list begins
     */
    var curList = "?"
    fc.getLines foreach { line =&amp;gt;
      line match {
        case ListExtractorRE(listname) =&amp;gt; val pList = new PriceList(listname)
          curList = pList.name
          pricelists += pList.name -&amp;gt; List()
        case InstExtractorRE(ticker, aktie) =&amp;gt; val inst = new Instrument(ticker, aktie) 
          pricelists(curList) = List(inst) ::: pricelists(curList)
        case _ =&amp;gt; None
      }
      
    } 
    return pricelists
  }
&lt;/pre&gt;
&lt;br /&gt;
I also broke out the model classes from the file package, and put it in another file called PriceModel.scala, you can also see that I am now using "Price" which is a better name than "Quote" so all references to Quote is now replaced with Price. The content of that file is:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="codebox-header"&gt;
PriceModel.scala
&lt;/div&gt;
&lt;pre class="codebox-content"&gt;package me.ironic.scabors.model

// To be able to handle Iterator[PriceItem]
trait PriceItem 

case class InvalidPrice(date: String, content:String) extends PriceItem
case class PriceList(name:String) extends PriceItem
case class Price(date: String, ticker :String, aktie : String,
    diffKr : Double, diffPerc : Double, buy : Double,
    sell : Double, last : Double, high : Double,
    low : Double, returnNo : Double, returnKr : Double
  ) extends PriceItem
case class Instrument(ticker:String, aktie: String)  
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
I also made a Service object so that the GUI does not need to know or depend on anything from the file package. So far as I have gotten with the GUI it just contains two methods, one to get all price lists and one to get the data for a chosen stock. Here is the code:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="codebox-header"&gt;
PriceService.scala
&lt;/div&gt;
&lt;pre class="codebox-content"&gt;package me.ironic.scabors.service

object PriceService {
  import me.ironic.scabors.file.FileHandler
  val pricelists = {
    println("# loading pricelists")
    FileHandler.loadPriceLists
  }
  
  def getPriceLists = {
     // using already loaded pricelists, i.e loading is done in val...
     pricelists
  }
  
  def getInstrument(inst: String) = {
    FileHandler.loadInstrument(inst)
  }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;h2&gt;

GUI programming done in Scala&lt;/h2&gt;
&lt;div&gt;
I decided to leave the idea of using Java FX that I had from the beginning and that I wrote in the beginning post. Instead I decided to use Scala Swing. Not so easy as it turned out, because first of all, it has been a couple of years since I last did any Java Swing programming and second of all, it was not very similar and not easy to transform Java Swing thinking to Scala Swing thinking. Well, it is less code, but surly, at this stage it looks really messy. Maybe it takes a while for my brain to melt Scala Swing programming and the fact that google got warm from all searching and few good examples does not help.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
I am to tired to explain most of the code, maybe in a later post, but here is what the purpose if the GUI is so far.&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Provide a text field to filter search for stocks "Instrument"&lt;/li&gt;
&lt;li&gt;Provide a button to perform the search&lt;/li&gt;
&lt;li&gt;Provide a ComboBox to present the filtered result from the search and to select stock to draw&lt;/li&gt;
&lt;li&gt;Provide Panel for drawing a Bar Chart of selected stock&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-fVq0VUf96N0/Tn5Rd-3BITI/AAAAAAAABdU/3_Jb9VFdjhw/s1600/Screen+Shot+2011-09-24+at+11.34.19+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="76" src="http://3.bp.blogspot.com/-fVq0VUf96N0/Tn5Rd-3BITI/AAAAAAAABdU/3_Jb9VFdjhw/s320/Screen+Shot+2011-09-24+at+11.34.19+PM.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
I have implemented and it works as I wand, except that the tedious job to provide the functionality to draw a bar chart is not done. Thea is a later&amp;nbsp;exercise&amp;nbsp;and a future post to describe. I let you see the messy code for this.&amp;nbsp;&lt;/div&gt;
&lt;div class="codebox-header"&gt;
ScaBorsApp.scala
&lt;/div&gt;
&lt;pre class="codebox-content"&gt;package me.ironic.scabors.app

import swing._
import java.awt.Dimension
import me.ironic.scabors.service._
import me.ironic.scabors.model._
object ScaBorsApp extends SimpleSwingApplication{
      
    def top = new MainFrame {
      title = "Technical Analysis for Stocks"
      val searchField = new TextField {
          text = "a"
          columns = 20
        }
       /*
       * Search button, will get the pricelist and do filter and sort to present in combo box
       */
      val searchBtn = new Button("Search") {
          action = Action("Search"){ 
            val pricelists = PriceService.getPriceLists map { kv =&amp;gt; kv._2 } flatMap { 
                case i: Instrument =&amp;gt; i 
                case x =&amp;gt; x
              } filter {
                i: Instrument =&amp;gt; i.aktie.toLowerCase().startsWith(searchField.text.toLowerCase)
              }
            
            val items = pricelists.toList sortWith {(i1,i2) =&amp;gt; i1.aktie.toLowerCase &amp;lt; i2.aktie.toLowerCase}
            combo.peer.setModel(
              ComboBox.newConstantModel(
                  for{p &amp;lt;- items} yield p.ticker + ": " + p.aktie
              )
            )
          }
        }
    
      /*
       * ComboBox to present filtered search for stocks, see searchButton action code how search is performed
       */
      import event._
      val combo = new ComboBox(List("-- Search to be able to select --")) {
        preferredSize = new Dimension(300,20)
        listenTo(selection)
        reactions += {
          case SelectionChanged(c) =&amp;gt;
            curInstrument = new Instrument(selection.item.split(":")(0),selection.item.split(":")(1).trim)
            curPriceList = PriceService.getInstrument(curInstrument.ticker)
            barchartPanel.updateList(curPriceList)

        }
      }
      
      var curInstrument: Instrument = Instrument("---","Not choosen")
      var curPriceList: List[PriceItem] = Nil
      
      /*
       * Top menu panel.
       */
      val menu = new FlowPanel {
        contents += new Label("Stock")
        contents += searchField
        contents += searchBtn
        contents += combo
      }
      
    val barchartPanel = new BarChartPanel(curPriceList)
      val scrollPane = new ScrollPane {
        preferredSize = new Dimension(1180,700)
        contents = barchartPanel
      }
      

      contents = new BorderPanel() {
        add(menu, BorderPanel.Position.North) 
        add(scrollPane, BorderPanel.Position.Center)
      }
     
      preferredSize = new Dimension(1200,800)
    }
}

class BarChartPanel(var list: List[PriceItem]) extends Panel {
  import java.awt.Color
  background = Color.white
  preferredSize = new Dimension(1180,680)
  override protected def paintComponent(g: Graphics2D) {
    super.paintComponent(g)
    
  } 
  
  def updateList(newList: List[PriceItem]) = {
    list = newList
    println("Updating list : " + list)
  }
}

&lt;/pre&gt;
&lt;br /&gt;
Yeah, thats right, I also gave up SBT, I am now using gradle to build. here is the code for that.
build.gradle
&lt;br /&gt;
&lt;div class="codebox-header"&gt;
build.gradle
&lt;/div&gt;
&lt;pre class="codebox-content"&gt;apply plugin: 'scala'

repositories {
    mavenCentral()
}

dependencies {
    // Libraries needed to run the scala tools
    scalaTools 'org.scala-lang:scala-compiler:2.9.1'
    scalaTools 'org.scala-lang:scala-library:2.9.1'

    // Libraries needed for scala api
    compile 'org.scala-lang:scala-library:2.9.1'

    // Libraries needed for scala.swing
//    scalaTools 'org.scala-lang:scala-swing:2.9.1'
    compile 'org.scala-lang:scala-swing:2.9.1'
}


task createSourceDirs &amp;lt;&amp;lt; {
  println "# Creating source dirs"
  ['src/main/scala','src/test/scala','src/main/java','src/test/java'].each { 
    println " - $it"
    new File(it).mkdirs()
  }
}
&lt;/pre&gt;
&lt;br /&gt;
Until later..... ciao!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-5326722610858241792?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VARWQfqj6yOAFxaAI9NdmbX9sIo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VARWQfqj6yOAFxaAI9NdmbX9sIo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VARWQfqj6yOAFxaAI9NdmbX9sIo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VARWQfqj6yOAFxaAI9NdmbX9sIo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/S_n0q8E0NFU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/5326722610858241792/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/09/scala-stock-charts-part-4-woking-with.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/5326722610858241792?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/5326722610858241792?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/S_n0q8E0NFU/scala-stock-charts-part-4-woking-with.html" title="Scala stock charts, part 4 - Woking with the GUI - 1" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-fVq0VUf96N0/Tn5Rd-3BITI/AAAAAAAABdU/3_Jb9VFdjhw/s72-c/Screen+Shot+2011-09-24+at+11.34.19+PM.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/09/scala-stock-charts-part-4-woking-with.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4MQHkzfip7ImA9WhdWGUU.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-7527538976287752762</id><published>2011-09-13T21:41:00.000+02:00</published><updated>2011-09-14T09:06:21.786+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-09-14T09:06:21.786+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Servlet" /><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="web-fragments" /><category scheme="http://www.blogger.com/atom/ns#" term="sbt" /><title>Web Fragment using Scala</title><content type="html">Do you want to use Scala in a Java web environment, or write web fragment using scala. Or maybe you would ask: why do it at all? The answer is, because you can!&lt;br /&gt;
&lt;br /&gt;
Well my little spike succeeded without any severe headache, actually so well that I did not got any at all and there was never any risk to get one.&lt;br /&gt;
&lt;br /&gt;
I used SBT to build my Scala code, probably I could use it to make the full application, but I used Netbeans to create a web projet for my spike.&lt;br /&gt;
&lt;br /&gt;
Setting up for SBT.&lt;br /&gt;
&lt;br /&gt;
create folder for the project, for example:&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;gt;mkdir webfrag&lt;/span&gt;&lt;br /&gt;
enter the directory and create and edit build.sbt for servlet programming&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;gt;touch build.sbt&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Put the following in the build-sbt file
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;name := "WebFrag"
 
version := "1.0"
 
organization := "me.ironic"
 
scalaVersion := "2.9.1"
 
scalaSource in Compile &amp;lt;&amp;lt;= baseDirectory(_ / "src")
 
scalaSource in Test &amp;lt;&amp;lt;= baseDirectory(_ / "test")
 
libraryDependencies += "javax" % "javaee-api" % "6.0" % "provided"
 
libraryDependencies += "log4j" % "log4j" % "1.2.16" % "provided"
 
libraryDependencies += "junit" % "junit" % "4.8.2" % "test"
&lt;/pre&gt;
&lt;br /&gt;
Create a Servlet in Scala in this way.&lt;br /&gt;
create &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;src/&lt;/span&gt; folder and create and edit a file for the servlet, for example &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;src/WebFragHello.scala&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
You can write what you want, but to make a form of HelloWorld-like example fill it with for example:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;&lt;span class="Apple-style-span"&gt;package me.ironic

import javax.servlet.annotation._
import java.io._
import javax.servlet._
import http._

@WebServlet(name = "WebFragHelloServlet", urlPatterns = {Array("/hello")})
class WebFragHelloServlet extends HttpServlet {
        override def doGet(req: HttpServletRequest, res: HttpServletResponse) : Unit = {
           val user = req.getParameter("user") match {
                case s: String =&amp;gt; s
                case _ =&amp;gt; "John Doe"
                }
           val out = res.getWriter()
           out.println(toHtml(user).toString())
        }
        def toHtml(user: String) = 
        &amp;lt;html&amp;gt;
       &amp;nbsp;&amp;lt;body&amp;gt;
         &amp;nbsp;&amp;lt;h1&amp;gt;Hello {user}&amp;amp;lt/h1&amp;gt;
       &amp;nbsp;&amp;lt;/body&amp;gt;
       &amp;nbsp;&amp;lt;/html&amp;gt;

}

&lt;/pre&gt;
&lt;br /&gt;
This code also "catches" the case that user parameter is not sent.A special feature seams to be that the scala code does not understand urlPattern as used in Java in the annotation, that is why I use {Array("/hello")} instead of {"/hello"}&lt;br /&gt;
&lt;br /&gt;
Compile and package the code with SBT&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;gt;sbt compile&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&amp;gt;sbt package&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
To make it work as a web-fragment: Put the compiled jar in WEB-INF/lib folder in a Servlet 3.0 server, you also need to put scala-library.jar in the server to be able to use scala objects. In netbeans you just need to add that jar-file to "libraries" folder in the test web project&lt;br /&gt;
&lt;br /&gt;
Surf to .../context-root/hello or&amp;nbsp;/context-root/hello?user=Ironic&lt;br /&gt;
&lt;br /&gt;
Neet isn't it?&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-7527538976287752762?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jRrV_zMhHgSKzwYijczL6Q_4neo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jRrV_zMhHgSKzwYijczL6Q_4neo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jRrV_zMhHgSKzwYijczL6Q_4neo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jRrV_zMhHgSKzwYijczL6Q_4neo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/H6FoiHe1u4U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/7527538976287752762/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/09/web-fragment-using-scala.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7527538976287752762?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7527538976287752762?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/H6FoiHe1u4U/web-fragment-using-scala.html" title="Web Fragment using Scala" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/09/web-fragment-using-scala.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ECQnc-fip7ImA9WhdaEE0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-6152341331317964843</id><published>2011-08-22T22:51:00.001+02:00</published><updated>2011-10-19T09:34:23.956+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T09:34:23.956+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><title>Scala stock charts, part 3 - Saving the quotes</title><content type="html">Going further in programming the functionality behind the technical analysis tool for stocks made in Scala we are going to save the daily quotes to separate file, we are also going to supply functionality to load the data for a stock, and save and read a price list divided in 3 lists.&lt;br /&gt;
&lt;br /&gt;
The format of the files is a bitt different, the Quotes I will save and read as they are, but the price list will have a different format that will look as jSon and is saved in a way that will be easy to read with pattern matching. For the matter of fact also the Stock-quotes for a single instrument will be read and transformed to a list of Quote's with pattern matching.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="codebox-header"&gt;FileHandler.scala&lt;/div&gt;&lt;pre class="codebox-content"&gt;/*
 @author Peter Johansson / Ironic programmer 
 */

package scala.ironic.file

object FileHandler {
  import java.io.{File,BufferedWriter,FileWriter,IOException}
  import scala.collection.mutable.{ HashMap =&amp;gt; MutableHashMap}
  import scala.collection.immutable.{ HashMap }
  import scala.io.Source
  
  val rootDir = { scala.util.Properties.userDir }
  val filesDir = { rootDir + "/files"}
  val logFile = { rootDir + "/" + "sBors.log"}
  val pricelistsFile = { rootDir + "/files/pricelists.txt"}
    
  def saveDailyQuotes(quotes: Iterator[QuoteItem]) = {
    log("## Saving daily qoutes")
    log("--- rootDir : " + rootDir)
    log("--- filesDir : " + filesDir)
    // Check that dir exista or creat if not
    checkDir(filesDir)
    
    var lists = new MutableHashMap[String,List[Quote]]
    var curList = "?"
    
    quotes foreach { quote =&amp;gt;
      quote match { 
        case q: Quote =&amp;gt; { 
                            saveQuoteToFile(q);
                            lists(curList) = List(q) ::: lists(curList)
                          }
        case qList: QuoteList =&amp;gt; {
                                    curList = qList.name;
                                    lists += qList.name -&amp;gt; List()
                                 }
        case _ =&amp;gt; log("! " + quote)
      }
    }
    
    // Save current lists to file...
    saveStockPriceList(lists)
    
  }
  
  def checkDir(dirName: String){
     val f = new File(dirName)
     if(!f.exists){
       f.mkdirs
       log("!! Created dir at : " + dirName)
     }
  }
  
  def saveQuoteToFile(quote: Quote) = {
    val fileName = scala.util.Properties.userDir + "/files/" + quote.ticker.replace(" ","_") + ".txt"
    val bw = new BufferedWriter(new FileWriter(fileName,true))
    bw.write(quote + "\n")
    bw.close()
  }
  
  def saveStockPriceList(pricelist: MutableHashMap[String,List[Quote]]){
    log("## Saving pricelists")
    val bw = new BufferedWriter(new FileWriter(pricelistsFile,false))
    val sb = new StringBuffer
    sb.append("{ \"pricelists\": [\n")
    for{ (listname,quotes) &amp;lt;- pricelist } {
      sb.append("\t{ \n\t\t\"listname\": \"" + listname + "\",\n")
      sb.append("\t\t\"stocks\": [\n")
      for{ quote &amp;lt;- quotes } {
        sb.append("\t\t\t{ \"ticker\": \""+quote.ticker + "\", \"aktie\": \"" + quote.aktie + "\"},\n")
      }
      sb.append("\t\t],\n")
      sb.append("\t},\n")
    }
    sb.append("}\n")
    
    bw.write(sb.toString)
    bw.close()
  }
  
  def log(msg: String){
    println(msg)
    val bw = new BufferedWriter(new FileWriter(logFile,true))
    bw.write(msg + "\n")
    bw.close()
    
  }
  
  def loadPriceLists : HashMap[String, List[Instrument]] = {
    log("## Loading pricelists")
    val pricelists = new HashMap[String, List[Instrument]]
    val fc = Source.fromFile(pricelistsFile)
    val ListExtractorRE = """.*"listname": "(.+)",.*\s*""".r
    val InstExtractorRE = """.*"ticker": "(.+)", "aktie": "(.+)".*\s*""".r
   
    fc.getLines foreach { line =&amp;gt;
      line match {
        case ListExtractorRE(listname) =&amp;gt; println("#### got listname : " + new QuoteList(listname))
        case InstExtractorRE(ticker, aktie) =&amp;gt; 
          println("# got instrument : " + new Instrument(ticker, aktie))
        case _ =&amp;gt; 
      }
      
    }
    return pricelists
  }
  
  def loadInstrument(ticker: String): List[QuoteItem] = {
    val fn = rootDir + "/files/" + ticker.replace(" ","_") + ".txt"
    val fc = Source.fromFile(fn)
    val QuoteExtractorRE = """Quote\(([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*),([^\,]*).*\)""".r
    println(QuoteExtractorRE)
    val quotelines = fc.getLines// filter( s=&amp;gt; ! s.startsWith("Quote")) 
    val quotes = quotelines map { line =&amp;gt;
      line match {
        case QuoteExtractorRE(date,ticker,aktie,diffKr,diffPerc,buy,sell,last,high,low,returnNo,returnKr) =&amp;gt; 
          Quote(date,ticker,aktie,diffKr.toDouble,diffPerc.toDouble,buy.toDouble,sell.toDouble,last.toDouble,high.toDouble,low.toDouble,returnNo.toDouble,returnKr.toDouble)
        case _ =&amp;gt; InvalidQuote("?", line)
      } 
    } 
    return quotes.toList
  }
  
  
}

&lt;/pre&gt;That is a lot of code that I wrote on the fly, but not as much as it would have been to do the same thing in Java. the nice thing is the pattern matching, that saves a lot pain that Java code would bring, for example reading every line in the file would not be more code, but also creating and getting the values to objects would give a lot of more code.&lt;br /&gt;
&lt;br /&gt;
[coming... Java code to do it as a comparison]&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;checkDir&lt;/span&gt;: is a method to create directory structure if it does not exist. Actually the first time loading the quotes there is no /files dir if it is not created manually, to avoid crashing, and open upp for the&amp;nbsp;possibility&amp;nbsp;to later use some config to have the data files in a directory of choice this is to ensure that the supplied configured dir will be created.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;saveDailyQuotes&lt;/span&gt;: traverse the content of a daily quote file and calls to save single quote and collect the price lists and calls to save these.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;saveStockPriceList&lt;/span&gt;: saves the current instruments divided in their lists to a file.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;saveQuoteToFile&lt;/span&gt;: append a single quote to its data file.&lt;br /&gt;
&lt;br /&gt;
Finally the methods for loading price lists and a single instrument&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;loadPriceLists&lt;/span&gt;: loads the pice lists back to a map with a list name and a list of Instruments. (Actually it is note yet finished, since I want to think out a way to avoid having a mutable HashMap, now it only prints out what it gets.)&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;loadInstrument&lt;/span&gt;: loads the file of a single stock to a list of Quote's&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
I am not completely happy with it, there is some duplication of code, for example I create new Writers in every method that save things. There is certainly things that can improve here. However, refactoring is a later issue, and this does what I want it to do, so for now it will have to do.&lt;br /&gt;
&lt;br /&gt;
Now the interesting part is beginning to appear, next thing is to make functionality to take car of the technical stuff for the charts to be drawn.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-6152341331317964843?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/zo_mASyLW0eVfSB6SE68WxlUJ5w/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zo_mASyLW0eVfSB6SE68WxlUJ5w/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/zo_mASyLW0eVfSB6SE68WxlUJ5w/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/zo_mASyLW0eVfSB6SE68WxlUJ5w/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/EpRN2-8x9bE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/6152341331317964843/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-3-saving-quotes.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6152341331317964843?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6152341331317964843?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/EpRN2-8x9bE/scala-stock-charts-part-3-saving-quotes.html" title="Scala stock charts, part 3 - Saving the quotes" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-3-saving-quotes.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ECQnc-cCp7ImA9WhdaEE0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-8282761050037410567</id><published>2011-08-19T16:46:00.001+02:00</published><updated>2011-10-19T09:34:23.958+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T09:34:23.958+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><category scheme="http://www.blogger.com/atom/ns#" term="stocks" /><title>Scala stock charts, part 2 - Getting the quotes</title><content type="html">I am Swedish so I am for the moment only interested in the Swedish stock market. I have found a site where to get the quotes, it is in text format and I intend to read this information and save it in in other files, one file for each instrument (I call on single stock instrument since it is a form of financial instrument).&lt;br /&gt;
&lt;br /&gt;
The format of the daily file is: (There is an example file to work with at : &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;http://sites.google.com/site/ironicprogrammer/files/20110817k.txt&lt;/span&gt;)&lt;br /&gt;
&lt;br /&gt;
&lt;div class="codebox-content"&gt;Slutkurser från OMX 2011-08-17																Slutkurser från Burgundy 2011-08-17				&lt;br /&gt;
Ticker	Aktie	+/-	+/-%	Köp	Sälj	Senast	Högst	Lägst	Oms (antal)	Oms (SEK)						Senast	Högst	Lägst	Oms (antal)	Oms (SEK)&lt;br /&gt;
OMX Stockholm Large Cap																				&lt;br /&gt;
ABB	ABB Ltd	-2,30	-1,66	136,40	136,50	136,40	139,40	136,40	3299500	453921900						136,6	139,4	136,6	183000	25240000&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;It is hard to see in that little box, however. first row is title information of the file.&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Slutkurser från OMX 2011-08-17[6 tabs]Slutkurser från Burgundy 2011-08-17	&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
That is not really&amp;nbsp;necessary&amp;nbsp;and can be skipped as the next line, but the next line is interesting to see the structure of the quotes:&lt;br /&gt;
&lt;br /&gt;
&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span class="Apple-style-span" style="color: #6fa8dc;"&gt;Ticker&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Aktie&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;+/-&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;+/-%&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Köp&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Sälj&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Senast&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Högst&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Lägst&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Oms (antal)&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Oms (SEK)&lt;/span&gt;[6 tabs]Senast&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Högst&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Lägst&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Oms (antal)&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;Oms (SEK)&lt;/span&gt;&lt;/div&gt;&lt;div class="p1"&gt;&lt;/div&gt;&lt;br /&gt;
&amp;nbsp;When reading the file I will only be interested in the quotes from OMX, the Burgundy I do not want so when saving the quotes to different files I will not&amp;nbsp;consider&amp;nbsp;these, so it is the blue marked fileds that would be used.&lt;br /&gt;
&lt;br /&gt;
The third row &lt;br /&gt;
&lt;br /&gt;
&lt;div class="p1"&gt;&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;OMX Stockholm Large Cap&lt;/span&gt;&lt;span class="Apple-tab-span"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
is the name of the first list of quotes. It continues like this in the file;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Listname&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Ticker Aktie ...&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;...&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Listname ...&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Ticker Aktie ...&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;...&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;I am only interested in the first three lists, the forth is named "Externa listan", and I will use this name to break the read.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;So lets start write som code (finally!!)&lt;/b&gt;&lt;/div&gt;&lt;div&gt;I use the REPL for a little bit trial before I put in in classes/objects.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;To test to read the file:&lt;/div&gt;&lt;div class="codebox-content"&gt;scala&amp;gt; val url = "http://sites.google.com/site/ironicprogrammer/files/"             &lt;br /&gt;
url: java.lang.String = http://sites.google.com/site/ironicprogrammer/files/&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val file = "20110817k.txt"                               &lt;br /&gt;
file: java.lang.String = 20110817k.txt&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; import scala.io.Source&lt;br /&gt;
import scala.io.Source&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val fc = Source.fromURL(url+file)&lt;br /&gt;
fc: scala.io.BufferedSource = non-empty iterator&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; fc.getLines foreach { println }&lt;br /&gt;
Slutkurser fr?n OMX 2011-08-17																Slutkurser fr?n Burgundy 2011-08-17				&lt;br /&gt;
Ticker	Aktie	+/-	+/-%	K?p	S?lj	Senast	H?gst	L?gst	Oms (antal)	Oms (SEK)						Senast	H?gst	L?gsOms (antal)	Oms (SEK)&lt;br /&gt;
OMX Stockholm Large Cap																			&lt;br /&gt;
ABB	ABB Ltd	-2,30	-1,66	136,40	136,50	136,40	139,40	136,40	3299500	453921900						136,6	139,4	136,6	183000	25240000&lt;br /&gt;
ALFA	Alfa Laval	-0,80	-0,68	117,50	117,60	117,50	119,10	114,80	1121500	131304100						117,6	118,9	114,49223	5760000&lt;/div&gt;&lt;div&gt;Ok, everything seams to work so this need to be taken car of in some other way.&lt;br /&gt;
&lt;br /&gt;
Lets put it in a bit more organized way:&lt;br /&gt;
&lt;br /&gt;
I put a file in src/main/scala/ironic that is named &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;QuoteLoad.scala&lt;/span&gt; with the following content:&lt;/div&gt;&lt;div class="codebox-header"&gt;QuoteLoad.scala&lt;/div&gt;&lt;pre&gt;&lt;div class="codebox-content"&gt;/*
 @author Peter Johansson / Ironic programmer 
 */

package scala.ironic

// To be able to handle Iterator[QuoteItem]
trait QuoteItem 

case class InvalidQuote(date: String, content:String) extends QuoteItem
case class QuoteList(name:String) extends QuoteItem
case class Quote(date: String, ticker :String, aktie : String,
    diffKr : Double, diffPerc : Double, buy : Double,
    sell : Double, last : Double, high : Double,
    low : Double, returnNo : Double, returnKr : Double
  ) extends QuoteItem

// Singleton to handle loading of quotes
object QuoteLoad {
   def main(args: Array[String]) = {
      
      loadQuotes("http://sites.google.com/site/ironicprogrammer/files/","20110817")
   }
   
   val QuoteExtractorRE = """([[^Ticker]^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t.*""".r
   val ListExtractorRE = """(OMX[^\t]+)\t+""".r
  
   def loadQuotes(url: String, date: String){
      import scala.io.Source
      
      val uri = url + date + "k.txt"
      val fc = Source.fromURL(uri) 

      val instruments = processLines(fc.getLines(),date)
      println (instruments)
      instruments foreach { inst =&amp;gt; inst match {
        case ilist: QuoteList =&amp;gt; 
            println(ilist)
        case q: Quote =&amp;gt;
            println(q)
        case _ =&amp;gt; println(inst)//
        }
      }
   }
  
    // Should go through the lines from the file and make a iterable with QuoteItem's. 
    def processLines(lines: Iterator[String],date: String) : Iterator[QuoteItem] = {
      val instruments = lines takeWhile(s=&amp;gt; ! s.startsWith("Externa listan")) 
      instruments map { inst =&amp;gt;  
         inst match {
          case ListExtractorRE(list) =&amp;gt; QuoteList(list)//"{\"instrumentList\": \"" + list + "\"}"
	  case QuoteExtractorRE(ticker,aktie,diffKr,diffPerc,buy,sell,last,high,low,returnNo,returnKr) 
            =&amp;gt; Quote(date,ticker,aktie,diffKr.replace(",",".").toDouble,diffPerc.replace(",",".").toDouble,buy.replace(",",".").toDouble,sell.replace(",",".").toDouble,last.replace(",",".").toDouble,high.replace(",",".").toDouble,low.replace(",",".").toDouble,returnNo.replace(",",".").toDouble,returnKr.replace(",",".").toDouble) 
          case _ =&amp;gt; InvalidQuote(date,inst.replace("\t","[tab]"))
        } 
      } 
    }
    
}
&lt;/div&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;b&gt;Ok there may be some features to explain here.&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
First of all, I make use of an singleton object here so I will be able to call from a terminal window. So it has a main method, but I also extract the functionality so the future client will be able to use the same methods to load quotes.&lt;br /&gt;
&lt;br /&gt;
There is a &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;trait&lt;/span&gt; so I can avoid getting &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Iterator[Any]&lt;/span&gt; and instead getting &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Iterator[QuoteItem]&lt;/span&gt; to be able to handle the &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;case class&lt;/span&gt; objects only, when I later will save the quotes to different files.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;QuoteList&lt;/span&gt;: is for holding the name of a list (there is different list of quotes according to its category, that is more or less the amount of trading that is done and the size of the companies)&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Quote&lt;/span&gt;: is for holding the quote for the day for a specific share&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;InvalidQuote&lt;/span&gt;: is for holding (and logging for error checking) an invalid row from the file.&lt;br /&gt;
&lt;br /&gt;
There is two regexp's that is handled later in pattern matching to extract the lists and quotes from the file. They look for the structure in the file for valid quotes and the simpler one is for extracting the list name that is only one text.&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;def loadQuotes(url: String, date: String)&lt;/span&gt;: handles the load from the URL and calls a helper method to extract the valid quotes and lists.&lt;br /&gt;
&lt;br /&gt;
This will have to do for a first version, later I will save these quotes to different files.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-8282761050037410567?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/vPlCNNtl4AVQN_SidBEnIyotpJc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vPlCNNtl4AVQN_SidBEnIyotpJc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/vPlCNNtl4AVQN_SidBEnIyotpJc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/vPlCNNtl4AVQN_SidBEnIyotpJc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/D7DgI7XTc10" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/8282761050037410567/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-2-getting.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/8282761050037410567?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/8282761050037410567?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/D7DgI7XTc10/scala-stock-charts-part-2-getting.html" title="Scala stock charts, part 2 - Getting the quotes" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/08/scala-stock-charts-part-2-getting.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0ECQnc-eyp7ImA9WhdaEE0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-3475329119887182527</id><published>2011-07-31T14:41:00.000+02:00</published><updated>2011-10-19T09:34:23.953+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-19T09:34:23.953+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><category scheme="http://www.blogger.com/atom/ns#" term="SSC (Scala Stock Charts)" /><category scheme="http://www.blogger.com/atom/ns#" term="stocks" /><title>Scala stock charts, part 1 - Thoughts about structure</title><content type="html">Having read through almost halfway, and not even coming to the functional part, in the online book "Programming Scala" by Alex Payne (&lt;a href="http://ofps.oreilly.com/titles/9780596155957"&gt;http://ofps.oreilly.com/titles/9780596155957&lt;/a&gt;), I am starting to get some ideas of what I want to try out in my new Technical Analysis program for stocks. &amp;nbsp;For sure I would like to use SBT (&lt;a href="https://github.com/harrah/xsbt/wiki"&gt;https://github.com/harrah/xsbt/wiki&lt;/a&gt;) as a building tool and try to use "Specs" for tests (&lt;a href="http://ofps.oreilly.com/titles/9780596155957/ScalaToolsLibs.html#ScalaSpecs"&gt;http://ofps.oreilly.com/titles/9780596155957/ScalaToolsLibs.html#ScalaSpecs&lt;/a&gt;).&lt;br /&gt;
&lt;br /&gt;
To start with I want to build my program using either common classes or &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;case classes&lt;/span&gt;, haven´t clearly figured that out, at least I want to use &lt;i&gt;companion objects&lt;/i&gt;. Anyway, I want to get in touch with different features in Scala so there probably will be some file handling with regexp in Scalas pattern matching. At the same way as I want to get in touch with as many features as possible, I also want to keep it as simple as possible so there probably will be som rewrites during the course of my little project as I learn more. If possible I will use trait also, but probably after a rework. I will start more "java-like" with more OO-structure and then try to "functionalize" it.&lt;br /&gt;
&lt;br /&gt;
I haven´t really figured out if I want o use JavaFX either. So I will try to build a library in Scala that ccan be used by client of choice, may be JavaFX but maybe also Flex or Swing. That I will figure out later.&lt;br /&gt;
&lt;br /&gt;
More about this later....&lt;br /&gt;
and then probably with some code :)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-3475329119887182527?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/uQZCQFaENe7TqccVy6D2_2Fmyks/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uQZCQFaENe7TqccVy6D2_2Fmyks/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/uQZCQFaENe7TqccVy6D2_2Fmyks/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uQZCQFaENe7TqccVy6D2_2Fmyks/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/N2uvD4cys5c" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3475329119887182527?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3475329119887182527?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/N2uvD4cys5c/scala-stock-charts-part-1-thoughts.html" title="Scala stock charts, part 1 - Thoughts about structure" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/07/scala-stock-charts-part-1-thoughts.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYHRnw-cCp7ImA9WhdREUw.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-2976589558796088422</id><published>2011-07-11T14:36:00.001+02:00</published><updated>2011-07-31T14:42:17.258+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-31T14:42:17.258+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Scala" /><title>Long time no see</title><content type="html">It has been a while since last post. Sorry about that! It has been a period of organizational problems (not as fun as programming problems that needs to be solved) that could have been written about, but to sit in the middle of a stormy change takes the energy away.&lt;br /&gt;
&lt;br /&gt;
Anyway, this is what I have been doing:&lt;br /&gt;
I started looking at Scala. For me it looks really interesting and I think it is something that is worth looking in to since it joins well with Java and runs on the JVM. You also need to write less code to do the same thing as with Java.&lt;br /&gt;
To learn Scala I have small idea that I should take my old Java-Swing tool for technical analysis of stocks on the Swedish stock market, set it up to use Scala with a JavaFX-client for the GUI. In this way I intend to get into Scala and at the same time JavaFX a little bit. this little tool of mine I have had as a project to learn new programming languages for over 10 years now. From the first version in an Excel spreadsheet it has taken the way over Turbo-pascal, Visual C++, maybe I also had a C#-client for a while until I returned to the Java Swing client.&lt;br /&gt;
&lt;br /&gt;
I also started to read a lot of agile books about modelling and developing and also a bit about Neuro Linguistic Programming but that is a completely different subject but may be good if you want to take a leading role and/or how to handle the users and other team members and just get better at asking questions as one area of self development.&lt;br /&gt;
&lt;br /&gt;
I will go on, so maybe there will soon be a posting about Scala or JavaFX or both. Or something completely different.&lt;br /&gt;
&lt;br /&gt;
Ciao&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-2976589558796088422?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/iamLqNemiuia3pLYHUo3pDcpHUM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iamLqNemiuia3pLYHUo3pDcpHUM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/iamLqNemiuia3pLYHUo3pDcpHUM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/iamLqNemiuia3pLYHUo3pDcpHUM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/dZAz8M3y3bA" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/2976589558796088422?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/2976589558796088422?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/dZAz8M3y3bA/long-time-no-see.html" title="Long time no see" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2011/07/long-time-no-see.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYCR3s_eCp7ImA9WhdREUw.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-6196533781838253752</id><published>2010-10-06T12:59:00.001+02:00</published><updated>2011-07-31T14:42:46.540+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-31T14:42:46.540+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Glassfish v3" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Problem with Grails on Glassfish v3?</title><content type="html">I almost gave up trying to run grails applications on Glassfish v3. Today I stumbled on a solution that was so easy that I should be ashamed I did not investigate a bit further. The problem is with some OSGi headers and is documented &lt;a href="http://jira.codehaus.org/browse/GRAILS-6096"&gt;here&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
Others have had the problem and the solution I found on nabble was pretty simple:&lt;br /&gt;
Add the following line in BuildConfig.groovy in your project that should run on a Glassfish v3.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;grails.project.war.osgi.headers=false&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This helps with the problem not being able to deploy a grails app on Glassfish v3 when you experience the following hints in the log on GF:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;SEVERE: Module type not recognized&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: red; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;SEVERE: There is no installed container capable of handling this application com.sun.enterprise.deploy.shared.FileArchive@68237947&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-6196533781838253752?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/c11xgZkIJO2YFFozf6lDCrcgsBE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c11xgZkIJO2YFFozf6lDCrcgsBE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/c11xgZkIJO2YFFozf6lDCrcgsBE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/c11xgZkIJO2YFFozf6lDCrcgsBE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/Yw6IKPRgEEg" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6196533781838253752?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6196533781838253752?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/Yw6IKPRgEEg/problem-with-grails-on-glassfish-v3.html" title="Problem with Grails on Glassfish v3?" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/10/problem-with-grails-on-glassfish-v3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkEAQngzeCp7ImA9Wx5RE00.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-8025160944292746791</id><published>2010-08-20T12:21:00.002+02:00</published><updated>2010-08-20T13:44:03.680+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-08-20T13:44:03.680+02:00</app:edited><title>Mountain climbing as analogy to programming.</title><content type="html">I just had vacation and I do not know why, but suddenly I got to think that walking/climbing a mountain (as that is one of my favorite things to do at vacation) could be used as an analogy for programming and learning programming.&lt;br /&gt;
&lt;br /&gt;
As a starting point we have a geek that have no physical ability to talk about (because geeks seldom do sports or go outside the house).&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Start with easy tracks&lt;/h2&gt;To be able to climb a mountain you need to be fit. If you start with a very steep path without being fit, you will get exhausted or fall down and die.&lt;br /&gt;
&lt;br /&gt;
Also keep the distance short so you do not overuse your muscles. you may want to take a hike to morrow as well and with aching limbs it will not be that easy or fun. &lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;That is to programming: start learning an easy programming language, do a little bit of learning every day, and eventually you will get it. Doing to much makes your brain tired and you may be so bored with the stuff that you may quit and do something else.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Increase the difficulty gradually&lt;/h2&gt;Having started the tracking experience, it may be boring to do the same path every time and after a while your fitness will tend to stagnate and adapt to what you are doing. That is, walk longer paths, and also more steep paths, the body tends to adapt to be able to handle the demands, but not more than necessary. We need to increase the work load to be able to handle more and more.When we are fitter, we can handle more difficult paths. However, even if it is good to challenge yourself a bit, do not overdo it (you may fall down and die). When you climb the mountain higher you will get a more panoramic view, it is beautiful and you can also start to see the path you took, and maybe also see different paths you can take to get to the same place.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;That is to programming&lt;/i&gt;: &lt;i&gt;Do not just learn the basics, they are good to have but can not help you in the long run, specially not if you want to have it as a profession. do the basics, repeat them, but when you feel comfortable with them, increase the amount of code you write, and also the difficulty of the code: that is, start doing design work, object oriented programming etc. Knowing the basics may help you write simple routines, but climbing higher will give you a better view of the surroundings and starting to put things together or how to do different designs to get to the same result.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;Walk different mountains&lt;/h2&gt;No we may not have made it to the top of the first one yet, but does that really matter? Not really, the last part of getting to the top of a mountain may be easy, but it is usually not. Take Matterhorn, or Mount Everest for example. A lot of people climb them, but not all of them go for the top. you can go to the mountain and go as far as you have the time to do, or as high as you have the strength to do. this does not mean that you do not have a meaningful experience or doing a bad work. You doing as much as you can do or are willing to do, and even if its it not to the top, you may reach much higher than most people will do in their lifetime.&lt;br /&gt;
&lt;br /&gt;
If you think that you did a mountain as good as you can or that you did it long enough to get bored. Try another one, it will give you another view, different tracks. It may have paths that is easier then the first mountain, but also paths that is harder. Maybe it will get you higher quicker and let you see more, maybe the paths are harder but the view is more rewarding than the first one.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;That is to programming: Learn different programming languages, the first one you learn may not be a very good one to use professionally, but it may be a good language for learning basic programming. The new language may give you more opportunities to solve things quicker than the first one you learned. It may also be harder to do stuff in it, but when they are done, it may be better, quicker to do what you want it to do. for example Java may be easier to learn and use than C++ but performance wise, some C++ programs do better. On the other hand Java is more platform independent than C++, so it may be a better option for some companies.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Different language can solve things easier than in other languages, or have better performance. the choice of language depends of the application you want to make. Learn more than one to widen your options and you will be better at your work.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h2&gt;So when do I get to the top?&lt;/h2&gt;Ok, lets be realistic. You may never get to the top. It depends on the mountain you decide to climb. Take Mount everest for example, it is the highest mountain we have, only a few made it to the top, and even fewer without injuries.&lt;br /&gt;
&lt;br /&gt;
Take most mountains that is not a walk in the park, you may never reach the top, but you can get far enough to be satisfied with the experience and the rewards it give.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;For programmers: not all programmers learn absolutely everything about one single programming language. they leave the top part for experts, that are fit to do the advanced stuff.&amp;nbsp;&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;&lt;br /&gt;
&lt;/i&gt;&lt;br /&gt;
&lt;i&gt;Anyway programming is not really about doing the most advanced stuff or the most dangerous stuff. It is about doing stuff that other can follow. someone is coming after you and may have to maintain stuff you did. If it is to difficult they may not be able to go all the way, or understand how the heck you were able to get up to that cliff. programming is about leading the way for other to follow, you take a path that is fairly easy, sometimes not so easy but still manageable, you draw a map so that other can take the same path and continue from there.&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;So what are you waiting for, go out and climb a new mountain today!&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-8025160944292746791?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/XelaX1VGg_ai0JDfyMjbNy6JO6I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XelaX1VGg_ai0JDfyMjbNy6JO6I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/XelaX1VGg_ai0JDfyMjbNy6JO6I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/XelaX1VGg_ai0JDfyMjbNy6JO6I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/wgHsFtB5bxU" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/8025160944292746791?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/8025160944292746791?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/wgHsFtB5bxU/mountain-climbing-as-analogy-to.html" title="Mountain climbing as analogy to programming." /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/08/mountain-climbing-as-analogy-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYBQ3g6cSp7ImA9WhdREUw.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-4430501402788374362</id><published>2010-05-04T19:05:00.002+02:00</published><updated>2011-07-31T14:42:32.619+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-31T14:42:32.619+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Glassfish v3" /><title>Struts2 on Glassfish v3</title><content type="html">OMG. It was not that trivial to get it going, at least not using Netbeans.&lt;br /&gt;
&lt;br /&gt;
First trying to use maven since I need to have some EJBs, but I gave up after not being able to get the archetype for Struts2 do any project for me, first complaining about no commons-collections-3.2.1, downloading it and got a little bit longer, but then could not find anything for struts2.1.6.&lt;br /&gt;
&lt;br /&gt;
So I went on creating a Struts2 project and got it to work by pointing out the jar files needed in Netbeans JavaEE webproject for Glassfish, YES there is a lot of jarfiles depending on each other since I also want to use TILES. Anyway, when I wanted the use the EJBs I had to create a EAR-project to hold everything together, what happens then?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Struts2 stops working&lt;/b&gt;, complaining of a lot of classes not loaded or cannot find. I spent almost all the afternoon before finally finding the problem.&lt;br /&gt;
&lt;br /&gt;
Netbeans ant-script wants to copy every f...ing jar file that is not a TLD to the lib of the EAR, NOT into the Web applications WEB-INF/lib, which then makes sence for the errors in the log: when loading from that EAR/lib folder, it can not find struts-core classes since that jar file is in the Web-app because it includes TLDs,&lt;br /&gt;
&lt;br /&gt;
So finally solved, hack the build-impl.xml and copy &lt;b&gt;ALL &lt;/b&gt;jar files to the web-app so it works.Why Netbeans do this way I do not know, maybe someone can inform me?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-4430501402788374362?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4pk00IdtrUFH8nWzg0Kmcsb-72Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4pk00IdtrUFH8nWzg0Kmcsb-72Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4pk00IdtrUFH8nWzg0Kmcsb-72Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4pk00IdtrUFH8nWzg0Kmcsb-72Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/U661w4AKUyQ" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/4430501402788374362?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/4430501402788374362?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/U661w4AKUyQ/struts2-on-glassfish-v3.html" title="Struts2 on Glassfish v3" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/05/struts2-on-glassfish-v3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0EDSX0_fyp7ImA9WxBaFkw.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-270973587530818781</id><published>2010-03-26T16:01:00.000+01:00</published><updated>2010-03-26T16:01:18.347+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-26T16:01:18.347+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="tutorial" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>Tutorials</title><content type="html">I have started a Java tutorial for starting up with Java. It can be found at my Google-site. The tutorial is basic Java.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://sites.google.com/site/ironicprogrammer/tutorials/ironic-programmer-java-tutorial"&gt;http://sites.google.com/site/ironicprogrammer/tutorials/ironic-programmer-java-tutorial&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
I will continuosly add more stuff to the site. Even if the first lessons may be a bit text to read. I will try to do programming by examples.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-270973587530818781?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/xPxXEBe3LUN1aTtWF46wbS8okYE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xPxXEBe3LUN1aTtWF46wbS8okYE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/xPxXEBe3LUN1aTtWF46wbS8okYE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/xPxXEBe3LUN1aTtWF46wbS8okYE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/lYOv6ux_rD0" height="1" width="1"/&gt;</content><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/270973587530818781?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/270973587530818781?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/lYOv6ux_rD0/tutorials.html" title="Tutorials" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/03/tutorials.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0ADQX08fSp7ImA9WxBUGUo.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-3989786309363551166</id><published>2010-03-07T16:04:00.003+01:00</published><updated>2010-03-07T16:29:30.375+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-07T16:29:30.375+01:00</app:edited><title>Scary, but maybe true</title><content type="html">A week ago I read the blogpost "&lt;a href="http://www.codinghorror.com/blog/2010/02/the-nonprogramming-programmer.html"&gt;The Non-Programming Programmer&lt;/a&gt;" by Jeff Atwood at Coding horror about that many applicants for programming work can not program at all.&lt;br /&gt;
&lt;br /&gt;
During my 10 years in the business I experienced a few myself. I wish I then had the examples to give and exclude some that can not program.&lt;br /&gt;
&lt;br /&gt;
Some examples:&lt;br /&gt;
&lt;h4&gt;What you can see as a teacher&lt;/h4&gt;At my first job as a teacher at university, part of my job was to lecture Pascal and C. Included was laborations in the language learned. Here I could see some serious laziness or lack of logic. One of the first tasks used to be to print out a matrix of the numbers 1..100 in a table of size 10x10. Of course this task is to learn some basic structures in the language learned like iterations as for-loop and conditionals like if. This should be a fairly simple task, but one guy actually just did a&lt;br /&gt;
&lt;pre class="codebox-content"&gt;writeln("1 2 3 4 5 6 7 8 9 10");
writeln("11 12 13 14 15 16 17 18 19 20");
&lt;/pre&gt;and so on.&lt;br /&gt;
Another example is the laziness of students. Some did not want to put in some work to learn the language, they just wanted to pass the class. There is different ways to do this. One is to try to get a group to add your name to the report, another way is to copy someone elses work. Now the stupid part of this, a teacher notice more than you may think. Someone that is not able to manage simple task and then all of a sudden turn up with a solution that looks pretty similar to the brightest student or that of someone that the teacher helped and put in some pattern of text that was put there as an example but is failry recognizable since it was a value you do not just put in.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;About applying programmers&lt;/h4&gt;Here we have programmers(? or not) that actually have a bigger mouth then ability to program. They do good at an interview and get the job. Later it turn out.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;that they do not at all know anything about Java, and the excuse is that they hoped that they soon shuould catch up and learn.&lt;/li&gt;
&lt;li&gt;the one I heard from the boss that applied (this was early 2000 when the market was hot). That they heard that the company was looking and maybe this person could be a candidate because he did som Basic 10 years ago and was thinking maybe he could do some programming.&lt;/li&gt;
&lt;li&gt;I have noticed, at an interview, think really carefull when people start to talk about project they have been in and saying: "In this project WE used this and that language/database." or "The application I worked is build in this and that technique and this database". The problem with this is that some uses the knowledge of others in their CV, just because they were in a project that had a very nice arhitechture does not mean they did anything of it.&lt;/li&gt;
&lt;/ul&gt;&lt;h4&gt;Programmers at work&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;One consultant I met at a task where we came in to look at why the clients web build in asp was so slow. Well the consultant they had there that have been working on the web did not know any SQL so in 4 different frames in a framset did a full lookup on the resultset of a table looped until the key was found and then used anothere key to do the same on another table.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;A programmer I worked with got the task to do some of the work when we were dividing an existing Session EJB into 3-4 smaller ones and at the same time change the medthods to return Collection of objects instead of a ResultSet. He actually did that, that is moved the code from the old one to the new one. What he missed was that the work is actually not finishe just because you move code, there is actually placed were you use the code, like a client that ALSO needs to be changed if you change the contract and move the methods it need. WHOOPS!&lt;/li&gt;
&lt;li&gt;Another man I worked with said he knew Java EE after he went throug a book, downloading the examples and compiled and run throug them once. But, when he did some actual coding, he did not reuse the methods I already had done, he made new ones with other names.&lt;/li&gt;
&lt;li&gt;Some programers seam to have the philosophy. "&lt;i&gt;If it compiles and run it must be right&lt;/i&gt;". NO it is not. Making the compiler happy is easy, making the application work is not the same as keeping the compiler happy, it is logic not syntax.&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;So that post was very welcomed and will be used in the future.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;While we are at it, try to solve&amp;nbsp;this one with recursion&lt;/b&gt;:&lt;/div&gt;&lt;div&gt;You have a class called Org to represent a organization. The root organization is the starting point and every instance of the class holds its own suborganizations and a reference to its parent.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/_Lg4o4B3FUAg/S5O_XgbwTgI/AAAAAAAABcI/RXLTziV2nAs/s1600-h/org_class.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="200" src="http://4.bp.blogspot.com/_Lg4o4B3FUAg/S5O_XgbwTgI/AAAAAAAABcI/RXLTziV2nAs/s200/org_class.png" width="198" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;pre class="codebox-header"&gt;Org.java
&lt;/pre&gt;&lt;pre class="codebox-content"&gt;public class Org{
    private long id;
    private Org parent;
    private List&lt;org&gt; children;
    private String name;

    public Org(long id, String name){
     ...
    }

    // getters / setters
    ....
}
&lt;/org&gt;&lt;/pre&gt;&lt;div&gt;Your task is to print the organization tree starting from the root.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;b&gt;&lt;span class="Apple-style-span" style="font-size: x-large;"&gt;Please share your own stories in the comments&lt;/span&gt;&lt;/b&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-3989786309363551166?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9cJhJaPLfpprifLVHcKtqJcFm3k/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9cJhJaPLfpprifLVHcKtqJcFm3k/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9cJhJaPLfpprifLVHcKtqJcFm3k/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9cJhJaPLfpprifLVHcKtqJcFm3k/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/6VFMw_H2ymg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/3989786309363551166/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2010/03/scary-but-maybe-true.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3989786309363551166?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3989786309363551166?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/6VFMw_H2ymg/scary-but-maybe-true.html" title="Scary, but maybe true" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_Lg4o4B3FUAg/S5O_XgbwTgI/AAAAAAAABcI/RXLTziV2nAs/s72-c/org_class.png" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/03/scary-but-maybe-true.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYMR30yeip7ImA9WxBaFUQ.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-7881005513157649045</id><published>2010-03-05T18:48:00.008+01:00</published><updated>2010-03-26T10:36:26.392+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-26T10:36:26.392+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="JSF2" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>File upload in JSF2 using base class</title><content type="html">Surfing the net for JSF2 resources can be a tedious task. Actually for file upload I actually found a couple of blogs describing how to do it the firs I found actually only (&lt;a href="http://dominikdorn.com/?p=169"&gt;FileUpload with JSF 2 and Servlet 3.0&lt;/a&gt;) made a taglib based on the second one. And it includes maven which I do not use.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;I had to remake the example a bit since I got question to provide a sample application. When extracting the code I noticed it did not work anymore. The reason is probably the one mentioned at this blog: &lt;a href="http://mcatr.blogspot.com/2010/01/multipart-requests-and-jsf.html"&gt;http://mcatr.blogspot.com/2010/01/multipart-requests-and-jsf.html&lt;/a&gt;&lt;/b&gt;.To make it work again, I did a solution that I am not happy with but it works. It takes the idea from this blog, joined with some from the other blogs mentioned. The former blog states that a multipart request to JSF 2 do not work, because it can not extract parameters from that kind of request.&lt;br /&gt;
&lt;br /&gt;
The problem is that for what I actually wanted it was not right and one was a little bit overkill. So as usual I took another approach. I got some helf from&amp;nbsp;&lt;a href="http://balusc.blogspot.com/2009/12/uploading-files-with-jsf-20-and-servlet.html"&gt;Uploading files with JSF 2.0 and Servlet 3.0&lt;/a&gt;&amp;nbsp;but that solution includes making a custom component and renderer, and is dependant on classes found in&amp;nbsp;&lt;a href="http://balusc.blogspot.com/2009/12/uploading-files-in-servlet-30.html"&gt;Uploading files in Servlet 3.0&lt;/a&gt;. I actually copied all the code and tried it...but I got a failure when loading the page. So I wanted something with less code. and simpler to use, and I have a case when I am going to store vCards in a database so a solution to save the file on disk means I have to modify code anyway.&lt;br /&gt;
&lt;br /&gt;
I took the following approach:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Use standard html &lt;span class="Apple-style-span" style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;lt;input type="file" /&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Use @ManagedBean&lt;/li&gt;
&lt;li&gt;I first had a base class to handle the file and extended the Managed bean with this one, but since this stopped working I had to add:&lt;/li&gt;

&lt;ul&gt;&lt;li&gt;A Wrapper to wrap multipart requests&lt;/li&gt;
&lt;li&gt;A Filter to catch multipart request and transform them to the Wrapper type.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;I also have a FacesUtil class to simplify getting request and parameters&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;Here is the code:&lt;/div&gt;&lt;h3&gt;In the view (vCardForm.xhtml)&lt;/h3&gt;&lt;pre class="codebox-content"&gt;&amp;lt;h:form enctype="multipart/form-data"&amp;gt;
 &amp;lt;input type="file" name="vcardFile"/&amp;gt;
 &amp;lt;h:commandButton action="#{vcardBean.uploadVCard}" value="Upload"/&amp;gt;
&amp;lt;/h:form&amp;gt;
&lt;/pre&gt;&lt;h3&gt;Class to represent the uploaded file (UploadFile.java)&lt;/h3&gt;I created a class to store some information about the file being upload, I can later choose if I want to store to disk or in a database, for example sending this to an EJB and the EJB descides what to do.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;package theironicprogrammer.model;

public class UploadFile {
 private String fileName;
 private String contentType;
 privte byte[] fileData;
 
 public UploadFile(String fileName, String contentType, byte[] fileData){
  this.fileName = fileName;
  this.contentType = contentType;
  this.fileData = fileData;
 }
 
 public String getFileName(){
  return fileName;
 }
 public String getContentType(){
  return contentType;
 }
 public byte[] getFileData(){
  return fileData;
 }
 
 public void setFileName(String fileName){
  this.fileName = fileName;
 }
 public void getContentType(String contentType){
  this.contentType = contentType;
 }
 public void setFileData(byte[] fileData){
  this.fileData = fileData;
 }
}
&lt;/pre&gt;&lt;h3&gt;The managed bean (VCardBean.java)&lt;/h3&gt;The interesting thing going on here is that i initialize the base class with the request for use of the request. Probably I could have done this in the constructor of the baseclass, but something you have to have to refactor.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;package theironicprogrammer.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.servlet.ServletOutputStream;
import theironicprogrammer.session.PersonUpdaterLocal;
import theironicprogrammer.http.MultiPart;

/**
 *
 * @author theironicprogrammer@gmail.com
 */
@ManagedBean(name="vcardBean")
@RequestScoped
public class VCardBean implements Serializable{
    @EJB PersonBean personBean;

    public String uploadVCard(){
        System.out.println("uploadVCard...");
        HttpServletRequest req = FacesUtil.getRequest();
        if(req instanceof MultipartRequestWrapper){
            MultipartRequestWrapper multi = (MultipartRequestWrapper)req;

            UploadFile uf = multi.findFile("vcardFile");
            if(uf != null){
                String fileExt = uf.getFileName().substring(uf.getFileName().lastIndexOf("."));
                System.out.println("Extension" + fileExt);
                // should actually check the extension, but no big deal for example
                personBean.saveVCard(uf, "testuser");

                FacesUtil.getFlash().put("messageCode", "vcard.upload.ok");
                FacesUtil.getFlash().put("fileName", uf.getFileName());
            }

        }
        return "vCardForm";
    }

    public String findVCard(){
        UploadFile uf = personBean.findVCard("testuser");
        if(uf == null) {
            FacesUtil.getFlash().put("messageCode", "vcard.upload.err.load");
            FacesUtil.getFlash().put("messageType", "errors");

        } else {
            ServletOutputStream output = null;
            try {
                output = FacesUtil.getResponse().getOutputStream();
                FacesUtil.getResponse().setHeader("Content-disposition", "attachment; filename=" + uf.getFileName());
                FacesUtil.getResponse().setContentType(uf.getContentType());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                baos.write(uf.getFileData());
                FacesUtil.getResponse().setCharacterEncoding("UTF-8");
                FacesUtil.getResponse().setContentLength(baos.size());
                baos.writeTo(output);
                output.flush();
            } catch (IOException ex) {
                FacesUtil.getFlash().put("messageCode", "vcard.upload.err.load");
                FacesUtil.getFlash().put("messageType", "errors");
                Logger.getLogger(VCardBean.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    output.close();
                } catch (IOException ex) {
                    FacesUtil.getFlash().put("messageCode", "vcard.upload.err.load");
                    FacesUtil.getFlash().put("messageType", "errors");
                    Logger.getLogger(VCardBean.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return "vCardForm";
    }

    public String removeVCard(){
        personBean.removeVCard("testuser");
        FacesUtil.getFlash().put("messageCode", "vcard.remove.ok");
        FacesUtil.getFlash().put("messageType", "messages");
        return "vCardForm";
    }

}

&lt;/pre&gt;&lt;br /&gt;
&lt;h3&gt;MultipartRequestWrapper&lt;/h3&gt;&lt;pre class="codebox-content"&gt;package ironicprogrammer.http;

import ironicprogrammer.model.UploadFile;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;

/**
 *
 * @author theironicprogrammer@gmail.com
 */
public class MultipartRequestWrapper extends HttpServletRequestWrapper {
    private static final String CONTENT_DISPOSITION = "content-disposition";
    private static final String CONTENT_DISPOSITION_FILENAME = "filename";

    private Hashtable&lt;string, string[]=""&gt; params = new Hashtable&lt;string, string[]=""&gt;();

    public MultipartRequestWrapper(HttpServletRequest request) {
        super(request);
        System.out.println("Created multipart wrapper....");
        try {
            System.out.println("Looping parts");            
            for (Part p : request.getParts()) {
                System.out.println(String.format("Part name: %1$s, contentType : %2$s", p.getName(), p.getContentType()));
                for(String header : p.getHeaderNames()){
                    System.out.println("Header name : " + header + ", value : " + p.getHeader(header));
                }
                byte[] b = new byte[(int) p.getSize()];
                p.getInputStream().read(b);
                params.put(p.getName(), new String[]{new String(b)});
            }
        } catch (IOException ex) {
            Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ServletException ex) {
            Logger.getLogger(MultipartRequestWrapper.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public UploadFile findFile(String attrName){
        UploadFile uf = null;
        try {

            Part p = findPart(attrName);
            if(p != null){
                String fileName = getFilename(p);
                System.out.println("Filename : " + fileName + ", contentType " + p.getContentType());
                byte[] b = new byte[(int) p.getSize()];
                p.getInputStream().read(b);
                params.put(p.getName(), new String[]{new String(b)});
                uf = new UploadFile(fileName, p.getContentType(), b);
            }

        } catch (IOException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uf;
    }

    private Part findPart(String name){
        HttpServletRequest request = (HttpServletRequest)getRequest();
        Part p = null;
        try {
            for (Part part : request.getParts()) {
                if(part.getName().equals(name)){
                    p = part;
                    break;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ServletException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        }

        return p;
    }

    private String getFilename(Part part) {
        for (String cd : part.getHeader(CONTENT_DISPOSITION).split(";")) {
            if (cd.trim().startsWith(CONTENT_DISPOSITION_FILENAME)) {
                return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;
    }

    @Override
    public String getParameter(String name) {
      String [] values = getParameterValues(name);
      if(values == null || values.length == 0) {
        return null;
      }

      return values[0];
    }

    @Override
    public Map&lt;string, string[]=""&gt; getParameterMap() {
      return params;
    }

    @Override
    public Enumeration&lt;string&gt; getParameterNames() {
      return params.keys();
    }

    @Override
    public String[] getParameterValues(String name) {
      return params.get(name);
    }

}

&lt;/string&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;h3&gt;MultipartRequestFilter&lt;/h3&gt;&lt;pre class="codebox-content"&gt;package ironicprogrammer.http;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author theironicprogrammer@gmail.com
 */
@WebFilter(urlPatterns="/*")
public class MultipartRequestFilter implements  Filter{
    private static final String REQUEST_METHOD_POST = "POST";
    private static final String CONTENT_TYPE_MULTIPART = "multipart/";


    public void init(FilterConfig filterConfig) throws ServletException {
        
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        if(isMultipartRequest(httpRequest)){
            System.out.println("Is multipart request.... wrapping it.");
            request = new MultipartRequestWrapper(httpRequest);
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        
    }

    public static final boolean isMultipartRequest(HttpServletRequest request) {
        return REQUEST_METHOD_POST.equalsIgnoreCase(request.getMethod())
            &amp;amp;&amp;amp; request.getContentType() != null
            &amp;amp;&amp;amp; request.getContentType().toLowerCase().startsWith(CONTENT_TYPE_MULTIPART);
    }

}

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Session bean for handling the storage.&lt;/h3&gt;For the example this only stores it in the session.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;package ironicprogrammer.session;

import ironicprogrammer.model.UploadFile;
import ironicprogrammer.utils.FacesUtil;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateless;

/**
 *
* @author theironicprogrammer@gmail.com
 */
@Stateless
public class PersonBean {

    /**
     * Saves a vcard for the person with supplied username
     */
    public void saveVCard(UploadFile file, String username){
        // Make use of session for example. No need to complicate with connection to filesystem or database
        System.out.println("Saving vcard for " + username);
        getVcards().put(username, file);
    }


    public UploadFile findVCard(String username){
        System.out.println("Getting vcard for" + username);
        return getVcards().get(username);
    }

    public void removeVCard(String username){
        System.out.println("Removing vcard for" + username);
        getVcards().remove(username);
    }

    private Map&lt;string, uploadfile=""&gt; getVcards(){
        Map&lt;string, uploadfile=""&gt; vcards = (Map&lt;string, uploadfile=""&gt;)FacesUtil.getRequest().getSession().getAttribute("vcards");
        if(vcards == null){
            vcards = new HashMap&lt;string, uploadfile=""&gt;();
            FacesUtil.getRequest().getSession().setAttribute("vcards", vcards);
        }
        return vcards;
    }
 
}

&lt;/string,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/string,&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;h3&gt;Finally the class to inherit to make it all work&lt;/h3&gt;This is now obsolete, but I keep it in case JSF will handle the mutlipart form in the future. But even then it may not be needed.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;package theironicprogrammer.http;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import theironicprogrammer.model.UploadFile;

/**
 *
 * @author theironicprogrammer@gmain.com
 */
public class MultiPart {
    private static final String CONTENT_DISPOSITION = "content-disposition";
    private static final String CONTENT_DISPOSITION_FILENAME = "filename";
   
    private HttpServletRequest request;
    
    protected MultiPart(){
        
    }

    public UploadFile findFile(String attrName){
        UploadFile uf = null;
        try {
            
            Part p = findPart(attrName);
            if(p != null){
                String fileName = getFilename(p);
                System.out.println("Filename : " + fileName + ", contentType " + p.getContentType());
                byte[] b = new byte[(int) p.getSize()];
                p.getInputStream().read(b);
                uf = new UploadFile(fileName, p.getContentType(), b);
            }
            
        } catch (IOException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        }
        return uf;
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    private Part findPart(String name){
        Part p = null;
        try {
            for (Part part : request.getParts()) {
                if(part.getName().equals(name)){
                    p = part;
                    break;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ServletException ex) {
            Logger.getLogger(MultiPart.class.getName()).log(Level.SEVERE, null, ex);
        }

        return p;
    }

    private String getFilename(Part part) {
        for (String cd : part.getHeader(CONTENT_DISPOSITION).split(";")) {
            if (cd.trim().startsWith(CONTENT_DISPOSITION_FILENAME)) {
                return cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;
    }
    
}

&lt;/pre&gt;&lt;br /&gt;
The only thing needed in the Managed bean is to call findFile("vcardFile") on the multipart request to get the file that is being uploaded and take action on that.&lt;br /&gt;
&lt;br /&gt;
A sample project (for netbeans) can be found here:&amp;nbsp;&lt;a href="http://sites.google.com/site/ironicprogrammer/home/jsf-file-upload-example"&gt;http://sites.google.com/site/ironicprogrammer/home/jsf-file-upload-example&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-7881005513157649045?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/f5-dkafv_rJIAh36WeULsXPdRYc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/f5-dkafv_rJIAh36WeULsXPdRYc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/f5-dkafv_rJIAh36WeULsXPdRYc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/f5-dkafv_rJIAh36WeULsXPdRYc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/27KTtaBXcnk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/7881005513157649045/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html#comment-form" title="25 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7881005513157649045?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7881005513157649045?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/27KTtaBXcnk/file-upload-in-jsf2.html" title="File upload in JSF2 using base class" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>25</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkIHQHs8eip7ImA9WxBbEk4.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-3891457870232644622</id><published>2010-01-10T18:45:00.000+01:00</published><updated>2010-03-10T17:28:51.572+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-03-10T17:28:51.572+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="JSF2" /><category scheme="http://www.blogger.com/atom/ns#" term="Glassfish v3" /><category scheme="http://www.blogger.com/atom/ns#" term="Netbeans 6.8" /><category scheme="http://www.blogger.com/atom/ns#" term="JSF" /><category scheme="http://www.blogger.com/atom/ns#" term="database" /><title>Netbeans tutorial "Generating a JavaServer Faces 2.0 CRUD Application from a Database"</title><content type="html">I went through&lt;a href="http://netbeans.org/kb/docs/web/jsf20-crud.html"&gt; this tutorial&lt;/a&gt; today. Nice example, but there was something I did not like. As a geek I sit on a sunday and try out a tutorial on my home computer. Lazy as I am I do not want to start my MySql instance, since adding more running components tend to slow down my almost four year old Toshiba laptop.&lt;br /&gt;
&lt;br /&gt;
Since I was going to run Glassfish v3 anyway I was thinking a better idea is to have the database in the form of JavaDB, so I tweaked the script to fit into Derby instead, not much tweaking to do actually and doing it made me happy to not be forced to start MySql (My mysql is by the way a bit old and installed the same time as I installed InstantRails long time ago, and probably needs to be updated). The insert script in the tutorial work more or less as it is in JavaDB just remove the 3 SET-clauses in the top and the COMMIT in the end and it should work.&lt;br /&gt;
&lt;br /&gt;
The script can be found at my google-site &lt;a href="http://sites.google.com/site/ironicprogrammer/home/jsf-2-0-tutorial-db-script"&gt;http://sites.google.com/site/ironicprogrammer/home/jsf-2-0-tutorial-db-script&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-3891457870232644622?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/YunnQLfq-p4g212E-f2mDLT2jEY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YunnQLfq-p4g212E-f2mDLT2jEY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/YunnQLfq-p4g212E-f2mDLT2jEY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/YunnQLfq-p4g212E-f2mDLT2jEY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/Rpb9vyGY5L0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/3891457870232644622/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2010/01/netbeans-tutorial-generating-javaserver.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3891457870232644622?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3891457870232644622?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/Rpb9vyGY5L0/netbeans-tutorial-generating-javaserver.html" title="Netbeans tutorial &quot;Generating a JavaServer Faces 2.0 CRUD Application from a Database&quot;" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2010/01/netbeans-tutorial-generating-javaserver.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUEAQ3oycSp7ImA9WxNaFks.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-7319903319172769083</id><published>2009-11-30T10:37:00.000+01:00</published><updated>2009-12-01T12:40:42.499+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-12-01T12:40:42.499+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><title>Quote to think about</title><content type="html">Started to read the book "Refactoring. Improving the Design of Existing Code" by Martin Fowler the other day. Here is a quotation from that book that is what programming is about:&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;"Any fool can write code that a computer can understand. Good programmers write code that humans can understand"&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
So tru, so tru...&lt;i&gt; &lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-7319903319172769083?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/5z1SVdCua79x4IlxL3StIYUe6U0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5z1SVdCua79x4IlxL3StIYUe6U0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/5z1SVdCua79x4IlxL3StIYUe6U0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5z1SVdCua79x4IlxL3StIYUe6U0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/W_L0jDDfq8g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/7319903319172769083/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/11/quote-to-think-about.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7319903319172769083?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/7319903319172769083?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/W_L0jDDfq8g/quote-to-think-about.html" title="Quote to think about" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/11/quote-to-think-about.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEEGQHc8fSp7ImA9WxNWGUs.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-1396545803917541554</id><published>2009-10-19T15:56:00.002+02:00</published><updated>2009-10-19T16:30:21.975+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-19T16:30:21.975+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Ajax" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Simple Ajax sample</title><content type="html">Using Ajax in Grails is quite simple. Grails have the prototype library making it easy to use Ajax in Grails.&lt;br /&gt;
&lt;br /&gt;
You can try it out by creating a Grails-project. Here is an example ant some explanation of what is nescessary to think of.&lt;br /&gt;
&lt;br /&gt;
1. Create a grails project: &lt;br /&gt;
&lt;pre class="codebox-content"&gt;grails create-app ajaxsample
&lt;/pre&gt;2. Create a controller:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;grails create-controller ajax
&lt;/pre&gt;&lt;br /&gt;
3. Create 2 .gsp-files in the /views/ajax folder; index.gsp and _itemList.gsp (_ means that it is a template)&lt;br /&gt;
&lt;br /&gt;
4. Change the /views/layout/main.gsp by adding the following row&lt;br /&gt;
&lt;pre class="codebox-content"&gt;&amp;lt;g:javascript library="prototype" /&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
5. In the index.gsp in your ajax folder, put in this code:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&amp;gt;
    &amp;lt;meta name="layout" content="main"/&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Ajax samples&amp;lt;/h1&amp;gt;

    &amp;lt;h2&amp;gt;Adding item to list:&amp;lt;/h2&amp;gt;
    &amp;lt;i&amp;gt;This example takes a string in a textfield and add it to a list (saved in the session) generated with a template&amp;lt;/i&amp;gt;
    &amp;lt;g:form name="listForm"&amp;gt;
    Item : &amp;lt;g:textField name="anItem" value=""/&amp;gt;
    &amp;lt;g:submitToRemote action="addItem" update="listItems" value="Add"/&amp;gt;&amp;lt;br/&amp;gt;
    &amp;lt;/g:form&amp;gt;

    &amp;lt;div id="listItems"&amp;gt;
    &amp;lt;/div&amp;gt;
    
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/pre&gt;&lt;br /&gt;
6. In your _itemList.gsp put this code:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;&amp;lt;table&amp;gt;
  &amp;lt;g:each in="${listitems}" var="item" status="i"&amp;gt;
  &amp;lt;tr class="${i%2==0?"even":"odd"}"&amp;gt;
    &amp;lt;td&amp;gt;${item}&amp;lt;/td&amp;gt;
    &amp;lt;td&amp;gt;&amp;lt;g:remoteLink action="deleteItem" params="[index:i]" update="listItems"&amp;gt;Remove&amp;lt;/g:remoteLink&amp;gt;&amp;lt;/td&amp;gt;
  &amp;lt;/tr&amp;gt;
  &amp;lt;/g:each&amp;gt;
&amp;lt;/table&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
7. Now the only thing we need is some code in the controller. Open the AjaxController and put this code:&lt;br /&gt;
&lt;pre class="codebox-content"&gt;class AjaxController {

    def index = { }

    def addItem = {
        if(session["listitems"] == null){
            // create new list if empty
            session["listitems"] = []
        }

        session["listitems"] &amp;lt;&amp;lt; params["anItem"]

        // render partial with the updated list
        String content = g.render(template:'itemList',model:[listitems:session["listitems"]])
        render content
    }

    def deleteItem = {
        def idx = params["index"].toInteger()
        if(session["listitems"] != null){
            // remove item at index
            session["listitems"].remove(idx)
        }

        // render partial with the updated list
        String content = g.render(template:'itemList',model:[listitems:session["listitems"]])
        render content
    }
}

&lt;/pre&gt;&lt;h2&gt;So what makes this work?&lt;/h2&gt;First of all, Grails supply the tags &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;submitToRemote&lt;/span&gt; which is used for the "Add" button and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;remoteLink&lt;/span&gt; which is used for the "remove" link. These are used to use Ajax from grails and the only thing needed to make them work is to include the "prototype" javascript library, which we did in the layout to make it available on all pages.  If you look on these tags you see the property &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;action=""&lt;/span&gt; and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;update="listItems"&lt;/span&gt; this properties tells the framework what action to call and what to do with the result when the action is finished.  Look at the index.gsp and you will see a &amp;lt;div&amp;gt; with the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;id="listItems"&lt;/span&gt;, if you remember this is the same id that you saw as text in the update-property, this i the glue between the action pointed out and where to put the result.  Pretty easy and straightforward, to make it short you need this &lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Add &lt;b&gt;prototype&lt;/b&gt; JavaScript library to the layout&lt;/li&gt;
&lt;li&gt;Add a DIV for the result&lt;/li&gt;
&lt;li&gt;Add some remote call and point it to the action you want to happen and where to put the result&lt;/li&gt;
&lt;/ol&gt;&lt;i&gt; &lt;b&gt;Note :&lt;/b&gt; Ajax is used for enhanced functionality and to not to have to reload pages. Therefor using templates with small fragments of code is a convenient way to reuse code-blocks and functionality. It is better to think a bit ahead of what is needed to make nice solutions and reuse of code. &lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-1396545803917541554?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/1Q3Shx9cjnapfCUw78gMGd1vjhE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1Q3Shx9cjnapfCUw78gMGd1vjhE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/1Q3Shx9cjnapfCUw78gMGd1vjhE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1Q3Shx9cjnapfCUw78gMGd1vjhE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/0cKn0Bu5Bjc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/1396545803917541554/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/10/simple-ajax-sample.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1396545803917541554?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1396545803917541554?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/0cKn0Bu5Bjc/simple-ajax-sample.html" title="Simple Ajax sample" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/10/simple-ajax-sample.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0cCR3c9fyp7ImA9WxNXGUw.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-3799651277049631245</id><published>2009-10-07T13:29:00.001+02:00</published><updated>2009-10-07T13:31:06.967+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-07T13:31:06.967+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="SSL" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Making SSL calls with Grails</title><content type="html">All of a sudden I realised that I have to call a service I have behind a SSL connection.&lt;br /&gt;
&lt;br /&gt;
Using grails, this is easy to set up. Just create a couple of Groovy-classes and use it from the controller. It may be nescessary though to add the certificat of the SSL site to your JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Adding the certificate&lt;/b&gt; &lt;br /&gt;
&lt;ul&gt;&lt;li&gt;download&amp;nbsp; the certificate you need, name it to something usefull, for example theSite.cer.&lt;/li&gt;
&lt;li&gt;use keytool to add it. (needs JAVA_HOME/bin in the path)&lt;br /&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: x-small;"&gt;keytool -import -keystore /pathToJava/jre/lib/security/cacerts -file theSite.cer&amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;b&gt;Class to make the call&lt;/b&gt;&lt;br /&gt;
this is the class to actually do the work, calling the SSL url you want to reach.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.SSLSocket
import java.security.*;

/**
 * @author theironicprogrammer@gmail.com
 */
class SSLConnector {
    String theurl
    String user
    String password
    public SSLConnector(url, user, pwd){
        this.theurl = url
        this.user = user
        this.password = pwd
    }

    public String responseString(){
        // Dynamic registration of JSSE provider
        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        // Need to be set
     System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");

        // Install Authenticator
        Authenticator.setDefault (new SSLAuthenticator(this.user,this.password));


        URL url = new URL(this.theurl);
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

        String line;

        StringBuffer buff = new StringBuffer()
        while ((line = br.readLine()) != null){
            buff.append(line)
        }

        br.close();

        return buff.toString()
    }    

}
&lt;/pre&gt;&lt;b&gt;Class for password authentication&lt;/b&gt;&lt;br /&gt;
this class is needed to authenticate you at the remote site.&lt;br /&gt;
&lt;pre class="codebox-content"&gt;/**
 * @author theironicprogrammer@gmail.com
 */
class SSLAuthenticator extends Authenticator{
    String user
    String password
    public SSLAuthenticator(un, pw){
        this.user = un
        this.password = pw
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
 
}
&lt;/pre&gt;&lt;br /&gt;
In the controller you can then use the class to get a responsestring from the remote server.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="codebox-content"&gt;// suggest to put these in config.groovy to get them from there, but not nescessary
def url = "http://pathToSite/.../thePage?id=x" // or grailsApplication.config.sslConnection.url,
def username = "theUsername" // or grailsApplication.config.sslConnection.username,
def password = "secret" // or grailsApplication.config.sslConnection.pwd,

// Create the connector
def ssl = new SSLConnector(url, username, password)
def responsString = ssl.responseString()

// do something...
&lt;/pre&gt;&lt;br /&gt;
/Peter&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-3799651277049631245?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/MEg494Rfjm4O09PFVLX9DWQrxgY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MEg494Rfjm4O09PFVLX9DWQrxgY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/MEg494Rfjm4O09PFVLX9DWQrxgY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/MEg494Rfjm4O09PFVLX9DWQrxgY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/J3xXq_E4bQ4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/3799651277049631245/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/10/making-ssl-calls-with-grails.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3799651277049631245?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3799651277049631245?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/J3xXq_E4bQ4/making-ssl-calls-with-grails.html" title="Making SSL calls with Grails" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>4</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/10/making-ssl-calls-with-grails.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMMSH4_fCp7ImA9WxNXFUQ.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-1948672738245686789</id><published>2009-10-03T19:21:00.000+02:00</published><updated>2009-10-03T19:21:29.044+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-03T19:21:29.044+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="EJB" /><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="glassfish" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Grails and EJB</title><content type="html">Read Jason Rudolphs article about grails and EJB a while ago.&amp;nbsp;&lt;a href="http://www.infoq.com/articles/grails-ejb-tutorial"&gt;Grails + EJB Domain Models Step-by-Step&lt;/a&gt;. It is a nice article but not what I want.&lt;br /&gt;
&lt;br /&gt;
However it seamed a little bit of inventing the wheel again to import the classes into Grails in the same package structure, and I also wanted some other things that EJB3 gave me.&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;I want the EJB level to handle application rules. In that way, I can reuse the EJBs from other places as well&lt;/li&gt;
&lt;li&gt;I have information i two different schemas in Oracle and grails was not supporting more than one.&lt;/li&gt;
&lt;li&gt;I like "hit and run" session beans to provide services. which means that I use interface in the sessionbeans to provide CRUD and listing methods.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
So I started to elaborate with putting the EJB.jar file in Grails library folder to try it out. It worked out nice, the only thing I have to do is to put some lookup code and start using the session beans. I also get access to all model POJOs.&lt;br /&gt;
&lt;br /&gt;
I like this approach because I can use grails rapid application development with the robustness of EJB3 without doing very much extra.&lt;br /&gt;
&lt;br /&gt;
The downside is that as I am a little bit lazy and avoid writing too much code, is that I have to run it on the same glassfish and I have to copy the jar-file every time I change the interface. I also have to make a war of the grails application and run it on glassfish.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;/Peter&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-1948672738245686789?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/24cIvpJsFKp_I8a6mvTp9FYGwRQ/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/24cIvpJsFKp_I8a6mvTp9FYGwRQ/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/24cIvpJsFKp_I8a6mvTp9FYGwRQ/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/24cIvpJsFKp_I8a6mvTp9FYGwRQ/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/5UMNeX9RTmg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/1948672738245686789/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/10/grails-and-ejb.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1948672738245686789?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/1948672738245686789?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/5UMNeX9RTmg/grails-and-ejb.html" title="Grails and EJB" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/10/grails-and-ejb.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0UBRnY9fip7ImA9WxNQFUs.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-4726745365667196687</id><published>2009-09-17T14:57:00.007+02:00</published><updated>2009-09-21T21:27:37.866+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-21T21:27:37.866+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="pdf" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Tweaking grails pdf plugin</title><content type="html">I had a little bit of problem when I wanted to use grails PDF plugin. (By the way Aaron Eischeid, thanks for providing this). However some things is not as wanted. &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="color: #38761d; font-family: 'Courier New', Courier, monospace;"&gt;pdfLink&lt;/span&gt;&lt;/b&gt;: this is a nice feature for generating PDF by providing an url to the page to generate as PDF. But I do not want to provide parameters by adding them to the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #9fc5e8;"&gt;url&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #9fc5e8;"&gt; &lt;/span&gt;in get-form.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="color: #38761d; font-family: 'Courier New', Courier, monospace;"&gt;pdfForm&lt;/span&gt;&lt;/b&gt;: Somewhat better, But this method gives some other problems. First of all, pictures is lost since the PDF only generates from string, also the layout is not automaticly included in the render call.&lt;br /&gt;
&lt;br /&gt;
So I did the followin tweaks. Sorry Aron, but I had to hack into the &lt;span style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #6fa8dc;"&gt;PdfService.groovy&lt;/span&gt;&lt;/span&gt; and &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #9fc5e8;"&gt;PdfController.groovy&lt;/span&gt;&lt;/span&gt; to get it work and also set up a template in a specific way.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;This is what I did&lt;/b&gt;:&lt;br /&gt;
In &lt;span style="color: blue; font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #6fa8dc;"&gt;PdfService.groovy&lt;/span&gt; &lt;/span&gt;I added a method to be able to generate a PDF with images for pdfForm, to do this the renderer need to know the baseUri to get find the image.&lt;br /&gt;
&lt;br /&gt;
I added this code:&lt;br /&gt;
&lt;div class="codebox-header"&gt;code&lt;br /&gt;
&lt;/div&gt;&lt;pre class="codebox-content"&gt;byte[] buildPdfDoc(content, baseUri) {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new StringBufferInputStream(content));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ITextRenderer renderer = new ITextRenderer();
    try {
      renderer.setDocument(doc, baseUri);
        renderer.layout();
        renderer.createPDF(baos);
        byte[] b = baos.toByteArray();
        return b
    }
    catch (Throwable e) {
        log.error e
    }
}
&lt;/pre&gt;&lt;br /&gt;
In the method pdfForm for the &lt;span style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;span style="color: #9fc5e8;"&gt;PdfController.groovy&lt;/span&gt;&lt;/span&gt;, I replaced code with call to the new method.&lt;br /&gt;
&lt;div class="codebox-header"&gt;code&lt;br /&gt;
&lt;/div&gt;&lt;pre class="codebox-content"&gt;def baseUri = request.scheme + "://" + request.serverName + ":" + request.serverPort +
   grailsAttributes.getApplicationUri(request)
....
//b = pdfService.buildPdfFromString(content)
b = pdfService.buildPdfDoc(content, baseUri)
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
I then made a template with the following structure to also use the layout, without the applyTemplate, I only got the template code (also note that the template is looked for in views/pdf/): &lt;br /&gt;
&lt;div class="codebox-header"&gt;code&lt;br /&gt;
&lt;/div&gt;&lt;pre class="codebox-content"&gt;&amp;lt;g:applyLayout name="main"&amp;gt;
    &amp;lt;h1&amp;gt;title&amp;lt;/h1&amp;gt;
    &amp;lt;b&amp;gt;something&amp;lt;/b&amp;gt;
&amp;lt;/g:applyLayout&amp;gt;
&lt;/pre&gt;&lt;br /&gt;
I have not done this with the options for controller and action, just for template, since the g.include seams to return empty content. But I will continue to explore.&lt;br /&gt;
&lt;br /&gt;
! An update. I realized that the pdf-plugin was build for grails 1.1. maybe this was the reason that the g.render in the plugin gave med nothing as a result. I was actually using grails 1.0.4. Using 1.1.1 however makes the action and controller work better. No pictures thoug, but they show when I am running pdf-plugin as an application after downloading the source.&lt;br /&gt;
&lt;br /&gt;
I will try to find the proper place to address these issues to improve the plugin.&lt;br /&gt;
&lt;br /&gt;
/Peter&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-4726745365667196687?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/cniT6a1cKYQ8UiV8CEsL1Gt9tpk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cniT6a1cKYQ8UiV8CEsL1Gt9tpk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/cniT6a1cKYQ8UiV8CEsL1Gt9tpk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/cniT6a1cKYQ8UiV8CEsL1Gt9tpk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/c2Rdd8lVNRM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/4726745365667196687/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/09/tweaking-grails-pdf-plugin.html#comment-form" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/4726745365667196687?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/4726745365667196687?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/c2Rdd8lVNRM/tweaking-grails-pdf-plugin.html" title="Tweaking grails pdf plugin" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>5</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/09/tweaking-grails-pdf-plugin.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMGSXoycSp7ImA9WxNRFUo.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-5352239496265345035</id><published>2009-09-09T10:19:00.005+02:00</published><updated>2009-09-10T11:53:48.499+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-10T11:53:48.499+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="japser" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><category scheme="http://www.blogger.com/atom/ns#" term="reports" /><title>Creating jasper reports in Grails</title><content type="html">I like to work with Grails, so to continue making useful stuff i elaborated a little bit about how to make reports in Grails using Japser (which is a nice reporting tool)&lt;br /&gt;
&lt;br /&gt;
My aim is to do reports in PDF and Excel. And since it don't think it is the f...ing business of the report file to know anything about the database I intend to use my own models to send into the report.&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Create an application&lt;/i&gt;:&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;grails create-app bookshelf&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;Install the plugin&lt;/i&gt;:&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;grails install-plugin jasper&lt;/span&gt;&lt;br /&gt;
This usually takes a while since Plugin list cache is almost always out of date when installing plugins. So go take a coffe or something.&lt;br /&gt;
&lt;br /&gt;
First of all I want a domain-class to hold books. So create that one and set up a few books in the BootStrap.groovy to have somthing to test with.&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;grails create-domain-class Book&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Add a few properties to the book, for example:&lt;br /&gt;
&lt;pre style="background-color: #efefef; border: 1px inset rgb(204, 204, 204); color: black; font-family: courier; font-size: 10pt; overflow: scroll;"&gt;class Book {
&amp;nbsp;&amp;nbsp;&amp;nbsp; String title
&amp;nbsp;&amp;nbsp;&amp;nbsp; String author
&amp;nbsp;&amp;nbsp;&amp;nbsp; String publisher
&amp;nbsp;&amp;nbsp;&amp;nbsp; String category
}
&lt;/pre&gt;&lt;br /&gt;
and add a few books in the BootStrap.groovy to have something&lt;br /&gt;
&lt;pre style="background-color: #efefef; border: 1px inset rgb(204, 204, 204); color: black; font-family: courier; font-size: 10pt; overflow: scroll;"&gt;def init = { servletContext -&amp;gt;
         new Book(title:'agile modeling', author:'Scott W. Ambler', publisher:'Whiley Computer Publishing', category:'Modeling').save()
         new Book(title:'The J2EE Architect\'s Handbook', author:'Derek C. Ashmore', publisher:'DVT Press', category:'Architecture').save()
         new Book(title:'Getting Started with Grails', author:'Jason Rudolph', publisher:'InfoQ', category:'Programming').save()
     }
&lt;/pre&gt;&lt;br /&gt;
To create a report from Jasper, you need to create a .jrxml and then compile it to.jasper report file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To create a report for my purpose, I used &lt;i&gt;&lt;b&gt;iReport&lt;/b&gt;&lt;/i&gt;. this is a tricky one because the jasper plugin which I got when installing is 0.9.7 and has a mistake in it that makes the reports fail. However to solve that look up the lib folder in the plugin-installment (using grails1.0.4 is in the project, but using 1.1.1 it ends up in your home folder) and remove the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;jasperreports-2.0.5.jar&lt;/span&gt;, otherwise you will end up with some NullPointerExceptions when tryring to run the report. Loocking there you see that the other jar file is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;jasperreports-3.1.2.jar&lt;/span&gt;, so the iReport version to download is 3.1.2.&lt;br /&gt;
&lt;br /&gt;
I choosed the standalone version but there is a version that plugs into Netbeans.&lt;br /&gt;
&lt;br /&gt;
Making the following report and compile it makes an .jasper file to use from grails.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/_Lg4o4B3FUAg/SqdjhquWTOI/AAAAAAAABa4/_DIQB3R2URE/s1600-h/Screenshot-iReport+3.1.2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/_Lg4o4B3FUAg/SqdjhquWTOI/AAAAAAAABa4/_DIQB3R2URE/s320/Screenshot-iReport+3.1.2.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;/div&gt;Now things is starting to get a little bit interesting. We now need to make a report of this. I genereated all stuff for book by using the command:&lt;br /&gt;
&lt;span style="font-family: 'Courier New',Courier,monospace;"&gt;&amp;nbsp;grails generate-all Book&lt;/span&gt;&lt;br /&gt;
we can then use the generated views to present the report.&lt;br /&gt;
&lt;br /&gt;
Add the following line in the view to get a PDF and a XLS report:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-color: #efefef; border: 1px inset rgb(204, 204, 204); color: black; font-family: courier; font-size: 10pt; overflow: scroll;"&gt;g:jasperReport action="createReport" controller="book" format="PDF" jasper="books" name="All Books"
g:jasperReport action="createReport" controller="book" format="XLS" jasper="books" name="All Books"
&lt;/pre&gt;&lt;br /&gt;
(of course they should be tagged, but this stupid blog editor does not want to show it if if make them as tags)&lt;br /&gt;
&lt;br /&gt;
for example under pagination, I used an action in the controller and a little bit of of code, not much really, to to what is needed.&lt;br /&gt;
&lt;pre style="background-color: #efefef; border: 1px inset rgb(204, 204, 204); color: black; font-family: courier; font-size: 10pt; overflow: scroll;"&gt;def createReport = {
        def books = Book.list()
        chain(controller:'jasper',action:'index',model:[data:books],params:params)
    }
&lt;/pre&gt;&lt;br /&gt;
That's it. Try it out yourself!&lt;br /&gt;
&lt;br /&gt;
(For&amp;nbsp; the lazy one, a zip of the project can be found here: &lt;a href="http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1"&gt;http://sites.google.com/site/ironicprogrammer/home/grails-and-jasper-1&lt;/a&gt; as an attachement)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-5352239496265345035?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KCY_W1CiSmj18pnHY9m3dvc_sCg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KCY_W1CiSmj18pnHY9m3dvc_sCg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KCY_W1CiSmj18pnHY9m3dvc_sCg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KCY_W1CiSmj18pnHY9m3dvc_sCg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/Is4YvEbhQik" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/5352239496265345035/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/09/creating-jasper-reports-in-grails.html#comment-form" title="16 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/5352239496265345035?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/5352239496265345035?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/Is4YvEbhQik/creating-jasper-reports-in-grails.html" title="Creating jasper reports in Grails" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_Lg4o4B3FUAg/SqdjhquWTOI/AAAAAAAABa4/_DIQB3R2URE/s72-c/Screenshot-iReport+3.1.2.png" height="72" width="72" /><thr:total>16</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/09/creating-jasper-reports-in-grails.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08HR3c9eSp7ImA9WxNREEo.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-3717535104235517919</id><published>2009-09-04T15:43:00.000+02:00</published><updated>2009-09-04T15:43:56.961+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-09-04T15:43:56.961+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Java" /><category scheme="http://www.blogger.com/atom/ns#" term="Web Service" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>GroovyWS or Java WS Client?</title><content type="html">We had a project this winter/spring which I designed to use Webservices on a web connected to EJBs, the clients we build in Grails for fast development.&lt;br /&gt;
&lt;br /&gt;
Well we started using GroovyWS because that gave us the opportunity to get up and running pretty quick.&lt;br /&gt;
&lt;br /&gt;
First we had some problems with the time it took to generate the stubs everytime we called a web service, however, this was becaus we stupidly put the creation of the client stubs in the actions so it was done every time. This was easy to sol by making a Groovy singleton class and let it create the service stubs once.&lt;br /&gt;
&lt;br /&gt;
Eventually I decided to move over to making JAX-WS clients in Java using Netbeans. The reasons for this was mostly to try to get better performance and the possibility to put on SSL on the Web Service layer. From information I found and the experience during the first month of the project, I realised some shortcommings with GroovyWS mainly:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;there is no support for https calls using GroovyWS&lt;/li&gt;
&lt;li&gt;for some reason I had to wrap lists into a class. GroovyWS only regarded the list as one object and took the first one and skipped the rest and making workarounds in the WS-layer to fix this is something I don't want&lt;/li&gt;
&lt;li&gt;restarting Glassfish sometimes made problems with the grailsapps and made them crash, this I think is because some of the services tokk some time to start and when GroovyWS tried to make stubs they were not there and after that the wars refused to work.&lt;/li&gt;
&lt;/ul&gt;However making a WSClient in Java from Netbeans is an easy task, even if Java 1.6_07 or later is required for JAX-WS it is not such a big deal to make an jar-file and put it into the grailsapps lib folder, after that it is easy to call the java-classes from the Groovy code.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-3717535104235517919?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/5xVzNeE7LMXAlxzD2Ar8s-Jhnyg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5xVzNeE7LMXAlxzD2Ar8s-Jhnyg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/5xVzNeE7LMXAlxzD2Ar8s-Jhnyg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/5xVzNeE7LMXAlxzD2Ar8s-Jhnyg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/5xOSxcCaoRw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/3717535104235517919/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/09/groovyws-or-java-ws-client.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3717535104235517919?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/3717535104235517919?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/5xOSxcCaoRw/groovyws-or-java-ws-client.html" title="GroovyWS or Java WS Client?" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>2</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/09/groovyws-or-java-ws-client.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C04MSXw-eSp7ImA9WxNXGU0.&quot;"><id>tag:blogger.com,1999:blog-3815776380138759375.post-6816855153497631250</id><published>2009-09-04T14:40:00.001+02:00</published><updated>2009-10-07T09:53:08.251+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-07T09:53:08.251+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="programming" /><category scheme="http://www.blogger.com/atom/ns#" term="i18n" /><category scheme="http://www.blogger.com/atom/ns#" term="grails" /><title>Grails i18n</title><content type="html">Got a little bit pissed of regarding language changing on Grails.&lt;br /&gt;
&lt;br /&gt;
Of the information I can find about it it should be fairly straightforward. Just providing the parameter "lang=xx". In my experience this works sometimes, but sometimes something magical happen running on Glassfish for a while. The switcing of language stops working. Don't know if this is a known problem.&lt;br /&gt;
&lt;br /&gt;
Anyway, for now I made a workaround using the following code:&lt;br /&gt;
&lt;div style="background-color: #dedede; border: 1px solid white; color: black; font-family: Courier; font-size: 1em;"&gt;&lt;pre&gt;def beforeInterceptor = [action:this.&amp;amp;checkLang, except:['']]

    def checkLang() {
        if(session["lang"] == null){
            session["lang"] = "sv"
        }
        if(params.lang){
            session["lang"] = params.lang
        }
       if(params.lang &amp;amp;&amp;amp; params.lang=="en"){
            def defaultLocale = new Locale("en","US")
            java.util.Locale.setDefault(defaultLocale)
       }         return
    }
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Which works for now. Should think there is a better solution. Feel free to inform me if you know.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3815776380138759375-6816855153497631250?l=ironicprogrammer.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/szWNAb9_7nMTkSfp0YJ2w3BDtOM/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/szWNAb9_7nMTkSfp0YJ2w3BDtOM/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/szWNAb9_7nMTkSfp0YJ2w3BDtOM/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/szWNAb9_7nMTkSfp0YJ2w3BDtOM/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/IronicProgrammer/~4/bHr8pGzC-VQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://ironicprogrammer.blogspot.com/feeds/6816855153497631250/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://ironicprogrammer.blogspot.com/2009/09/grails-i18n.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6816855153497631250?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/3815776380138759375/posts/default/6816855153497631250?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/IronicProgrammer/~3/bHr8pGzC-VQ/grails-i18n.html" title="Grails i18n" /><author><name>Peter</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_Lg4o4B3FUAg/SjTjGc-xvMI/AAAAAAAAA9A/tBySRWE2lw4/S220/bennon.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://ironicprogrammer.blogspot.com/2009/09/grails-i18n.html</feedburner:origLink></entry></feed>

