Wednesday, July 3, 2013

Spring roo table column title

I wanted to add a tooltip to the columns in my table and I am using table.tagx by Spring Roo.
Because of the limitations in this tag you can't do it without changeing the tag library itself , I was able to do it and below are the steps to do that
  •  In the the table tag file where we are trying to add a new column to this table , under the td tag I defined the parameter title as  title ="${colTxt}".
  • But the colTxt is defined inside the td tag , so in order to allow the above to run we will have to define the colTxt filter before td like below

<c:set var="colTxt">
<spring:eval expression="item[column]" htmlEscape="false" />
</c:set>

Now the table will show tooltip/title for the columns in it.

Just in case if you need the whole tag file below is a copy paste of table.tagx file


 <jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" xmlns:spring="http://www.springframework.org/tags" xmlns:form="http://www.springframework.org/tags/form" xmlns:fmt="http://java.sun.com/jsp/jstl/fmt" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
  <jsp:directive.tag import="java.util.ArrayList" />
  <jsp:output omit-xml-declaration="yes" />
  <jsp:directive.attribute name="id" type="java.lang.String" required="true" rtexprvalue="true" description="The identifier for this tag (do not change!)" />
  <jsp:directive.attribute name="data" type="java.util.Collection" required="true" rtexprvalue="true" description="The collection to be displayed in the table" />
  <jsp:directive.attribute name="path" type="java.lang.String" required="true" rtexprvalue="true" description="Specify the URL path" />
  <jsp:directive.attribute name="typeIdFieldName" type="java.lang.String" required="false" rtexprvalue="true" description="The identifier field name for the type (defaults to 'id')" />
  <jsp:directive.attribute name="create" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Include 'create' link into table (default true)" />
  <jsp:directive.attribute name="update" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Include 'update' link into table (default true)" />
  <jsp:directive.attribute name="delete" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Include 'delete' link into table (default true)" />
  <jsp:directive.attribute name="render" type="java.lang.Boolean" required="false" rtexprvalue="true" description="Indicate if the contents of this tag and all enclosed tags should be rendered (default 'true')" />
  <jsp:directive.attribute name="z" type="java.lang.String" required="false" description="Used for checking if element has been modified (to recalculate simply provide empty string value)" />
  <c:if test="${empty render or render}">
    <c:set var="columnProperties" scope="request" />
    <c:set var="columnLabels" scope="request" />
    <c:set var="columnMaxLengths" scope="request" />
    <c:set var="columnTypes" scope="request" />
    <c:set var="columnDatePatterns" scope="request" />
    <jsp:doBody />
    <c:if test="${empty typeIdFieldName}">
      <c:set var="typeIdFieldName" value="id" />
    </c:if>
    <c:if test="${empty update}">
     <c:set var="update" value="true" />
     
    </c:if>
    <c:if test="${empty delete}">
    <!-- Added by Sajjad , if this is an admin user then he should be able to delete a best practice otherwise no      -->
<!--
       <c:choose>
   <c:when test="${user.admin}">
    <c:set var="delete" value="true" />
   </c:when>
   <c:otherwise>
    <c:set var="delete" value="true" />
   </c:otherwise>
    </c:choose>
 -->
        <c:set var="delete" value="true" />
     
<!--         <c:set var="delete" value="true" />  -->
    </c:if>
    <spring:message var="typeName" code="menu_item_${fn:toLowerCase(fn:split(id,'_')[fn:length(fn:split(id,'_')) - 1])}_new_label" htmlEscape="false" />
    <c:set var="lengths" value="${fn:split(columnMaxLengths, '&#9999;')}" scope="request" />
    <c:set var="types" value="${fn:split(columnTypes, '&#9999;')}" scope="request" />
    <c:set var="patterns" value="${fn:split(columnDatePatterns, '&#9999;')}" scope="request" />
    <spring:eval var="colCounter" expression="1" />
    <table>
      <thead>
        <tr>
          <c:forTokens items="${columnLabels}" delims="${'&#9999;'}" var="columnHeading">
            <th>
              <c:out value="${columnHeading}" />
              <spring:eval var="colCounter" expression="colCounter  + 1" />
            </th>
          </c:forTokens>
          <th></th>
          <c:if test="${update}">
            <th></th>
            <spring:eval var="colCounter" expression="colCounter  + 1" />
          </c:if>
          <c:if test="${delete}">
            <th></th>
            <spring:eval var="colCounter" expression="colCounter  + 1" />
          </c:if>
        </tr>
      </thead>
      <c:forEach items="${data}" var="item">
        <tr>
          <c:forTokens items="${columnProperties}" delims="${'&#9999;'}" var="column" varStatus="num">
            <c:set var="columnMaxLength" value="${lengths[num.count-1]}" />
            <c:set var="columnType" value="${types[num.count-1]}" />
            <c:set var="columnDatePattern" value="${patterns[num.count-1]}" />
            <c:set var="colTxt">
              <spring:eval expression="item[column]" htmlEscape="false" />
            </c:set>
          
            <td title="${colTxt}">
              <c:choose>
                <c:when test="${columnType eq 'date'}">
                  <spring:escapeBody>
                    <fmt:formatDate value="${item[column]}" pattern="${fn:escapeXml(columnDatePattern)}" var="colTxt" />
                  </spring:escapeBody>
                </c:when>
                <c:when test="${columnType eq 'calendar'}">
                  <spring:escapeBody>
                    <fmt:formatDate value="${item[column].time}" pattern="${fn:escapeXml(columnDatePattern)}" var="colTxt"/>
                  </spring:escapeBody>
                </c:when>
                <c:otherwise>
<!--                   <c:set var="colTxt">
                     <spring:eval expression="item[column]" htmlEscape="false" />
                   </c:set> -->
                </c:otherwise>
              </c:choose>
              <c:if test="${columnMaxLength ge 0}">
                <c:set value="${fn:substring(colTxt, 0, columnMaxLength)}" var="colTxt" />
              </c:if>
              <c:out value="${colTxt}" />
            </td>
          </c:forTokens>
          <c:set var="itemId"><spring:eval expression="item[typeIdFieldName]"/></c:set>
          <td class="utilbox">
            <spring:url value="${path}/${itemId}" var="show_form_url" />
            <spring:url value="/resources/images/show.png" var="show_image_url" />
            <spring:message arguments="${typeName}" code="entity_show" var="show_label" htmlEscape="false" />
            <a href="${show_form_url}" alt="${fn:escapeXml(show_label)}" title="${fn:escapeXml(show_label)}">
              <img alt="${fn:escapeXml(show_label)}" class="image" src="${show_image_url}" title="${fn:escapeXml(show_label)}" />
            </a>
          </td>
          <c:if test="${update}">
            <td class="utilbox">
            <!-- Added by sajjad if the user is not the creator of this record then don't show update link  -->
             <c:if test="${item.networkId == user.networkId}">
           
               <spring:url value="${path}/${itemId}" var="update_form_url">
                 <spring:param name="form" />
               </spring:url>
               <spring:url value="/resources/images/update.png" var="update_image_url" />
               <spring:message arguments="${typeName}" code="entity_update" var="update_label" htmlEscape="false" />
               <a href="${update_form_url}" alt="${fn:escapeXml(update_label)}" title="${fn:escapeXml(update_label)}">
                 <img alt="${fn:escapeXml(update_label)}" class="image" src="${update_image_url}" title="${fn:escapeXml(update_label)}" />
               </a>
           </c:if>   
            </td>
          </c:if>
          <c:if test="${delete}">
            <td class="utilbox">
              <!-- Added by sajjad if the user is not the creator of this record then don't show delete link  -->
              <c:if test="${item.networkId == user.networkId}">
               <spring:url value="${path}/${itemId}" var="delete_form_url" />
               <spring:url value="/resources/images/delete.png" var="delete_image_url" />
               <form:form action="${delete_form_url}" method="DELETE">
                 <spring:message arguments="${typeName}" code="entity_delete" var="delete_label" htmlEscape="false" />
                 <c:set var="delete_confirm_msg">
                   <spring:escapeBody javaScriptEscape="true">
                     <spring:message code="entity_delete_confirm" />
                   </spring:escapeBody>
                 </c:set>
                 <input alt="${fn:escapeXml(delete_label)}" class="image" src="${delete_image_url}" title="${fn:escapeXml(delete_label)}" type="image" value="${fn:escapeXml(delete_label)}" onclick="return confirm('${delete_confirm_msg}');" />
                 <c:if test="${not empty param.page}">
                   <input name="page" type="hidden" value="1" />
                 </c:if>
                 <c:if test="${not empty param.size}">
                   <input name="size" type="hidden" value="${fn:escapeXml(param.size)}" />
                 </c:if>
               </form:form>
            </c:if>  
            </td>
          </c:if>
        </tr>
      </c:forEach>
      <tr class="footer">
        <td colspan="${colCounter}">
          <c:if test="${empty create or create}">
            <span class="new">
              <spring:url value="${path}" var="create_url">
                <spring:param name="form" />
              </spring:url>
              <a href="${create_url}">
                <spring:url value="/resources/images/add.png" var="create_img_url" />
                <spring:message arguments="${typeName}" code="global_menu_new" var="add_message" htmlEscape="false" />
                <img alt="${fn:escapeXml(add_message)}" src="${create_img_url}" title="${fn:escapeXml(add_message)}" />
              </a>
            </span>
            <c:out value=" " />
          </c:if>
          <c:if test="${not empty maxPages}">
            <util:pagination maxPages="${maxPages}" page="${param.page}" size="${param.size}" />
          </c:if>
        </td>
      </tr>
    </table>
  </c:if>
</jsp:root>


Sunday, June 9, 2013

Printing web page with CCS applied

I was trying to print a web page which has only a normal HTML table in it and was getting crazy by the fact that whenever I print it using browser's ctrl+p button it was always ignoring all the CSS applied to the page.

I only found out in the below discussion that while including style sheet to your web page if you don't define any value for "media" attribute or if you define it as "media=screen" the CSS will not be applied when you try to take the print out of this page

http://stackoverflow.com/questions/6921573/is-there-no-difference-between-no-media-and-media-all-in-css-link/6921600#6921600

So for me the solution was to include my stylesheet as

<link rel="stylesheet" media="all" type="text/css" href="MyStyleSheet.css" />

Tuesday, May 28, 2013

Refresh a web page after given time


If you want your web page to refresh itself or redirect to someother page after a given time below is the way to do that without going into the pain of writing java script or anyother script

Below code will refresh/call the given page after 1200 seconds = 20 Mins.

  • Replace "URlToYourPage" to any page you want
  • The time is given as seconds as a first parameter in content attribute you can change it to whatever time you want

<meta http-equiv="refresh" content="1200;url=URlToYourPage">

Thursday, May 23, 2013

Multithreading - Scheduling tasks in Java

Java provides us a very good mechanism to schedule any tasks to be executed after a specified time for once or for each specified time period. Take a look at following example which will print a single line after given specified time. We can use this approach to write scheduled  routines who will refresh /recycle heavy resources in our applications (both server and client) .


public class TestTimerAndSechaduledTask {
      Timer timer = new Timer();
      //**interval after which the task will be executed
      int intervel=100;
      int period=100;
    
      private TestTimerAndSechaduledTask(){
            ScheduledClass scheduledClass = new ScheduledClass();
            //**will execute task after given intervel(when programe is executed)
            //**and then after after each time period specified
            timer.schedule(scheduledClass, intervel,period);
      }
    
      /**
       * Class which will be used by timer to eecute any task
       * @author sajjad.paracha
       *
       */
      public class ScheduledClass extends TimerTask{

            public void run() {
                  System.out.println("======>timer task executed    ");
            }
      }
      public static void main (String args[]){
            TestTimerAndSechaduledTask testTimerAndSechaduledTask =new TestTimerAndSechaduledTask();
      }
}


Example : I have defined such scheduled routine in a chating server which after specified time can get rid of extra useless resources on server such as logged out client sessions ,InputStreams,OupputStreams and client socket refrences.
 

Friday, April 5, 2013

org.springframework.expression.spel.SpelEvaluationException: EL1027E:(pos ): Indexing into type


Cause Of Exception

I faced this problem and found out that a getter method for a field in  my JPA Entity class was not following the proper java bean naming convention , when I was trying to fetch that column in my list page using below Spring MVC code

Code Causing Problem

< table:column label="My Property Label" property="myProperty" id="myclass_myProperty" z="user-managed" / >

Solution

The getter method was defined as getmyProperty() , notice "m" I changed it to getMyProperty() and was able to get rid of this exception.

There could be other causes but for me once I understood the problem the solution was very simple.

Sunday, March 10, 2013

cannot run eclipse on windows 7



I faced a problem today where my eclipse suddenly stopped working.
I was trying to run eclipse on my machine but I saw below alert  

"The Publisher could not be varified are you sure you want to run this software?
Publisher : Unknown Publisher"
 
I press ok and just nothing happens , eclipse didn’t start 
First of all try to run eclipsec.exe file in your eclipse installation folder if you see an error message like below

“Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/Object”

You won’t be able to run eclipse using the eclipse.exe file

Solution

Make sure that you have Jdk1.6 installed on your machine  , open the eclipse.ini file and add “-vm” argument to it like below

-vm

D:\software\jdk1.6.0_20\bin\javaw.exe

Please make sure you replace the path to your javaw.exe file according to the JDK installation at your machine. Now try to run eclipse and you should be able to run it now.

Note :
If you still see the above alert message when you try to start eclipse , try to copy your jdk folder from program files to your  D (or C) drive and update the “-vm” argument in eclipse.ini accordingly.

I was able to run eclipse after doing the above  steps. 

 

 

Friday, January 25, 2013

Benefits Of Going Towards SOA And Avoiding Software Silos


Many Organizations believe in shift of approach from creating individual applications to creating software baskets having many modules inside them integrated with each other.

This is good but I believe we have to take care of some good design principles here which if we don’t we will end up having big silos instead of small ones like we have now.
I believe SOA (Service Oriented Architecture) is the natural choice here, which simply says stop integrating and begin service enabling.
So what does Service enabling your software mean? It means your software should be publishing services which can then be used by any other application in your organization.
Service enabling basically means both , integration and flexibility or reusability.

To understand the concept let’s look into the traditional approach, whenever an application needs data from another application we do one of below things
1.       Directly communicate with the database and get the data from it.
Advantages:
·         It’s quick
·         No need to go through painful process of approvals
·         No need to wait for someone else to provide you the data or to develop something in his application you can use.
Disadvantages
·         Since you are not the owner of this data , and nobody knows that you are using it.The owner may change the data or it’s nature at any time without informing you and you will start getting all sort of errors in your application.
·         Let’s say you applied some business rules on the data which you discussed with the owner of that application you applied the rules in your application. Now in future if the rules change you will have to keep track of where else you are using the same rules and it would become night mare in some cases.
·         The ownership of business rules should be with a proper person/group , but in this case it’s not.
2.       Force the user to open the other application, login again and view the needed information.
a.       What if your user needs more information then shown in that page?
b.      What if they want to see less information? Or wants to add more stuff to the same page?
c.       In many cases you can’t just wait for the project lead of that application to create the page you want.

A much Better Approach:
While designing new Applications
We keep in our mind the service enabling principles , we can easily judge that this functionality of our application can be opened as a service or a web service. And then keep on creating those web services.
Of course we will need to maintain an inventory for those web services in future , but we can even continue  developing web services and then organize those in an inventory in future.
Having Service Inventory will help in easily finding an already written service , organize services in proper categories , finding and approaching the owner of the service. Making sure we don’t redo anything.

What If an application was already developed and now you need to integrate it with other applications
We need to be very careful in these cases , many times reinvention of the wheel (developing whole application from scratch) is not needed , instead you can decide to keep using whatever there is since user is already utilizing the software with almost no problems and do all the new enhancements in a way that where ever you see an integration between two system you don’t integrate you service enable it.
Which simply means instead of writing tightly coupled code between the two applications , write the integration as service open for other systems in future if needed.
See this is the difference , if we decide to go by creating bigger systems integrated all the related modules inside one basket and ignore the service enabling principle we will end up having bigger problems J.
However sometimes based on the requirement it can be decided to rewrite the whole application , and this brings us to another point if the same application was being developed in a way that it had reusable services , re writing the same application will be less painful because you can still reuse a lot of service made for old application.

The below pictures will help us understand Silos and what can happen in future J