Showing posts with label Liferay. Show all posts
Showing posts with label Liferay. Show all posts

Aug 21, 2013

[Solved] Task cannot continue because ECJ is not installed — in Eclipse ant

I know this is known error but still I put it here as it could be helpful.
   
Task cannot continue because ECJ is not installed.

ECJ was automatically installed.

Please rerun your task. Total time: 1 second    

If you get above error in your IDE, just follow below steps to resolve it.   

  1. In Eclipse, Go To Window --> Preferences --> Ant -> Runtime
  2. Select "Ant Home Entries (Default)"
  3. Add External JAR (ecj.jar)  from your plugin-sdk->lib folder
  4. Ant should now be able to compile from your build.xml

May 8, 2013

Liferay auto complete field example




  • Create field for auto complete

          <div id="autoCompleteDiv">
               <aui:input type="text" name="xyz" id="txtAutoComplete" label="Auto Complete Field" />
          </div>

         NOTE :- You can use <span> also insted of <div>

  • Create Resource URL

         <portlet:resourceURL var="autoCompleteName" id="resourceId" />


  • Write AUI script for auto complete field

         <aui:script use="aui-autocomplete, autocomplete-filters, autocomplete-highlighters">
                  var dataSource = new A.DataSource.IO(
                  {
                        source: '<%=autoCompletName.toString()%>'
                  });
                  var autocomplete = new A.AutoComplete(
                  {
                             dataSource: dataSource,
                             matchKey: 'name',
                             schema: {
                             resultListLocator: 'response',
                             resultFields: ['key', 'name']
                  },
                    schemaType:'json',
                    contentBox: '#autoCompleteDiv',
                    input:'#<portlet:namespace />txtAutoComplete',
                    typeAhead: false
                   });
                autocomplete.generateRequest = function(query) {
                         return {
                             request: '&keywords=' + query
                         };
                 }
                 autocomplete.render();
                });
         </aui:script>

  • Write Resource method that serve to resource URL

          @ResourceMapping("resourceId")
   public void serveResource(ResourceRequest request, ResourceResponse response)
          {
               JSONObject json = JSONFactoryUtil.createJSONObject();
              JSONArray results = JSONFactoryUtil.createJSONArray();

             String keyword = ParamUtil.getString(resourceRequest, DisplayTerms.KEYWORDS);

             List filteredEntries = getFilteredEntries(keyword);

            for (Object entries : filteredEntries)
            {
               Object[] entry = (Object[]) entries;

               JSONObject listEntry = JSONFactoryUtil.createJSONObject();

              listEntry.put("key", Long.parseLong("" + entry[0]));
              listEntry.put("name", "" + entry[1]);

              results.put(listEntry);
          }
           json.put("response", results);
           PrintWriter writer = resourceResponse.getWriter();
           writer.print(json.toString());

        }

        private static List getFilteredEntries(String query) throws SystemException
       {
            List entries = null;
            DynamicQuery xzyQuery = DynamicQueryFactoryUtil.forClass(Xyz.class);
            if (Validator.isNotNull(query))
           {
             xzyQuery.add(RestrictionsFactoryUtil.ilike("title", "%" + query + "%"));    
         
             ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
             projectionList.add(ProjectionFactoryUtil.property("xyzId"));
             projectionList.add(ProjectionFactoryUtil.property("title"));
             xzyQuery.setProjection(projectionList);

              entries = XyzLocalServiceUtil.dynamicQuery(xzyQuery);
            }
           return entries;
        }

NOTE :- Here I used "@ResourceMapping("resourceId")" because I used Spring, make appropriate changes as per your requirement to call the " serveResource(...)" method.

May 7, 2013

Liferay Custom Query

 Step by step process of "How to use custom query"

There are four steps to create custom query and use it.
NOTE :- Here I assume that you know about liferay service builder.

step 1:- custom-sql folder


  • Create custom-sql folder in docroot/WEB-INF/src package
  • Add new default.xml file in above created folder.
  • The content of default.xml is as under
                <?xml version="1.0" encoding="UTF-8"?>
               <custom-sql>
                  <sql file="custom-sql/query.xml" />
                  <sql file="custom-sql/anotherquery.xml" />
              </custom-sql>
  • You can add multiple sql file in above content.
  • Now create query.xml in same folder
  • Write your sql-query in query.xml as under.
               <?xml version="1.0" encoding="UTF-8"?>
               <custom-sql>
                    <sql id="com.test.services.xyz.service.persistence.uniqueId">


  <![CDATA[
select * from xyz x where x.userId=? AND x.name=?;
  ]]>
  </sql>
           </custom-sql > 
  • The sql id is unique so use the package name of service persistence class and then (xyz) uniqueId.
    • Like "com.test.services.xyz.service.persistence.uniqueId"
    • Here "com.test.services.xyz.service.persistence" is package path of "xyz" service.
    • "Xyz" is the entity name.
    • Here "xyz" is the service created for means table name which we created using service builder.

step 2:- creating service finder impl


  • Create "XyzFinderImpl.java" in "com.test.services.xyz.service.persistence" package of service.
  • NOTE:- Give the name of finder impl related to the service name
    • Here in our case the service is "XYZ" so we create "XyzFinderImpl.java"
  • The content of finder impl is as under
          package com.test.services.xyz.service.persistence;

          import com.liferay.portal.kernel.dao.orm.QueryPos;
          import com.liferay.portal.kernel.dao.orm.SQLQuery;
          import com.liferay.portal.kernel.dao.orm.Session;
          import com.liferay.portal.kernel.exception.SystemException;
          import com.liferay.portal.kernel.util.StringUtil;
          import com.liferay.portal.kernel.util.Validator;
          import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
          import com.liferay.util.dao.orm.CustomSQLUtil;

          public class XyzFinderImpl extends BasePersistenceImpl<xyz> implements XyzFinder
          {
             public static String CUSTOM_SQL = "com.test.services.xyz.service.persistence.uniqueId";
   
              public List getXYZ(Long userId, String name) throws SystemException
              {
                  Session session = null;
                  String sql = null;
                  try
                  {
                      session = openSession();
                      if (Validator.isNotNull(userId))
                      {
                          sql = CustomSQLUtil.get(CUSTOM_SQL);
                      } 
                      SQLQuery query = session.createSQLQuery(sql);
                      QueryPos qPos = QueryPos.getInstance(query);
                      qPos.add(userId);
                      qPos.add(name);

           //           query.executeUpdate();
                     return (List) query.list();
                  } catch (Exception e)
                  {
                      e.printStackTrace();
                  }
              }

          }

  • Right now "XyzFinder" interface is not available so no need to worry for that.
  • After doing this much build the service again so it's create appropriate classes and interfaces for you.

step 3:-Define method in service Impl


  • Open XyzLocalServiceImpl generated while first time you build service for that.
  • Define method that call the finder method of "XyzFinderImple.java" class as below.
                    public List getUser(Long userId, String name) throws SystemException
                   {
                      return  XyzFinderUtil.getXYZ(userId, name);
                    }
    • Now build service again and after that you are ready to use the custom query.

    step 4:- use of custom-query


    • Call the method as below in your class.
                 List users = XyzLocalServiceUtil.getUser(12345,"test");





    Apr 26, 2013

    Private page as default landing page after login in to Liferay


    1.  Create one public page for login name it (Login) and give friendly url as (/home) or other then login because login is reserved key word of liferay, also do not create other page here(public).

    2.  Now create first private page called (welcome) and give friendly url as(/welcome) or other as per              requirement.

    3.  Create other pages as per requirement on private pages.

    4.  Open control panel-->go to portal settings-->and give default landing page as ("/group/guest/welcome")  the (/welcome) is what we defined on step no 2.

    5.  Also have to give membership to the user.

      5.1   So for that go to control panel-->portal settings-->select tab(Default User Associations)

      5.2  Write the site name(liferay.com) in Sites text field in new line because liferay consider per line user     association.

    And Save it

    That's it you are done.

    NOTE:- You can also set Default Logout Page using step no 4 and set Default Logout Page as (/home) .