Posts

Showing posts with the label liferay

Analyzing the Portlet Performance in liferay

Set the following log in the  tomcat/lib/META-INF/portal-log4j-ext.xml <category name="com.liferay.portlet.InvokerPortletImpl">        <priority value="DEBUG" />     </category> This will cause the performance of each portlet render to be logged, for example:  render for welcomeportlet_WAR_welcomeportlet takes 1 ms render for employmentportlet_WAR_linksportlet takes 80 ms

File Utility methods in liferay

Liferay has many utility method to handle the file like copyDirectory , copyFile , unZip , delete , createTmpfile etc.. In real scenerio to unzip the file we need to write pure java code but liferay has utility method to unzip the file Have a look into the fileUtil class and look into the method unzip(File source, File destination) public static void unzip(File source, File destination) { PortalFilePermission.checkCopy(_getPath(source), _getPath(destination)); getFile().unzip(source, destination); } Also internally if we look into the how liferay is unzip, look into the ExpandTask class and look into the method as public static void expand(File source, File destination) { Expand expand = new Expand(); expand.setDest(destination); expand.setProject(AntUtil.getProject()); expand.setSrc(source); expand.execute(); } Their it is using the Expand class(org.apache.too...

Supported properties in hook by liferay

In Liferay we know that we can override some of the properties using hooks. To remember all those supported properties is quite difficult. Also how can we know  whether that particular properties  is supported or not ? So we have class called  HookHotDeployListener class to check the supported properties. Look into the  SUPPORTED_PROPERTIES string array variable where it listed all the supported properties. This is how it is listed public class HookHotDeployListener extends BaseHotDeployListener implements PropsKeys { public static final String[] SUPPORTED_PROPERTIES = { "admin.default.group.names", "admin.default.role.names", "admin.default.user.group.names", "asset.publisher.asset.entry.query.processors", "asset.publisher.display.styles", "asset.publisher.query.form.configuration", "auth.forward.by.last.path", "auth.public.paths", "auth.verifier.pipeline", "auto.deploy.liste...

Creating the scheduler dynamically in liferay

We can create the scheduler by defining the entries in the liferay-portlet.xml with specfying the only particular entries. what if scheduler what to change based on the user input. so here is the code, String cron = "0 0/1 * 1/1 * ? *"; Portlet portlet = PortletLocalServiceUtil.getPortletById("new_WAR_UserSearchportlet"); Trigger trigger = null; try { trigger = TriggerFactoryUtil.buildTrigger(TriggerType.CRON, Scheduler.class.getName(), Scheduler.class.getName(), new Date(), null, cron); } catch (SchedulerException e) { e.printStackTrace(); } Message message = new Message(); message.put(SchedulerEngine.CONTEXT_PATH, portlet.getContextPath()); message.put(SchedulerEngine.MESSAGE_LISTENER_CLASS_NAME, Scheduler.class.getName()); message.put(SchedulerEngine.PORTLET_ID, portlet.getPortletId()); try { SchedulerEngineUtil.schedule(trigger, StorageType.PERSISTED, "", "liferay/scheduler_dispatch", mes...

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!!!

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 ...

Hiding the portlet

In some seceniros you may need to hide the portlet, In liferay it is easy as just add the attribute. renderRequest.setAttribute(WebKeys.PORTLET_CONFIGURATOR_VISIBILITY, false); That it !!! Above renderRequest  will set  in hiding 

Accessing JSON through javascript in custom portlet

Image
Scenrio:           Accessing the other custom portlet JSON services in our custom portlet through the javascript. Step:1 Enable remote-servie = true in service.xml as shown and build service to generate the services <service-builder package-path="com.liferay.testjson"> <namespace>custom</namespace>          <entity name="jsontest" remote-service="true" local-service="true" uuid="true">                      <column name="id" type="int" primary="true" />                       <column name="name" type="String"></column>                     <column name="desc" type="String"></column>     </entity>   </service-builder> Step2:   Open the class XXXXServiceImpl.class ...

Focusing the form field in Liferay

  Whenever we create from in liferay ,it is required cursor to focus on the first field.   Suppose we can have form like this   <aui:from name="fm" method="post" action="${updateActionURL}">             <aui:input  name="title" label = "Title"/>        <aui:input  name="desc" label = "Desc"/>       <aui:button type="submit" value ="save"/> <.aui:form>  If we think title should be highlited on page load , following script is also required. <aui:script> Liferay.Util.focusFormField(document.<portlet:namespace/>fm.<portlet:namespace/>title); </aui:script> Deploy your project and refresh your page . We can see the first field is highlighted. These source are avaliable in the follwing path.       portal-web/docroot/html/js/liferay

Success message and Error message in liferay

Success Message                   In Class                           SessionMessages.add(request, "record-updated");                In JSP                           <liferay-ui:success key="record-updated" message="record-updated-sucessfully" />                Language.properties                            record-updated  = Your record updated successfully.          ...

Submitting the form with ajax(using alloy UI) i.e with out page refresh

    In rare it may happens, you have to submit the form with out refresh the page (actionURL ) and save the parameters in the DB.    Jus Copy the below code , Here i am using alloy UI to submit the form and here not need to append any parametrs to the URL .you will get the parameters values from request parameters. <portlet:actionURL var="formsubmissionURL" /> <aui:script use="aui-io-request,aui-node" > Liferay.provide(window,'submitForm', function() { var A = AUI(); A.io.request('${formsubmissionURL}',{ method: 'POST', form: { id: '<portlet:namespace />fm' }, on: { success: function(){     }   }   });   }); </aui:script > <aui:form action="#" method="POST" id="fm" name="fm" >      <aui:input type="text" name="name"/>      <aui:button type="submit" value="Save" /...

Sending mail in liferay

 In most of your project you may come across sending an mail to the user when ever updated. In liferay its pretty easy to do with that        InternetAddress fromAddress = new InternetAddress(user.getEmailAddress()) // from address         InternetAddress toAddress = new InternetAddress(user.getEmailAddress())  // to address             String body = ContentUtil.get("/templates/sample.tmpl", true);  // email body , here we are getting email structure creating the content folder in the src and create the file with the extension as tmpl . Here i have created a folder called content and also created file as sample.tmpl             String subject = "subject" // email subject                         body = StringUtil.replace(b...

Creating scheduler in Liferay

  To create scheduler in liferay is easy just adding an entry in liferay-portal.xml and adding your custom logic in do receive method. 1.Add the following entry in liferay-portlet.xml after icon tag and before instansable tag.    " <scheduler-description> " tag you can give your custom description   " <scheduler-event-listener-class> " tag write your custom class where you write your custom scheduler  logic. " cron trigger class " tag , it identifies at what scheduler runs , here scheduler runs at 12 clock at sunday.             <scheduler-entry>            <scheduler-description>scheduler entry</scheduler-description>              <scheduler-event-listener-class>                 com.sample.scheduler.Sch...

Parsing in Liferay(Using SAX Parser reader)

  Liferay provided default classes to parse the xml content.   Have look at this SAXReaderUtil  which will be useful to parse the xml.   For Example:     Consider the WebContent(Journal article) where we have added metadata ,consider to fetch it.    First of all this metadata content will be store in Journal article table of Content column as <?xml version="1.0"?> <root available-locales="en_US" default-locale="en_US">    <dynamic-element instance-id="trtrtrt454" name="Title" type="text" index-type="">         <dynamic-content><![CDATA[Test Title]]></dynamic-content>     </dynamic-element>         <dynamic-element instance-id="Adgbcd456" name="Desc" type="text" index-type="">         <dynamic-content><![CDATA[Test Desc]]></dynamic-content> ...

service bulider(service.xml) column types

When ever your are writing the service.xml , you may have doubts what type the liferay supports. Here we go,   Take look at the servicebulider class and method getSqlType(-,-,-) . So whenever you have to define data Type have a glance at this class.  Refer the " com.liferay.portal.tools.servicebuilder.dependencies " package what columns , Tables will allowed to add in the service.xml. bad_table_names.txt - Refer the this txt file what table names to be avoided. bad_column_names.txt - Refer this to be avoided the column name. Also refer the .ftl how they are generated the files.

Reteriving guest premissions for the login portlet

  Some times unknowingly admin  may remove the guest permissions for the login portlet and when we try to access the portlet it says "you do not have permission to view the portlet" . Then we have to retrive back the permisions but portlet is not accessiable  what to do! Here we go , Add the following properties in portal-ext.properties  auto.login.hooks=com.liferay.portal.security.auth.ParameterAutoLogin , Whenever we add parameter in portal-ext.properties we need to resart the server. Access the url as follows  http://localhost:8080/c/portal/login?parameterAutoLoginLogin=yourscreennameOrEmailAddress&parameterAutoLoginPassword=password Retrieve back the permission changes and remove the auto.login.hooks from the portal-ext.properties. Thats it you have done and you will able to see the login portlet

Useful classes in liferay

1. Many we require to split , shorten, replace, matches, contains and many more  to check the string so liferay provided StringUtil.java 2. HTMLUtil .java class is also useful to extract the html and more, this class is useful, have look at this class. 3. StringPool.java class is useful many senerioes.