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                !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[]> params) {
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             params.put("p_p_id", new String[] {portletId});
119             params.put(
120                 "p_p_state", new String[] {WindowState.MAXIMIZED.toString()});
121         }
122         else {
123             params.put("p_p_id", new String[] {portletId});
124             params.put(
125                 "p_p_state", new String[] {WindowState.NORMAL.toString()});
126         }
127 
128         params.put("p_p_lifecycle", new String[] {"0"});
129         params.put("p_p_mode", new String[] {PortletMode.VIEW.toString()});
130 
131         String groupId = friendlyURLPath.substring(x + 1, y);
132 
133         params.put(namespace + "groupId", new String[] {groupId});
134 
135         String articleId = null;
136 
137         if (z == -1) {
138             articleId =
139                 friendlyURLPath.substring(y + 1, friendlyURLPath.length());
140 
141             params.put(namespace + "articleId", new String[] {articleId});
142         }
143         else {
144             articleId = friendlyURLPath.substring(y + 1, z);
145 
146             params.put(namespace + "articleId", new String[] {articleId});
147 
148             String templateId =
149                 friendlyURLPath.substring(z + 1, friendlyURLPath.length());
150 
151             params.put(namespace + "templateId", new String[] {templateId});
152         }
153 
154         params.put(
155             namespace + "struts_action",
156             new String[] {"/journal_content/view"});
157     }
158 
159     private static final boolean _CHECK_MAPPING_WITH_PREFIX = true;
160 
161     private static final String _MAPPING = "journal_content";
162 
163     private static final String _PORTLET_DEFAULT_INSTANCE =
164         PortletKeys.JOURNAL_CONTENT + "_INSTANCE_0000";
165 
166     private static final String _PORTLET_ID = PortletKeys.JOURNAL_CONTENT;
167 
168 }