poornerd

my thoughts on programming and other nerdy stuff

May 3, 2013
by poornerd
0 comments

Wysiwyg Editing of Play! Framework Templates with the Aloha Editor

aloha-savesOk, so it is only partial editing, but still this is an interesting step in the “no CMS” direction. What I have done is modified the example code which I posted on github last week for inline editing of HTML5 elements with the Aloha WYSIWYG Editor to actually save the new content back in to the Scala Template.

I find this useful for allowing non-techie colleagues to edit and optimize text and wordings in more content oriented templates. Of course it does directly modify the source, so in the case of a live website you would want to do this in the development environment and then commit, push, deploy the new version – hence “no CMS“.

In my example I have limited the frontend code to a block in the footer template. It is only rendered into the page if the following value is set to true in the application.conf:

play-aloha.admin=true

One of the non-elegant parts of the code is deciding which template file to look for the HTML to replace. Right now it is hard coded. I supposed the most pragmatic way would be to create a hashtable mapping URL’s to filenames.

Here you can see the changed HTML in the scala template:

git-diff

 

The code is in github here: https://github.com/poornerd/play-aloha

If you have read this far, you may as well follow me on Twitter:

April 26, 2013
by poornerd
5 Comments

How to integrate the Aloha Wysiwyg Editor with Play! Framework

UPDATE: code checked in on github: https://github.com/poornerd/play-aloha

play-alohaI am constantly needing to update static content in Templates in my Play! Framework project, so I decided to integrate the Aloha Editor, an HTML5 WYSIWYG Editor.

It is free to use, and they offer an example of integration an saving with a POST to the server on this page: http://www.aloha-editor.org/howto-store-data-with-aloha-editor-ajax.php

Basically all I had to do in the frontend was integrate the editor as described, and add a class “editable” to the element that should be editable.

<h1 class="editable" id="c1title">Example headline.</h1>

Then I had to create a POST action in my controller:

public static Result aloha() {
   final Map<String, String[]> values = request().body().asFormUrlEncoded();
   final String content = values.get("content")[0];
   final String contentId = values.get("contentId")[0];
   final String pageId = values.get("pageId")[0];
   System.out.println("pageId:" + pageId);
   System.out.println("contentId:" + contentId);
   System.out.println("Content:" + content);
   // save the content or whatever here...
   return ok();
}

Then I modified the aloha-save.js from the Aloha Example (which I copied from the above mentioned website), to post to the route for my action instead of save.php.

That was pretty much it. Make sure you add an id to the HTML elements so you can tell which on is which. The pageId ends up being the URL of the page, so that is not always helpful.

If you want to find out what I am doing with this, keep tuned to this blog or follow me on twitter!

If you have read this far, you may as well follow me on Twitter:

April 19, 2013
by poornerd
10 Comments

CRUD for Play! Framework 2

Update: yes, those are questions interspersed in the article… please answer in the comments!
crudOne of the main things I missed from Play 1.x is the CRUD Module. After reading posts on the forum about that one or more are in the works, I decided to check them out today.

I found a few projects – all which have key differences:

Am I missing any projects? Let me know in the comments…

Right now I like how easy it is to get fully automatic CRUD Screens using “play2-crud” from njin, but I wish that I was able to build on that more like in the CRUD Module from Play 1.x.

The other “play2-crud” from Hakan Dilek on the other hand has a good basis for writing your own CRUD screens and DAOs, but for a quick start it requires too much coding.

Which of these – from a generic perspective – is the best? Why? Let me know in the comments…

My wish list:

  • Parameterizeable (is that a word) Templates/Tags for the various screens (Search Form, List, Edit, View) which default to the standard view, but where you can specify which fields or columns to show.  These could be included in existing templates.
  • Bootstrap integration with the ability to use the projects Bootstrap / Theme for the automatic screens, as well as turn on/off Bootstrap.
  • Tools for “reverse database generation” of Beans (Ebeans, JPA, etc.)
  • Optional JSON interfaces, with Swagger
  • Optional Security with ?? and Deadbolt

What features do you want? Let me know in the comments…

Maybe it is time to join forces and work on CRUD module that many would find useful and use.

If you have read this far, you may as well follow me on Twitter:

April 8, 2013
by poornerd
0 comments

How deploy Play! Framework Apps with different configurations to Cloudbees

cloudbees-playHave you ever wanted to deploy the same Application with multiple configurations to Cloudbees, without changing your environment variables each time while using the command line?

I spent a ton of time trying to figure it out, and it ended up being a punctuation problem.

So basically, this is it:

However, if you want to script it, like I did, you want to be able to start the deployment process from the shell as well.

The documentation said:
cloudbees-deploy-config
Deploy a configuration of your app to a Run@Cloud app id. Arguments are:
(1) the base name of a conf file in your project's conf directory, defaulting to "application"/
(2) Optional. The application id to which this configuration should deploy. You can omit this
arg if you have either set cloudbees.applicationId in the config file from the first
arg or have set the project ID in your PlayProject.
Example usage: `> cloudbees-deploy-config live`, where live.conf exists in the project's conf/
directory and contains a key cloudbees.applicationId.

So I tried: play cloudbees-deploy-config xxxxx

That did not work – I got an error like this:
[success] Total time: 110 s, completed Mar 15, 2013 9:08:36 PM
[error] Not a valid command: xxxxx (similar: run)
[error] Expected '/'
[error] Expected ':'
[error] Not a valid key: xxxxx (similar: run)
[error] xxxxx

Thanks to help on stackoverflow, I discovered that it was just missing quotes!

play "cloudbees-deploy-config xxxxx"

Here is the link to the Stackoverflow thread if you want more details:  http://stackoverflow.com/questions/15441628/how-can-i-specify-which-application-conf-to-user-when-deploying-on-cloudbees

If you have read this far, you may as well follow me on Twitter:

March 27, 2013
by poornerd
0 comments

How to quickly start a Play! Framework Project and Deploy it in the Cloud

playstartjavaJumpstart your Play! Framework 2.1 Java project, by checking out my project scaffold from GitHub: https://github.com/poornerd/playstartjava

Demo running @cloudbees : http://playstartjava.poornerd.cloudbees.net/

The Project Includes:

Follow the instructions on the Cloudbees link after you signup and install the Cloudbees SDK and then just do this to deploy:
play cloudbees-deploy

Thanks to @jaliss for all his help with SecureSocial, and @_JamesWard for adding some libs to WebJars for me!

If you have read this far, you may as well follow me on Twitter:

March 5, 2013
by poornerd
1 Comment

How to switch back and forth between Play! Framework Versions

When I first read about the Play Version Manager I first thought it was some sort of tool for versioning.

When Play! Framework 2.1.0 came out, I finally realized that it was a tool for switching back and forth between various Play! Framework versions.

So download the Play Version Manager from github: https://github.com/kaiinkinen/pvm

You can do things like “pvm use 2.1.0” or “pvm use 2.0.4“.

Here are some of the supported commands:


$ pvm [tab][tab]
alias          copy-packages  help           
list           run            uninstall      
version        clear-cache    deactivate     
install        ls             unalias        
use

So, download it, and give it a try.

If you have read this far, you may as well follow me on Twitter:

March 1, 2013
by poornerd
3 Comments

Our Energy Saving Server Farm

Ok, so 3 VMWare ESXi Servers running 20-30 Virtual Machines only slightly qualifies as a “Server Farm”, but we will be saving a ton of electricity (sorry Stadtwerke München ) at SiteForce.

IMG_8056Our new setup:

  • 3 Mac Mini Server 2,0 GHz Quad Core with 16 GB each running VMWare ESXi 5.0 which peak at just over 60 Watt each (max 180 Watt) – all the old servers, including mail have been moved to virtual machines, except the Open Directory which is on a QNAP NAS Server
  • 2 QNAP NAS Servers (with iSCSI for VMWare) at about 45 Watt each  (second one is used for redundancy and backups)
  • Telephone System will be virtualized with Sipgate
  • 2 low-power, fanless Cisco Switches ( < 40 Watt each)
  • new Firewall Appliance (< 50 Watt)
  • 1 KVM
  • 1 older Mac Mini as a Backup Server
  • NO MORE air conditioning

IMG_8057We are moving from:

  • 2 x VMWare ESXi Servers which are Dual Quad Core XEON (2,2 GHz) with 24 GB RAM, and dual power supplies which peak at 650 Watt each (that is 4 power supplies x 650 Watt!!!)
  • Mac Pro Dual G5 – with Open Directory and our Mail Server (now on a virtual machine) – with (I am guessing) 450+ Watt 
  • Firewall – old PC with 400 Watt 
  • 2 x Quantum Snap Server ( 2 x 400+ Watt)
  • QNAP NAS (50 Watt)
  • Air Conditioner (300-500 Watt)
  • 2 x 5+ year old rack mount switches (200 Watt ?)
  • 2 x old PC’s as Servers (2 x 300 Watt?)
  • Siemens HiCom Telephone System ( 200 Watt?)

Of course we needed that air conditioning to keep the temp down in the room with all that equipment, and I am sure that some of it is energy efficient and did not always draw full power, but it is safe to say that we had anywhere from 3000-6000 Watt when you add the KVM and Monitor in the rack!

Aside from a few annoying hums from the QNAP NAS Servers, it could almost sit on the desk next to me.  Now we are talking something along the lines of 300-475 Watt!!!

That is a HUGE difference.  I realize that this is not a very scientific or accurate calculation, but the savings are obvious.

We have a little less CPU Power, but we were already overpowered for our necessary virtual machines, and if we need more CPU for a project now, we just go cloud.

If you want to know more about our MacMini Server VMWare ESXi setup with iSCSI then stay tuned, I’ll write that up one of these days, too.

If you have read this far, you may as well follow me on Twitter:

February 10, 2013
by poornerd
0 comments

Authenticating an Play! Framework Application via Oauth2 and Instagram using Secure Social

Screen Shot 2013-02-10 at 23.33.09In an ongoing quest to make sure that I can easily start a Play! Framework 2.0 project and connect with another popular Social Website via their API, I have just created an Instagram Provider for the Secure Social plugin for Play! Framework.   And it just works….

Many Thanks to Jorge (@jaliss) and his work on Secure Social: http://securesocial.ws/ – he has now added my XING and Foursquare Providers to the master branch – they should be in on the next release!

The pull request is in github here: https://github.com/jaliss/securesocial/pull/149

If you have read this far, you may as well follow me on Twitter:

February 7, 2013
by poornerd
5 Comments

My Upgrade from Play! Framework 2.0.4 to 2.1.0 went smooth except for these few things

Today I decided to upgrade from Play Framework 2.0.4 to the newly released 2.1.0.

I followed their helpful guide: http://www.playframework.com/documentation/2.1.0/Migration

Here is a list of the problems I had, and how I solved them:

  1. When it says change the import in the Build.scala to 
    import play.Project._

    Don’t forget the “play.

  2. It says that “the mainLang parameter for the project is not required anymore” – but it really means remove it.
  3. I also had to Google to figure how to update this dependency:
    "com.typesafe" %% "play-plugins-mailer" % "2.1-RC2"
  4. I also ended up having to update the dependencies for Deadbolt as well to the following:
    "be.objectify" %% "deadbolt-core" % "2.1-SNAPSHOT",
    "be.objectify" %% "deadbolt-java" % "2.1-SNAPSHOT",

Otherwise, follow the instructions – the rest was pretty straight forward.

I also used a pull request from the PVM (Play Version Manager) to download and switch Play Framework Versions.

If you have read this far, you may as well follow me on Twitter:

February 1, 2013
by poornerd
2 Comments

Using twitter4j with Play! Framework and Secure Social is this easy

Screen Shot 2013-02-01 at 14.54.45During yesterday’s personal Hackathon, I started a project which I might introduce here sometime.  But the coolest revelation was (again) how easy it was to get up and running.

  1. Create a new Play Project
  2. Add Secure Social and configure it for Twitter, and use the InMemoryUserService from the examples.  (all this is described here http://securesocial.ws/guide/getting-started.html and only takes a minute)
  3. Add the dependecy to twitter4j to your Build.scala like this:
    "org.twitter4j"% "twitter4j-core"% "3.0.3"
  4. Secure your controller action method to force the (Login) Authentication with Twitter.  Remember – because you are using the InMemoryUserService none of the Authentication data is stored – you will have to reconnect each time.
    @SecureSocial.SecuredAction
  5. I then added these standard methods to get the Authenticated Twitter User, Token, Secret and the twitter4J Connection: (The tokenSecret, token and current User are coming from the Secure Social Oauth1 Connection, and are used to authenticate the Twitter Connection.
    public static Twitter getTwitterInstance() {
    // The factory instance is re-useable and thread safe.
    TwitterFactory factory = new TwitterFactory();
    Twitter twitter = new TwitterFactory().getInstance();twitter.setOAuthConsumer(Play.application().configuration().getString("securesocial.twitter.consumerKey"), Play.application().configuration().getString("securesocial.twitter.consumerSecret"));
    twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(token(), tokenSecret());
    twitter.setOAuthAccessToken(accessToken);
    return twitter;
    }
    public static String tokenSecret() {
    String retval = "";
    scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();
    while (iterator.hasNext()) {
    OAuth1Info oAuth1Info = iterator.next();
    retval = oAuth1Info.secret();
    }
    return retval;
    }
    public static String token() {
    String retval = "";
    scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();
    while (iterator.hasNext()) {
    OAuth1Info oAuth1Info = iterator.next();
    retval = oAuth1Info.token();
    }
    return retval;
    }
    public static Identity getCurrentUser() {
    return (Identity) ctx().args.get(SecureSocial.USER_KEY);
    }
  6. Then I added some code in my Controller to list (for example) my Followers

    long cursor = -1;
    IDs ids;
    System.out.println("Listing following ids.");
    do {
    ids = twitter.getFriendsIDs(cursor);
    for (long id : ids.getIDs()) {
    twitter4j.User twitterUser = twitter.showUser(id);
    twitterUsers.put(twitterUser.getScreenName(), new TwitterUser(id,twitterUser));
    System.out.println(id);
    }
    } while ((cursor = ids.getNextCursor()) != 0);

Yes, that is it…

If you have read this far, you may as well follow me on Twitter: