1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journalcontent;
24  
25  import com.liferay.portal.kernel.portlet.FriendlyURLMapper;
26  import com.liferay.portal.kernel.portlet.LiferayPortletURL;
27  import com.liferay.portal.kernel.portlet.LiferayWindowState;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.util.PortletKeys;
32  
33  import java.util.Map;
34  
35  import javax.portlet.PortletMode;
36  import javax.portlet.WindowState;
37  
38  /**
39   * <a href="JournalContentFriendlyURLMapper.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Raymond Augé
43   *
44   */
45  public class JournalContentFriendlyURLMapper implements FriendlyURLMapper {
46  
47      public String buildPath(LiferayPortletURL portletURL) {
48          String friendlyURLPath = null;
49  
50          String strutsAction = GetterUtil.getString(
51              portletURL.getParameter("struts_action"));
52  
53          WindowState windowState = portletURL.getWindowState();
54  
55          if ((strutsAction.equals("/journal_content/view")) &&
56              ((windowState == null) ||
57               (!windowState.equals(LiferayWindowState.EXCLUSIVE)))) {
58  
59              String portletId = portletURL.getPortletId();
60              String groupId = portletURL.getParameter("groupId");
61              String articleId = portletURL.getParameter("articleId");
62              String templateId = portletURL.getParameter("templateId");
63  
64              if (Validator.isNotNull(portletId) &&
65                  Validator.isNotNull(groupId) &&
66                  Validator.isNotNull(articleId)) {
67  
68                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
69                      portletId = _PORTLET_ID;
70                  }
71  
72                  friendlyURLPath =
73                      "/journal_content/" + portletId + "/" + groupId + "/" +
74                          articleId;
75  
76                  portletURL.addParameterIncludedInPath("groupId");
77                  portletURL.addParameterIncludedInPath("articleId");
78  
79                  if (Validator.isNotNull(templateId)) {
80                      friendlyURLPath += "/" + templateId;
81  
82                      portletURL.addParameterIncludedInPath("templateId");
83                  }
84              }
85          }
86  
87          if (Validator.isNotNull(friendlyURLPath)) {
88              portletURL.addParameterIncludedInPath("p_p_id");
89  
90              portletURL.addParameterIncludedInPath("struts_action");
91          }
92  
93          return friendlyURLPath;
94      }
95  
96      public String getMapping() {
97          return _MAPPING;
98      }
99  
100     public boolean isCheckMappingWithPrefix() {
101         return _CHECK_MAPPING_WITH_PREFIX;
102     }
103 
104     public void populateParams(
105         String friendlyURLPath, Map<String, String[]> params) {
106 
107         int w = friendlyURLPath.indexOf("/", 1);
108         int x = friendlyURLPath.indexOf("/", w + 1);
109         int y = friendlyURLPath.indexOf("/", x + 1);
110         int z = friendlyURLPath.indexOf("/", y + 1);
111 
112         if (x == -1) {
113             return;
114         }
115 
116         String portletId = friendlyURLPath.substring(w + 1, x);
117 
118         String namespace =
119             StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
120 
121         if (Validator.equals(portletId, _PORTLET_ID)) {
122             portletId = _PORTLET_DEFAULT_INSTANCE;
123 
124             namespace = StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
125 
126             params.put("p_p_id", new String[] {portletId});
127             params.put(
128                 "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
129         }
130         else {
131             params.put("p_p_id", new String[] {portletId});
132             params.put(
133                 "p_p_state", new String[] {WindowState.NORMAL.toString()});
134         }
135 
136         params.put("p_p_lifecycle", new String[] {"0"});
137         params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
138 
139         String groupId = friendlyURLPath.substring(x + 1, y);
140 
141         params.put(namespace + "groupId", new String[] {groupId});
142 
143         String articleId = null;
144 
145         if (z == -1) {
146             articleId =
147                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
148 
149             params.put(namespace + "articleId", new String[] {articleId});
150         }
151         else {
152             articleId = friendlyURLPath.substring(y + 1, z);
153 
154             params.put(namespace + "articleId", new String[] {articleId});
155 
156             String templateId =
157                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
158 
159             params.put(namespace + "templateId", new String[] {templateId});
160         }
161 
162         params.put(
163             namespace + "struts_action",
164             new String[] {"/journal_content/view"});
165     }
166 
167     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
168 
169     private static final String _MAPPING = "journal_content";
170 
171     private static final String _PORTLET_DEFAULT_INSTANCE =
172         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
173 
174     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
175 
176 }