001
014
015 package com.liferay.portlet.wiki.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.Constants;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.security.auth.PrincipalException;
022 import com.liferay.portal.service.ServiceContext;
023 import com.liferay.portal.service.ServiceContextFactory;
024 import com.liferay.portal.struts.PortletAction;
025 import com.liferay.portlet.wiki.DuplicateNodeNameException;
026 import com.liferay.portlet.wiki.NoSuchNodeException;
027 import com.liferay.portlet.wiki.NodeNameException;
028 import com.liferay.portlet.wiki.model.WikiNode;
029 import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
030 import com.liferay.portlet.wiki.util.WikiCacheThreadLocal;
031 import com.liferay.portlet.wiki.util.WikiCacheUtil;
032
033 import javax.portlet.ActionRequest;
034 import javax.portlet.ActionResponse;
035 import javax.portlet.PortletConfig;
036 import javax.portlet.PortletPreferences;
037 import javax.portlet.RenderRequest;
038 import javax.portlet.RenderResponse;
039
040 import org.apache.struts.action.ActionForm;
041 import org.apache.struts.action.ActionForward;
042 import org.apache.struts.action.ActionMapping;
043
044
047 public class EditNodeAction extends PortletAction {
048
049 @Override
050 public void processAction(
051 ActionMapping actionMapping, ActionForm actionForm,
052 PortletConfig portletConfig, ActionRequest actionRequest,
053 ActionResponse actionResponse)
054 throws Exception {
055
056 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057
058 try {
059 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060 updateNode(actionRequest);
061 }
062 else if (cmd.equals(Constants.DELETE)) {
063 deleteNode(actionRequest);
064 }
065 else if (cmd.equals(Constants.SUBSCRIBE)) {
066 subscribeNode(actionRequest);
067 }
068 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
069 unsubscribeNode(actionRequest);
070 }
071
072 sendRedirect(actionRequest, actionResponse);
073 }
074 catch (Exception e) {
075 if (e instanceof NoSuchNodeException ||
076 e instanceof PrincipalException) {
077
078 SessionErrors.add(actionRequest, e.getClass());
079
080 setForward(actionRequest, "portlet.wiki.error");
081 }
082 else if (e instanceof DuplicateNodeNameException ||
083 e instanceof NodeNameException) {
084
085 SessionErrors.add(actionRequest, e.getClass());
086 }
087 else {
088 throw e;
089 }
090 }
091 }
092
093 @Override
094 public ActionForward render(
095 ActionMapping actionMapping, ActionForm actionForm,
096 PortletConfig portletConfig, RenderRequest renderRequest,
097 RenderResponse renderResponse)
098 throws Exception {
099
100 try {
101 long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
102
103 if (nodeId > 0) {
104 ActionUtil.getNode(renderRequest);
105 }
106 }
107 catch (Exception e) {
108 if (e instanceof NoSuchNodeException ||
109 e instanceof PrincipalException) {
110
111 SessionErrors.add(renderRequest, e.getClass());
112
113 return actionMapping.findForward("portlet.wiki.error");
114 }
115 else {
116 throw e;
117 }
118 }
119
120 return actionMapping.findForward(
121 getForward(renderRequest, "portlet.wiki.edit_node"));
122 }
123
124 protected void deleteNode(ActionRequest actionRequest) throws Exception {
125 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
126
127 String oldName = getNodeName(nodeId);
128
129 WikiCacheThreadLocal.setClearCache(false);
130
131 WikiNodeServiceUtil.deleteNode(nodeId);
132
133 WikiCacheUtil.clearCache(nodeId);
134
135 WikiCacheThreadLocal.setClearCache(true);
136
137 updatePreferences(actionRequest, oldName, StringPool.BLANK);
138 }
139
140 protected String getNodeName(long nodeId) throws Exception {
141 WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
142
143 return node.getName();
144 }
145
146 protected void subscribeNode(ActionRequest actionRequest) throws Exception {
147 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
148
149 WikiNodeServiceUtil.subscribeNode(nodeId);
150 }
151
152 protected void unsubscribeNode(ActionRequest actionRequest)
153 throws Exception {
154
155 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
156
157 WikiNodeServiceUtil.unsubscribeNode(nodeId);
158 }
159
160 protected void updateNode(ActionRequest actionRequest) throws Exception {
161 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
162
163 String name = ParamUtil.getString(actionRequest, "name");
164 String description = ParamUtil.getString(actionRequest, "description");
165
166 ServiceContext serviceContext = ServiceContextFactory.getInstance(
167 WikiNode.class.getName(), actionRequest);
168
169 if (nodeId <= 0) {
170
171
172
173 WikiNodeServiceUtil.addNode(name, description, serviceContext);
174 }
175 else {
176
177
178
179 String oldName = getNodeName(nodeId);
180
181 WikiNodeServiceUtil.updateNode(
182 nodeId, name, description, serviceContext);
183
184 updatePreferences(actionRequest, oldName, name);
185 }
186 }
187
188 protected void updatePreferences(
189 ActionRequest actionRequest, String oldName, String newName)
190 throws Exception {
191
192 PortletPreferences preferences = actionRequest.getPreferences();
193
194 String hiddenNodes = preferences.getValue(
195 "hiddenNodes", StringPool.BLANK);
196 String visibleNodes = preferences.getValue(
197 "visibleNodes", StringPool.BLANK);
198
199 String regex = oldName + ",?";
200
201 preferences.setValue(
202 "hiddenNodes", hiddenNodes.replaceFirst(regex, newName));
203 preferences.setValue(
204 "visibleNodes",
205 visibleNodes.replaceFirst(regex, newName));
206
207 preferences.store();
208 }
209
210 }