Posts

Showing posts from November, 2013

Default Request parameters in actionRequest

In Liferay, Apart from the request parameter values it gives 2 more additional parameters.  1.formDate    -   Date  2.javax.portlet.action - Method you have called. We can retrieve the parameters from the actionRequest as  ParamUtil.getString(actionRequest, "title");  I have seen the parameters from the below code as follows  Map<String, String[]> requestMap = actionRequest.getParameterMap();                                 Set<Entry<String, String[]>> set = requestMap.entrySet();            Iterator<Entry<String, String[]>> it = set.iterator();                      while (it.hasNext()) {             Map.Entry<String, String[]> entry = (Entry<String, String[]>) it.next();                         System.out.println("entry ===="+entry.getKey());                         String[] paramValues = entry.getValue();                for (int i = 0; i < paramValues.length; i++) {              

Removing prefix value for table created by service builder

In Liferay , Whenever we are writing the service bulider we specify the "namespace" in  service.xml so that table will be created as prefix namespace. If you look into the below service.xml , In DB it creates as test_library . <service-builder package-path="com.test"> <author>BNG</author> <namespace>test</namespace> <entity name=" library " local-service="true" remote-service="false"> <column name="libraryId" type="long" primary="true" />    ----------------    </entity> </service-builder>  If you want to remove the prefix while creating the table in DB. we need to keep auto-namespace-tables = "false". <service-builder package-path="com.test" auto-namespace-tables="false" > Thats it!!!

Avoiding Multiple Submits(Refresh page) in Liferay

We often see after submitting the form and refresh the page you will see the same result will be adding in the DB. So how to get rid of this? Here we have 2 options to eliminate this behaviour. 1. By Declaring the tag in the liferay-portlet.xml after <icon> tag           <action-url-redirect> true </action-url-redirect> 2.After doing our logic we can redirect to specified URL by using actionResponse.    actionResponse.sendRedirect(PortalUtil.getLayoutURL(themeDisplay.getLayout(),themeDisplay));   Here we are  redirecting to the friendly url of that page. The above will be redirect to the below URL as                     //group/test/test1----- (group/<your-community-name>/pageName) .

How Liferay is identifying the browser and device

Image
In this article we can know how liferay is identifying the browser and device. Press Ctrl_Shift+J to see the elements. Look into the javascript it shows weather It is an Chrome or Firfox or IE or Iphone etc..     Open the top_js.jspf file we have find the following snipetts.     isChrome: function() { return <%= BrowserSnifferUtil.isChrome(request) %>; }, isFirefox: function() { return <%= BrowserSnifferUtil.isFirefox(request) %>; }, isGecko: function() { return <%= BrowserSnifferUtil.isGecko(request) %>; }, isIe: function() { return <%= BrowserSnifferUtil.isIe(request) %>; }, isIphone: function() { return <%= BrowserSnifferUtil.isIphone(request) %>; }, In JavaScript:                    Browser:                   Liferay.Browser.isIe()         // returns true or false                   Liferay.Browser.isMozilla() // returns true or false In VM file:   In

Form Validation In Liferay -- Client Side validation

In Liferay we can do client side validation very easily.  We use <aui:validator>  tag to do validations. It allows "name" and "errormessage" attributes Names it allows are: alpha alphanum date digits email equalTo number acceptFiles min minLength max maxLength range rangeLength required url custom Here custom is use to give custom validations , Below Exampleis the custom validation for email validation .In Liferay for email sometimes its is not working. So i am writing the custom validation.  Just Copy & paste the code and try it will perfectly works <aui:validator name=" custom " errorMessage="Please enter valid email address">       function (val, fieldNode, ruleValue) {           var result = false;         var emailRegexStr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;         var isvalid = emailRegexStr.test(val); if (isvalid) {  result = true;

Creating URL in Javascript in Liferay

Sometimes it may requires to create URL in Javascript. In Liferay we can create any URL in Javascript.  In Normal Case we can create Action URL as            < portlet:actionURL var="testURL" name="update" /> In Javascript we create as           <aui:script>            AUI().ready('liferay-portlet-url', function(A){           var actionURL = Liferay.PortletURL. createActionURL ();          actionURL .setParameter("cmd", "test");            });          </aui:script>    To create renderURL we put as Liferay.PortletURL. createRenderURL() ; in above script.    To Create resourceURL we put as Liferay.PortletURL. createResourceURL() ; Refer "LiferayPortletURL" interface to get more idea. Apart from the setting the parameter(setParameter) to the URL we can have setPlid("----") , setWindowState("----"); also.

Relation between Tag cloud and Blogs in liferay

If we place tagcloud and blogs portlet in one page .When we click on the tagcloud automatically blogs portlet is rendering the tagged entries. So, how does it works? Open the portlet-custom.xml search with the "148" here 148 refers to portlet name.  At end of the portlet tag you will find the " <supported-public-render-parameter>tag</supported-public-render-parameter> ". Also search with "33" none other than blogs portlet. You will find the following line "<supported-public-render-parameter>tag</supported-public-render-parameter> ". Then how does it works in blogs?  Open the view.jsp(html/portlet/blogs folder). you will find the below line              String assetTagName = ParamUtil.getString(request, "tag");  where it is retrieving the tagname when we click on the tag. Following lines will do lot of things for retrieving the entries.     AssetEntryQuery assetEntryQuery = new AssetEntryQuery(B

Accessing SOAP and JSON webservices(WSDL)

JSON webservice          http://localhost:8080/api/jsonws Axis Webservice       http://localhost:8080/api/axis Custom Portlet JSON webservices    http://localhost:8080/test-JSON-portlet/api/jsonws Custom Portlet AXIS webservices      http://localhost:8080/test-JSON-portlet/api/axis

Search Container in liferay

List<Article> articles= ArticleLocalServiceUtil.getArticles(0, -1); <liferay-ui:search-container delta="4" emptyResultsMessage="Sorry. There are no items to display." iteratorURL="<%= iteratorURL %>"> <liferay-ui:search-container-results  total="<%= articles size() %>" results="<%= ListUtil.subList(articles, searchContainer.getStart(), searchContainer.getEnd()) %>"/> <liferay-ui:search-container-row modelVar="article" className="Article"> <% articleDetailsURL.setParameter("articleId", Long.toString(article.getArticleId())); %> <liferay-ui:search-container-column-text name="Article Title" property="articleTitle" href="<%= articleDetailsURL.toString() %>" /> <liferay-ui:search-container-column-text name="Desc" property="description" /> <liferay-ui:search-container

Redirecting user default landing page in liferay

Define "Default Landing Page" in Control Panel ---> Portal setting  and put below logic in your class String defaultPagePath = PrefsPropsUtil.getString(themeDisplay.getCompanyId(),  PropsKeys.DEFAULT_LANDING_PAGE_PATH); response.sendRedirect(defaultPagePath); The above redirtects to the page where you have defined the value in the portal setting.

Adding CK Editor in Liferay

<form>                             <table>                                <tr>                                    <td>                                      <aui:field-wrapper label="Editor">                                         <liferay-ui:input-editor editorImpl=" <%= EDITOR_WYSIWYG_IMPL_KEY %>" />                                         <aui:input name="dynamicContent" id="dynamicContent" type="hidden" />                                      </aui:field-wrapper>                                    </td>                                </tr>                            </table> <input type="submit"  value="Save" onclick="<%= renderResponse.getNamespace() %>save();"/> <%! public static final String EDITOR_WYSIWYG_IMPL_KEY = "editor.wysiwyg.portal-    web.docroot.html.portlet.blogs.edit_entry.jsp&

Encryption and Decryption In Liferay

In Liferay , you can encrypt and decrypt  the senstive data. Liferay has provided easy way to encrypt the data. Have a look at the  Encryptor class , we find the methods to encrypt and decrypt the data. Here is an example of encryption of data. private String encrptData(ActionRequest actionRequest,String data ) throws PortalException, SystemException, EncryptorException{ ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest. getAttribute(WebKeys.THEME_DISPLAY); Company company = CompanyLocalServiceUtil.getCompany(themeDisplay.getCompanyId()); Key key = company.getKeyObj(); return Encryptor.encrypt(key, data); } The same string we can decrypt the following line as ,                   Encryptor.decrypt(key, data); So we may have an doubt which encryption algorithm liferay is using go to the portal.properties look at the following 2 properties.  company.encryption.algorithm=DES   company.encryption.key.size=56

Recovering the admin password in liferay

If you forgot the admin password, don't worry if you rights to access the DB , you can execute the below query to set admin password as "test". UPDATE User_ SET password_='qUqP5cyxm6YcTAhz05Hph5gvu9M=' WHERE emailAddress='test@liferay.com'; "qUqP5cyxm6YcTAhz05Hph5gvu9M=" is an encrypted form of "Test". EmailAddress can be your admin email address . In liferay by default it is an "test@liferay.com".

JavaScript Console in liferay

We can enable javascript console  in liferay. Add the following parameters in portal-ext.properties.     #     # Set this to true to enable the display of JavaScript logging.     #           javascript.log.enabled=true After adding parameter and restarting the server at the bottom you can see the console

Disabling cache in liferay

    ##     ## Browser Cache     ##     #     # Set this to true if you want the portal to force the browser cache to be     # disabled. It will only disable the cache for the rendered HTML response     # It will not have an impact on static content or other resources.     #     #browser.cache.disabled=true Add above parameter in portal-ext.properties .The above parameter ensures browser never cache the results. What liferay is doing internally to disable cache? Here we go, Have look at the ServicePreAction class , this ServicePreAction class excutes for every request if (PropsValues.BROWSER_CACHE_DISABLED ||(PropsValues.BROWSER_CACHE_SIGNED_IN_DISABLED && signedIn)) {                 response.setDateHeader(HttpHeaders.EXPIRES, 0);                 response.setHeader(HttpHeaders.CACHE_CONTROL,                                HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);                 response.setHeader( HttpHeaders.PRAGMA,                             HttpH

Impersonation in liferay --- adding additional parameter (doAsUserId)

Liferay has great functionality of impersonation. Admin can impersonate any user and can see weather user can able to see the content. So if we develop the custom portlet we should ensure impersonation should work. So for your portlet when you create URL manually . Call portalUtil.addReservedParameter(URL , themedisplay); This method takes care of the weather to add  addtional parameter to the URL.

Adding Cookies in Java/Liferay

In Liferay , when user is logging into ,he/she can remember username . so that whenever user again  tries to login no need to enter his crenditinals. Following are things liferay is doing to add username in cookie in LoginUtil.java Cookie loginCookie = new Cookie(CookieKeys.LOGIN, login); if (Validator.isNotNull(domain)) {    // Here domain is "www.liferay.com" loginCookie .setDomain(domain); } loginCookie .setMaxAge(loginMaxAge);  // Age of the Cookie loginCookie .setPath(StringPool.SLASH);    CookieKeys.addCookie(request, response, loginCookie , secure);