Saturday 17 October 2015

ThreadLocal in java

In real life we comes to a situation where we need a loged in user information or the Company Information in the implementation Classes. The way we are following is override the Implementation class in the HOOK.

In this we are passing the information into the parameter in the Method.

So many times we need to get the user information like username or company id .  In this case we will get themedisplay object from request and put information.

But Liferay has provided various thread local which will provide this information directly.
Following are some of the Examples

PrincipalThreadLocal : This thread local provide information regarding the Logged in User.
CompanyThreadLocal :  This thread local provides information about regarding company id.
LocaleThreadLocal : Provides locale of the current logged in user.





Monday 10 June 2013

Sonar for maven

It is the easiest way to configure sonar for the maven based project.

Following are the changes you need for analyzing code using sonar.

1> Create a database for the sonar like "sonar"

2>Make a new entry into the setting.xml into the .m2 directory.

<profiles>
        <profile>
            <id>sonar</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!-- EXAMPLE FOR MYSQL -->
                <sonar.jdbc.url>
                  jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8
                </sonar.jdbc.url>
                <sonar.jdbc.driverClassName>com.mysql.jdbc.Driver</sonar.jdbc.driverClassName>
                <sonar.jdbc.username>root</sonar.jdbc.username>
                <sonar.jdbc.password>root</sonar.jdbc.password>
                <sonar.host.url>
                  http://localhost:9000
                </sonar.host.url>
            </properties>
        </profile>
     </profiles>


3> Start the server again.
4> Now execute the command mvn sonar:sonar and hit "localhost:9000" from browser.
5> It's ready for the code analsis.

Please provide me comments if I have missed any thing to make it more helpful.




Friday 19 April 2013

Use of bean shell in liferay.


In the control panel -server-serveradministrator we have various tabs. In this tabs we have scripts.
In this we have various languages in the drop down. One language is bean shell.

It is used to execute the the java code directl. Like we want to get all the users or want to update user information we can do directly by bean shell.

Example: From the code snippet we are getting the user first name directly.

Maven based portlet in liferay.


Hi All,

Here we have a first example to make a maven based portlet in liferay. To make a maven based portlet first we need to configure maven.

1> Install the maven into your system

2> Set the System variable MAVEN_HOME like C:\Program Files\apache-maven-2.2.1

3> Add the path of the bin folder in to the Path into the system variable.  %MAVEN_HOME%\bin;

4> Now we need to make a maven repository where all the dependencies jar will be saved.To make this need to make a repository folder and need to give path of that folder into the c:/.m2/settings.xml

1:   <?xml version="1.0"?>  
2:      <settings>  
3:        <localRepository>D:/maven</localRepository>  
4:      </settings>  

   Here mention the central repository where all dependencies jar will be exist.

    In some case the .ms folder doesn,t exist you need to make it in the user folder using following folder

    md c:\Users\aalap.desai\.m2

5> Now mvn instalation has been completed now make a maven base portlet.

6>Form command prompt execute the command :mvn archetype:generate
    On the command prompt get the archtype for maven.

     

7> Select the option which kind of the liferay plugin package need to make.  Here we have a option to         make  hook,portlet,portlet with service builder etc.

8> Lets take a example to make a portlet have a use of service builder. For this we need to select the option           57.
9> It will ask for the version of the liferay you want to make the portet. Example: 6.1.20 I have selected 4
    then it will ask for the groupId,artifactId,snapshot-version.
10> After that it will     ask for conformation. Once you have confirm it will the folder with the name maven which has two folder one is maven-portlet and another is maven-portlet-service

11> In this manually need to amke the entry of

    abvoe the</projet> tag.

12> In maven-portlet src > main > webapps > web-inf > service.xml

13> Make the entity change you want to make you can make and execute the command -->> mvn liferay:build service.
    All the appropriate classes will be generated

14> Attached a sample maven portlet.
Maven sample portlet


Please let me know if any help is needed. Please let me know if I have missed anything.  

Thursday 14 March 2013

Migrate from ant to maven portlet.

1> First of all execute the command : mvn archetype:generate
    When execute this command the option comes to select which kind of the liferay plugin want to make. For example i want to make a service builder then the option withe in value liferay-service then while structure will be generated. In the option we have various option to make maven based hook,ext,portlets.

2> run the command : mvn eclipse:eclise
This will make the code into the project so we can directly import the code into the eclipse.

3> In the ant base portlet we have portlet structure as following.
bookmark-portlet
 |_ docroot
       |_css
       |_js
       |_jsp
       |_meta-inf
       |_web-inf
                |_lib
            |_src
            |_liferay-display.xml
            |_liferay-hook.xml
            |_liferay-plugin-package.properties
            |_liferay-portlet.xml
            |_portlet.xml
            |_web.xml

 |_ build.xml

 For the maven portlet we have following structure.

bookmark-portlet
 |_src
      |_main
            |_java (our java code comes here)
        |_ resource(our property files should come here)        
        |_web-apps
                  |_css
              |_js
              |_jsp
              |_web-inf
                        |_liferay-display.xml
                    |_liferay-hook.xml
                |_liferay-plugin-package.properties
                |_liferay-portlet.xml
                |_portlet.xml
                |_web.xml

For migration from ant portlet to the maven portlet put the all the java classes in to the main.java class. Put al the propertiesis and file of the meta-inf into the resource.


for the service buildes two folders has been generated like

bookmark
     |_bookmark-portlet(All the portlet related classes)
     |_bookmark-service-portlet(All the classes generated by the service builder.)
 |_pom.xml    

4> After doing this run the command mvn clean install it will meke the war into the target folder of the respective portlet. Take a portlet put it into the deploy folder of the tomcat.

Monday 10 September 2012

Scheduler in liferay Using cron scheduler



In so many cases we need to execute some again and again after some specific time of the duration. At that time scheduler makes our life easy.
In the liferay the have supporting quartz scheduler and the cron scheduler.

Here is the example to run the quartz scheduler in the lifrray.

First of we need to make the entry into the liferay-portlet.xml

<scheduler-entry>
            <scheduler-event-listener-class>com.aalap.mom.util.DailyTasksCreateScheduler</scheduler-event-listener-class>
            <trigger>
                  <cron>
                    <cron-trigger-value>     0 0/15 * 1/1 * ? *</cron-trigger-value>
                </cron> 
            </trigger>
<scheduler-entry>
In the above example the scheduler going to execute after every 15 mins. Ths site http://www.cronmaker.com . helps to get the expression to run the scheduler at the specific time.

If we want to run the scheduler at some time point like two am in the morning we have to give the expression 0 0 2 1/1 * ? * in the <cron-trigger-value> tag.

In the <scheduler-event-listener-class> we are defining the event listener class which define the logic of the scheduler.
In this we have DailyTasksCreateScheduler we have put our logic to create dailytasks.


public class DailyTasksCreateScheduler implements MessageListener {
    private static Log log = LogFactoryUtil.getLog("com.radisson.mom.util.DailyTasksCreateScheduler");
  
    public void receive(Message paramMessage) throws MessageListenerException {
 NEED TO PUT THE LOIC FOR THE SCHEDULER HERE.
}
}



Wednesday 27 June 2012

Display journal article in to the custom.jsp

Some time we need to display journal article into our custom jsp not from the web content display portlet.

Following is the code snippet for fetching the journal article and display it into the custom.jsp

1)Code for the controller of the portlet.
JournalArticle article = null;
JournalArticleDisplay display = null;

article = JournalArticleLocalServiceUtil.getArticleByUrlTitle(themedisplay.getScopeGroupId(), formatUrlTitle(name));

Function for getting the formatted url title.

private String formatUrlTitle(String name){
        return name.toLowerCase().replaceAll(" ","-");
    }

Then get the journal article display object  for the journal article

display = JournalArticleLocalServiceUtil.getArticleDisplay(themedisplay.getScopeGroupId(),article.getArticleId(),WebKeys.WINDOW_STATE,themedisplay.getLanguageId(),themedisplay);

set the article display object
request.setAttribute("post_of_the_week", article);



On the jsp side.

<%
JournalArticleDisplay display = (JournalArticleDisplay)renderRequest.getAttribute("post_of_the_week");

%>

<%=display.getContent() %>