001
014
015 package com.liferay.portlet.communities.action;
016
017 import com.liferay.portal.DuplicateGroupException;
018 import com.liferay.portal.GroupFriendlyURLException;
019 import com.liferay.portal.GroupNameException;
020 import com.liferay.portal.NoSuchGroupException;
021 import com.liferay.portal.RequiredGroupException;
022 import com.liferay.portal.kernel.dao.orm.QueryUtil;
023 import com.liferay.portal.kernel.servlet.SessionErrors;
024 import com.liferay.portal.kernel.util.Constants;
025 import com.liferay.portal.kernel.util.ParamUtil;
026 import com.liferay.portal.liveusers.LiveUsers;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.model.GroupConstants;
029 import com.liferay.portal.model.MembershipRequest;
030 import com.liferay.portal.model.MembershipRequestConstants;
031 import com.liferay.portal.security.auth.PrincipalException;
032 import com.liferay.portal.service.GroupServiceUtil;
033 import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
034 import com.liferay.portal.service.MembershipRequestServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.service.ServiceContextFactory;
037 import com.liferay.portal.struts.PortletAction;
038 import com.liferay.portal.theme.ThemeDisplay;
039 import com.liferay.portal.util.PortalUtil;
040 import com.liferay.portal.util.WebKeys;
041 import com.liferay.portlet.communities.util.CommunitiesUtil;
042
043 import java.util.List;
044
045 import javax.portlet.ActionRequest;
046 import javax.portlet.ActionResponse;
047 import javax.portlet.PortletConfig;
048 import javax.portlet.RenderRequest;
049 import javax.portlet.RenderResponse;
050
051 import org.apache.struts.action.ActionForm;
052 import org.apache.struts.action.ActionForward;
053 import org.apache.struts.action.ActionMapping;
054
055
058 public class EditGroupAction extends PortletAction {
059
060 public void processAction(
061 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062 ActionRequest actionRequest, ActionResponse actionResponse)
063 throws Exception {
064
065 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066
067 try {
068 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
069 updateGroup(actionRequest);
070 }
071 else if (cmd.equals(Constants.DELETE)) {
072 deleteGroup(actionRequest);
073 }
074
075 sendRedirect(actionRequest, actionResponse);
076 }
077 catch (Exception e) {
078 if (e instanceof NoSuchGroupException ||
079 e instanceof PrincipalException) {
080
081 SessionErrors.add(actionRequest, e.getClass().getName());
082
083 setForward(actionRequest, "portlet.communities.error");
084 }
085 else if (e instanceof DuplicateGroupException ||
086 e instanceof GroupFriendlyURLException ||
087 e instanceof GroupNameException ||
088 e instanceof RequiredGroupException) {
089
090 SessionErrors.add(actionRequest, e.getClass().getName(), e);
091
092 if (cmd.equals(Constants.DELETE)) {
093 actionResponse.sendRedirect(
094 ParamUtil.getString(actionRequest, "redirect"));
095 }
096 }
097 else {
098 throw e;
099 }
100 }
101 }
102
103 public ActionForward render(
104 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105 RenderRequest renderRequest, RenderResponse renderResponse)
106 throws Exception {
107
108 try {
109 ActionUtil.getGroup(renderRequest);
110 }
111 catch (Exception e) {
112 if (e instanceof NoSuchGroupException ||
113 e instanceof PrincipalException) {
114
115 SessionErrors.add(renderRequest, e.getClass().getName());
116
117 return mapping.findForward("portlet.communities.error");
118 }
119 else {
120 throw e;
121 }
122 }
123
124 return mapping.findForward(
125 getForward(renderRequest, "portlet.communities.edit_community"));
126 }
127
128 protected void deleteGroup(ActionRequest actionRequest) throws Exception {
129 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130 WebKeys.THEME_DISPLAY);
131
132 long groupId = ParamUtil.getLong(actionRequest, "groupId");
133
134 if ((groupId == themeDisplay.getDoAsGroupId()) ||
135 (groupId == themeDisplay.getScopeGroupId())) {
136
137 throw new RequiredGroupException(String.valueOf(groupId));
138 }
139
140 GroupServiceUtil.deleteGroup(groupId);
141
142 LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
143 }
144
145 protected void updateGroup(ActionRequest actionRequest) throws Exception {
146 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
147 WebKeys.THEME_DISPLAY);
148
149 long userId = PortalUtil.getUserId(actionRequest);
150
151 long groupId = ParamUtil.getLong(actionRequest, "groupId");
152
153 String name = ParamUtil.getString(actionRequest, "name");
154 String description = ParamUtil.getString(actionRequest, "description");
155 int type = ParamUtil.getInteger(actionRequest, "type");
156 String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
157 boolean active = ParamUtil.getBoolean(actionRequest, "active");
158
159 ServiceContext serviceContext = ServiceContextFactory.getInstance(
160 Group.class.getName(), actionRequest);
161
162 Group group = null;
163
164 if (groupId <= 0) {
165
166
167
168 group = GroupServiceUtil.addGroup(
169 name, description, type, friendlyURL, active, serviceContext);
170
171 LiveUsers.joinGroup(
172 themeDisplay.getCompanyId(), group.getGroupId(), userId);
173 }
174 else {
175
176
177
178 group = GroupServiceUtil.updateGroup(
179 groupId, name, description, type, friendlyURL, active,
180 serviceContext);
181
182 if (type == GroupConstants.TYPE_COMMUNITY_OPEN) {
183 List<MembershipRequest> membershipRequests =
184 MembershipRequestLocalServiceUtil.search(
185 groupId, MembershipRequestConstants.STATUS_PENDING,
186 QueryUtil.ALL_POS, QueryUtil.ALL_POS);
187
188 for (MembershipRequest membershipRequest : membershipRequests) {
189 MembershipRequestServiceUtil.updateStatus(
190 membershipRequest.getMembershipRequestId(),
191 themeDisplay.translate(
192 "your-membership-has-been-approved"),
193 MembershipRequestConstants.STATUS_APPROVED);
194
195 LiveUsers.joinGroup(
196 themeDisplay.getCompanyId(),
197 membershipRequest.getGroupId(),
198 new long[] {membershipRequest.getUserId()});
199 }
200 }
201 }
202
203
204
205 long publicLayoutSetPrototypeId = ParamUtil.getLong(
206 actionRequest, "publicLayoutSetPrototypeId");
207 long privateLayoutSetPrototypeId = ParamUtil.getLong(
208 actionRequest, "privateLayoutSetPrototypeId");
209
210 CommunitiesUtil.applyLayoutSetPrototypes(
211 group, publicLayoutSetPrototypeId, privateLayoutSetPrototypeId);
212 }
213
214 }