001
014
015 package com.liferay.portlet.wiki.action;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.Validator;
021 import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
022 import com.liferay.portal.model.Layout;
023 import com.liferay.portal.security.auth.PrincipalException;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.ServiceContextFactory;
026 import com.liferay.portal.theme.ThemeDisplay;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portal.util.WebKeys;
029 import com.liferay.portlet.wiki.NoSuchNodeException;
030 import com.liferay.portlet.wiki.NoSuchPageException;
031 import com.liferay.portlet.wiki.model.WikiNode;
032 import com.liferay.portlet.wiki.model.WikiPage;
033 import com.liferay.portlet.wiki.model.WikiPageConstants;
034 import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
035 import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
036 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
037 import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
038 import com.liferay.portlet.wiki.util.WikiUtil;
039
040 import javax.portlet.PortletRequest;
041
042 import javax.servlet.http.HttpServletRequest;
043
044
048 public class ActionUtil {
049
050 public static WikiNode getFirstVisibleNode(PortletRequest portletRequest)
051 throws PortalException, SystemException {
052
053 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
054 WebKeys.THEME_DISPLAY);
055
056 WikiNode node = null;
057
058 int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
059 themeDisplay.getScopeGroupId());
060
061 if (nodesCount == 0) {
062 Layout layout = themeDisplay.getLayout();
063
064 ServiceContext serviceContext = ServiceContextFactory.getInstance(
065 WikiNode.class.getName(), portletRequest);
066
067 serviceContext.setAddGroupPermissions(true);
068
069 if (layout.isPublicLayout()) {
070 serviceContext.setAddGuestPermissions(true);
071 }
072 else {
073 serviceContext.setAddGuestPermissions(false);
074 }
075
076 node = WikiNodeLocalServiceUtil.addDefaultNode(
077 themeDisplay.getDefaultUserId(), serviceContext);
078 }
079 else {
080 node = WikiUtil.getFirstNode(portletRequest);
081
082 if (node == null) {
083 throw new PrincipalException();
084 }
085
086 return node;
087 }
088
089 portletRequest.setAttribute(WebKeys.WIKI_NODE, node);
090
091 return node;
092 }
093
094 public static WikiNode getNode(PortletRequest portletRequest)
095 throws Exception {
096
097 HttpServletRequest request = PortalUtil.getHttpServletRequest(
098 portletRequest);
099
100 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
101 WebKeys.THEME_DISPLAY);
102
103 long nodeId = ParamUtil.getLong(request, "nodeId");
104 String nodeName = ParamUtil.getString(request, "nodeName");
105
106 WikiNode node = null;
107
108 try {
109 if (nodeId > 0) {
110 node = WikiNodeServiceUtil.getNode(nodeId);
111 }
112 else if (Validator.isNotNull(nodeName)) {
113 node = WikiNodeServiceUtil.getNode(
114 themeDisplay.getScopeGroupId(), nodeName);
115 }
116 else {
117 throw new NoSuchNodeException();
118 }
119 }
120 catch (NoSuchNodeException nsne) {
121 node = ActionUtil.getFirstVisibleNode(portletRequest);
122 }
123
124 request.setAttribute(WebKeys.WIKI_NODE, node);
125
126 return node;
127 }
128
129 public static void getPage(HttpServletRequest request) throws Exception {
130 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
131 WebKeys.THEME_DISPLAY);
132
133 long nodeId = ParamUtil.getLong(request, "nodeId");
134 String title = ParamUtil.getString(request, "title");
135 double version = ParamUtil.getDouble(request, "version");
136
137 WikiNode node = null;
138
139 try {
140 if (nodeId > 0) {
141 node = WikiNodeServiceUtil.getNode(nodeId);
142 }
143 }
144 catch (NoSuchNodeException nsne) {
145 }
146
147 if (node == null) {
148 node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
149
150 if (node != null) {
151 nodeId = node.getNodeId();
152 }
153 }
154
155 if (Validator.isNull(title)) {
156 title = WikiPageConstants.FRONT_PAGE;
157 }
158
159 WikiPage page = null;
160
161 try {
162 page = WikiPageServiceUtil.getPage(nodeId, title, version);
163
164 if (page.isDraft()) {
165 throw new NoSuchPageException();
166 }
167 }
168 catch (NoSuchPageException nspe) {
169 if (title.equals(WikiPageConstants.FRONT_PAGE) && (version == 0)) {
170 ServiceContext serviceContext = new ServiceContext();
171
172 Layout layout = themeDisplay.getLayout();
173
174 serviceContext.setAddGroupPermissions(true);
175
176 if (layout.isPublicLayout()) {
177 serviceContext.setAddGuestPermissions(true);
178 }
179 else {
180 serviceContext.setAddGuestPermissions(false);
181 }
182
183 boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
184
185 try {
186 WorkflowThreadLocal.setEnabled(false);
187
188 page = WikiPageLocalServiceUtil.addPage(
189 themeDisplay.getDefaultUserId(), nodeId, title, null,
190 WikiPageConstants.NEW, true, serviceContext);
191 }
192 finally {
193 WorkflowThreadLocal.setEnabled(workflowEnabled);
194 }
195 }
196 else {
197 throw nspe;
198 }
199 }
200
201 request.setAttribute(WebKeys.WIKI_PAGE, page);
202 }
203
204 public static void getPage(PortletRequest portletRequest) throws Exception {
205 HttpServletRequest request = PortalUtil.getHttpServletRequest(
206 portletRequest);
207
208 getPage(request);
209 }
210
211 }