001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.Company;
020    
021    import java.util.Date;
022    import java.util.List;
023    
024    import org.apache.abdera.factory.Factory;
025    import org.apache.abdera.i18n.iri.IRI;
026    import org.apache.abdera.model.Entry;
027    import org.apache.abdera.model.Feed;
028    import org.apache.abdera.model.Person;
029    import org.apache.abdera.model.Text;
030    import org.apache.abdera.protocol.server.RequestContext;
031    import org.apache.abdera.protocol.server.TargetType;
032    import org.apache.abdera.protocol.server.context.ResponseContextException;
033    import org.apache.abdera.protocol.server.impl.AbstractEntityCollectionAdapter;
034    
035    /**
036     * @author Igor Spasic
037     */
038    public abstract class BaseEntityCollectionAdapter<T>
039            extends AbstractEntityCollectionAdapter<T> {
040    
041            @Override
042            public String getAuthor(RequestContext requestContext) {
043                    String author = null;
044    
045                    try {
046                            Company company = AtomUtil.getCompany();
047    
048                            author = company.getName();
049                    }
050                    catch (Exception e) {
051                            _log.error(e, e);
052                    }
053    
054                    return author;
055            }
056    
057            @Override
058            public String getHref(RequestContext requestContext) {
059                    return requestContext.urlFor(
060                            TargetType.TYPE_COLLECTION, collectionName);
061            }
062    
063            @Override
064            public String getId(RequestContext requestContext) {
065                    String id = AtomUtil.createIdTagPrefix(collectionName);
066    
067                    id = id.concat("feed");
068    
069                    return id;
070            }
071    
072            @Override
073            public String getId(T entry) {
074                    String id = AtomUtil.createIdTagPrefix(collectionName);
075    
076                    id = id.concat("entry:");
077                    id = id.concat(getName(entry));
078    
079                    return id;
080            }
081    
082            @Override
083            public String getName(T entry) {
084                    return getEntryId(entry);
085            }
086    
087            protected BaseEntityCollectionAdapter(String collectionName) {
088                    this.collectionName = collectionName;
089            }
090    
091            @Override
092            protected String addEntryDetails(
093                            RequestContext requestContext, Entry entry, IRI feedIri, T entryObj)
094                    throws ResponseContextException {
095    
096                    String link = getLink(entryObj, feedIri, requestContext);
097    
098                    entry.addLink(link);
099    
100                    List<Person> authors = getAuthors(entryObj, requestContext);
101    
102                    if (authors != null) {
103                            for (Person author : authors) {
104                                    entry.addAuthor(author);
105                            }
106                    }
107    
108                    entry.setId(getId(entryObj));
109    
110                    Text text = getSummary(entryObj, requestContext);
111    
112                    if (text != null) {
113                            entry.setSummaryElement(text);
114                    }
115    
116                    entry.setTitle(getTitle(entryObj));
117                    entry.setUpdated(getUpdated(entryObj));
118    
119                    return link;
120            }
121    
122            @Override
123            protected void addFeedDetails(Feed feed, RequestContext requestContext)
124                    throws ResponseContextException {
125    
126                    super.addFeedDetails(feed, requestContext);
127    
128                    AtomPager atomPager = AtomUtil.getPager(requestContext);
129    
130                    if (atomPager != null) {
131                            String url = String.valueOf(requestContext.getResolvedUri());
132    
133                            atomPager.setFeedPagingLinks(feed, url);
134                    }
135            }
136    
137            @Override
138            protected Feed createFeedBase(RequestContext requestContext) {
139                    Factory factory = requestContext.getAbdera().getFactory();
140    
141                    Feed feed = factory.newFeed();
142    
143                    feed.addAuthor(getAuthor(requestContext));
144    
145                    String url = String.valueOf(requestContext.getResolvedUri());
146    
147                    url = AtomUtil.resolveCollectionUrl(url, collectionName);
148    
149                    feed.addLink(url);
150                    feed.addLink(url, "self");
151    
152                    feed.setId(getId(requestContext));
153                    feed.setTitle(getTitle(requestContext));
154                    feed.setUpdated(new Date());
155    
156                    return feed;
157            }
158    
159            protected abstract String getEntryId(T entry);
160    
161            protected String collectionName;
162    
163            private static Log _log = LogFactoryUtil.getLog(
164                    BaseEntityCollectionAdapter.class);
165    
166    }