One of the issues when I write unit test is having to create TestSuite after TestSuite at package level to run all the test in a package or run all tests in a project. And I really dislike the repetitiveness of it.
And sometimes I just like to test all classes in a package and its sub packages rather than running all the tests in a project due to the amount of time it takes to setup all the necessary resources.
Finally I decided to do something about it and started digging into JUnit to figure out a way to automate testing classes in a package without having to add test classes to a TestSuite.
The initial version of Suite I developed added all classes from the package a class was marked with RunWith annotation as well as all classes from sub-packages recursively.
It was all great. I could just mark a class with @RunWith(DynamicSuite.class) annotation at the root of a project’s root package and it would
run all the tests for the project. Or I could mark a class in a package with @RunWith(DynamicSuite.class) annotation and I could test just the classes in the package and its sub-packages.All sweet.
But later in some cases I only wanted to run classes in a package and not all classes in its sub-packages as well due to the speed of test as some tests in sub-packages were resource intensive. So I had to come up with a finer-grained test Suite mechanism.

To tackle that issue, I decided to add an Annotation -@NonRecursive – that marked a Test entry point ( class marked with @RunWith(DynamicSuite.class) annotation as being non-recursive.
I wanted to be able to test just a package when I wanted to without testing sub-packages, but I also wanted a over-riding mechanism to be able to run all tests in a project or a package and its sub-package such that if the entry point class is not marked as @NonRecursive , all the @NonRecursive annotaitons get ignored in subsequent sub-packages and all classes in the package and below get executed.

Here’s the DynamicSuite Class code.

/**
 *TestSuite execute all test classes from the package of  <br>
 * classes that are annotated to run with this class<br>
 * It loads all classes in the same package as the setup class and
 * loads classes from sub packages recursively.
 * If a sub-package contains a class that's marked to be run with this class<br>
 * it only loads that class so that the marked class can in turn load and execute<br>
 * test classes in its package and its subpackages.<br><br>
 * To cut it short: This class loads and executes all test classes in its package and its
 * subpackages recursively.<br<br>
 * Example usage:Just create an empty class and add <code>@RunWith</code> annotation<br>
 *
 *  <code>@RunWith(DynamicSuite.class)<br>
 * public class BatchTest {<br>
 * //no methods or body needed.<br>
 * }</code>
 * <br><br>
 * To test only classes in a package and ignore test classes in subpackages mark the class with @NonRecursive.
 * To test all test classes in a project mark a class at a project's  root test  package  with <code>@RunWith(DynamicSuite.class)</code>
 * and it will run all test classes in the project.
 * The only catch so far is that all the methods of all test classes gets run under
 * this the setup class and the results are all grouped under that class , at least from what I can see in Netbeans.
 *
 *
 * @author anjan
 */
public class DynamicSuite extends Suite {

    static boolean isEntrySuite=true;
    public DynamicSuite(Class<?> setupClass) throws InitializationError {

        super(setupClass, getTestClasses(setupClass,setupClass.isAnnotationPresent(NonRecursive.class)));

            //this will run after the initial setup has been done;
            //If a  Class is marked as NonRecursive and its the entry point of tests
            // it won't load test classes from subpackages.
            //but if the Class is marked as NonRecursive and its not the entry point
           // of tests the annotation will be ignored and all classes from subpackages of the containing
          //package will be loaded
            isEntrySuite=false;

    }

    public static Class<?>[] getTestClasses(Class setupClass ,boolean nonRecursive) {

        List<Class> classes = null;

        String packageName = setupClass.getPackage().getName();
        String path = setupClass.getProtectionDomain().getCodeSource().getLocation().getFile();
        path = path + packageName.replace('.', File.separatorChar);

        File dir = new File(path);

        try {
            classes = findTestClasses(dir, setupClass,nonRecursive);
        } catch (Exception ex) {
            throw new RuntimeException("Problem loading classes from package[" + packageName + "]", ex);
        }
        return classes.toArray(new Class[classes.size()]);
    }

    /**
     * recursively finds Classes in test package <br>
     * if it finds a class annotated with @RubWith(DynamicSuite.class)
     * if loads that class and ignores all classes and sub-packages in that package
     * because the loaded class is responsible for loading classes under ite hierarchy
     * @param dir   The root directory
     * @return The classes
     * @throws ClassNotFoundException
     */
    private static List<Class> findTestClasses(File dir, Class setupClass, boolean nonRecursive) throws ClassNotFoundException {
        List<Class> classes = new ArrayList<Class>();
        if (!dir.exists()) {
            return classes;
        }
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
               //only load classes in subpackage if the setup class isn't marked as NonRecursive
                // or its marked as NonRecursive but its not the entry point class
                if(!nonRecursive ||(nonRecursive && !isEntrySuite))
                classes.addAll(findTestClasses(file, setupClass,nonRecursive));
            }
            else if (file.getName().endsWith(".class")) {

                String className = file.getName().substring(0, file.getName().length() - 6);

                if (!className.equals(DynamicSuite.class.getSimpleName())) {

                    String packageDirPath = file.getPath();
                    packageDirPath = packageDirPath.substring(packageDirPath.indexOf("classes"));
                    String pkg = packageDirPath.replace(File.separatorChar, '.').substring(8).replace("." + file.getName(), "");
                    Class cls = Class.forName(pkg + '.' + className);
                    if (cls.isAnnotationPresent(RunWith.class)) {
                        RunWith ann = (RunWith) cls.getAnnotation(RunWith.class);

                        if (ann.value().equals(DynamicSuite.class)) {
                           //ignore the class being run, otherwise the program will crash with StackOverflowError
                            if (!cls.equals(setupClass)) {
                                classes = new ArrayList<Class>();
                                classes.add(cls);
                                break;
                            }
                        } else {
                            classes.add(cls);
                        }

                    } else {
                        classes.add(cls);
                    }

                }
            }
        }

        return classes;
    }

}

The Annotation class:

/**
 *Used to Mark DynamicSuite as NonRecursive so that:<br>
 * If a Suite that's being run is marked as NonRecursive, it will only run classes<br>
 * within the same package.<br>
 * But If the  Suite being run is not marked as  NonRecursive,<br>
 * it ignores all NonRecursive annotations in Suites contained in subpackages of the package the<br>
 * Suite class belongs to.<br>
 * The rationale is to be able to test only Tests in a package when desired at the same time<br>
 * being able to test all tests in a project or in a package and subpackages when running when the Suite being run is<br>
 * not marked as NonRecursive.<br>
 * @author anjan
 */
@Retention(RetentionPolicy.RUNTIME)
public @interface NonRecursive {

}

Sample code:
Running all test classes recursively

@RunWith(DynamicSuite.class)
public class EntryPoint {

}

Sample code:
Running only test classes in a package

@NonRecursive
@RunWith(DynamicSuite.class)
public class EntryPoint {

}

Before Google provided HTTP reverse-geocoding service, the world was a dark place if you needed reverse-geocoding in your application.
The only way to perform reverse-geocoding was using javascript API that too using third party library.
Since the application I was working on a year or so back required reverse-geocoding- worse it was central to our geo-location based reporting – I had to find a
way of reverse geocoding high volume longitude/latitude pairs on the server side.
After contemplating various solutions/non-solutions I decided to give myself some time to come up with a solution that didn’t
require us to pay for premium service or ditch googlemaps altogether.
That’s when I came across HtmlUnit. As soon as I read about its capabilities, I suddenly had a brilliant idea!
The idea was to use HtmlUnit to access a page with a small googlemap loaded ( reverse geocoding using GDirection required an instance of googlemap to be present)
on it, that has a form for entering longitude and latitude, a button to submit reverse-geocoding request and a DIV element for populating reverse-geocoded address response
from Google.

Javascript code:

  // GMap2 object
            var map;
            // GReverseGeocoder object
            var rg;
            // text input fields
            var lat;
            var lng;
            // result div
            var addressText;

            function load() {
                if (GBrowserIsCompatible()) {
                    lat = document.getElementById("lat");
                    lng = document.getElementById("lng");
                    addressText = document.getElementById("address");

                    map = new GMap2(document.getElementById("map"));
                    map.setCenter(new GLatLng(-33.83955, 151.2084), 15);
                    map.addControl(new GLargeMapControl());
                    rg = new GReverseGeocoder(map);

                    // add listner for the result
                    GEvent.addListener(rg, "load", function(response){
                        addressText.innerHTML=response.address
                    });
                    //add listener for error response
                    GEvent.addListener(rg, "error", function(lastpoint){
                        addressText.innerHTML = "ERROR:point " + lastpoint;
                    });

                    // Listener to detect clicks on map to get coordinates to fill in the lat and lng fields-for testing purpose.
                    GEvent.addListener(map, "click", function(marker, point){
                        lat.value=point.lat();
                        lng.value=point.lng();
                    });

                }  

            }

            // get the input form lat and lng fields and issue a reverse geocode
            // request
            function reverse(){
                var point = new GLatLng(lat.value,lng.value);
                rg.reverseGeocode(point);
                return false;
            }

HTML code:

 <form name="rev" id="rev"  action="" method="post"  onSubmit=" reverse();return false;">
            <table>
                <tr><td>Latitude (WGS84)</td><td><input id="lat" name="lat" type="text" size="20" value='' /></td></tr>
                <tr><td>Longitude (WGS84)</td><td><input id="lng" name="lng" type="text" size="20" value='' /></td></tr>
                <tr><td><input name="submit" id="submit" type="submit" onClick="reverse()" value="Get Address"></td><td> address:<div id="address"></div></td></tr>

                <tr><td colspan="2"><div id="map" style="width:400px; height:400px;"></div></td></tr>
            </table>

        </form>

I wrote a Java method which accessed the webpage, populated longitude/latitude textbox in the webpage, clicked the submit button, and wait for the response
to be populated in the div element -all done using HtmlUnit’s headless browser. After sleeping for 2 seconds the method then read the content of the address div element.
And it worked flawlessly.

Java Code:

public static String latLngToAddress(Float lat, Float lon) {

        try {
            WebClient wc = new WebClient();
            wc.setThrowExceptionOnScriptError(false);

            HtmlPage page = (HtmlPage) wc.getPage("http://localhost:8084/ReverseGeocoder/");

            HtmlForm form = page.getFormByName("rev");
            HtmlInput latInput = (HtmlTextInput) form.getInputByName("lat");
            latInput .setValueAttribute(lat.toString());

            HtmlTextInput lngInput = (HtmlTextInput) form.getInputByName("lng");
            lngInput.setValueAttribute(lon.toString());

            HtmlSubmitInput button = (HtmlSubmitInput) form.getInputByName("submit");
            button.click();

            Thread.sleep(2000);

            return page.getElementById("address").getTextContent();

        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

I spent quite a bit of time then to finally come up with this solution. But before I finished the application, Google finally provided HTTP reverse-geocoding service which meant that there was
no need to use my solution. But at least I’m glad I tried it and came up with a solution when none existed :-)

I use a custom very lightweight persistence library that does lazy loading using CGLIB Enhanced proxy.
THe generated proxy for domain objects use Key/value Map data fed to it via a static factory method.
When a get or set method is called for the wrapped domain, the proxy check the value for the field in the Map,
if it finds an entry for the field, which means the field hasn’t been initialised, it sets the field value from Map and removes
the entry from the Map.
If no entry is found in the Map, the method returns value of the field of the domain object encapsulated by the getter method
rather than getting it from the Map.

Of course if the field is a domain object, then the object is fetched using the PK value in the Map.
That gives me the same lazy loading voodoo magic like Hibernate- the 10 pound gorrila.
The persistence is backed by a cache which looks at a domain’s metadata and
only caches those that are specified as cacheable.

Everything works like a charm except that the CGLIB enhanced objects don’t play nice with XStream at all.
When serializing enhanced objects XStream spits out gory details of classe/superclasses/interfaces and full dissection of a Map that is fed to a static method when creating the proxy object, when all i want is the pojo object wrapped by the proxy.

I believe the XStream library simply gets the value of the field rather than calling the getter method using reflection when serializing.
That must be the reason that in my case all serialised objects have empty values for fields because the fields don’t get set when the proxy is
created, they get set only setter method is called on it or getter method is called for the first time .
I think that’s the same problem with Hibernate and I guess that’s the reason it throws Lazy Initilization Exception.

I tried the CGLIBEnhancedConverter that comes with the library but it didn’t work for me.
I couldn’t find a simple enough solution in the internet and the ones I found were all Hibernate specific.
Since this is the second time I had been trying to solve the issue, I decided to push forward and find a work around myself.

After trying out a few different solutions I finally came up with a dead simple solution.
IT involves a static method to deproxy the enhanced domain objects and a XStream custom converter class.
There may be performance cost as it uses reflection eventhough getter setter methods for domain classes are cached for reuse.

Here’s the simplified method.


  /**
     * Deproxies a CGLIB enhanced proxy object
     * @param <T>
     * @param proxy
     * @return deproxied domain object
     */
    public static <T > T deproxyReflection(final T proxy) {

      Class entityClass = proxy.getClass().getSuperclass();

        T entity = (T) ClassUtil.getInstance(entityClass);

        Field[] fields =  entityClass.getDeclaredFields();

        for (Field f : fields) {
            try {

	 // no need to set the values of collections as they won;t be serialized.
                //we just need the actaul properties taht amp to table columns
                if(List.class.isAssignableFrom(f.getType()))
                    continue;

                //I use a utility method to fetch getter/setter methods.
                //it simply loops through the object and super class to find the methods.
//so instead of screwing arund with the field's security settings we'll use getters/setters to ensure its unintrusive
                Method getter = ClassUtil.getPojoMethod(entityClass, f.getName(), ClassUtil.MethodType.GETTER);
                Method setter = ClassUtil.getPojoMethod(entityClass, f.getName(), new Class[]{f.getType()}, ClassUtil.MethodType.SETTER);

                Object val = getter.invoke(proxy, null);
                setter.invoke(entity, new Object[]{val});

            } catch (Exception ex) {
                Logger.getLogger(EntityProxyFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                f.set(entity, f.get(proxy));
            } catch (Exception ex) {
                Logger.getLogger(EntityProxyFactory.class.getName()).log(Level.SEVERE, null, ex);
            } 

        }

        return entity;
    }

We need to register a converter with XStream that used the above method to sanitize the domain objects before serializing.


/**
 * XStream converter to convert a CGLIB enhanced proxy  to the wrapped pojo object.
 *
 */
public class CGLIBEnhancedEntityConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return (Enhancer.isEnhanced(clazz)  || clazz == CGLIBMapper.Marker.class) ;
    }

    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {

            // deproxy before sending it off the chain for serialization. ANd that's it.
           // XStream will get a simple pojo object and it will serialize it like any other pojo object.
         context.convertAnother(EntityProxyFactory.deproxyReflection(source));

    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

    throw new Error("not supported");
    }

}

And that’s it. That’s all it takes.
If a domain object has other domain objects as fields, those fields will not be handled by the above method.
For that there are two choices. Firstly you can look at the field’s class type and say if t implements or extends your base Domain class/interface, you can deproxy them recursively.
In my case all domain objects implement a bare minimum interface called IEntity with only one getId() method.

the above method can be modified to recursively deproxy/sanitize a domain’s fields if they are proxied objects.


//.............
            Object val = getter.invoke(proxy, null);

                if (IEntity.class.isAssignableFrom(f.getType()) {
                    val=deproxy(val);//recursively deproxy the fields if they are enhanced objects
                }
                setter.invoke(entity, new Object[]{val});

If you only have to serialize a handful of domain objects, it might be better to register converters
for those fields at startup instead of using the above method.

Say, we have a domain class State with Country as one of its field, we can register the above converter to convert the country object as below.


//.............
//entity manager that handles all persistence needs
IEntityManager em= (IEntityManager) BeanFactory.getBean("entityManager");

//fetch a list of State objects with id smaller than3
       List<State> list= em.fetch(State.class, "id<3");

//uses factory method to instantiate Xstream
xstream xstream= XStreamFactory.getXstream(Format.JSON);

CGLIBEnhancedEntityConverter cglibConverter= new CGLIBEnhancedEntityConverter();

//register default converter
xstream.registerConverter(cglibConverter);

//register field converter. country field for State objects
xstream.registerLocalConverter(State.class, "country", cglibConverter);

//spit out the serialized json objects
 System.out.println(xs.toXML(list));

here’s the result without deproxying the domain object:
The before version :-)


{"list": [
  {
    "code": "",
    "name": "",
    "daylightSavingStart": "",
    "daylightSavingEnd": "",
    "CGLIB$BOUND": true,
    "CGLIB$CALLBACK_0": {
      "@class": "com.barahisolutions.proxy.EntityProxyFactory$EntityMethodInterceptor",
      "data": [
        {},
        {
          "default": {
            "loadFactor": 0.75,
            "threshold": 12
          },
          "int": 16,
          "int": 8,
          "string": "id",
//..................................................
//there's 10 times more of this garbage which i didn;t bother to print out.

And here's the nice and clean pojo version of the deproxied proxy objects.
I used the recursive option to inspect properties if objects and deproxy them if they are proxied domain
objects.
The after version :-)


{"list": [
  {
    "id": 1,
    "code": "NSW",
    "name": "New South Wales",
    "utcOffset": 10.0,
    "daylightSavingOn": false,
    "daylightSavingStart": "",
    "daylightSavingEnd": "",
    "country": {
      "id": 18,
      "code": "",
      "name": "Barbados"
    }
  },
  {
    "id": 2,
    "code": "VIC",
    "name": "Victoria",
    "daylightSavingOn": false,
    "daylightSavingStart": "",
    "daylightSavingEnd": "",
    "country": {
      "id": 12,
      "code": "AUS",
      "name": "Australia"
    }
  }
]}

I recently had to build a HTML select list with items grouped by category .
The list had to be populated using ajax as using simple jsp tag wasn’t enough as I needed other
properties besides simple key/ value and group for validation and displaying description of the selected item using javascript .

Since grouping objects can be quite quite handy for displaying grouped data in forms and in reports I ended up creating a utility method in java which takes a List of objects and a field name to group the objects by ,
and returns a map with keys as the values of the field name and values a a list of objects under this group.

Here’s the method .


    /**
     * Groups objects using field name provided
     *  @param list the list of objects to group
     * @param field the field to group the objects by
     * @return new Map containing list of objects.
     * The Map's key is the property specified in criteria object
     */

public static <V> Map<String,List<V>> group(List<V> list, String field) {

            //used for indexing to avoid too much looping
	     HashMap<String,K> index= new HashMap<String,K>();
      // the container for grouped data. K is the data type for the field's value.
        HashMap<K,List<V>> groupedData= new HashMap<K, List<V>>();

      // the method to invoke to get the value of the 'field' prop from objects
         Method m=null;

        try {
          V obj=list.get(0);
       // Used a handy Util Class's method for getting the getter method for 'field'
          m = ClassUtil.getMethod(obj.getClass(), "get"+StringUtil.getFirstUpper(field));
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(ListUtil.class.getName()).log(Level.SEVERE, null, ex);

            return null;
        }

       //iterate through the list of objects passed as parameter
        for (V object : list) {

	//the 'field' value of object
             K columnVal=null;

             try {
                    columnVal = (K) m.invoke(object, null);

                     // group of objects under columnVal group
                       List<V> group=groupedData.get(index.get(columnVal.toString()));

                if(group==null){
                    group= new ArrayList<V>();
                groupedData.put(columnVal, group);
                index.put(columnVal.toString(), columnVal);
                }
                //add the object to the group
                group.add(object);

                } catch (Exception ex) {
                    Logger.getLogger(ListUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
        }

        return groupedData;

}

here’s a simple test:


   List<Command> commands= new ArrayList();

//create objects to populate a list;

        Command comm= new DeviceCommand();
        comm.setCommand("CONTROL comm1");
        comm.setCommandType(CommandType.CONTROL);
        comm.setName(" CONTROL command 1");
        commands.add(comm);

          Command comm2= new DeviceCommand();
        comm2.setCommand("comm2");
        comm2.setCommandType(CommandType.CONTROL);
        comm2.setName(" CONTROL command 2");
        commands.add(comm2);

          Command comm3= new DeviceCommand();
        comm3.setCommand("INFORMATION comm1");
        comm3.setCommandType(CommandType.INFORMATION);
        comm3.setName("INFORMATION command 1");
        commands.add(comm3);

          Command comm5= new DeviceCommand();
        comm5.setCommand("SETTING comm1");
        comm5.setCommandType(CommandType.SETTING);
        comm5.setName("SETTING command 1");
        commands.add(comm5);

          Command comm6= new DeviceCommand();
        comm6.setCommand("SETTING comm2");
        comm6.setCommandType(CommandType.SETTING);
        comm6.setName("SETTING command 2");
        commands.add(comm6);

           Command comm8= new DeviceCommand();
        comm8.setCommand("OTHER comm1");
        comm8.setCommandType(CommandType.OTHER);
        comm8.setName("OTHER command 1");
        commands.add(comm8);

          Command comm4= new DeviceCommand();
        comm4.setCommand("CONTROL comm4");
        comm4.setCommandType(CommandType.CONTROL);
        comm4.setName("CONTROL  command 4");
        commands.add(comm4);

           Command comm7= new DeviceCommand();
        comm7.setCommand("SETTING comm3");
        comm7.setCommandType(CommandType.SETTING);
        comm7.setName("SETTING command 3");
        commands.add(comm7);

          Command comm9= new DeviceCommand();
        comm9.setCommand("OTHER comm2");
        comm9.setCommandType(CommandType.OTHER);
        comm9.setName("OTHER command 2");
        commands.add(comm9);

          Command comm10= new DeviceCommand();
        comm10.setCommand("OTHER comm3");
        comm10.setCommandType(CommandType.OTHER);
        comm10.setName("OTHER command 3");
        commands.add(comm10);

      //convert  list of Command objects into a map with commandTpe as key and list of Commands within
    // that group as  map value.
        Map<String,List<Command>> grouped=ListUtil.group(commands, "commandType");

	//print out the grouped map data.
        System.out.println(grouped.toString());

And the string representation of the grouped data :

{OTHER=[OTHER command 1, OTHER command 2, OTHER command 3], SETTING=[SETTING command 1, SETTING command 2, SETTING command 3], CONTROL=[ CONTROL command 1,  CONTROL command 2, CONTROL  command 4], INFORMATION=[INFORMATION command 1]}

why css still sucks for layout

June 11th, 2009 | Posted by admin in css | front end - (0 Comments)

I’m really pissed off with the CSS fanboys let me tell you. Their devotion to CSS-P is so religious that their collective argument sounds annoyingly rhetorical at best.
Is there such thing as pragmatism?? I’m aware that pragmatism isn’t always the desired way to build software and systems. Heck, If I’m building a software to control nuclear reactors, I would be scared to let my pragmatism rule the software design, I can’t risk blowing a whole city or country and I’d have to take utmost care in every step I take to ensure I follow proper protocols and procedures so I don’t fry a city or entire country accidentally.

But lets face it, designing a website is not the same as designing a nuclear reactor control system.
And as such I can afford to say screw semantics, screw standards, screw them all if they screw up my time and resources and force me to think less of design and shitload more of hacks to just get things right.
I don’t care to get things just right. I want things to be damn right to start with. And if semantically incorrect methods permit me to do so, so be it.

I was just wondering, if HTML table was repackaged with absolutely no changes to any of its markup structure and behavior and called grid and renamed tr,td,th too, would all the CSS-P fanboys use it??

    <grid>
    <row>
   < unit> "Grid Glory"
   < /unit>
   < /row">
   < /grid>

To me its utter nonsense to not use a feature just because of semantic concerns and feeble arguments of SEO, accessibility etc.

The thing that really pisses me off though is the fact that as soon as someone raises concern about CSS/DIV based designs, all the CSS fanboys gang up and point their fingers and tell people that the reason they have problem with CSS/DIV based layout is because they are just plain ass dumb.

These bright new fanboys are pretty fanatic and everywhere I read, their arguments are just the same. Standard arguments, boxed arguments.

And they talk about the amount of code in the markup that makes table-based layout horrible to maintain not to mention the bandwidth penalty.

As if the amount of css hack that has to be conjured to do simple layouts doesn’t need maintenance and as if it loads by magic on the browser and bandwidth issue becomes non-issue….

CSS /DIV based layout is counter-intuitive to say the least. Who starts thinking in terms of float and clear when designing anyway??
If I want to add a new element to the a layout, I not only have to think about the size of the element but I have to make damn sure that the rest of the page doesn’t break by a mere addition of an element in a page. Which forces me to look at the whole design in terms of pixels, margins and paddings to add be able to add
a new element to a layout item. And that doesn’t prevent content/elements bleeding off the layout container.

And how about overlap problems??
I can break the best laid out CSS site by merely adding one more element to it……

CSS has merely opened a new job market and allowed people to make loads of money out of it. And the fanboys of CSS/DIV layout guard their self-interest with great zealot because without CSS they will be out of jobs. I mean, who’s going to write a page full of CSS hack to give us merely a 3-column fluid layout with column-size constraints. And then they write one more page full of CSS hacks to prevent the content from bleeding out of their containers, with partial success.

Web-development has taken a great leap backward as a result of the push to use CSS/DIV/SPAN for layouts.
Because we spend most of the time not in designing pages but in applying hacks to make them look devent on most browsers.

If the fanboys close their eyes for one minute and forget everything bad that had been hammered into their head about tables and think of it as a grid, u’ll be damn surprised how intuitive it is to design in terms of grids. All decent desktop GUI toolkit provide grid-based layout which allows us to apply constraint to layout grid-elements in relation to other grid elements and so on.

Why?

The fanboys might argue that the GUI toolkit designers were mere dumb people who still hold on to the grid-based layout of last century and blah blab and some more blah.

And when the fanboys run out of ammunition, they fallback to blaming browsers and declare its the fault of browsers and nothing else.
And meanwhile they bleed to death trying to make a simple layout work in most browsers with tons of css hacks which still doesn’t quite do the job. why?

CSS is ill-equipped to handle layouts…..The language or pseudo-language of CSS was designed by people who have no knack for Designs, if they did, the language would have been more intuitive than this terrible piece of work. I come from a programming background and I love beautiful designs and I like to pretend I can create cool designs and sites. For me CSS/DIV layout makes no sense from either a programmer’s prespective or a designer’s.

From a programmer’s perspective I like parameter driven development to simplify things, and from a designers perspective, I’d hate to have to hand-code the whole friggin page design and have to come up with countless hacks to overcome both CSS and browser shortcomings.

CSS sucks so bad that even though billions of people use the internet everyday, yet no decent visual designing tool has come out so far. The one decent WYSWYG editor Dreamweaver fails miserably when designing layouts in CSS.
Where’s the forward mobility promised by this new technology??

Javascript is a tricky language. Since I learned programming with Java, I always find myself trying to find its equivalence in other languages I come across.

I know its bad in that in that it kind of stops me from exploring the potentials provided by other languages in full. Its like going to a Chinese restaurant and eating fried rice everytime because fried rice is the only dish that is close to what I’m used to eating daily.
There’s such variety in Chinese food, and yet if I go there and I eat fried-rice evertyime Its a sign I have a bit of a problem opening myself up to new things. And that’s a big problem.

The world has such diversity in every-aspect imaginable, ( with the exception of mob-justice and mob-mentality which seem to cross all cultural and geographical boundary as it seems), and not being able to appreciate the diversity and learn for it is a great pity.

Back to programming, not all constructs  found in languages are sole property of a  given language. They are  generally abstractions provided by languages to solve problems effectively and those abstractions can be applied in other languages even if they don’t provide such explicit constructs.

There was a time when I treated javascript like lepers, something to avoid at all cost, except when it was really unavoidable.
But then I had a change of perspective, at a philosophical level -dare I day. ‘If I avoid something to such length then I must be shit-scared of it for some reason’.
And by nature I hate to admit my fears for the fear of being tagged a wussy, a chicken.
Of course I can find things to justify my avoidance of javascript to others without admitting my fear, but how about myself?
What do I tell myself when I go to sleep??
And since I couldn’t find reasonable arguments to fool myself into good sleep because my rational self always counter-argued that ‘you  fear what you don’t understand’, and my arguments against javascript were just my lame attempt to hide my fears , I decided to deal with my fears for once and for good.

And since then I have been quite happy with my javascript experience.

Since I’m still a Java fan-boy, though I have a feeling that it won’t be for  too long, Scala! here I come,  I tried to find solution to variable scoping in javascript so that I have more control over objects I create and the API’s I expose. This has been a big issue due to the need for me to use at least 3-4 third-party libraries to get my job done for the current project I’m rewriting.
And the project has a massive javascript code-base.

Looking back at the code I did last year makes me cringe. There was no modularity and there was too much interdependency between components and objects.

(Hmm… component decoupling is entirely another topic for another sleepless night.)

And this time around I decided to do thing the right way – at least to my best knowledge.

After long hours prowling the internet, finally I managed to understand the trick to accomplish what I wanted.

So here’s an example of an object with everything pubic, the dumbest example.


var Person= function(name ,surname, sex, dob){

this.name=name;
this.surname=surname;
this.sex=sex;
this.dob=dob;

this.getFullName=function(){

return this.surname+","+this.name;
}

this.getAge=function(){

return (new Date().getYear() - this.dob.getYear()) ) ;// I'm being lazy here

}

}

// another a little less dumb-looking flavor with prototyping but dumb nonetheless
var Person= function(name ,surname, sex, dob){

this.name=name;
this.surname=surname;
this.sex=sex;
this.dob=dob;
}

Person.prototype.getFullName=function(){
return this.surname+","+this.name;
}
Person.prototype.getAge=function(){
return (new Date().getYear() - this.dob.getYear()) ) ;
}

All the properties in of the object are accessible to any function/code that instantiates Person object. And getFullName() is nothing more but a handy method for getting both name and surname.
And the calling code can access dob and compute age by itself.
So everything is accessible.

var person = new Person('inny', 'minny', 'yes please', new Date(1901, 0, 1) );

var fullname= person.surname+","+person.name;

var age=  (new Date().getYear() - person .dob.getYear()) ) ;

Of course one can argue that we can differentiate the difference between private and public by using underscore for private properties such as _dob ( as a lot of people insist on doing).

But who are we fooling? That’s just a convention we use, and if you work in a large team there’s no guarantee that someone else will not get smart and start mutating the underscored variable and shit might hit the fan depending on how critical the property is.

Instead of developing ad-hoc convention I believe its more ideal to use the best of what a language provides without having to define our own rule which can be broken by anyone who cares to break it. Sometimes I don’t trust myself,let alone trust others.

So if we need to hide the DOB for privacy reason and only expose the age,  and we need to hide the individual name and surname for some reason, here’s how its done:

var Person= function(name ,surname, sex, dob){

this.sex=sex;

this.getFullName=function(){

return surname+","+name;
}

this.getAge=function(){

return  (new Date().getYear() - .dob.getYear()) ) ;

}
}
Person.prototype.getGender=function(){
var sex=this.sex,tolowerCase();
if( sex=='male' || sex=='m' )
return 'male';
else if( sex=='female' || sex=='f' )
rerturn 'female';
else
return 'god knows';

}

Person.prototype.getSpecies=function(){

return 'mammal';
}

In the above example, any instantiating code has only access to getFullName() , getAge() functions and ‘sex’ variable of person object.
But we allow ‘sex’ variable to be declared in two forms, long and short. And we return ‘sex’ in  long format with the help o getGender() helper function.

The above example can be broken down as following;

All variables except sex are private as they are constructor-scoped.
getFullName() and getAge() are privilaged function that have access to private variables name, surname and sex and thus are privileged/protected functions.

getGender() is a public function that has access to public variable sex.
the new getSpecies() is a public function that returns a value that’s entirely in its own scope.

So, there we have it, private, privileged and public access in javascript.

If we want to use module pattern the above can be modified to:

var Person= function(name ,surname, sex, dob){

this.sex=sex;
return{

getFullName:function(){

return surname+","+name;
},

getAge:function(){

return  (new Date().getYear() - this.dob.getYear()) ) ;

},
getSpecies:function(){

return 'mammal';
}

}
}

An example of creating static object with private and public scope:

var Human = function() {

var species = "mammal";

return {

alert: function(){alert(species);}

};
}();

Human.alert();//displays 'mammal'

Here the variable species is private.
The function gets executed immediately due to () at the end of the function definition and will return a public static method alert()

That sums up variable scopes in javascript.

I had used a function and a little bootstrapping trick to solve my problem with including files in PHP and I was happy with it.
But recently while working on a project that has a heavy javascript codebase, I started having trouble managing  all javascript file imports and all those script tags made pages look really ugly.
And also I really longed for java’s clas importing capabilities in javascript such that I could import a single file based on package name or I could import a whole package.
Since some of the pages I’m working requires over 15 script imports, some third-party libraries and some of our own files.
And importing scripts one by one suffers from latency problems as it increases page load time and can contribute to user annoyances.
So I longed for  java’s style of package and class  import and  decided to try my hand on writing some script to emulate imports based on packages.

I jsut used the  similar principle I used with php, but the problem is importing all files in a package isn’t possible in javascript because  javascript cannot read files form disk let alone iterate through a folder and read file content.
For that there was nothing I could do except use some server-side help.

here’s the js file code with  function for importing js file and js files contained in a package(folder).


//create namespace to aviod any present/ future  variable/object/funciton conflicts

if (typeof Anzaan == "undefined" || !Anzaan) {

Anzaan = {};
}

// array to hold all imported files so as not to import them twice
Anzaan.imports = [];

//server side URL for loading whole package content
Anzaan.packageLoaderURL='/Framework/packageLoader/';

//set the base folder for importing files
Anzaan.importBase='js/';

/**
* import a class using the java naming syntax for a class name.
*@param module the module to import
*@param  config object literal with two properties-
* config.packageLoaderURL - the server url for loading all files in a folder/package as a stream
* config.importBase - the base path for importing files
* use config only if necessary otherwise just modify Anzaan.packageLoaderURL and Anzaan.importBase
* of course you can use a different namespace or just use no namespace
*/
function Import(module, config) { //com.anzaan.framework.*

if(config){
if(config.packageLoaderURL)
Anzaan.packageLoaderURL=config.packageLoaderURL;

if(config.importBase)
Anzaan.importBase=config.importBase;
}

// if this import has already happened, don't bother,
if (Anzaan.imports[module] )
return ;

var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';

var src='';
var folders=module.split(".");
for(var i=0;i<folders.length;i++){

if(folders[i].indexOf('*') < 0)
src+=folders[i]+"/";
}
src=src.substring(0,src.length-1);//remove the last slash

if (module.indexOf('*') < 0) { //if not package import
src=Anzaan.importBase+src+".js";
script.src=src;

} else {
script.src=Anzaan.packageLoaderURL+"?package="+Anzaan.importBase+src;
}

head.appendChild(script);
Anzaan.imports[module] = module;
}

The config parameter is optional. It shouldn’t really be used unless you want to load files from locations other tahn the default location of your application and want to use other URL for loading script files.

Just modify the global variables Anzaan.packageLoaderURL and Anzaan.importBase to set the base path and the serverside URL for loading scripts in folder.
And of course rename Anzaan to something more meaningful for you.

Now on the server side here’s a simple php script that services the script file requests.
It loads contents from js flies that reside in folder specified by ‘package’  variable.

packageLoader.php
————————————————

<?php

$folder=$_GET["package"]; // get the package name

$dir = opendir($folder); //open directory

while (($file = readdir($dir)) !== false) {
if (strrpos($file, '.js')) {

echo file_get_contents($folder.'/'.$file); //printout the content of file

}
}

?>

and here’s a Java Servlet for doing the same thing:

JSPackageLoaderServlet.java

public class JSPackageLoaderServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/plain");
ServletOutputStream out = response.getOutputStream();

String packagePath = request.getParameter("package");

StringBuilder sb = new StringBuilder();

File myDir = new File(getServletContext().getRealPath(packagePath));
if (myDir.exists() && myDir.isDirectory()) {
File[] files = myDir.listFiles();
for (int y = 0; y < files.length; y++) {

String line = "";
BufferedReader in = new BufferedReader(new FileReader(files[y]));

while ((line = in.readLine()) != null) {
sb.append(line);

}

}

}
out.print(sb.toString());
}
}

Here’s the usage:

Import('anzaan.event.EvenHandler');

I really like the way java handles packages and the way it import classes based on package description.
Last year while  developing  a php  based CMS application, I faced shitload of trouble when it came to file including.
The problem was if I was making a normal page request, realtive URL’s  worked this way:
“../framework/service/DataService.php” and I used

inlcude_once("../framework/service/DataService.php")

to include a script.

But for accessing files using AJAX request for some weird reason I had to use single dot at the start of the path – instead :

inlcude_once("./framework/service/DataService.php").
<pre>

And as such I hit a kind of brick-wall because say I have a script that I need to accesss normally and via AJAX call, and the file included some other files, then what do i do?  I either have to put duplicate includes one with single dot and one with double dots at the begining and there was no way I was going to do such stupid things.
So I set out to solve this truely annoying problem.
I found some scripts in the net that did what I needed, almost….
I stole a script from the net and modified to suit my need. And since then my life has been so easy  you won’t believe.
with that scrip all I had to do was import files just the way I do in good old Java.
Even better I started using the same conventions as java.

So my imports look like this nowaday:

import("com.barahisolutions.cms.model.MenuPermission");

and if I want to import all files in a package I use the same comvention as java and
and use ‘*’ instead of file name.

import("com.barahisolutions.cms.model.*");

The code looks very neat and all my scripts are neatly organised in packages and life has been good ever since.

I divide all my scripts into two main folders. One classses to hold all project specific classes and one lib to hold all my library files. and i set these folders as include path using ini_set. I just have to do one inculde_once statement to include the file that has the import function and for the rest I use import function to include the required classes and files.

Of course its not all that simple. Because relative path to the classes and lib folders may be different depending on where the page/script that uses the import function is located. so instead of using ini_set in every place I want to use the import function, I used a little trick to solve the problem. I created one more function bootstrap and used it to calculate the relative path to the classes and lib folder before doing the ini_set.
From there all I have to do is include this file as a bootstrap for my applicaitons and I was ready use the import function for all my file include needs.

Here’s a stripped down version of the bootstrap file with the required funcitons.

bootstrap.php.

// array to keep track of all the already included files
$_imports = array();

// set the include path to the classes and lib folder
function bootstrap(){

//get the current script's path  relative to the applicaiton root folder
$rel_paths=explode('/',$_SERVER['SCRIPT_NAME']);
$path="";

// calculate the depth relative to the root folder
if(sizeof($rel_paths)>2){
for($i=2; $i< (sizeof($rel_paths)-1); $i++){
$path.="../";
}
}
// set the include path for our application's library and class files
ini_set('include_path', $path.'lib' . (DIRECTORY_SEPARATOR == '/' ? ':' : ';') .
$path.'classes'.(DIRECTORY_SEPARATOR == '/' ? ':' : ';') );

}
//just execute the funciton.
bootstrap();

/**
* Import a class using the java naming syntax for a class name.
* use * to include all files in a package
* @param string $name The name of the package to be imported
* @return void
*/

function import($import) {

// if this import has already happened, don't bother,
if (isset($_imports[$import]) )
return true;

// seperate import into a package and a class
$lastDot = strrpos($import, '.');
$class = $lastDot ? substr($import, $lastDot + 1) : $import;
$package = substr($_import, 0, $lastDot);

// create a folder path out of the package name
$folder = '' . ($package ? str_replace('.', '/', $package) : '');
$file = "$folder/$class.php";

if ($class != '*') {
// add the class and it's file location to the imports array

include_once($file);

} else {
// add all the classes from this package and their file location to the imports array
// first log the fact that this whole package was alread imported
$_imports["$package.*"] = 1;
$dir = opendir($folder);
while (($file = readdir($dir)) !== false) {
if (strrpos($file, '.php')) {
include_once("$folder/$file");
}
}
}

$_imports[$import] = $import;
}

That’s all that is required.

Now if you include this file, you  can simple use the import statement to import files that you need in your script.

samle usage: (its assumed that the file that actually uses MenuController class has already included the bootstrap.php file.
class MenuController.php

//import all the required classes
import('com.barahisolutions.cms.model.Menu');
import('com.barahisolutions.cms.dao.MenuDAO');
import('com.barahisolutions.cms.dao.UserPermissionDAO');
import('com.barahisolutions.framework.menu.VerticalMenu');

class MenuController {

// your business logic here

}

I have been reading a lot of posts on functional style programming and its amazing how things can be simplified  by using functional style of programming.
I find it hard to get my head around a lot of the functional programming concepts like Monad and Functors.

I seem to understand what they are when I read articles on them but I keep forgetting  in a few days time and I have to start over again. Perhaps the problem is that I haven’t been able to find use cases for them in my everyday programming which happens to be Java mostly and javascript and PHP occasionally.

Javascript is actually great for functional style programming as Functions are first-class citizens in Javascript.

But recently I actually managed to apply a little functional style programming to solve a recurring task in a java application I’m working on.

I need to transform  full-blown objects into combo-box friendly objects that  I can transform into JSON object and send it to webpages for use in forms.

Let’s take an example of a Person object. The drop-down box for person should have person’s id as value and
for display text  it should be a combination of surname and name.

For ease of use  for getting display text let’s   say we have  a Person class that  returns  name and surname combination  through its toString() method.

So, here’s our person class.

class Person {

private Integer id;
private String name;
private String surname;

// other interesting properties
..........

// getters and setter methods
........
.....
public String toString(){
return surname+", "+name;
}

}

And I have a class called Combovalue.

class Combovalue {

Integer value;
String  label;

public ComboValue(Integer value, String label){
this.value=value;
this.label=label;
}

// getters and setters
.........

}

Now that we necessary classes we need a handy way to map  a List of Person to a List of  comboValues.

For that  we’ll use a Utility class  for mapping and  an interface that has a method to do the transformation .

First is class ListMap which has a static method which takes a list of Type A  objects and returns a List of Type B object.
Well, for our example, the method should take a list of Person and return a List of ComboValue.

And we need an interface that has a method that takes an argument of type A and returns an object of type B.
Of course for our example it should take a person and return a ComboValue.

Here’s the interface that should be implemented to do the object transformation. Its not really transformation per se, but let’s call it that for simplicity.

It takes a parameter of type A and return a value of type B.

public interface ITransformer<A,B> {

public  B transform(A value);

}

Here’s the class whose static method  transforms  a list of type A to list of type B.

public  class ListMap {

public  static <A, B  > List<B>  map(  List<A> list, ITransformer<A,B> f ){

List<B> values= new ArrayList<B>();

for( int i=0; i<list.size();i++){

values.add( f.transform(list.get(i)) );

}
return values;
}

}

Now the usage:
Say we have a method in one of our classes in our application that handles this transformation.
It retrives a list of Person from database, passes it to the map() method of ListMap  serialize it into JSON and returns it.

Class JSONService{

XStream xstream ;

public JSONService(){
xstream = new XStream(new JettisonMappedXmlDriver());

}

public String getPersonCombos(){

//Assume we have a DAO class to retrieve data for us as List.

List<Person> list= pesonDao.getList();

//now the magic
List<ComboValue> combos = ListMap.map(list, new ITransformer<Person, ComboValue>() {

public ComboValue transform(Person person) {

return new ComboValue(person.getId(), person.toString());

}
});

//use XStream to serialize the list into JSON data.
return xstream.toXML(combos)

}

}

That’s alll the magic.

Transforming object by object can be a pain in the back-side. And the above technique is hardly that useful.
As we’ll end up creating methods for each domain to return combo data. And if we have say over 50 domain objects its a pain. And the above abstraction doesn’t ease our pain much

So, Say we define an interface  called Entity that has a single method.

public interface Entity {

public Integer getId();

}

Our person class and all our Domain classes implement this interface.

and our Person class becomes:


class Person implements Entity {

// the rest is unchanged
}

now lets modify our JSONService class.

Class JSONService{

XStream xstream ;

public JSONService(){
xstream = new XStream(new JettisonMappedXmlDriver());

}

public String getCombo( Class clazz){

//Assume we have a DAO factory with a method that returns a DAO object for a given class  to retrieve data for us as List.

List<clazz> list= DAOFactory.get(clazz).getList();

//Since all our domain classes implement Entity interface we pass in the argument type as Entity

List<ComboValue> combos = ListMap.map(list, new ITransformer<Entity, ComboValue>() {

public ComboValue transform(Entity entity) {

return new ComboValue(entity.getId(),  entity.toString());

}
});

//use XStream to serialize the list into JSON data.
return xstream.toXML(combos)

}

}

And now we have a nice way of transforming any entity into ComboValue object and returning it as JSON !

With Java Generics and a bit of inspiration from functional programming we have a reusable classes  that forms a component that will always obey the fundamental rule of  returning List of type B for any supplied list of type A.

Hardly rocket science, but useful piece of abstraction nevertheless.

I have been developing  vehicle-tracking system at work  for the past 1 year  or so and we use google for mapping.
Their enterprise license isn’t cheap and we didn’t  have  a viable alternative. I looked into OSM and found their coverage of Australia  not quite upto what we needed. We thought google has all  we needed and settled for it.
Few months back  a security company from Fiji showed keen  interest in buying our product as well as acting as our distributor in Fiji.
That’s where the problem began. You see, Google’s coverage of Fiji is next to nothing, no roads, just a few zoom levels and that was it.

Like all good customers, their patience on the two months timeframe i had given them to get Fiji’s map up and running ran out before the deadline  and last week I promised them I’ll show them at least something within a week.  And of course I had no clue how I was going to keep that promise.

I tried my hand on all sorts of things, I tried to configure and run our own map server to spit our tiles for Fiji,  I tried overlaying Fiji’s map data (which the client provided as shapefiles and it only covered a part of Fiji) on google map, I used good-old Firebug to debug OpenGTS’s
tracking website to see how they did their tracking using Open Street Map.

I had two plans.
Plan A: whatever it takes get the fiji’s map to load inside google map so I can use the existing javascript codebase that relies on google’s api.
Plan B: Use OSM for Fiji and google for rest,  and rewrite javascript code and use OpenLayer  javascript  library as the main library to allow users to switch ebtween providers easily.

Since Plan A is less painful, I tried to find ways to accomplish it. And Plan A  indeed turned out to be extremely painless if there’s such a thing.

I won’t go detail out all the things that failed, but I’ll definitely detail out what worked.

So, to all my fellow colleagues, cursing themselves in their little office cubicle, tearing their hair our trying to integrate OSM in google map,
here’s the solution:

We’ll create a new map type and point google to OSM ’s tile provider to get map tiles from OSM instead of fetching them from google’s servers.

Firstly, We’ll create a new GTileLayer for osm.


var osmTileLayer= new GTileLayer(new GCopyrightCollection(”"),1,17);
osmLayer.myLayers=’osm’;
osmLayer.myFormat=’image/jpeg’;

This is where we define a custom function for fetchng tiles from OSM map server.
This function will be user by google to fetch tiles from OSM everytime there’s request for tiles.


osmLayer.getTileUrl=function(a,b,c) {

return ‘http://c.tile.openstreetmap.org/’+b+’/'+a.x+’/'+a.y+’.png’;

}

Then we create a map type and add it to our good old google map.

var osmLayer=[osmTileLayer];
var OSMMap = new GMapType(osmLayer, G_SATELLITE_MAP.getProjection(), “OSM”, G_SATELLITE_MAP);
map.addMapType(OSMMap );

And that’s it !!
That’s all it takes to add Open Street Map as a new map type in google map.

here’s the complete code:
————————————————


var osmTileLayer= new GTileLayer(new GCopyrightCollection(”"),1,17);
osmLayer.myLayers=’osm’;
osmLayer.myFormat=’image/jpeg’;
osmLayer.getTileUrl=function(a,b,c) {

return ‘http://c.tile.openstreetmap.org/’+b+’/'+a.x+’/'+a.y+’.png’;

}

var osmLayer=[osmTileLayer];
var OSMMap = new GMapType(osmLayer, G_SATELLITE_MAP.getProjection(), “OSM”, G_SATELLITE_MAP);
map.addMapType(OSMMap );

—————————————————–

and here’s the final result:

s640x4801