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