1   /**
2    * Copyright (c) 2000-2007 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.util.GetterUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.util.PortletKeys;
31  
32  import java.util.Map;
33  
34  import javax.portlet.PortletMode;
35  import javax.portlet.WindowState;
36  
37  /**
38   * <a href="JournalContentFriendlyURLMapper.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Raymond Augé
42   *
43   */
44  public class JournalContentFriendlyURLMapper implements FriendlyURLMapper {
45  
46      public String getMapping() {
47          return _MAPPING;
48      }
49  
50      public String buildPath(LiferayPortletURL portletURL) {
51          String friendlyURLPath = null;
52  
53          String strutsAction = GetterUtil.getString(
54              portletURL.getParameter("struts_action"));
55  
56          if (strutsAction.equals("/journal_content/view")) {
57              String portletId = portletURL.getPortletId();
58              String groupId = portletURL.getParameter("groupId");
59              String articleId = portletURL.getParameter("articleId");
60              String templateId = portletURL.getParameter("templateId");
61  
62              if (Validator.isNotNull(portletId) &&
63                  Validator.isNotNull(groupId) &&
64                  Validator.isNotNull(articleId)) {
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              portletURL.addParameterIncludedInPath("struts_action");
84          }
85  
86          return friendlyURLPath;
87      }
88  
89      public void populateParams(String friendlyURLPath, Map params) {
90          int w = friendlyURLPath.indexOf("/", 1);
91          int x = friendlyURLPath.indexOf("/", w + 1);
92          int y = friendlyURLPath.indexOf("/", x + 1);
93          int z = friendlyURLPath.indexOf("/", y + 1);
94  
95          if (x == -1) {
96              return;
97          }
98  
99          String portletId = friendlyURLPath.substring(w + 1, x);
100 
101         String namespace =
102             StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
103 
104         if (Validator.equals(portletId, _PORTLET_ID)) {
105             portletId = _PORTLET_DEFAULT_INSTANCE;
106 
107             namespace = StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
108 
109             params.put("p_p_id", portletId);
110             params.put("p_p_state", WindowState.MAXIMIZED.toString());
111         }
112         else {
113             params.put("p_p_id", portletId);
114             params.put("p_p_state", WindowState.NORMAL.toString());
115         }
116 
117         params.put("p_p_action", "0");
118         params.put("p_p_mode", PortletMode.VIEW.toString());
119 
120         String groupId = friendlyURLPath.substring(x + 1, y);
121 
122         params.put(namespace + "groupId", groupId);
123 
124         String articleId = null;
125 
126         if (z == -1) {
127             articleId =
128                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
129 
130             params.put(namespace + "articleId", articleId);
131         }
132         else {
133             articleId = friendlyURLPath.substring(y + 1, z);
134 
135             params.put(namespace + "articleId", articleId);
136 
137             String templateId =
138                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
139 
140             params.put(namespace + "templateId", templateId);
141         }
142 
143         params.put(namespace + "struts_action", "/journal_content/view");
144     }
145 
146     private static final String _MAPPING = "journal_content";
147 
148     private static final String _PORTLET_DEFAULT_INSTANCE =
149         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
150 
151     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
152 
153 }