1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journalcontent;
16  
17  import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
18  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
19  import com.liferay.portal.kernel.portlet.LiferayWindowState;
20  import com.liferay.portal.kernel.portlet.Router;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.PortletKeys;
26  
27  import java.util.Map;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.WindowState;
31  
32  /**
33   * <a href="JournalContentFriendlyURLMapper.java.html"><b><i>View Source</i></b>
34   * </a>
35   *
36   * @author Raymond Augé
37   */
38  public class JournalContentFriendlyURLMapper implements FriendlyURLMapper {
39  
40      public String buildPath(LiferayPortletURL portletURL) {
41          String friendlyURLPath = null;
42  
43          String strutsAction = GetterUtil.getString(
44              portletURL.getParameter("struts_action"));
45  
46          WindowState windowState = portletURL.getWindowState();
47  
48          if ((strutsAction.equals("/journal_content/view")) &&
49              ((windowState == null) ||
50               (!windowState.equals(LiferayWindowState.EXCLUSIVE) &&
51                !windowState.equals(LiferayWindowState.POP_UP)))) {
52  
53              String portletId = portletURL.getPortletId();
54              String groupId = portletURL.getParameter("groupId");
55              String articleId = portletURL.getParameter("articleId");
56              String templateId = portletURL.getParameter("templateId");
57  
58              if (Validator.isNotNull(portletId) &&
59                  Validator.isNotNull(groupId) &&
60                  Validator.isNotNull(articleId)) {
61  
62                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
63                      portletId = _PORTLET_ID;
64                  }
65  
66                  friendlyURLPath =
67                      "/journal_content/" + portletId + "/" + groupId + "/" +
68                          articleId;
69  
70                  portletURL.addParameterIncludedInPath("groupId");
71                  portletURL.addParameterIncludedInPath("articleId");
72  
73                  if (Validator.isNotNull(templateId)) {
74                      friendlyURLPath += "/" + templateId;
75  
76                      portletURL.addParameterIncludedInPath("templateId");
77                  }
78              }
79          }
80  
81          if (Validator.isNotNull(friendlyURLPath)) {
82              portletURL.addParameterIncludedInPath("p_p_id");
83  
84              portletURL.addParameterIncludedInPath("struts_action");
85          }
86  
87          return friendlyURLPath;
88      }
89  
90      public String getMapping() {
91          return _MAPPING;
92      }
93  
94      public Router getRouter() {
95          return router;
96      }
97  
98      public boolean isCheckMappingWithPrefix() {
99          return _CHECK_MAPPING_WITH_PREFIX;
100     }
101 
102     public void populateParams(
103         String friendlyURLPath, Map<String, String[]> parameterMap,
104         Map<String, Object> requestContext) {
105 
106         int w = friendlyURLPath.indexOf("/", 1);
107         int x = friendlyURLPath.indexOf("/", w + 1);
108         int y = friendlyURLPath.indexOf("/", x + 1);
109         int z = friendlyURLPath.indexOf("/", y + 1);
110 
111         if (x == -1) {
112             return;
113         }
114 
115         String portletId = friendlyURLPath.substring(w + 1, x);
116 
117         String namespace = PortalUtil.getPortletNamespace(portletId);
118 
119         if (portletId.equals(_PORTLET_ID)) {
120             portletId = _PORTLET_DEFAULT_INSTANCE;
121 
122             namespace = StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
123 
124             parameterMap.put("p_p_id", new String[] {portletId});
125             parameterMap.put(
126                 "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
127         }
128         else {
129             parameterMap.put("p_p_id", new String[] {portletId});
130             parameterMap.put(
131                 "p_p_state", new String[] {WindowState.NORMAL.toString()});
132         }
133 
134         parameterMap.put("p_p_lifecycle", new String[] {"0"});
135         parameterMap.put(
136             "p_p_mode", new String[] {PortletMode.VIEW.toString()});
137 
138         String groupId = friendlyURLPath.substring(x + 1, y);
139 
140         parameterMap.put(namespace + "groupId", new String[] {groupId});
141 
142         String articleId = null;
143 
144         if (z == -1) {
145             articleId =
146                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
147 
148             parameterMap.put(namespace + "articleId", new String[] {articleId});
149         }
150         else {
151             articleId = friendlyURLPath.substring(y + 1, z);
152 
153             parameterMap.put(namespace + "articleId", new String[] {articleId});
154 
155             String templateId =
156                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
157 
158             parameterMap.put(
159                 namespace + "templateId", new String[] {templateId});
160         }
161 
162         parameterMap.put(
163             namespace + "struts_action",
164             new String[] {"/journal_content/view"});
165     }
166 
167     public void setRouter(Router router) {
168         this.router = router;
169     }
170 
171     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
172 
173     private static final String _MAPPING = "journal_content";
174 
175     private static final String _PORTLET_DEFAULT_INSTANCE =
176         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
177 
178     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
179 
180     protected Router router;
181 
182 }