In this post I will discuss how to add group by using our program.
my jsp page
userGroup.jsp
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="com.liferay.portal.service.UserGroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.UserGroup"%>
<%@page import="com.liferay.portal.service.RegionServiceUtil"%>
<%@page import="com.liferay.portal.model.Region"%>
<%@page import="java.text.Format"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.service.PhoneLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Phone"%>
<%@page import="com.liferay.portal.model.Contact"%>
<%@page import="com.liferay.portal.model.Address"%>
<%@page import="com.liferay.portal.service.AddressLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.service.CountryServiceUtil"%>
<%@page import="com.liferay.portal.model.Country"%>
<%@page import="java.util.List"%>
<%@page import="com.register.common.RegisterConstants"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.util.Iterator"%>
<%
PortletURL updateUserPassword = renderResponse.createActionURL();
updateUserPassword.setParameter(ActionRequest.ACTION_NAME,"addUserGroup");
%>
<aui:form name="fm" method="POST" action="<%=updateUserPassword.toString()%>" >
<aui:input name="group_Name" label="groupName">
</aui:input>
<aui:input name="group_street1" label="street1" >
</aui:input>
<aui:input name="group_street2" label="street2" >
</aui:input>
<aui:input name="group_postalCode" label="postalCode" >
</aui:input>
<aui:input name="group_city" label="city" >
</aui:input>
<aui:select name="group_countryId" label="country"/>
<aui:select name="group_regionId" label="state" />
<aui:input name="group_phoneNumber">
</aui:input>
</aui:form>
my action class
NewGroupAdd.java
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalClassInvoker;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.service.AddressLocalServiceUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.PhoneLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserGroupLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.register.common.CommonUtil;
import com.register.common.RegisterConstants;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import com.mail.MailUtil;
import org.apache.log4j.Logger;
public class NewGroupAdd extends MVCPortlet {
public void addUserGroup(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException
{
ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);
String groupName = ParamUtil.getString(actionRequest, "group_Name");
String groupStreet1 = ParamUtil.getString(actionRequest, "group_street1");
String groupStreet2 = ParamUtil.getString(actionRequest, "group_street2");
String groupZipcode = ParamUtil.getString(actionRequest, "group_postalCode");
String groupCity = ParamUtil.getString(actionRequest, "group_city");
String groupPhoneNumber = ParamUtil.getString(actionRequest, "group_phoneNumber");
long groupCountryId = ParamUtil.getLong(actionRequest, "group_countryId");
long groupRegionId = ParamUtil.getLong(actionRequest, "group_regionId");
try {
userGroup = UserGroupLocalServiceUtil.addUserGroup(themeDisplay.getUserId(), themeDisplay.getCompanyId(), groupName, "", serviceContext);
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
try {
UserGroupLocalServiceUtil.addUserUserGroup(user.getUserId(), userGroup.getUserGroupId());
} catch (SystemException e) {
_log.error(e);
}
int businessAddressTypeId = CommonUtil.getAddressTypeId("business");
int businessPhoneTypeId = CommonUtil.getPhoneTypeId("business");
try {
userGroupAddress = AddressLocalServiceUtil.addAddress(user.getUserId(), UserGroup.class.getName(), userGroup.getUserGroupId(),
groupStreet1, groupStreet2, "", groupCity, groupZipcode, groupRegionId,
groupCountryId, businessAddressTypeId, true, true, serviceContext);
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
}
}
Whenever used any object first check that it is null or not then used that object specially when we are reading data from jsp to action class, some time bcz of spelling mistakes we are getting null value and if we use that object/string then null pointer exception generate.
my jsp page
userGroup.jsp
<%@page import="com.liferay.portal.service.UserLocalServiceUtil"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Group"%>
<%@page import="com.liferay.portal.service.UserGroupLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.UserGroup"%>
<%@page import="com.liferay.portal.service.RegionServiceUtil"%>
<%@page import="com.liferay.portal.model.Region"%>
<%@page import="java.text.Format"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="com.liferay.portal.service.PhoneLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.Phone"%>
<%@page import="com.liferay.portal.model.Contact"%>
<%@page import="com.liferay.portal.model.Address"%>
<%@page import="com.liferay.portal.service.AddressLocalServiceUtil"%>
<%@page import="com.liferay.portal.model.User"%>
<%@page import="com.liferay.portal.service.CountryServiceUtil"%>
<%@page import="com.liferay.portal.model.Country"%>
<%@page import="java.util.List"%>
<%@page import="com.register.common.RegisterConstants"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@page import="com.liferay.portal.kernel.util.ListUtil"%>
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="java.util.Iterator"%>
<%
PortletURL updateUserPassword = renderResponse.createActionURL();
updateUserPassword.setParameter(ActionRequest.ACTION_NAME,"addUserGroup");
%>
<aui:form name="fm" method="POST" action="<%=updateUserPassword.toString()%>" >
<aui:input name="group_Name" label="groupName">
</aui:input>
<aui:input name="group_street1" label="street1" >
</aui:input>
<aui:input name="group_street2" label="street2" >
</aui:input>
<aui:input name="group_postalCode" label="postalCode" >
</aui:input>
<aui:input name="group_city" label="city" >
</aui:input>
<aui:select name="group_countryId" label="country"/>
<aui:select name="group_regionId" label="state" />
<aui:input name="group_phoneNumber">
</aui:input>
</aui:form>
my action class
NewGroupAdd.java
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalClassInvoker;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.service.AddressLocalServiceUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.PhoneLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserGroupLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.register.common.CommonUtil;
import com.register.common.RegisterConstants;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletSession;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.servlet.http.HttpServletRequest;
import com.mail.MailUtil;
import org.apache.log4j.Logger;
public void addUserGroup(ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException
{
ServiceContext serviceContext = ServiceContextFactory.getInstance(User.class.getName(), actionRequest);
String groupName = ParamUtil.getString(actionRequest, "group_Name");
String groupStreet1 = ParamUtil.getString(actionRequest, "group_street1");
String groupStreet2 = ParamUtil.getString(actionRequest, "group_street2");
String groupZipcode = ParamUtil.getString(actionRequest, "group_postalCode");
String groupCity = ParamUtil.getString(actionRequest, "group_city");
String groupPhoneNumber = ParamUtil.getString(actionRequest, "group_phoneNumber");
long groupCountryId = ParamUtil.getLong(actionRequest, "group_countryId");
long groupRegionId = ParamUtil.getLong(actionRequest, "group_regionId");
try {
userGroup = UserGroupLocalServiceUtil.addUserGroup(themeDisplay.getUserId(), themeDisplay.getCompanyId(), groupName, "", serviceContext);
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
try {
UserGroupLocalServiceUtil.addUserUserGroup(user.getUserId(), userGroup.getUserGroupId());
} catch (SystemException e) {
_log.error(e);
}
int businessAddressTypeId = CommonUtil.getAddressTypeId("business");
int businessPhoneTypeId = CommonUtil.getPhoneTypeId("business");
try {
userGroupAddress = AddressLocalServiceUtil.addAddress(user.getUserId(), UserGroup.class.getName(), userGroup.getUserGroupId(),
groupStreet1, groupStreet2, "", groupCity, groupZipcode, groupRegionId,
groupCountryId, businessAddressTypeId, true, true, serviceContext);
} catch (PortalException e) {
_log.error(e);
} catch (SystemException e) {
_log.error(e);
}
}
}
Whenever used any object first check that it is null or not then used that object specially when we are reading data from jsp to action class, some time bcz of spelling mistakes we are getting null value and if we use that object/string then null pointer exception generate.
No comments:
Post a Comment