001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.journal.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
021    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
022    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.util.Tuple;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.asset.model.AssetRenderer;
032    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
035    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
036    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
037    import com.liferay.portlet.journal.model.JournalArticle;
038    import com.liferay.portlet.journal.model.JournalArticleResource;
039    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
040    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
041    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
042    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
043    import com.liferay.portlet.journal.service.permission.JournalPermission;
044    
045    import java.util.HashMap;
046    import java.util.List;
047    import java.util.Locale;
048    import java.util.Map;
049    
050    import javax.portlet.PortletRequest;
051    import javax.portlet.PortletURL;
052    import javax.portlet.WindowState;
053    import javax.portlet.WindowStateException;
054    
055    /**
056     * @author Julio Camarero
057     * @author Juan Fern??ndez
058     * @author Raymond Aug??
059     * @author Sergio Gonz??lez
060     */
061    public class JournalArticleAssetRendererFactory
062            extends BaseAssetRendererFactory {
063    
064            public static final String TYPE = "content";
065    
066            @Override
067            public AssetRenderer getAssetRenderer(long classPK, int type)
068                    throws PortalException, SystemException {
069    
070                    JournalArticle article =
071                            JournalArticleLocalServiceUtil.fetchJournalArticle(classPK);
072    
073                    if (article == null) {
074                            JournalArticleResource articleResource =
075                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
076                                            classPK);
077    
078                            if (type == TYPE_LATEST_APPROVED) {
079                                    article = JournalArticleLocalServiceUtil.fetchDisplayArticle(
080                                            articleResource.getGroupId(),
081                                            articleResource.getArticleId());
082                            }
083    
084                            if (article == null) {
085                                    article = JournalArticleLocalServiceUtil.fetchLatestArticle(
086                                            articleResource.getGroupId(),
087                                            articleResource.getArticleId(),
088                                            WorkflowConstants.STATUS_ANY);
089                            }
090    
091                            if ((article == null) && (type == TYPE_LATEST)) {
092                                    article = JournalArticleLocalServiceUtil.fetchLatestArticle(
093                                            classPK, WorkflowConstants.STATUS_ANY);
094                            }
095                    }
096    
097                    JournalArticleAssetRenderer journalArticleAssetRenderer =
098                            new JournalArticleAssetRenderer(article);
099    
100                    journalArticleAssetRenderer.setAssetRendererType(type);
101    
102                    return journalArticleAssetRenderer;
103            }
104    
105            @Override
106            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
107                    throws PortalException, SystemException {
108    
109                    JournalArticle article =
110                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
111                                    groupId, urlTitle);
112    
113                    return new JournalArticleAssetRenderer(article);
114            }
115    
116            @Override
117            public String getClassName() {
118                    return JournalArticle.class.getName();
119            }
120    
121            @Override
122            public List<Tuple> getClassTypeFieldNames(
123                            long classTypeId, Locale locale, int start, int end)
124                    throws Exception {
125    
126                    DDMStructure ddmStructure =
127                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
128    
129                    List<Tuple> fieldNames = getDDMStructureFieldNames(
130                            ddmStructure, locale);
131    
132                    return ListUtil.subList(fieldNames, start, end);
133            }
134    
135            @Override
136            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
137                    throws Exception {
138    
139                    DDMStructure ddmStructure =
140                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
141    
142                    List<Tuple> fieldNames = getDDMStructureFieldNames(
143                            ddmStructure, locale);
144    
145                    return fieldNames.size();
146            }
147    
148            @Override
149            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
150                    throws Exception {
151    
152                    Map<Long, String> classTypes = new HashMap<Long, String>();
153    
154                    List<DDMStructure> ddmStructures =
155                            DDMStructureServiceUtil.getStructures(
156                                    groupIds,
157                                    PortalUtil.getClassNameId(JournalArticle.class.getName()));
158    
159                    for (DDMStructure ddmStructure : ddmStructures) {
160                            classTypes.put(
161                                    ddmStructure.getStructureId(), ddmStructure.getName(locale));
162                    }
163    
164                    return classTypes;
165            }
166    
167            @Override
168            public String getType() {
169                    return TYPE;
170            }
171    
172            @Override
173            public String getTypeName(Locale locale, boolean hasSubtypes) {
174                    if (hasSubtypes) {
175                            return LanguageUtil.get(locale, "basic-web-content");
176                    }
177    
178                    return super.getTypeName(locale, hasSubtypes);
179            }
180    
181            @Override
182            public PortletURL getURLAdd(
183                    LiferayPortletRequest liferayPortletRequest,
184                    LiferayPortletResponse liferayPortletResponse) {
185    
186                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
187                            PortletKeys.JOURNAL);
188    
189                    portletURL.setParameter("struts_action", "/journal/edit_article");
190    
191                    return portletURL;
192            }
193    
194            @Override
195            public PortletURL getURLView(
196                    LiferayPortletResponse liferayPortletResponse,
197                    WindowState windowState) {
198    
199                    LiferayPortletURL liferayPortletURL =
200                            liferayPortletResponse.createLiferayPortletURL(
201                                    PortletKeys.JOURNAL, PortletRequest.RENDER_PHASE);
202    
203                    try {
204                            liferayPortletURL.setWindowState(windowState);
205                    }
206                    catch (WindowStateException wse) {
207                    }
208    
209                    return liferayPortletURL;
210            }
211    
212            @Override
213            public boolean hasAddPermission(
214                            PermissionChecker permissionChecker, long groupId, long classTypeId)
215                    throws Exception {
216    
217                    if ((classTypeId > 0) &&
218                            !DDMStructurePermission.contains(
219                                    permissionChecker, classTypeId, ActionKeys.VIEW)) {
220    
221                            return false;
222                    }
223    
224                    return JournalPermission.contains(
225                            permissionChecker, groupId, ActionKeys.ADD_ARTICLE);
226            }
227    
228            @Override
229            public boolean hasPermission(
230                            PermissionChecker permissionChecker, long classPK, String actionId)
231                    throws Exception {
232    
233                    return JournalArticlePermission.contains(
234                            permissionChecker, classPK, actionId);
235            }
236    
237            @Override
238            public boolean isLinkable() {
239                    return _LINKABLE;
240            }
241    
242            @Override
243            public boolean isListable(long classPK) throws SystemException {
244                    JournalArticle article =
245                            JournalArticleLocalServiceUtil.fetchLatestArticle(
246                                    classPK, WorkflowConstants.STATUS_APPROVED, true);
247    
248                    if ((article != null) && article.isIndexable()) {
249                            return true;
250                    }
251    
252                    return false;
253            }
254    
255            @Override
256            protected String getIconPath(ThemeDisplay themeDisplay) {
257                    return themeDisplay.getPathThemeImages() + "/common/history.png";
258            }
259    
260            private static final boolean _LINKABLE = true;
261    
262    }