Sunday, August 10, 2014

My Java rest service for my Angular Web application

To get all the companies to my web application I had to write a REST service in Java. I'm using the Eclipse Luna version together with the javax.ws libraries from the jersey implementation (see
http://jersey.java.net/ ) 
This is the simplest implementation.

Before that i have written my own data classes and put them into a separate project called ContractsDB. From the Project I export the JAR file and will use it in my Rest project.

I'm not using JPA (Java persistence api), since I think in combination with the web services it is to complicated to get going and to distribute.

My rest service looks as following:


 package com.testres;

import java.util.List;

import javax.ws.rs.GET;
// import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import ContractsDB.Models.*;

// http://localhost:9080/TestRest/rest/RestCompanies
// http://localhost:9080/TestRest/rest/RestCompanies/name    to order by name

@Path("RestCompanies/{orderby}")
public class GetCompanies {

     // This method is called if TEXT_PLAIN is request

    @GET
    @Produces("application/json") // MediaType.APPLICATION_JSON)

    public String Get(@PathParam("orderby") String p_OrderBy) {
        Company_Factory cf = new Company_Factory();
        List<Company> Company_List = cf.QueryCompany("", p_OrderBy);
        Gson gson = new GsonBuilder().create();
        return gson.toJson(Company_List);
    }
}


As you can see I return from  my own query object, also I am using the Google JSon library to return json syntax.

I test the service with a firefox plugin:


It seems to be working, next step will be to create a page and retrieve the data with AngularJS.




Saturday, August 9, 2014

Starting my Angular Project

Last several weeks I have been starting my AngularJS adventures. With the download of the latest version and Eclipse to develop Restfull Services I thought I would build a simple contracts administration. I have put my database in MySql just for convenience. I must say the easiest way to start is just to copy the Angular scripts to a subfolder Scripts in my HTML root directory, and then add the scripts reference to my index.html page.


I use the angular-route.min.js since I want to make a Singe web page application using the ng-view directive. Something that will add to the complexity immediately.

The script tags I put just beforethe end body tag, since i want to load the HTML first. Just to see if the Angular framework works I have put the following code:


<html ng-app>
  <head>
   </head>
  <body>
    <div>
    <input type="text" ng-model="data.message" />
    <h1>{{ data.message }}</h1>
    </div>
    <script src="Scripts/angular.min.js"></script>    
  </body>
</html>



Notice the ng-app and the ng-model="data.message". Both directives will be used to indicate to the Angular java-script that the source should be treated by the angular framework.
If your browser shows : data.message in the screen something is not right. It should present you with an input box and retype everything you type into the box like so: 



if it shows this:



.....somethings wrong. Either you did not add the ng-app tag, or it did not reference the javascripts files in the right way.

If it show the right screen that's it. you're off to a start in the wonderful Angular.Js world.

Sunday, December 13, 2009

Inversion of Control

With the latest Genwise Software factory project we are using a pattern that is called Inversion of Control, or also known as Dependency Injection (what's in a name). Getting our heads around this pattern took a while and it was hard to find a simple sample that just demonstrates the simplest implementation of this technique. (more complex implementations using service locators can be found elsewhere). In this simplest sample a movie database printer prints movies from different movie sources. One is supposed to be memory and the other a text file.

You can download the sample here

Friday, November 20, 2009

GSF beta 1 approaching

After our DSL research and string templating adventures we are now at the point to almost release a public beta for the GSF. (GenWise Software Factory) We have decided to create DSL's for the Data modeling and Business Object modeling. We have created templates for the Domain (Traditional Business Objects) , the DAO (Data access object layer) and some example code how to use it all in a WPF application. Below is an example Database scheam imported with one of our import utilities.



Basically what you get when you use GSF are the following modules:
- VS 2005 integration
- Database modeling with import schema from SQl-server or Oracle
- Business object class diagram created from the Datamodel but extendable with OO features
- DOA and Domain generation with partial classes and partial methods
- MVVM framework (Model view - View model pattern)
- Made for WPF binding but not limited to WPF.

Saturday, November 29, 2008

Microsoft DSL Tools

At GenWise we are currently investigating using the Microsoft DSL tools for code generation. I must say I am really impressed by what Microsoft has done with this tool. Not only make it possible to model your ideas, but also to instantiate that model and make incarnations for the real world. It is highly customizable and expandable. Together with string templating you can make very useful code generation environments. It takes a specific way to look at your implementation though if you want to preserve your own additions to he generated code. You will have to implement lots of base classes and code for partial class use from the beginning.
In general the extensibility is not so well documented, and there seems to be only a few specialists around. I do not know if we will come to a marketable product at this point in time, but I'm sure if you keep an eye on Genwise.com that sooner or later you will be able to find more about our DSL adventures.

Tuesday, February 19, 2008

Dicom the easy way

I have been searcing for a while for an open source DICOM library. The thing I hate the most about open source is that many things are started by one man shops, and after a while development stops. A good example of a nice Dicom library is OpenDicom. See www.opendicom.org. Problem is that development is stopped and the documentation is not that great.

I wanted to report on a great Dicom library I came across today. It is called ClearCanvas See www.clearcanvas.ca

It is unbelievable how accessible thay made dicom file access and dicom file viewing.
I wanted to modify some dicom file programmatically in VS 2005. You can do this very easily with the Clearcanvas SDK. The SDK is vary well documented and it took me about 30 minutes to get code going to change a patient Id in a dicom image, somthing I was not able to do very easily with other open source dicom libraries.

The library is also documented very well, with sample code, and the site has a good forum.

Here is the code:


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using ClearCanvas.Dicom;

namespace CCDicom

{

    public partial class DicomAttibuteViewer : Form

    {

        public DicomAttibuteViewer()

        {

            InitializeComponent();

        }

 

        private void btGo_Click(object sender, EventArgs e)

        {

            DicomFile _df = new DicomFile(@"C:\user\DotNet\OpenDicom\DicomSampleFiles\00000014.dcm");

            _df.Load(DicomReadOptions.Default);

            DicomAttributeCollection _ds = _df.DataSet;

            foreach (DicomAttribute _da in _ds)

            {

                if (_da.Tag.Group == 16 && _da.Tag.Element == 32)

                {

                    // Patient ID

                    _da.SetString(0, "mypatientid");

                }

                if (_da.Tag.Group == 10)

                {

                }

                this.listBox1.Items.Add(_da.Tag +"->" + _da.ToString());

            }

            _df.Save(@"c:\user\DotNet\OpenDicom\DicomSampleFiles\erik1.dcm");

 

        }

    }

}

Wednesday, January 23, 2008

Using Log4net in a Winforms Application

I was trying to use log4 net in my Winforms application and could not get it to work that easily. Here are the steps i performed to get it to works.

1. Offcourse first add the log4net dll to your application as a reference.
2. Then initiate logging as following:

public ILog log;
public void InitiateLogging()
{
log4net.Config.XmlConfigurator.Configure();
this.log = LogManager.GetLogger("user");
}

3. Do not forget to make a log4 net section in your application config file. Here is my config file (my application is named IMSBEWFM. The config file is named: ImsBeWfm.EXE.config

< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
< configSections>
< section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
< /configSections >
< appSettings file="" >
< clear />
< add key="log4net.Internal.Debug" value="true"/>
< /appSettings>
< log4net>
< appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
< file value="general.txt"/>
< appendToFile value="true"/>
< maximumFileSize value="100KB"/>
< rollingStyle value="Size"/>
< layout type="log4net.Layout.PatternLayout">
< conversionPattern value="%d{HH:mm:ss} [%t] %-5p %c - %m%n"/>
< /layout>
< /appender>
< root>
< level value="DEBUG"/>
< appender-ref ref="GeneralLog"/>
< /root>
< logger name="NHibernate" additivity="false">
< level value="DEBUG"/>
< appender-ref ref="GeneralLog"/>
< /logger>
< /log4net>
< /configuration>


4. Notice the:
< add key="log4net.Internal.Debug" value="true"/ >

That line is very usefull in the debug output window of your application log4net will tell you any errors while initializing, tracking down problems. Later when it works you can remove that line.

Now to log someting is quite easy:

log.Debug("Sucessfull login");

Be carefull if you want to log for a released application, then you will have to change level value = "DEBUG" to level value = "INFO" and use log.info in your code to log to the released application.

Excelent reference is: here