Sunday, 26 November 2017

Working with Dynamic Query In Liferay

Introduction of Dynamic query:-

Liferay provides several ways by which we can retrieve data from database. One of them is dynamic query. You can easily fire complex query using dynamic query and it will reduce overhead of creating custom finder methods


How to build Dynamic Query:


DynamicQuery dynamicQuery= DynamicQueryFactoryUtil.forClass(Entity_Name.class, PortalClassLoaderUtil.getClassLoader());
OR
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Entity_Name.class, PortletClassLoaderUtil.getClassLoader());


Note 1: If you working with dynamic query inside your custom portlet and trying to access liferay core entity (i.e, database) then use Portal Class Loader.

 Example like :
Dealing with Liferay's built in entities like Users, Organizations, Assets, AssetCategories 
As DynamicQuery API looks for current classloader, for access, but as you are dealing with Portal level
class [User.class], you have to specify portalClassLoader as below.

 ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();  
 DynamicQuery query =DynamicQueryFactoryUtil.forClass(User.class, portalClassLoader);  

-
Note 2: Dealing with custom entity within portlet.

DynamicQuery dynamicQuery=DynamicQueryFactoryUtil.forClass(Application.class);

example1:-
1.SELECT * FROM User_ WHERE lastName='Bloggs'
Dynamic Query:
DynamicQuer  dynamicQuery  = DynamicQueryFactoryUtil.forClass(User.class, PortalClassLoaderUtil.getClassLoader());
dynamicQuery.add(PropertyFactoryUtil.forName("lastName").eq("Bloggs"));
List<User> userList = UserLocalServiceUtil.dynamicQuery(dynamicQuery);
Example2:-
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(MyCustomTable.class);
dynamicQuery.add(PropertyFactoryUtil.forName("status").eq("Pending")
dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(10122);

Above query will search in table MycustomTable for records which has status as

Pending and userId as 10122. If you want to sort your records in particular order that also you can do.  Example3:- 2. SELECT * FROM User_ WHERE lastName like 'ord%' Dynamic Query: DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(User.class,
PortalClassLoaderUtil.getClassLoader()); dynamicQuery.add(PropertyFactoryUtil.forName("lastName").like("ord%")); List<User> userList = UserLocalServiceUtil.dynamicQuery(dynamicQuery);
About Dynamic Query: I am assuming that you are aware of Liferay Service Builder and
you have basic understanding of persisting data by service builder tool.
Consider that you have developed your portlet and you have persisted
a lot many records in the database. You are a java developer and
you don't have much knowledge on queering database.
Liferay Dynamic Query is the rescue in this case.
Liferay provides API to query the database just like writing java program.
You don't need to bother about low level database query.
One point to remember here that Dynamic Query is only to fetch data. Below are the API we will be working with to write dynamic query. com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil com.liferay.portal.kernel.dao.orm.DynamicQuery com.liferay.portal.kernel.dao.orm.Criterion com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil com.liferay.portal.kernel.dao.orm.Projection com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil com.liferay.portal.kernel.dao.orm.ProjectionList com.liferay.portal.kernel.dao.orm.OrderFactoryUtil

Monday, 18 September 2017

Get value from hidden field - jQuery

How can I get the value using jQuery?

<input type="hidden" value="" id='h_v' class='h_v'>

Use val() instead of text() var hv = $('#h_v').val(); alert(hv);

Wednesday, 6 September 2017

Check Null with ternary operator

At its most basic, the ternary operator, also known as the conditional operator, can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right hand side of Java statements.


Example:-

<c:out value="<%=item.getMiddleName()!=null?item.getFirstName()+' '+item.getMiddleName()+' '+item.getLastName():item.getFirstName()+' '+item.getLastName()%>"></c:out>

Thursday, 17 August 2017

Clear form input fields except radio/select menu values?

Clear form input fields except radio/select menu values?


First way  ---


$(':input','#myform').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');

                          (OR)


$('#FormIDReset').trigger('click');
<button class=" " type="reset" id="FormIDReset">Clear</button>


Wednesday, 4 January 2017

JQuery DataTable Sample Example

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css"/>

<div>
<table id="regionTable" class="table table-hover table-striped table-bordered">
 <thead>
     <tr>
          <th>OrganizationId</th>
         <th>Parent OrganizationId</th>
         <th>Type</th>
          <th>Code</th>
         <th>Action</th>
     </tr>
 </thead>
 <tbody>
 
    <tr>
     <td>fhf</td>
     <td>fh</td>
     <td>fhf</td>
     <td>fh</td>
     <td><button class="edit" >Edit File</button>
      <a href="#"> <button class="remove" >Delete File</button></a></td>
   
    </tr>
   
   
 
 

 </tbody>
</table>
</div>


<script type="text/javascript">
$(document).ready(function() {
    $('#regionTable').DataTable();
} );
</script>

Sunday, 11 September 2016

JSON Serialization in Liferay

Many times we have requirement to convert Liferay service response or any list into JSON object. Most of the time we are doing this manually.

JSONArray jsonArr = JSONFactoryUtil.createJSONArray();
for (User user : userList)
{
JSONObject json = JSONFactoryUtil.createJSONObject();
json.put("name", user.getFullName());
json.put("id", user.getUserId());
jsonArr.put(json);
}
return jsonArr.toString();



There are other ways as well by which we can convert List into JSON string.
Service builder has support for JSON  serialization,too. The json-enabled attribute of entity tag specifies whether or not the entity should be annotated for JSONserialization.
By default, if the remote-service value is true, then thejson-enabled value is true.
Column tag has the same attribute, json-enabled.
It specifies whether or not the column should be annotated for JSON serialization. By default, if the json-enabled value in the entity element is true, then the json-enabled value in the column element is true.


<entity name="TestJSON" table="json_test" local-service="true" remote-service="false" json-enabled="true">
<!-- PK fields -->
<column name="keyId" type="long" primary="true" />
<!-- Audit fields -->
<column name="companyId" type="long" />
<column name="groupId" type="long" />
<!-- Other fields -->
<column name="firstName" type="String" json-enabled="true" />
<column name="lastName" type="String" json-enabled="true"/>
<column name="email" type="String" json-enabled="false"/>
<!-- Finder methods -->
</entity>

json enabled is true for enttity TestJson so all the column tags will have by default json-enabled as true.
In case if you want to exclude any property to not to appear when you serialize then you can set json-enabled as false. For email column json-enabled is set to false.



Now you can easily convert your list returned using Liferay services into JSON format.

JSONSerializer jsonSerializer = JSONFactoryUtil.createJSONSerializer();
String json = jsonSerializer.serialize(list);



Same way you can use serialize OOB data as well like user.

 List<User> userList = UserLocalServiceUtil.getUsers(-1, -1);
 String json = JSONFactoryUtil.looseSerialize(userList);

 By default collections are excluded from serialization . You can include them using below way.
 String json  = JSONFactoryUtil.looseSerialize(userList,"organizations")

 Covert List to JSONArray:-

Might be you won't be aware that you can't use existing portal *JsonSerializer.java class in Liferay plugin portlets. So if you want to get JsonArray response of existing liferay service then you can use below code :


import net.sf.json.JSONArray;

List myList = UserLocalserviceUtil.getUsers();
JSONArray jsonArray = JSONArray.fromObject(MyList);



Monday, 11 July 2016

New Liferay Interview Questions

1) What are the various ways in which you can customize the default behavior of Liferay that ships to you?
2) What is the difference between a Liferay Hook and Liferay EXT?
3) Suppose I want to customize the behavior of Calendar portal that comes out of the box with Liferay, how can I do that?
4) How will you configure Liferay to authenticate users from LDAP?
5) What is inter portlet communication and how can that be achieved?
6) What is the difference between view and edit modes of a portlet?
7) What is service.xml and how will you use Liferay service builder?
8 ) Suppose I have a portlet written for Liferay 5.2.3 and I want to run it on Liferay 6, how can I do that? What if I get some spring and hibernate related exceptions with Liferay 6?
9) There is a requirement from the client that out of 4 portlets that you have written only 2 should be visible to a particular category of users. How will you achieve that? What if some fields in the remaining 2 portlets have also to be controlled based on user role?
10) What is the purpose of portal-ext.properties?
11) What steps will you follow to change the layout and theme of the portal pages?
12) Suppose I want to change the background color of the footer in Liferay, what steps should I follow?
13) Which JSR is being implemented by Liferay?
14) What improvements would you like to see in Liferay 6 portal?
15) There are 1000 users using the portlet developed by you, how will you take care of transaction management.
16) A client has two different portal applications for Sales and HR department. The client wants to run these portal applications on same server but with separate URL’s like http://:/sales and http://:/hr. How will you achieve that?
17) What are the minimum Java methods one needs to override in order to write a portlet?
18) How will you make the user to go from one JSP to another JSP page in a portlet?
19) While building a service using service builder, what is the purpose of specifying remoteservice=true in service.xml?
20) How to control the category in which the liferay portlet created by me will be displayed?
21) What is hot deployment feature of Liferay? Where will you place your portlets for them to be deployed without server restart?
22) Does Liferay support email notifications out of the box for portlets involving workflow?
23) What is the purpose of mode and persistence packages which are created by the Liferay service builder?
24) If there are 3 portlets and all of them need to talk to different database systems (say MysSQL, Oracle and SQL Server) because of some legacy appliation requirements. How will you make them connect to different databases?
25) Can you configure the database connection pool created by Liferay?
26) Can you list down various tools and frameworks used by Liferay? e.g. It uses Lucene for search
27) Suppose a client is pursuing a Liferay based solution to their requirements, what non-functional clarifications will you ask from the customer?
28) Is there a build number associated to a portlet assigned by the service builder? Where can we find the current build number?
29) Have you tried the Liferay 6 Eclipse plugin? What all limitations have you found?
30) How do you log events in a portlet? Do I need to configure Log4J for every portlet or is there a common place where one can configure Log4J so that all Liferay portlets can use logging API?
31) Do you know how to update your setup of Liferay 5.2.3 to Liferay 6?
32) Can I run my Struts 1.3 based legacy application as a portlet application? How?
33) Is it a good design to meet the customer requirements by creating a single portlet or splitting them into multiple portlets? What criteria will you use to split the functionality into multiple portlets?
34) What advantages do you see in running the Liferay portal application on a application server like JBoss than running it on a web server like Tomcat?

35) What all other portals have you heard of? How do you compare them to Liferay?
36) How will you implement i18 in a portal application? What if you want the user to choose his language preference?
37) There is a portlet application for managing images similar to Flickr. The user wants to see the images in full screen. Will Liferay allow you to do that?
38) I have a war file of the portlet which is created using service builder, what is the difference between pasting this war file in the deploy folder of Liferay and application folder of server (like webapps in case of Tomcat)?
39) How will you setup a clustered Liferay portal on 5 servers available to you?
40) How much user base can Liferay support without affecting performance?
41) There is a JRE shipped with Liferay package, do we not need a JDK and what is the purpose of bundled JRE?
42) Suppose there is a requirement from client to implement search functionality in Liferay portlet, does Liferay provides any API out of the box to support search?
43) What are the various ways in which one can override the default behaviour of Liferay?
44) The service builder can also build WSDL for you. What is the benefit of this feature provided by Liferay?
45) How will you build a portlet that can be added only single or multiple times in to the portal application?
46) Is it possible to set a session time out for a portlet which is different from other portlets and the Liferay portal?
47) What is the flow of HTTP request in Liferay?
48) The JSPPortlet.java extends  the MVCPortlet.java class. What purpose does MVCPortlet.java fulfill?
49) How do you perform Junit tests on your portlet? How do tackle the dependency on the MVCPortlet.java?
50) Is it mandatory to have the view logic in view.jsp? Can one configure some other JSP (say MyJSP.jsp) to be default view page than view.jsp?