001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.softwarecatalog;
022:
023: import com.liferay.portal.kernel.portlet.BaseFriendlyURLMapper;
024: import com.liferay.portal.kernel.portlet.LiferayPortletURL;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.util.PortletKeys;
030:
031: import java.util.Map;
032:
033: import javax.portlet.PortletMode;
034: import javax.portlet.WindowState;
035:
036: /**
037: * <a href="SCFriendlyURLMapper.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Jorge Ferrer
040: *
041: */
042: public class SCFriendlyURLMapper extends BaseFriendlyURLMapper {
043:
044: public String getMapping() {
045: return _MAPPING;
046: }
047:
048: public String getPortletId() {
049: return _PORTLET_ID;
050: }
051:
052: public String buildPath(LiferayPortletURL portletURL) {
053: String friendlyURL = null;
054:
055: String tabs1 = portletURL.getParameter("tabs1");
056:
057: String action = GetterUtil.getString(portletURL
058: .getParameter("struts_action"));
059:
060: if (action.equals("/software_catalog/view")) {
061: friendlyURL = "/software_catalog/" + tabs1;
062: } else if (action
063: .equals("/software_catalog/view_product_entry")) {
064: String productEntryId = portletURL
065: .getParameter("productEntryId");
066:
067: friendlyURL = "/software_catalog/products/"
068: + productEntryId;
069:
070: portletURL.addParameterIncludedInPath("productEntryId");
071: } else if (action
072: .equals("/software_catalog/edit_product_entry")) {
073: String productEntryId = portletURL
074: .getParameter("productEntryId");
075:
076: if (Validator.isNotNull(productEntryId)) {
077: friendlyURL = "/software_catalog/products/"
078: + productEntryId + "/edit";
079:
080: portletURL.addParameterIncludedInPath("productEntryId");
081: } else {
082: friendlyURL = "/software_catalog/products/new";
083: }
084: } else if (action
085: .equals("/software_catalog/edit_product_version")) {
086: String productEntryId = portletURL
087: .getParameter("productEntryId");
088: String productVersionId = portletURL
089: .getParameter("productVersionId");
090:
091: if (Validator.isNotNull(productVersionId)) {
092: friendlyURL = "/software_catalog/products/"
093: + productEntryId + "/versions/"
094: + productVersionId + "/edit";
095:
096: portletURL.addParameterIncludedInPath("productEntryId");
097: portletURL
098: .addParameterIncludedInPath("productVersionId");
099: } else {
100: friendlyURL = "/software_catalog/products/"
101: + productEntryId + "/versions/new";
102: }
103: } else if (action
104: .equals("/software_catalog/edit_framework_version")) {
105:
106: String frameworkVersionId = portletURL
107: .getParameter("frameworkVersionId");
108:
109: if (Validator.isNotNull(frameworkVersionId)) {
110: friendlyURL = "/software_catalog/framework_versions/"
111: + frameworkVersionId + "/edit";
112:
113: portletURL
114: .addParameterIncludedInPath("frameworkVersionId");
115: } else {
116: friendlyURL = "/software_catalog/framework_versions/new";
117: }
118: } else if (action.equals("/software_catalog/edit_license")) {
119:
120: String licenseId = portletURL.getParameter("licenseId");
121:
122: if (Validator.isNotNull(licenseId)) {
123: friendlyURL = "/software_catalog/licenses/" + licenseId
124: + "/edit";
125:
126: portletURL.addParameterIncludedInPath("licenseId");
127: } else {
128: friendlyURL = "/software_catalog/licenses/new";
129: }
130: } else if (action.equals("/software_catalog/search")) {
131:
132: friendlyURL = "/software_catalog/search";
133: }
134:
135: if (Validator.isNotNull(friendlyURL)) {
136: portletURL.addParameterIncludedInPath("p_p_id");
137: portletURL.addParameterIncludedInPath("struts_action");
138: portletURL.addParameterIncludedInPath("tabs1");
139: }
140:
141: return friendlyURL;
142: }
143:
144: public void populateParams(String friendlyURLPath, Map params) {
145: params.put("p_p_id", _PORTLET_ID);
146:
147: if (!params.containsKey("p_p_action")) {
148: params.put("p_p_action", "0");
149: }
150:
151: params.put("p_p_state", WindowState.MAXIMIZED.toString());
152: params.put("p_p_mode", PortletMode.VIEW.toString());
153:
154: int x = friendlyURLPath.indexOf(StringPool.SLASH, 1);
155:
156: String[] urlFragments = StringUtil.split(friendlyURLPath
157: .substring(x + 1), StringPool.SLASH);
158:
159: String resourceIdParam = getResourceIdParam(urlFragments[0]);
160:
161: if (urlFragments.length == 1) {
162: addParam(params, "struts_action", "/software_catalog/view");
163: addParam(params, "tabs1", urlFragments[0]);
164: } else if (urlFragments.length == 2) {
165: if (urlFragments[1].equals("new")) {
166: addParam(params, "struts_action",
167: getEditAction(urlFragments[0]));
168: addParam(params, "tabs1", urlFragments[0]);
169: } else if (urlFragments[0].equals("products")) {
170: addParam(params, "struts_action",
171: "/software_catalog/view_product_entry");
172: addParam(params, "tabs1", urlFragments[0]);
173: addParam(params, resourceIdParam, urlFragments[1]);
174: }
175: } else if (urlFragments.length == 3) {
176: if (urlFragments[2].equals("edit")) {
177: addParam(params, "struts_action",
178: getEditAction(urlFragments[0]));
179: addParam(params, "tabs1", urlFragments[0]);
180: addParam(params, resourceIdParam, urlFragments[1]);
181: }
182: } else if (urlFragments.length == 4) {
183: if (urlFragments[3].equals("new")) {
184: addParam(params, "struts_action",
185: getEditAction(urlFragments[2]));
186: addParam(params, "tabs1", urlFragments[0]);
187: addParam(params, resourceIdParam, urlFragments[1]);
188: }
189: } else if (urlFragments.length == 5) {
190: if (urlFragments[0].equals("products")
191: && urlFragments[4].equals("edit")) {
192:
193: addParam(params, "struts_action",
194: getEditAction(urlFragments[2]));
195: addParam(params, "tabs1", urlFragments[0]);
196: addParam(params, resourceIdParam, urlFragments[1]);
197: addParam(params, getResourceIdParam(urlFragments[2]),
198: urlFragments[3]);
199: }
200: }
201: }
202:
203: protected String getEditAction(String resource) {
204: String action = null;
205:
206: if (resource.equals("my_products")
207: || resource.equals("products")) {
208: action = "edit_product_entry";
209: } else if (resource.equals("versions")) {
210: action = "edit_product_version";
211: } else if (resource.equals("framework_versions")) {
212: action = "edit_framework_version";
213: } else if (resource.equals("licenses")) {
214: action = "edit_license";
215: } else {
216: return null;
217: }
218:
219: return "/software_catalog/" + action;
220: }
221:
222: protected String getResourceIdParam(String resource) {
223: if (resource.equals("my_products")
224: || resource.equals("products")) {
225: return "productEntryId";
226: } else if (resource.equals("versions")) {
227: return "productVersionId";
228: } else if (resource.equals("framework_versions")) {
229: return "frameworkVersionId";
230: } else if (resource.equals("licenses")) {
231: return "licenseId";
232: } else if (resource.equals("discussion")) {
233: return "messageId";
234: } else {
235: return null;
236: }
237: }
238:
239: private static final String _MAPPING = "software_catalog";
240:
241: private static final String _PORTLET_ID = PortletKeys.SOFTWARE_CATALOG;
242:
243: }
|