Posts

Showing posts from April, 2014

Checking weather the user has the role Organization Administrator.

Whenever we are creating the Organization we can assign the user to the Organization also we can assign the Organization role to the User. As of now we have 2 roles. 1.Organization Owner. 2. Organization Administrator. User who created the Organization will have the role "Organization Owner". But we need to manually assign the "Organization Administrator" to the user. In programmatically to check weather user has roles or not here is the code. ThemeDisplay  themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY); try {         Role role = RoleLocalServiceUtil.getRole(themeDisplay.getCompanyId(),RoleConstants.ORGANIZATION_ADMINISTRATOR);  //Get the Role List<UserGroupRole> userGroupList = UserGroupRoleLocalServiceUtil.getUserGroupRoles(themeDisplay.getUserId()); //Getting the Roles of the User for(UserGroupRole userGroupRole :userGroupList) { //Iterating the loop      if(role.getRoleId() == userGroupRole.getRoleId()) {

Checking the Weather user has userdelete Permission or not.

First of all we need the permission checker we can get it from the themedisplay . PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();  boolean deleteStatus  = false; try { Getting the all the Role of the User List<Role> roleList = RoleLocalServiceUtil.getUserRoles(themeDisplay.getUserId());     for(Role role : roleList) { Iterating the all the roles of the user           if(permissionChecker.hasPermission(themeDisplay.getScopeGroupId(), User.class.getName(),                          role.getRoleId(), ActionKeys.DELETE)) {            deleteStatus = true;         System.out.println("roleId"+role.getName());          }     } } catch (SystemException e) {           e.printStackTrace(); }

Ajax Implementation in the Liferay

Creating the Resource URL Liferay's default uses the resource url for ajax requests          <portlet:resourceURL id="addResource" var="addResourceURL">         </portlet:resourceURL> Creating the Ajax function creating the Jquery  ajax function function <portlet:namespace/>getEntries(){                 var url = "${addResourceURL}";                         $.ajax({                                 type : "POST",                                 url : url,                                 dataType : "JSON",                                 success : function(data) {                                         if(data.Exist){                                                                                       }                                 },                                 error : function(XMLHttpRequest, textStatus, errorThrown) {                                                            

Assigning and Unassigning user to the userGroup

Assinging users to the userGroups .       UserLocalServiceUtil.addUserGroupUsers(userGroupId, addUserIds); Removing users from the UserGroups.       UserLocalServiceUtil.unsetUserGroupUsers(userGroupId, removeUserIds);

Converting the Hits into the UserList

 By default liferay indexes the content and So when ever we are searching the any content in the liferay, It reterives from the index. Suppose we think we need search the Users which we need to implement programmatically. Liferay has one utility function which search the user and returns in the form of a Hits. Hits hits = UserLocalServiceUtil.search(-,-,-,-,-,-,-,-,-,-,-,); So how can convert the hits into UserList.  Here the code. public List<User> getUserList(Hits hits) throws PortalException, SystemException {   List<Document> documents = hits.toList(); // Converting the Hits into the Documents List<User> userList = new ArrayList<User>();         for (Document document : documents) {                 long userId = GetterUtil.getLong(document.get(Field.USER_ID)); // Getting the Individual User by userId.                 try {                         User user = UserLocalServiceUtil.getUser(userId);                         user

Getting the search container checkbox values in the Liferay

In liferay if we see the search container they have the chexkboxes which default liferay creates. So how do we reterive the those checkbox values in the javascript? Here is the code to retrieve the chexkbox values in javascript. Liferay.provide( window, '<portlet:namespace />updateStatus',  // Name of the javascript function . function(cmd) {       var updateStatus = true;        var updateUserIds = Liferay.Util.listCheckedExcept(document.<portlet:namespace />fm, "<portlet:namespace />allRowIds");  // Getting the checked checkboxes values , Here "fm" is the form name .       if (!updateUserIds) {            updateStatus = false;       } // Here based on the user click we are displaying the message . else if (cmd == "deActive") {      if (!confirm('<%= UnicodeLanguageUtil.get(pageContext, "are-you-sure-you-want-to-deactivate-the-  selected-users") %>')) {        updateStatus = false;  

Deleting the user and Changing the status : Active and Deactive of the user

Here is the following code to delete the user and change the status of the user In Liferay After deactivating the user then only we can delete the user. It is the below code what liferay is using to delete and update the status of the user.  protected void deleteUsers(ActionRequest actionRequest) throws Exception {              String cmd = ParamUtil.getString(actionRequest, Constants.CMD);              // Getting the List of all the Users              long[] deleteUserIds = StringUtil.split(ParamUtil.getString(actionRequest, "deleteUserIds"), 0L);              // Based on the user status selected we are changing the status              for (long deleteUserId : deleteUserIds) {                        if (cmd.equals(Constants.DEACTIVATE) ||  cmd.equals(Constants.RESTORE)) {                                 int status = WorkflowConstants.STATUS_APPROVED;                                 if (cmd.equals(Constants.DEACTIVATE)) {                              

Hiding the Default Error message

Image
In the Liferay, when ever we get the exception by default  error message will be displayed. So how to remove that message as below? Just add the below code at the class level where your excepting the error can occur. Code as follows as SessionMessages.add(actionRequest, (LiferayPortletConfig)portletConfig.getPortletName() + SessionMessages. KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE); Thats it , we have disabled the default error message. Anyhow we are discussing about the error message, here we will discuss how to create the error message, Creating the Error message in the Liferay . In Class : SessionErrors.add(actionRequest, "error-key"); In JSP: <%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> <liferay-ui:error key="error-key">        <liferay-ui:message key="validation-perference-exception" /> </liferay-ui:error>

Adding the custom field for the OOTB using the expando

Suppose if we think to add the custom field to the organization while creating the organization. here is the following code. Organization organization = null;  ThemeDisplay themeDisplay  =  (ThemeDisplay) renderRequest. getAttribute (WebKeys.THEME_DISPLAY);                 long userId = themeDisplay.getUserId();                                try {                      int statusId = ListTypeConstants.ORGANIZATION_STATUS_DEFAULT;                     ServiceContext serviceContext = ServiceContextFactory. getInstance (Organization.class.getName(), renderRequest);                                                 Map<String,Serializable> expandoBridgeAttributes = new HashMap<String, Serializable>();                  expandoBridgeAttributes.put(" test83 ", "Liferay"); Here "test83" is the field name of the custom attribute.                                                 serviceContext.setExpandoBridgeAttributes(expandoBridge

Creating the Custom field for the portlet

Creating the customfield in the custom portlet 1. Create the class and extend with BaseCustomAttributesDisplay . 2. Make an entry in liferay-portal.xml  with tag as (custom-attribute-display) by defining the class    Thats it now the entry will be available to add the custom field in  the control panel. Go to the control Panel and click on the custom field. You can see the our portlet custom field value avaliable there.     Click on the Edit link and add the field name and select field type  from the dropdown and also edit the permission to view for the user. Now your custom field avaliable to add in the portlet. 3.       To display the  custom attributes in the jsp as editable keep  editable as "true" if not "false" , like below         <liferay-ui:custom-attributes-available className="<%= Article.class.getName() %>">                         <liferay-ui:custom-attribute-list                                         className="&