Tuesday, August 30, 2011

Embedding Images to a Tapestry5 page very easily

OK I'll admit that the title might be problem but then again that's up to you.

Here's the thing, adding dynamic images in Tapestry5 templates is pie. You can read up on it here. But that seems to be best way if you are dealing with a file system or uploaded files. But what if the source is JSON data and doesn't need to be manipulated? Where we'd rather just stick it in directly into the template and display it.

Apparently this is just as easy as pie. First off, I'm dealing with a Base64 string as representation of the image within the JSON data. The image format is JPG, by the way for the just curious. Fortunately, Firefox and Chrome like Base64 strings. Then we can basically do this.

<img src="data:image/jpg;base64,iVBORw0KGgoAAAANS..." />

So what we need then is a webservice that pulls the JSON, process it a bit and pass it into the template.

public interface ICustomerService {

    public JsonObject getCustomerFromWebService(String id);
    
    public Customer getCustomer(String id);
        
    public Map<String, String> getCustomerHistory(String id, String code);
}

The ICustomerService is quite straight-forward. The implementation is where it gets interesting.

//Don't forget to add this into Tapestry5's service registry using binder.bind()    
    @Inject
    private Messages messages;

    JsonSystem system = StandardConfig.createSystem();
    
    public Customer getCustomer(String id) {
        JsonObject obj = getCustomeFromWebService(id);
        Customer customer = new Customer();
        // various gets;
        // lets skip to the interesting parts
        customer.setPhotoB64(obj.getString("ImageB64"));
        return customer;
    }

    public JsonObject getCustomerFromWebService(String id) {
        StringBuilder url = new StringBuilder( messages.get("localhost.mywebservice") );
        url.append(id);
        JsonObject object = system.get(url.toString()).asObject();
        return object;
    }


The first thing here is getting the URL of my web service - which I'm hosting locally. My dilemma was how to do this not in code so if the web service URL changes, I don't have to recompile. My answer was to use the Tapestry5 global properties via the messages catalog. All Tapestry5 web applications have this properties file in the WEB-INF folder. All you need to do is just @Inject the org.apache.tapestry5.ioc.Messages object and then you have now access.

The next bit is using ItemScript. ItemScript is very good JSON toolkit. I think you have noticed that getting JSON data with ItemScript is quite easy. Just create a JsonSystem object and then supply the URL. You should then get a JsonObject which now you can parse for data.

The final parts is just basically now use the new service. We'll start with the Index.java file.

@Inject
    private ICustomerService iCustomerService;
    
    public Customer getCustomer(){
        //let's try out our web service
        return iCustomerService.getCustomer("2033405");
    }
.....


The Index.tml file is just as easy. Add this right after the ${message:greeting} line.

<p>Test Customer</p>
<p> ${Customer.firstname}, ${Customer.lastname}</p>
<img src="data:image/jpg;base64,${Customer.photoB64}" />


Here is a screenshot.
The guy in the image is Aristotle "yoyong" Ucab. He's the guy who wrote the web service.

I don't really know where the heck this photo was taken.

Anyway, have fun with this.

Sunday, August 28, 2011

Drupal Themes and Favicons

A favicon also known as a shortcut icon, Web site icon, URL icon, or bookmark icon, is a file containing one or more small icons, most commonly 16×16 pixels, associated with a particular Web site or Web page. Its that small image that you see on you Firefox tab along with the name of the webpage.

Clients like favicons but making one can be easy or hard, it depends on your approach. If you're old school, then you can use MS-Paint or GIMP or if you're like me,I cheat. I use a web favicon generator. I use favicon.ico generator. Most amazing thing about favicon.cc is that its free. FREE!

You have a favicon, you'll need to copy it to your theme folder. Make sure that is on the same folder as your .info file. Don't put in a sub folder like say img or css.

You'll need to edit your theme .info file. Here is a excerpt of mine theme .info file.

; ------- Declare default theme information

name        = LGR 960.gs
description = Omega Sub-Theme for LGR
screenshot = screenshot.png
core       = 6.x
base theme = omega

; ------- Declare default theme features
features[] = logo
features[] = name
features[] = slogan
features[] = mission
features[] = node_user_picture
features[] = comment_user_picture
features[] = search
features[] = favicon
features[] = primary_links
features[] = secondary_links

The thing here is the theme features section where it says feature[] = favicon. This basically says to Drupal to look for your favicon on the same level folder where the .info is placed and use it. Normally, this is automatic to Drupal themes especially if you subtheme one of its many primary themes like Zen or Omega.


Monday, August 22, 2011

ucertify.com's prepkit for Cisco640-802

I was asked to review Ucertify.com's prepkit for Cisco640-802. I was intrigued. I immediately thought about comparing it to the other Cisco prepkits that I already know like Testking and Pass4sure. I was caught by surprise that Ucertify's prepkit was very different than that of the other guys.

The other guys had a question dump packed into the database. Ideally, this would work for you if you are already experienced in the field of computer networking and need to get that certification fast. But this would be a disadvantage to someone without the training or experience. Sure they can memorize by rote the answers to the questions but I doubt that they understood anything. This is where Ucertify's prepkit comes in.

Enhance your understanding
The prepkit is like this whole ecosystem of learning Cisco in a single piece of software. Not just Q and A database that you can browse.

The prepkit has this "Enhance your understanding" section which I find very nice. It complements the study helper (AKA study guide) that comes with the prepkit.

The prepkit has this "Create a fixed time test" feature. I thought it was a hooky feature until I tried it out. It basically allows you to create limited time test. Say you have a break for 15-minutes, you can spin this up and get a custom 15-minute test which can be in "Test mode" or "Learn mode".

You see, Ucertify prepkit test has modes like "Test mode" and "Learn mode" when its time to do a test. The difference being that "Test mode" lets me see the feedback at the end of the test while "Learn mode" provides me with the feedback immediately after each answer.

So far, I am very impress with Ucertify's Cisco prepkit and I haven't tried out every feature out yet. I would certainly recommend this to someone who wants that Cisco certification with the actual skills to back them up. Let's face it, how many people do you think you can convince when all you can do is talk and not the walk?

Friday, August 19, 2011

Rediscovering blog syndication on Facebook

This blog of mine always had a feed but I didn't use it much other than that odd programming experiment like trying out that new library for parsing XML data. That was until someone at groups.drupal.org mention that you can add your blog post into your Facebook wall automatically. You betcha the light bulb went TING!

Someone needs to update that Help page!
So jumped to Facebook's help center to read up on using this feature. You should get the sense that you'll need to use the "Notes" application for this. But that when things started to go a little bit sideways.

There is no "Import a blog" link in mine. WTF! But no worries, I did figure out what the "Edit import settings" is all about. And the link is not on the right side of the page.

With that solved, now I can automatically publish my post into my Facebook wall once I save it. Gravy!


Wednesday, August 17, 2011

And then one day....CM7 came

Yes you read that right! CyanogenMod 7.1.0 can now be installed on your Cherry Mobile MagnumHD and I have pictures to prove it!

Well...its not exactly my phone as of yet. I pulled that one from the XDA-Developers site. But it shows that you can now install Cyanogen on your Magnum. The CSL-Mi410 is basically the same phone as the CM-MagnumHD.

For the full set of images proving this fact go here.

As for me, I'm waiting for a bit. The images were put up on August 13, 4 days before this post. I'm taking a see and wait approach.

I'll probably try it out this weekend or something.

*BIG GRIN*

Monday, August 1, 2011

Maven and db4o

db4o (database for objects) is an embeddable open source object database for Java and .NET developers. It is developed, commercially licensed and supported by Versant.

I use db4o on a lot of personal projects. I even have started using db4o on a few of my Android projects. But I keep looking up that Maven snippet for adding db4o. So that snippet goes something like this:

<project>
  <repositories>
    <repository>
      <id>source.db4o</id>
      <url>http://source.db4o.com/maven/</url>
    </repository>
  </repositories>
  ...
  <dependencies>
    <dependency>
      <groupid>com.db4o</groupid>
      <artifactid>db4o-core-java5</artifactid>
      <version>8.1-SNAPSHOT</version>
    </dependency>
    ...
  </dependencies>
</project>

By adding this snippet to my Maven POM file. I should be adding the latest db4o jars to my project. db4o is currently right now on its 8th version.

Thanks to Pascal Thivent for this. Hey, you gotta give credit where credit is due. Edit(5/1/2012): changed repository url and artifactId