X

Subscribe to BloggerHow updates..

Recommend us on Google+

Get updates in your mail

New posts will get delivered to your mail box directly.Its free and we never spam you. Enter your mail here and click on subscribe. Check your mail box for a confirmation mail.

Get updates in your RSS reader

Subscribe using RSS and read updates using your favorite RSS reader..
RSS

Like us on Facebook

Get updates directly on your Facebook wall.

Follow us on Twitter

Get updates as tweets..

Circle me on Google plus

Add me to your circle and get new post updates in Google plus stream.

To set preference for a portlet we need to follow below steps

1.Add below entries in liferay-portlet.xml file

<configuration-action-class>com.portlet.action.configuratoionImpl</configuration-action-class>

Before <instanceable>true</instanceable>

2.Create a custom action class which implements ConfigurationAction interface.

A sample Class
public class configuratoionImpl implements ConfigurationAction {

       @Override
       public void processAction(PortletConfig portletConfig, ActionRequest actionRequest,
                     ActionResponse actionResponse) throws Exception {

                           String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
                           ThemeDisplay themeDisplay=(ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
                           if (!cmd.equals(Constants.UPDATE)) {
                                  return;
                           }
//Fetching String values from configuration.jsp page
String noofrecords = ParamUtil.getString(actionRequest, "recordsperpage");
String uname = ParamUtil.getString(actionRequest, "uname");
                          
       String portletResource = ParamUtil.getString(
                                  actionRequest, "portletResource");

PortletPreferences preferences =
                                  PortletPreferencesFactoryUtil.getPortletSetup(
                                         actionRequest, portletResource);
//set all values in preferences and store                           
preferences.setValue("noofrecords", noofrecords);
preferences.setValue("uname", uname);

preferences.store();
                          
SessionMessages.add(actionRequest, portletConfig.getPortletName() + ".doConfigure");
             
       }

       @Override
       public String render(PortletConfig arg0, RenderRequest renderRequest,
                     RenderResponse renderResponse) throws Exception {
              //redirect to configuration jsp
              return "/jsp/configuration.jsp";
       }

}

3.Create a configuration.jsp under the jsp folder

<%
                String redirect = ParamUtil.getString(request, "redirect");

                PortletPreferences preferences = renderRequest.getPreferences();
                String portletResource = ParamUtil.getString(request, "portletResource");
                if (Validator.isNotNull(portletResource)) {
                                preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
                }
                               
%>

<liferay-portlet:actionURL portletConfiguration="true" var="configurationURL" />

<form action="<%= configurationURL.toString() %>" method="post" name="<portlet:namespace />fm">
                <input name="<portlet:namespace />redirect" type="hidden" value="<%= HtmlUtil.escape(redirect) %>" />
                <input name="<portlet:namespace /><%= Constants.CMD %>" type="hidden" value="<%= Constants.UPDATE %>" />
               
                <aui:fieldset label="Configurations">
                <aui:layout>
                <aui:column  first="true">
                                               
                                <table >
                                               
                                                <tr>
                                                                <td><liferay-ui:message key="name" />&nbsp;&nbsp;</td>
                                                                <td  class="lpad15">
                                                                                <aui:input name="name" type="text"  label="" value='<%=  preferences.getValue("uname", "") == null ? "10" : preferences.getValue("uname", "") %>' ></aui:input>
                                                                </td>
                                                </tr>
                                               
                                                <tr>
                                                                <td><liferay-ui:message key="RecordsperPage" />&nbsp;&nbsp;</td>
                                                                <td  class="lpad15">
                                                                                <aui:input name="recordsperpage" type="text"  label="" value='<%=  preferences.getValue("noofRecords", "") == null ? "10" : preferences.getValue("noofRecords", "") %>' ></aui:input>
                                                                </td>
                                                </tr>
                                                <tr>
                                                                <td>
                                                                                <aui:button type="submit" value="save"></aui:button>
                                                                </td>
                                                </tr>
                                </table>
               
               
                </aui:column>
                </aui:layout>
                </aui:fieldset>
</form>


Localize the portlet in liferay

1.Add the below property in Portlet.xml file

<resource-bundle>content.Language</resource-bundle>

Before <portlet-info> parameter

2.Create a folder (content)under src/ 

3.Create Language.properties file and add key value pair for all required constants of a portlet

Configurations save=Save Configurations
javax.portlet.description=Calendar
javax.portlet.title=Calendar

You can run the build Languages target  by right click Language. Properties file to create list of Language property files under the content folder.



On successful build you can get the below output.



To store document media files in different Location,We need to overite the below properties with our new location where we need the files to be stored

By default document media files are stored below location

dl.hook.file.system.root.dir=${liferay.home}/data/document_library


If the requirements to access files from document media and do some operaions on that file ,then the below code snippet will help us.

long folderId = 0;
try {
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
List<DLFolder> groupFolder = DLFolderLocalServiceUtil.getFolders(
themeDisplay.getScopeGroupId(),
DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
boolean IsFolderExits = false;
if (groupFolder.size() > 0) {
for (DLFolder folders : groupFolder) {
                              //To get particalar folderId
      if ("FolderName".equalsIgnoreCase(folders
.getName().trim())) {
folderId = folders.getFolderId();
IsFolderExits = true;
break;
}

}
}

                  String file = null;
List<DLFileEntry> fileEntries = null;
if (Validator.isNotNull(folderId)) {

try {
                              //To get the List of files exits in that particular folder
fileEntries = DLFileEntryLocalServiceUtil.getFileEntries(
themeDisplay.getScopeGroupId(), Long.valueOf(folderId));

} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
if (fileEntries.size() > 0) {

for (DLFileEntry entries : fileEntries) {
                                   //This will provide us to get the complete file Location of document Media
file = PropsUtil.get("dl.hook.file.system.root.dir") + "/"
+ entries.getCompanyId() + "/"
+ entries.getFolderId() + "/" + entries.getName()
+ "/" + entries.getVersion();
}
}
}


} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}

long folderId = 0;
try {
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(themeDisplay.getScopeGroupId());
                       //To get the List of folders of an Organization
List<DLFolder> groupFolder = DLFolderLocalServiceUtil.getFolders(
themeDisplay.getScopeGroupId(),
DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
   //To create a folder in an Organization
DLFolder folder = DLFolderLocalServiceUtil.addFolder(
themeDisplay.getUserId(),
themeDisplay.getScopeGroupId(),
DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
"Folder Name", "Folder Description",
serviceContext);

}

} catch (SystemException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}

Sender Portlet
1.Add below parameter in Portlet.xml

 <supported-publishing-event>
           <qname xmlns:x="http://liferay.com/events">x:ipc-parameters</qname>
  </supported-publishing-event>

2.Get the values from the sender jsps and  Set Events in your action class

@Override
public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
// TODO Auto-generated method stub
QName qName = new QName("http://liferay.com/events", "ipc-parameters");
String name = ParamUtil.getString(actionRequest, "name","");
int age = ParamUtil.getInteger(actionRequest, "age");
double salary = ParamUtil.getDouble(actionRequest, "salary");
String parameters=name+','+String.valueOf(age)+','+String.valueOf(salary);

actionResponse.setEvent(qName, parameters.toString());

}

Receiver Portlet:
1.Add below parameter in Portlet.xml of receiver portlet
 <supported-processing-event>
            <qname xmlns:x="http://liferay.com/events">x:ipc-parameters</qname>
 </supported-processing-event>

and add below parameter after end of </portlet> and before end of </portlet-app>

<event-definition>
    <qname xmlns:x="http://liferay.com/events">x:ipc-parameters</qname>
</event-definition>  

2.Get the events values in process Events Method

@Override
@javax.portlet.ProcessEvent(qname = "{http://liferay.com/events}ipc-parameters")
public void processEvent(EventRequest request, EventResponse response)
throws PortletException, IOException {
Event event = request.getEvent();
String paramtervalues = (String) event.getValue();
response.setRenderParameter("parameters", paramtervalues);
}

try {
String[] roleNames={"Role A","Role B"};

ThemeDisplay themeDisplay=(ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);

for(int j=0;j<roleNames.length;j++){
Map<Locale, String> title=new HashMap<>();
Map<Locale, String> desc=new HashMap<>();
if(!(RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId(), roleNames[j]).getName().toString().trim()).equalsIgnoreCase(roleNames[j].trim())){
RoleLocalServiceUtil.addRole(themeDisplay.getUserId(), themeDisplay.getCompanyId(), roleNames[j], title, desc, 1);
}
}
Role role=RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId(), "Role A");
List<User> users=UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());
UserLocalServiceUtil.addRoleUsers(role.getRoleId(), users);

} catch (SystemException | PortalException e) {
e.printStackTrace();
}

To Top