1   /**
2    * Copyright (c) 2000-2008 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                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
67                      portletId = _PORTLET_ID;
68                  }
69  
70                  friendlyURLPath =
71                      "/journal_content/" + portletId + "/" + groupId + "/" +
72                          articleId;
73  
74                  portletURL.addParameterIncludedInPath("groupId");
75                  portletURL.addParameterIncludedInPath("articleId");
76  
77                  if (Validator.isNotNull(templateId)) {
78                      friendlyURLPath += "/" + templateId;
79  
80                      portletURL.addParameterIncludedInPath("templateId");
81                  }
82              }
83          }
84  
85          if (Validator.isNotNull(friendlyURLPath)) {
86              portletURL.addParameterIncludedInPath("p_p_id");
87              portletURL.addParameterIncludedInPath("struts_action");
88          }
89  
90          return friendlyURLPath;
91      }
92  
93      public void populateParams(
94          String friendlyURLPath, Map<String, String[]> params) {
95  
96          int w = friendlyURLPath.indexOf("/", 1);
97          int x = friendlyURLPath.indexOf("/", w + 1);
98          int y = friendlyURLPath.indexOf("/", x + 1);
99          int z = friendlyURLPath.indexOf("/", y + 1);
100 
101         if (x == -1) {
102             return;
103         }
104 
105         String portletId = friendlyURLPath.substring(w + 1, x);
106 
107         String namespace =
108             StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
109 
110         if (Validator.equals(portletId, _PORTLET_ID)) {
111             portletId = _PORTLET_DEFAULT_INSTANCE;
112 
113             namespace = StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
114 
115             params.put("p_p_id", new String[] {portletId});
116             params.put(
117                 "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
118         }
119         else {
120             params.put("p_p_id", new String[] {portletId});
121             params.put(
122                 "p_p_state", new String[] {WindowState.NORMAL.toString()});
123         }
124 
125         params.put("p_p_lifecycle", new String[] {"0"});
126         params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
127 
128         String groupId = friendlyURLPath.substring(x + 1, y);
129 
130         params.put(namespace + "groupId", new String[] {groupId});
131 
132         String articleId = null;
133 
134         if (z == -1) {
135             articleId =
136                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
137 
138             params.put(namespace + "articleId", new String[] {articleId});
139         }
140         else {
141             articleId = friendlyURLPath.substring(y + 1, z);
142 
143             params.put(namespace + "articleId", new String[] {articleId});
144 
145             String templateId =
146                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
147 
148             params.put(namespace + "templateId", new String[] {templateId});
149         }
150 
151         params.put(
152             namespace + "struts_action",
153             new String[] {"/journal_content/view"});
154     }
155 
156     private static final String _MAPPING = "journal_content";
157 
158     private static final String _PORTLET_DEFAULT_INSTANCE =
159         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
160 
161     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
162 
163 }