001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.atom;
016    
017    import com.liferay.portal.kernel.atom.AtomRequestContext;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.auth.CompanyThreadLocal;
026    import com.liferay.portal.service.CompanyLocalServiceUtil;
027    import com.liferay.portal.util.PortalUtil;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    import org.apache.abdera.protocol.server.RequestContext;
032    
033    /**
034     * @author Igor Spasic
035     */
036    public class AtomUtil {
037    
038            public static String createCollectionLink(
039                    AtomRequestContext atomRequestContext, String collectionName) {
040    
041                    return createEntryLink(atomRequestContext, collectionName, null);
042            }
043    
044            public static String createEntryLink(
045                    AtomRequestContext atomRequestContext, String collectionName,
046                    String entryName) {
047    
048                    StringBundler sb = new StringBundler(5);
049    
050                    String targetBasePath = atomRequestContext.getTargetBasePath();
051    
052                    sb.append(targetBasePath);
053    
054                    sb.append(CharPool.SLASH);
055                    sb.append(collectionName);
056    
057                    if (entryName != null) {
058                            sb.append(CharPool.SLASH);
059                            sb.append(entryName);
060                    }
061    
062                    String entryLink = sb.toString();
063    
064                    String resolvedUri = atomRequestContext.getResolvedUri();
065    
066                    int pos = resolvedUri.indexOf(targetBasePath);
067    
068                    if (pos != -1) {
069                            entryLink = resolvedUri.substring(0, pos) + entryLink;
070                    }
071    
072                    return entryLink;
073            }
074    
075            public static String createFeedTitleFromPortletName(
076                    AtomRequestContext atomRequestContext, String portletId) {
077    
078                    String portletTitle = null;
079    
080                    try {
081                            Company company = getCompany();
082    
083                            portletTitle = company.getName();
084                    }
085                    catch (Exception e) {
086                            return null;
087                    }
088    
089                    User user = getUser(atomRequestContext);
090    
091                    portletTitle = portletTitle.concat(StringPool.SPACE);
092    
093                    portletTitle = portletTitle.concat(
094                            PortalUtil.getPortletTitle(portletId, user));
095    
096                    portletTitle = portletTitle.trim();
097    
098                    return portletTitle;
099            }
100    
101            public static String createIdTagPrefix(String title) {
102                    Company company = null;
103    
104                    try {
105                            company = getCompany();
106                    }
107                    catch (Exception e) {
108                            return StringPool.BLANK;
109                    }
110    
111                    StringBundler sb = new StringBundler(5);
112    
113                    sb.append("tag:");
114                    sb.append(company.getWebId());
115                    sb.append(StringPool.COLON);
116                    sb.append(title);
117                    sb.append(StringPool.COLON);
118    
119                    String idTagPrefix = sb.toString();
120    
121                    return idTagPrefix.toLowerCase();
122            }
123    
124            public static Company getCompany() throws PortalException, SystemException {
125                    long companyId = CompanyThreadLocal.getCompanyId();
126    
127                    return CompanyLocalServiceUtil.getCompanyById(companyId);
128            }
129    
130            public static AtomPager getPager(RequestContext requestContext) {
131                    return (AtomPager)requestContext.getAttribute(
132                            RequestContext.Scope.REQUEST, _PAGER);
133            }
134    
135            public static User getUser(AtomRequestContext atomRequestContext) {
136                    return (User)atomRequestContext.getRequestAttribute(_USER);
137            }
138    
139            public static String resolveCollectionUrl(
140                    String url, String collectionName) {
141    
142                    String collection = CharPool.SLASH + collectionName + CharPool.SLASH;
143    
144                    int collectionIndex = url.indexOf(collection);
145    
146                    if (collectionIndex == -1) {
147                            return url;
148                    }
149    
150                    collectionIndex += collectionName.length() + 1;
151    
152                    int questionIndex = url.indexOf(CharPool.QUESTION, collectionIndex);
153    
154                    if (questionIndex != -1) {
155                            url =
156                                    url.substring(0, collectionIndex) +
157                                            url.substring(questionIndex);
158                    }
159                    else {
160                            url = url.substring(0, collectionIndex);
161                    }
162    
163                    return url;
164            }
165    
166            public static void saveAtomPagerInRequest(
167                    AtomRequestContext atomRequestContext, AtomPager atomPager) {
168    
169                    atomRequestContext.setRequestAttribute(_PAGER, atomPager);
170            }
171    
172            public static void saveUserInRequest(
173                    HttpServletRequest request, User user) {
174    
175                    request.setAttribute(_USER, user);
176            }
177    
178            public static String setPageInUrl(String url, int page) {
179                    int pageIndex = url.indexOf("page=");
180    
181                    if (pageIndex == -1) {
182                            int questionIndex = url.indexOf(CharPool.QUESTION);
183    
184                            if (questionIndex == -1) {
185                                    url += CharPool.AMPERSAND;
186                            }
187                            else {
188                                    url += CharPool.AMPERSAND;
189                            }
190    
191                            return url + "page=" + page;
192                    }
193    
194                    int endIndex = url.indexOf(CharPool.AMPERSAND, pageIndex);
195    
196                    if (endIndex == -1) {
197                            url = url.substring(0, pageIndex);
198                    }
199                    else {
200                            url = url.substring(0, pageIndex) + url.substring(endIndex + 1);
201    
202                            url += CharPool.AMPERSAND;
203                    }
204    
205                    url += "page=" + page;
206    
207                    return url;
208            }
209    
210            private static final String _PAGER = AtomUtil.class.getName() + ".pager";
211    
212            private static final String _USER = AtomUtil.class.getName() + ".user";
213    
214    }