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.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.GetterUtil;
024    import com.liferay.portal.kernel.util.ListUtil;
025    import com.liferay.portal.kernel.util.Tuple;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.PortletURLFactoryUtil;
034    import com.liferay.portlet.asset.model.AssetRenderer;
035    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
038    import com.liferay.portlet.dynamicdatamapping.service.permission.DDMStructurePermission;
039    import com.liferay.portlet.journal.NoSuchArticleException;
040    import com.liferay.portlet.journal.model.JournalArticle;
041    import com.liferay.portlet.journal.model.JournalArticleResource;
042    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
043    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
044    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
045    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
046    import com.liferay.portlet.journal.service.permission.JournalPermission;
047    
048    import java.util.HashMap;
049    import java.util.List;
050    import java.util.Locale;
051    import java.util.Map;
052    
053    import javax.portlet.PortletRequest;
054    import javax.portlet.PortletURL;
055    import javax.portlet.WindowState;
056    import javax.portlet.WindowStateException;
057    
058    /**
059     * @author Julio Camarero
060     * @author Juan Fern??ndez
061     * @author Raymond Aug??
062     * @author Sergio Gonz??lez
063     */
064    public class JournalArticleAssetRendererFactory
065            extends BaseAssetRendererFactory {
066    
067            public static final String TYPE = "content";
068    
069            @Override
070            public AssetRenderer getAssetRenderer(long classPK, int type)
071                    throws PortalException, SystemException {
072    
073                    JournalArticle article = null;
074    
075                    try {
076                            article = JournalArticleLocalServiceUtil.getArticle(classPK);
077                    }
078                    catch (NoSuchArticleException nsae1) {
079                            JournalArticleResource articleResource =
080                                    JournalArticleResourceLocalServiceUtil.getArticleResource(
081                                            classPK);
082    
083                            boolean approvedArticleAvailable = true;
084    
085                            if (type == TYPE_LATEST_APPROVED) {
086                                    try {
087                                            article = JournalArticleLocalServiceUtil.getDisplayArticle(
088                                                    articleResource.getGroupId(),
089                                                    articleResource.getArticleId());
090                                    }
091                                    catch (NoSuchArticleException nsae2) {
092                                            approvedArticleAvailable = false;
093                                    }
094                            }
095    
096                            if ((type != TYPE_LATEST_APPROVED) || !approvedArticleAvailable) {
097                                    article = JournalArticleLocalServiceUtil.getLatestArticle(
098                                            articleResource.getGroupId(),
099                                            articleResource.getArticleId(),
100                                            WorkflowConstants.STATUS_ANY);
101                            }
102                    }
103    
104                    JournalArticleAssetRenderer journalArticleAssetRenderer =
105                            new JournalArticleAssetRenderer(article);
106    
107                    journalArticleAssetRenderer.setAssetRendererType(type);
108    
109                    return journalArticleAssetRenderer;
110            }
111    
112            @Override
113            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
114                    throws PortalException, SystemException {
115    
116                    JournalArticle article =
117                            JournalArticleServiceUtil.getDisplayArticleByUrlTitle(
118                                    groupId, urlTitle);
119    
120                    return new JournalArticleAssetRenderer(article);
121            }
122    
123            @Override
124            public String getClassName() {
125                    return JournalArticle.class.getName();
126            }
127    
128            @Override
129            public List<Tuple> getClassTypeFieldNames(
130                            long classTypeId, Locale locale, int start, int end)
131                    throws Exception {
132    
133                    DDMStructure ddmStructure =
134                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
135    
136                    List<Tuple> fieldNames = getDDMStructureFieldNames(
137                            ddmStructure, locale);
138    
139                    return ListUtil.subList(fieldNames, start, end);
140            }
141    
142            @Override
143            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
144                    throws Exception {
145    
146                    DDMStructure ddmStructure =
147                            DDMStructureLocalServiceUtil.getDDMStructure(classTypeId);
148    
149                    List<Tuple> fieldNames = getDDMStructureFieldNames(
150                            ddmStructure, locale);
151    
152                    return fieldNames.size();
153            }
154    
155            @Override
156            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
157                    throws Exception {
158    
159                    Map<Long, String> classTypes = new HashMap<Long, String>();
160    
161                    for (long groupId : groupIds) {
162                            List<DDMStructure> ddmStructures =
163                                    DDMStructureLocalServiceUtil.getStructures(
164                                            groupId,
165                                            PortalUtil.getClassNameId(JournalArticle.class.getName()));
166    
167                            for (DDMStructure ddmStructure : ddmStructures) {
168                                    classTypes.put(
169                                            ddmStructure.getStructureId(),
170                                            ddmStructure.getName(locale));
171                            }
172                    }
173    
174                    return classTypes;
175            }
176    
177            @Override
178            public String getType() {
179                    return TYPE;
180            }
181    
182            @Override
183            public String getTypeName(Locale locale, boolean hasSubtypes) {
184                    if (hasSubtypes) {
185                            return LanguageUtil.get(locale, "basic-web-content");
186                    }
187    
188                    return super.getTypeName(locale, hasSubtypes);
189            }
190    
191            @Override
192            public PortletURL getURLAdd(
193                            LiferayPortletRequest liferayPortletRequest,
194                            LiferayPortletResponse liferayPortletResponse)
195                    throws PortalException, SystemException {
196    
197                    ThemeDisplay themeDisplay =
198                            (ThemeDisplay)liferayPortletRequest.getAttribute(
199                                    WebKeys.THEME_DISPLAY);
200    
201                    if (!JournalPermission.contains(
202                                    themeDisplay.getPermissionChecker(),
203                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_ARTICLE)) {
204    
205                            return null;
206                    }
207    
208                    long classTypeId = GetterUtil.getLong(
209                            liferayPortletRequest.getAttribute(
210                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
211    
212                    if ((classTypeId > 0) &&
213                            !DDMStructurePermission.contains(
214                                    themeDisplay.getPermissionChecker(), classTypeId,
215                                    ActionKeys.VIEW)) {
216    
217                            return null;
218                    }
219    
220                    PortletURL portletURL = PortletURLFactoryUtil.create(
221                            liferayPortletRequest, PortletKeys.JOURNAL,
222                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
223    
224                    portletURL.setParameter("struts_action", "/journal/edit_article");
225    
226                    return portletURL;
227            }
228    
229            @Override
230            public PortletURL getURLView(
231                    LiferayPortletResponse liferayPortletResponse,
232                    WindowState windowState) {
233    
234                    LiferayPortletURL liferayPortletURL =
235                            liferayPortletResponse.createLiferayPortletURL(
236                                    PortletKeys.JOURNAL, PortletRequest.RENDER_PHASE);
237    
238                    try {
239                            liferayPortletURL.setWindowState(windowState);
240                    }
241                    catch (WindowStateException wse) {
242                    }
243    
244                    return liferayPortletURL;
245            }
246    
247            @Override
248            public boolean hasPermission(
249                            PermissionChecker permissionChecker, long classPK, String actionId)
250                    throws Exception {
251    
252                    return JournalArticlePermission.contains(
253                            permissionChecker, classPK, actionId);
254            }
255    
256            @Override
257            public boolean isLinkable() {
258                    return _LINKABLE;
259            }
260    
261            @Override
262            protected String getIconPath(ThemeDisplay themeDisplay) {
263                    return themeDisplay.getPathThemeImages() + "/common/history.png";
264            }
265    
266            private static final boolean _LINKABLE = true;
267    
268    }