1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.util.GetterUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.util.PortletKeys;
24  
25  import java.util.Map;
26  
27  import javax.portlet.PortletMode;
28  import javax.portlet.WindowState;
29  
30  /**
31   * <a href="JournalContentFriendlyURLMapper.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Raymond Augé
35   */
36  public class JournalContentFriendlyURLMapper implements FriendlyURLMapper {
37  
38      public String buildPath(LiferayPortletURL portletURL) {
39          String friendlyURLPath = null;
40  
41          String strutsAction = GetterUtil.getString(
42              portletURL.getParameter("struts_action"));
43  
44          WindowState windowState = portletURL.getWindowState();
45  
46          if ((strutsAction.equals("/journal_content/view")) &&
47              ((windowState == null) ||
48               (!windowState.equals(LiferayWindowState.EXCLUSIVE) &&
49                !windowState.equals(LiferayWindowState.POP_UP)))) {
50  
51              String portletId = portletURL.getPortletId();
52              String groupId = portletURL.getParameter("groupId");
53              String articleId = portletURL.getParameter("articleId");
54              String templateId = portletURL.getParameter("templateId");
55  
56              if (Validator.isNotNull(portletId) &&
57                  Validator.isNotNull(groupId) &&
58                  Validator.isNotNull(articleId)) {
59  
60                  if (portletId.equals(_PORTLET_DEFAULT_INSTANCE)) {
61                      portletId = _PORTLET_ID;
62                  }
63  
64                  friendlyURLPath =
65                      "/journal_content/" + portletId + "/" + groupId + "/" +
66                          articleId;
67  
68                  portletURL.addParameterIncludedInPath("groupId");
69                  portletURL.addParameterIncludedInPath("articleId");
70  
71                  if (Validator.isNotNull(templateId)) {
72                      friendlyURLPath += "/" + templateId;
73  
74                      portletURL.addParameterIncludedInPath("templateId");
75                  }
76              }
77          }
78  
79          if (Validator.isNotNull(friendlyURLPath)) {
80              portletURL.addParameterIncludedInPath("p_p_id");
81  
82              portletURL.addParameterIncludedInPath("struts_action");
83          }
84  
85          return friendlyURLPath;
86      }
87  
88      public String getMapping() {
89          return _MAPPING;
90      }
91  
92      public boolean isCheckMappingWithPrefix() {
93          return _CHECK_MAPPING_WITH_PREFIX;
94      }
95  
96      public void populateParams(
97          String friendlyURLPath, Map<String, String[]> parameterMap) {
98  
99          int w = friendlyURLPath.indexOf("/", 1);
100         int x = friendlyURLPath.indexOf("/", w + 1);
101         int y = friendlyURLPath.indexOf("/", x + 1);
102         int z = friendlyURLPath.indexOf("/", y + 1);
103 
104         if (x == -1) {
105             return;
106         }
107 
108         String portletId = friendlyURLPath.substring(w + 1, x);
109 
110         String namespace =
111             StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
112 
113         if (Validator.equals(portletId, _PORTLET_ID)) {
114             portletId = _PORTLET_DEFAULT_INSTANCE;
115 
116             namespace = StringPool.UNDERLINE + portletId + StringPool.UNDERLINE;
117 
118             parameterMap.put("p_p_id", new String[] {portletId});
119             parameterMap.put(
120                 "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
121         }
122         else {
123             parameterMap.put("p_p_id", new String[] {portletId});
124             parameterMap.put(
125                 "p_p_state", new String[] {WindowState.NORMAL.toString()});
126         }
127 
128         parameterMap.put("p_p_lifecycle", new String[] {"0"});
129         parameterMap.put(
130             "p_p_mode", new String[] {PortletMode.VIEW.toString()});
131 
132         String groupId = friendlyURLPath.substring(x + 1, y);
133 
134         parameterMap.put(namespace + "groupId", new String[] {groupId});
135 
136         String articleId = null;
137 
138         if (z == -1) {
139             articleId =
140                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
141 
142             parameterMap.put(namespace + "articleId", new String[] {articleId});
143         }
144         else {
145             articleId = friendlyURLPath.substring(y + 1, z);
146 
147             parameterMap.put(namespace + "articleId", new String[] {articleId});
148 
149             String templateId =
150                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
151 
152             parameterMap.put(
153                 namespace + "templateId", new String[] {templateId});
154         }
155 
156         parameterMap.put(
157             namespace + "struts_action",
158             new String[] {"/journal_content/view"});
159     }
160 
161     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
162 
163     private static final String _MAPPING = "journal_content";
164 
165     private static final String _PORTLET_DEFAULT_INSTANCE =
166         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
167 
168     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
169 
170 }