Sunday, May 31, 2015

Log4j configuration for JUnit log-statements to be displayed in Eclipse Console.

Problem Statement: In Eclipse Console while running the JUnit you are getting following error:

log4j:WARN Please initialize the log4j system properly

Diagnosis: Log4J needs to be configured for this logging to work properly. Most likely the log4j.properties (or log4j.xml) file isn't in the root of your test classpath.

Solution:
1. Open "Run Configurations" dialog (click the "Run" menu and go to "Run Configurations")
2. Go to the "Classpath" tab
3. Select the "User Entries" and click the "Advanced" button on the right side.
4. Now select the "Add folder" radio button.
5. Select the "test/resources" folder


Thursday, May 28, 2015

Batch Script: Deploy to local Tomcat.

 :: deploy.bat  
 ::  
 :: Runs maven build commands on project folder,  
 :: copies generated binary to application server,  
 :: rename the binary to simpler name and  
 :: re-starts the application server.  
 ::  
 :: @author Ameya Aloni  
 ::  
 @ECHO OFF  
 @REM Declare the variables  
 SET project.home=<C:\path\to\project\directory>  
 SET app.server.home=<C:\path\to\application\server\home>  
 SET app.server.port=8282  
 SET app.context=<applicationcontext>  
 SET working.directory=%CD%  
 @REM Echoes information:  
 ECHO %project.home%  
 ECHO %app.server.home%  
 ECHO %app.server.port%  
 ECHO %app.context%  
 ECHO %working.directory%  
 ECHO.  
 CLS  
 CD %project.home%  
 ECHO Generating the war file...  
 CALL mvn clean package -Dmaven.test.skip=true  
 ECHO.  
 @REM Read war file name  
 FOR %%F IN (%project.home%\target\*.war) DO (  
   SET project.war.file.name=%%F  
   ECHO Found war file: %project.war.file.name%  
   GOTO rename  
 )  
 IF NOT EXIST %project.home%\target\*.war (  
   ECHO *** Aborting deployment as build has failed. ***  
   GOTO eof  
 )  
 :rename  
 SET renamed.project.war.file.name=%project.home%\target\%app.context%.war  
 ECHO Renaming war file: %project.war.file.name% to %renamed.project.war.file.name%  
 MOVE %project.war.file.name% %renamed.project.war.file.name%  
 ECHO.  
 @REM Cleanup existing app deployment.  
 IF NOT EXIST %app.server.home%\webapps\%app.context%.war (  
   GOTO deleteFolder  
 )  
 ECHO Removing war file: %app.server.home%\webapps\%app.context%.war  
 DEL %app.server.home%\webapps\%app.context%.war  
 :deleteFolder  
 IF NOT EXIST %app.server.home%\webapps\%app.context% (  
   GOTO copyWarFile  
 )  
 ECHO Removing war folder: %app.server.home%\webapps\%app.context%  
 RD /S /Q %app.server.home%\webapps\%app.context%  
 ECHO.  
 :copyWarFile  
 ECHO Copying war file: %renamed.project.war.file.name% to %app.server.home%\webapps  
 COPY %renamed.project.war.file.name% %app.server.home%\webapps  
 ECHO.  
 CD %app.server.home%\bin  
 ECHO Restarting application server...  
 NETSTAT -na | find "LISTENING" | find /C /I ":%app.server.port%" > NUL  
 IF ERRORLEVEL 1 (  
   ECHO Application server is stopped. Starting new instance...  
   ECHO.  
 ) ELSE (  
   ECHO Application server is running. Stopping existing instance...  
   CALL %app.server.home%\bin\shutdown.bat  
   ECHO.  
 )  
 CALL %app.server.home%\bin\startup.bat  
 :eof  
 @REM Back to working directory is:  
 CD %working.directory%  

Tuesday, August 5, 2014

Java Control Panel Issue

Java Control Panel for Java 6 64-bit or below looks like:

My machine had Java 6 64-bit as default running on Windows 7 64-bit. So when the Java Control Panel is opened from Control Panel -> Programs -> Java; it looks like above.
Java Control Panel for Java 7 32-bit looks like:

But I also had Java 7 which browser plugins were using and I wanted to Security Exception to the Site Lists and I was expecting above interface for Security Tab. I later figured out that it was because of multiple Java versions. Eventually I found that my 64-bit Java was installed in "Program Files" while 32-bit Java was installed in "Program Files (x86)". My browser being 32-bit was using 32-bit Java. So, I opened above Control Panel by running "Program Files (x86) -> java -> jre7 -> bin -> javacpl.exe" file.

Tuesday, January 15, 2013

Having Multiple Instances for Liferay Portlet

Use: For allowing to add more than one portlet instance on same page (where each portlet instance has uniquely generated portlet-id).

How to:
a. Update "liferay-portlet.xml" configuration as:
 <portlet>  
    ...  
   <instanceable>true</instanceable>  
   <!-- <scopeable>true</scopeable>  
   <ajaxable>false</ajaxable> -->  
    ...  
 </portlet>  
b. In JSP files (eg. view.jsp) append unique portlet-id to (AJAX) URL parameters as:
 "<portlet:namespace />myparam1=" + someparamval;  
c. In Java files get the unique portlet-id as:
 public String render(PortletConfig config, RenderRequest renderRequest, RenderResponse renderResponse) {  
   ...  
   System.out.println("Portlet Id:" + PortalUtil.getPortletId(renderRequest));  
   System.out.println("Portlet Namespace:" + PortalUtil.getPortletNamespace(PortalUtil.getPortletId(renderRequest)));  
   ...  
 }  
Points to be considered:
a. URL parameters may conflict
b. Any kind of refresh may affect all instances together
c. Preferences may conflict
d. If preference names are modified then existing Production user preference data will be lost
e. Overall performance may deteriorate

Effect:
a. If a normal URL parameter say "param1"  would need to be sent from jsp / js as "portletuniqueid_param1"
b. If a normal URL parameter say "param1"  would need to be read in java as "portletuniqueid_param1"

Friday, September 7, 2012

SQL & Liferay Dynamic Queries Compared...

SQL 1:
select * from mycustomtable where status='Pending' and userId=10122 order by modifiedDate desc, requestId asc;
DQ 1:
 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);  
 dynamicQuery.add(PropertyFactoryUtil.forName("status").eq("Pending");  
 dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(10122);  
 Order defaultOrder = OrderFactoryUtil.desc("modifiedDate");  
 Order secondOrder = OrderFactoryUtil.asc("requestId");  
 dynamicQuery.addOrder(defaultOrder);  
 dynamicQuery.addOrder(secondOrder);  

SQL 2:
select * from mycustomtable where (subject like '%Test Subject%') and ((create_date between '10/08/2012' and '10/09/2012') or (status='Pending'));
DQ 2:
 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);  
 Criterion criterion = null;  
 criterion = RestrictionsFactoryUtil.like("subject", StringPool.PERCENT + "Test Subject"+ StringPool.PERCENT);  
 criterion = RestrictionsFactoryUtil.and(criterion, RestrictionsFactoryUtil.between("create_date",10/08/2012,10/09/2012));  
 criterion = RestrictionsFactoryUtil.or(criterion, RestrictionsFactoryUtil.eq("status", "Pending"));  
 dynamicQuery.add(criterion);  

SQL 3:
select max(col1) from mycustomtable where status='Pending';
DQ 3:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);  
 dynamicQuery.add(PropertyFactoryUtil.forName("status").eq("Pending");  
 dynamicQuery.setProjection(ProjectionFactoryUtil.projectionList().add(Projections.max("col1"));

SQL 4:
 select count(entryId) from mycustomtable group by userId;
DQ 4:
 DynamicQuery query = DynamicQueryFactoryUtil.forClass(MyCustomTable.class)  
   .setProjection(ProjectionFactoryUtil.projectionList()  
   .add(ProjectionFactoryUtil.groupProperty("userId"))  
   .add(ProjectionFactoryUtil.count("entryId")));  

SQL 5:
 select count(*) from mycustomtable where status='Pending';
DQ 5:
 DynamicQuery query = DynamicQueryFactoryUtil.forClass(MyCustomTable.class)  
   .add(Property.forName("status").eq('Pending'))  
   .setProjection(Projections.rowCount());  

SQL 6:
 select msct.somecol from myfirstcustomtable mfct, mysecondcustomtable msct  
   where mfct.matchcol1=msct.matchcol1 and mfct.status='Pending'  
   order by othercol desc;  
DQ 6:
 DynamicQuery dq1 = DynamicQueryFactoryUtil.forClass(  
   MyFirstCustomTable.class, "mfct")  
   .setProjection(ProjectionFactoryUtil.property("somecol"))  
   .add(PropertyFactoryUtil.forName("mfct.matchcol1").eqProperty("msct.matchcol1"))  
   .add(PropertyFactoryUtil.forName("mfct.status").eq("Pending"));  
 DynamicQuery dq2 = DynamicQueryFactoryUtil.forClass(  
   MySecondCustomTable.class, "msct")  
   .add(PropertyFactoryUtil.forName("msct.msctPK").in(dq1))  
   .addOrder(OrderFactoryUtil.desc("msct.othercol"));  

Steps for building a custom SQL finder:
1. Create a new finder called CompetenceLevelFinderImpl in the /generated/service/persistence directory.
2. Let this class extend BasePersistence.
3. Now do a 'build-service' on the project.
4. The ServiceBuilder autogenerates the following two extra files : CompetenceLevelFinder.java and CompetenceLevelFinderUtil.java
5. Now open the CompetenceLevelFinderImpl.java file and let this class extend the CompetenceLevelPersistenceImpl class and implement CompetenceLevelFinder. (Assumed that the CompetenceLevel entity is defined in the service.xml and that the needed classes are also autogenerated by ServiceBuilder.)
6. Now add the needed functionality to the CompetenceLevelFinderImpl class and do a build-service.
7. You can now use the added functionality.

Useful Links:
http://www.liferay.com/community/wiki/-/wiki/Main/Service+Builder+Finders

Sunday, January 22, 2012

CSS / HTML - drawing a rectangle with round corners

This was my first encounter with CSS3 and it was pretty interesting too.

Following
CSS / HTML code-snippet helps drawing a rectangle with round corners:

Code:


Output:



The key to make it browser independent is to make it work on older versions of Internet Explorer. For this you will need an .htc file. This file contains a bunch of javascript functions that will ensure round edges to an html element like div or td.