Saturday, 9 May 2015

Page URLs With Breakaway Binding

Creating a basic implementation of the 'Pages' functionality gave me an idea for how to handle the dynamic URLs. First, URLs need a default suggestion. Second page authors need the option to create there own URL.

A page's URLs should default to the page's title. Spaces are replaced by hyphens. Diacritic marks will also need to be handled correctly. For now diacritic marks are on the backlog.

The URL property within the entity class is indirectly updated by changes to the title. The indirection allows for string replacement to happen. The string replacement ensure compliant URLs for each page.
    <h:inputText
        value="#{pageWriter.page.title}"
        id="title"
        >
        <f:ajax event="keyup"
                listener="#{pageWriter.updateURL()}"
                render="url"
                disabled="#{pageWriter.page.freestyleUrl}"
        />
        <f:ajax event="blur"
                disabled="!#{!pageWriter.page.freestyleUrl}"
        />
    </h:inputText>
When in freestyle mode fewer ajax requests are needed and therefore made by disabling/enabling the 'keyup' and 'blur' events respectively; as can be seen above.

As well as a default URL, it is nice to let authors create their own URL. Allowing a freestyle URL as well as a default URL involves a breakaway binding. This binding needs to be linked and unlinked at the author's will.

Here is the JSF checkbox's declaration.
    <h:selectBooleanCheckbox value="#{pageWriter.page.freestyleUrl}">
        <f:ajax
            event="change"
            render="url"
        />
    </h:selectBooleanCheckbox>
This checkbox switches the freestyle mode.

Here is the JSF URL input field's declaration.
    <h:inputText
        class="form-control" 
        id="url"
        disabled="#{!pageWriter.page.freestyleUrl}"
        value="#{pageWriter.page.url}"
    >
        <f:ajax 
            event="blur"
            render="url"
            execute="url"
            disabled="#{!pageWriter.page.freestyleUrl}"
        />
    </h:inputText>
The blog platform's page entities now have following fields:
  • url
  • freestyleURL
  • title
  • description
  • body (i.e. the content of the page)
  • created by etc. will come later.
Here is the end result:


Sunday, 3 May 2015

Displaying 'No Pages'

Using JSF's ui:fragment wrapper element is easy to display a 'No pages' message.  This is what I have came up with:

PageDAOBean.java
    [...]

    public int countAllPages(){
        Query q = em.createQuery("SELECT count(p) FROM Page p");
        Number result = (Number) q.getSingleResult();
        return result.intValue();
    }

    [...]


Pages.java (a JSF helper class)
    [...]

    public int countAll(){
        return pb.countAllPages();
    }

    [...]

pages.xhtml (JSF)
    [...]

    <ui:fragment rendered="#{pages.countAll() == 0}" >
        <h:outputText value="No pages yet. Why not add a page now?" />
    </ui:fragment>
                
    <ui:fragment rendered="#{pages.countAll() > 0}" >
        Doing now :-)
    </ui:fragment>

    [...]


I have been using this pattern on each of the list pages. If you know of a better way, feel free to let me know.

Saturday, 2 May 2015

Pages

Blogs normally have one or two pages. An example of what I mean by a page would be an 'about' page.

For now the blog platform's pages will include the following fields:
  • Title
  • Description
  • Body (i.e. the content of the page)
The view will be implemented on the front-end. The back-end will have the create, update, and delete functionality.

The URL pattern to retrieve a page in the front-end will be:

/<hyphenated page title>.html

I will use the page's title to query the database for the record. The .html URL ending will give the regex a solid stopping point. This will let the Java container 404 more invalid requests. Only the requests that match the pattern will hit the database. For the invalid requests that do hit the database something akin to this should do for raising a 404 page not found.

public void findPage(Long id) throws IOException {
        page = ps.getPageById(id);
        if (page == null) {
            FacesContext.getCurrentInstance().getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND, "page not found");
            FacesContext.getCurrentInstance().responseComplete();
        }
    }

Anyway, time to implement it.

Friday, 1 May 2015

Dashboard Started

The root of the back-end needs some sort of a dashboard. Not quite sure what to put on it yet but it cannot remain empty.

For now it looks like this.

I feel it will take shape soon.

The box on the left is the back-end's menu. The boxes on the right are summaries of implemented functionality.

Monday, 27 April 2015

Gmail as a mailto Handler in Google Chrome

For the time being I have added a message CRUD to the blog platform. I thought it would be nice to add a reply button using a mailto anchor. With the idea that the button launches the user's mail client.

<a href="mailto:test@test.com">Reply</a>

I wanted to test this out with my own email client Gmail. Gmail is cloud based. As Gmail is cloud based, there is no mailto handler entry in the PC's regulatory.

In Chrome all cloud base mail clients have an icon in the address bar. You click on the icon to register Gmail (or any other cloud mail client) as the mailto handler.


I did not know that until now.

Saturday, 25 April 2015

Why Java EE?

The modern Java EE framework takes care of many humdrum development tasks. Humdrum tasks like security can be taken care of for you, you simply can declare the what and how you wish to secure without polluting the business logic. Using only light XML files and annotations, the framework supercharges the development of many types of projects. Beyond the EE framework Java itself has state and state is truly awesome.

I am a Java software developer by profession too and I happen to be a big fan of the Java EE framework.

Thursday, 23 April 2015

I like Google Blogger but...

I like Google blogger but I cannot ever remember how to template it. Therefore I decided to begin making a blog platform that is independent of a template engine but has a similar functionality.

Here is a preview of its back-end with the work done so far.


As the egg cannot come before the chicken I am blogging about the creation of the new platform here, somewhat ironically, on Google Blogger.