Tuesday, May 9, 2017

Send Notification to Mobile device using java



This is mobile era. Most of portal have it's own mobile app. Many time we have situation to access portal data from mobile app using web services. Also need to notify mobile user by notification after processing request successfull. I spend more then a day to find the api and implement to achive this requirement. So here i am sharing how we can send mobile notification from java.


public static Response sendAndroidNotification(String deviceToken,String message,String title) {
       OkHttpClient client = new OkHttpClient();
       MediaType mediaType = MediaType.parse("application/json");
       JSONObject obj = new JSONObject();
       JSONObject msgObject = new JSONObject();
       msgObject.put("body", message);
       msgObject.put("title", title);
      // msgObject.put("icon", ANDROID_NOTIFICATION_ICON);
       msgObject.put("color", "#ff0000");

       obj.put("to", deviceToken);
       obj.put("notification",msgObject);

       RequestBody body = RequestBody.create(mediaType, obj.toString());
       Request request = new Request.Builder().url(FIRE_BASE_SERVER_URL).post(body)
               .addHeader("Content-Type", "application/json")
               .addHeader("Authorization", "key="+SERVER_KEY).build();

       Response response;
try {
response = client.newCall(request).execute();
_log.info( "Response : " + response.body().string());
return response;
} catch (IOException e) {
_log.info(e.getMessage(),e);
}

return null;
}


Note:
FIRE_BASE_SERVER_URL = https://fcm.googleapis.com/fcm/send
SERVER_KEY = Your app server key. you can find it from firebase portal.
deviceToken = Your deviceId is generate when you install app. you can find it from firebase portal.


This is very simple implementation to send notification to mobile device using java.

HTH!!

Thank you,
Ketan Savaliya

Tuesday, April 18, 2017

Liferay7: Changes in search container tag

Hi,

Liferay search container is the very common practice for liferay developer to display list of data. So, here is am writing something interesting for search container in liferay new version called Liferay7 or Liferay DXP.

Earlier or older version, we use or setup search container tag like below....

<liferay-ui:search-container
emptyResultsMessage="no-users-were-found"
iteratorURL="<%= iteratorURL %>"
>
          <liferay-ui:search-container-results
                   results="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()), searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()) %>"
                     total="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()).size() %>"
        />
       ...
       ...
       ...
</liferay-ui:search-container>


result & total attribute are configure in the liferay-ui:search-container-results tag. While in DXP version the total attribute move to the liferay-ui:search-container tag. So, we just have to
setup results attribute into liferay-ui:search-container-results. Below is the example how we use search container in liferay7 or DXP.


<liferay-ui:search-container
emptyResultsMessage="no-users-were-found"
iteratorURL="<%= iteratorURL %>"
 total="<%= UserLocalServiceUtil.getUserCount(themeDisplay.getUserId()) %>"
>
              <liferay-ui:search-container-results
                      results="<%= UserLocalServiceUtil.getUser(themeDisplay.getUserId()), searchContainer.getStart(), searchContainer.getEnd(), searchContainer.getOrderByComparator()) %>"            
             />
       ...
       ...
       ...
</liferay-ui:search-container>


total is moved from liferay-ui:search-container-results to liferay-ui:search-container tag is only change in search container in Liferay7 or DXP.

HTH!!

Regards,
Ketan Savaliya

Tuesday, March 28, 2017

Liferay7 : Removed the liferay-ui:journal-article Tag

 Hi,

In liferay till version 6.2, Very easy way to display journal article using `liferay-ui:journal-article`. The `liferay-ui:journal-article` tag has been removed.  Now, You should use the `liferay-ui:asset-display` tag instead. below is the piece example for how to use it.


**Example**

Old code:

    <liferay-ui:journal-article
        articleId="<%= article.getArticleId() %>"
    />

New code:   

 <liferay-ui:asset-display
            className="<%= JournalArticle.class.getName() %>"
            classPK="<%= journalArticle.getResourcePrimKey() %>"
            template="<%= AssetRenderer.TEMPLATE_FULL_CONTENT %>"
        />

HTH!!

Friday, July 29, 2016

Adding a Plugins Portlet to the Liferay Control Panel

A "Gotcha!" situation came up when you need to add your custom portlet to the control panel section.

Now the important thing is that as a developer you can actually decide which of the items on the left menu are shown. in simple words which section of liferay control panel is display.

Furthermore you can add any custom portlet to the desired place[portal, server, content] in the left menu and make it part of the Control Panel. Custom portlet's  liferay-portlet.xml need some entries to be place your custom portlet at desired place in Control Panel. That is look similar to this:

<control-panel-entry-category>portal</control-panel-entry-category> <control-panel-entry-weight>1.0</control-panel-entry-weight>

=> The first element determines to which section of the menu will the portlet be added
     This entry value may be very based on liferay version. here i have given control panel category for Liferay 6.1 and 6.2.
 
   1 )  Liferay 6.1 control panel category values of either my, content, portal, server to place the portlet in the area in the control panel.

  2) Liferay 6.2 control panel category values is extended from the previous version. possible value listed below.
      For My Account Section : my
      For Control Panel Section: users, sites, apps, configuration
      For Site Administration Section: site_administration.pages, site_administration.content, site_administration.users, site_administration.configuration
    
=> The second one determines the position. default value is 1.5.

After doing above configuration for your custom portlet, please deploy and check your control panel respected section.

You are done with what all you need to do, Enjoy!!

HTH!!


Thanks,
Ketan Savaliya

Thursday, July 28, 2016

Create liferay service builder portlet using maven tool

Hello LRExpert,


Now a days maven as building tool is common practice during development. Lifeary is also support maven tool for building liferay plugin. this article is for those who just start using maven or use maven first to create portlet.

Here i have given steps to create new liferay portlet using maven tool. Please follow simple 11 steps given below.

1) Install Maven
    Find specific liferay maven package for liferay version. here in my case i am using liferay-portal-     6.2-ce-ga6. so, supported liferay maven package is liferay-portal-maven-6.2-ce-ga6.zip. liferay         maven package is nothing but just a zip file. unzip that maven package whenever you want. Insall     maven plugin followed by below steps...

    - Open CMD windows
    - Go to directory location where you just unzip file (i.e liferay-portal-maven-6.2-ce-ga6.zip )
    - Execute command Ant istall
    - Wait untill it's finished execution.
    - You are done with you Liferay Maven Plugin Installation.

2) Go to location in CMD window where you need to create liferay maven portlet (suppose d:/lrproject/Source)
     
3) Execute command : mvn archetype:generate
    This will display list of maven project which you can create like...
     ...
     ...
    221: remote -> com.liferay.maven.archetypes:liferay-ext-archetype (Provides an archetype to create Liferay extensions.)
222: remote -> com.liferay.maven.archetypes:liferay-hook-archetype (Provides an archetype to create Liferay hooks.)
223: remote -> com.liferay.maven.archetypes:liferay-layouttpl-archetype (Provides an archetype to create Liferay layout templates.)
224: remote -> com.liferay.maven.archetypes:liferay-portlet-archetype (Provides an archetype to create Liferay portlets.)
225: remote -> com.liferay.maven.archetypes:liferay-portlet-icefaces-archetype (Provides an archetype to create Liferay ICEfaces portlets.)
226: remote -> com.liferay.maven.archetypes:liferay-portlet-jsf-archetype (Provides an archetype to create Liferay JSF portlets.)
227: remote -> com.liferay.maven.archetypes:liferay-portlet-liferay-faces-alloy-archetype (Provides an archetype to create Liferay Faces Alloy portlets.)
228: remote -> com.liferay.maven.archetypes:liferay-portlet-primefaces-archetype (Provides an archetype to create Liferay PrimeFaces portlets.)
229: remote -> com.liferay.maven.archetypes:liferay-portlet-richfaces-archetype (Provides an archetype to create Liferay RichFaces portlets.)
230: remote -> com.liferay.maven.archetypes:liferay-portlet-spring-mvc-archetype (Provides an archetype to create Liferay Spring MVC portlets.)
231: remote -> com.liferay.maven.archetypes:liferay-servicebuilder-archetype (Provides an archetype to create Liferay Service Builder portlets.)
232: remote -> com.liferay.maven.archetypes:liferay-theme-archetype (Provides an archetype to create Liferay themes.)
233: remote -> com.liferay.maven.archetypes:liferay-web-archetype (Provides an archetype to create Liferay webs.)
...
...
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 817: 231 + Enter [Find your liferay plugin type number from above list and enter it as input for above step]

4) After Enter above steps, it's display liferay version list like...
Choose com.liferay.maven.archetypes:liferay-servicebuilder-archetype version:
1: 6.1.0
2: 6.1.1
3: 6.1.2
4: 6.1.10
5: 6.1.20
6: 6.1.30
7: 6.1.30.1
8: 6.2.0-B1
9: 6.2.0-B2
10: 6.2.0-B3
11: 6.2.0-M5
12: 6.2.0-M6
13: 6.2.0-RC1
14: 6.2.0-RC2
15: 6.2.0-RC3
16: 6.2.0-RC4
17: 6.2.0-RC5
18: 6.2.0-ga1
19: 6.2.1
20: 6.2.2
21: 6.2.4
22: 6.2.5
23: 6.2.10.4
24: 6.2.10.5
25: 6.2.10.6
26: 6.2.10.7
27: 6.2.10.8
28: 6.2.10.9
29: 6.2.10.10
30: 6.2.10.11
31: 6.2.10.12
32: 6.2.10.13
33: 6.2.10.14
34: 6.2.10.15
35: 7.0.0-m1
36: 7.0.0-m2
Choose a number: 36: 19 + Enter [Select liferay version, here i am using 6.2.1 ]

5) Define value for property 'groupId': : com.liferay.portlet.search 
    [project groupid in pom.xml]

6) Define value for property 'artifactId': : my-search-portlet

7) Define value for property 'version':  1.0-SNAPSHOT: : SNAPSHOT-1.0.0 
[Optional...Enter your preferred project version. Other wise default 1.0-SNAPSHOT]

8) Define value for property 'package':  com.liferay.project.search: : com.liferay.project.search
[Optional...Enter your preferred package path. Other wise default com.liferay.project.search]

9) Confirm properties configuration:
     groupId: com.liferay.project.search
     artifactId: my-search-portlet
     version: SNAPSHOT-1.0.0
     package: com.liferay.project.search
     Y: : Y [Enter Y for yest, N for no]

10) your cmd screen final output is....
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: liferay-servicebuilder-archetype:6.2.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.liferay.project.search
[INFO] Parameter: artifactId, Value: my-search-portlet
[INFO] Parameter: version, Value: SNAPSHOT-1.0.0
[INFO] Parameter: package, Value: com.liferay.project.search
[INFO] Parameter: packageInPathFormat, Value: com/liferay/project/search
[INFO] Parameter: package, Value: com.liferay.project.search
[INFO] Parameter: version, Value: SNAPSHOT-1.0.0
[INFO] Parameter: groupId, Value: com.liferay.project.search
[INFO] Parameter: artifactId, Value: my-search-portlet
[INFO] project created from Archetype in dir: d:\lrproject\Source\my-search-portlet
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 25:01 min
[INFO] Finished at: 2016-07-28T19:29:27+05:30
[INFO] Final Memory: 16M/229M
[INFO] ------------------------------------------------------------------------

11) Enjoy you are Done. [your maven service builder portlet is create @ d:\lrproject\Source\my-search-portlet]


Veryfirst time you looks it's long process, but once you have more practice to create liferay plugin. then it's very easy to create maven project for liferay.

Hope this Help!!

Thanks,
Ketan Savaliya

Get value from Structure Field for Journal Article/Web Content in Liferay.


During your liferay development, many time you need get value from DDM Structure field. So, here i have piece of code which gives you value of structure field for journal article/ web content.

Suppose you have article content field data(from JournalArticle table) something like (Mention only one field for example.)...


<?xml version="1.0"?>

 <root available-locales="en_US" default-locale="en_US">
  <dynamic-element name="country" type="text" index-type="" index="0" instance-id="Page_Title">
  <dynamic-content language-id="en_US"><![CDATA[India]]></dynamic-content>
  </dynamic-element> 
 </root>


Then your code to get value of country field is like...

JournalArticle article = JournalArticleLocalServiceUtil.getArticle(...);

Document document = SAXReaderUtil.read(article.getContent());

Node node = document.selectSingleNode("/root/dynamic-element[@name='country']/dynamic-content");

String country = node.getText();


if you have multiple field then your code looks like...(Suppose your field have two times as repeated field )

1) Access first element value is....

Node node = document.selectSingleNode("/root/dynamic-element[@name='country' and @index='0']/dynamic-content");
String country = node.getText()


2) Access second element value is....

Node node = document.selectSingleNode("/root/dynamic-element[@name='country' and @index='1']/dynamic-content");
String country = node.getText()



HTH...!!!


Thanks,
Ketan Savaliya





Liferay MySQL Error 'option sql_select_limit=default' at line 1

Hello LRDeveloper,

Sometime when you setup your fresh tomcat even you start first time with you mysql db, you got error something like... You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 

This is nothing more complex but problem with your tomcat\lib\ext\mysql.jar file. just download update version of mysql connector jar and replace it with old tomcat\lib\ext\mysql.jar. make sure your new file have name like mysql.jar only.

HTH and save you valuable time.

NOTE: above solution is worked for me if i update mysql connector with "mysql-connector-java-5.1.6.jar" version and rename it with mysql.jar. Liferay version i am using 6.1.20.


Thank you!!


Thanks,
Ketan Savaliya