Saturday, 20 September 2014

add user to a usergroup programmatically liferay

This simple code in which I am adding a user in a user group.
This post also having the code of adding user if it user doesn't exist.
my jsp page code

<%
PortletURL updateUserPassword = renderResponse.createActionURL();
updateUserPassword.setParameter(ActionRequest.ACTION_NAME, "addUserGrouppublic ");
User userr = themeDisplay.getUser();
List<UserGroup> userUserGroups = UserGroupLocalServiceUtil.getUserUserGroups(userr.getUserId());
if(!userUserGroups.isEmpty())
{
Iterator userUserGroupItr = userUserGroups.iterator();
UserGroup userUserGroup = (UserGroup)userUserGroupItr.next();
long classPK = userUserGroup.getUserGroupId();

}
%>

<aui:form name="fm" method="POST" action="<%=updateUserPassword.toString()%>" >

 <aui:input name="groupID1" type="hidden" value="<%= userUserGroup.getUserGroupId() %>" />
<div id="phone-fields">
    <div class="lfr-form-row lfr-form-row-inline">
      <div class="row-fields" style="display:flex;">
        <aui:input fieldParam='groupMemberName1' id='groupMemberName1' name="groupMemberName1" label="group-member-name" />
        <aui:input fieldParam='groupMemberEmail1' id='groupMemberEmail1' name="groupMemberEmail1" label="group-member-email" />
        <aui:select id="groupMemberGender1" name="groupMemberGender1" label="group-member-gender">
          <aui:option value="male" label="group-member-female"></aui:option>
          <aui:option value="female" label="group-member-male"></aui:option>
        </aui:select>
    <aui:input fieldParam='groupMemberJob1' id='groupMemberJob1' name="groupMemberJob1" label="group-member-job" />
      </div>
    </div>
  </div>
</aui:form>


action class
AddUser.java{
public void addUserGrouppublic (ActionRequest actionRequest, ActionResponse actionResponse)
  throws PortalException, SystemException
{
    User usr = themeDisplay.getUser();
User loggedUser = null;
        try {
loggedUser = PortalUtil.getUser(actionRequest);
} catch (PortalException e1) {
e1.printStackTrace();
} catch (SystemException e1) {
e1.printStackTrace();
}
long companyId = PortalUtil.getCompanyId(actionRequest);
 Locale locale =PortalUtil.getLocale(actionRequest);
String groupMemberName = (actionRequest.getParameter("groupMemberName1"); 
String groupMemberEmail = (actionRequest.getParameter("groupMemberEmail1");
String groupMemberGender = actionRequest.getParameter("groupMemberGender1");
String groupMemberJob = (actionRequest.getParameter("groupMemberJob1"); 
long userGroupId = Long.parseLong((actionRequest.getParameter("groupID1")));
boolean female = true;
if(Validator.isNotNull(groupMemberGender)) if(groupMemberGender.equalsIgnoreCase("male")) female = false;
try {
try {
usr = UserLocalServiceUtil.getUserByEmailAddress(themeDisplay.getCompanyId(), groupMemberEmail);
} catch(NoSuchUserException ne){
//if user doesn't exist then enter this catch
//System.out.println("outer>>>..NoSuchUserException");
//ne.printStackTrace();
usr = UserLocalServiceUtil.createUser(CounterLocalServiceUtil.increment(User.class.getName()));
usr.setEmailAddress(groupMemberEmail);
usr.setFirstName(groupMemberName);
usr.setLastName(groupMemberName);
usr.setScreenName(groupMemberName+"_"+CounterLocalServiceUtil.increment(User.class.getName()));
try {
usr = UserLocalServiceUtil.addUser(loggedUser.getUserId(), companyId, true, "", "", true, "", groupMemberEmail, 0l,"", locale, groupMemberName, "", groupMemberName, 0, 0, female, 01, 04, 1987, "", new long[]{}, new long[]{}, new long[]{}, new long[]{}, true, serviceContext);
} catch (PortalException e) {
//System.out.println("outer>>>..PortalException");
e.printStackTrace();
}
}
catch (PortalException e) {
//System.out.println("inner>>>..PortalException");
e.printStackTrace();
}
//System.out.println("HELLOO>>>>>"+usr.getUserId()+">>>>>>>>>>"+userGroupId);
UserGroupLocalServiceUtil.addUserUserGroup(usr.getUserId(), userGroupId);
}
catch (SystemException e) {
//System.out.println("outer>>>..SystemException");
e.printStackTrace();
}

}

No comments:

Post a Comment