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.documentlibrary.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.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.FileVersion;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.ListUtil;
027    import com.liferay.portal.kernel.util.Tuple;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.asset.model.AssetRenderer;
034    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
035    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
036    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
037    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
038    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
039    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
040    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
041    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
042    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryTypePermission;
043    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
044    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
045    
046    import java.util.ArrayList;
047    import java.util.HashMap;
048    import java.util.List;
049    import java.util.Locale;
050    import java.util.Map;
051    
052    import javax.portlet.PortletRequest;
053    import javax.portlet.PortletURL;
054    import javax.portlet.WindowState;
055    import javax.portlet.WindowStateException;
056    
057    /**
058     * @author Julio Camarero
059     * @author Juan Fern??ndez
060     * @author Raymond Aug??
061     * @author Sergio Gonz??lez
062     */
063    public class DLFileEntryAssetRendererFactory extends BaseAssetRendererFactory {
064    
065            public static final String TYPE = "document";
066    
067            @Override
068            public AssetRenderer getAssetRenderer(long classPK, int type)
069                    throws PortalException, SystemException {
070    
071                    FileEntry fileEntry = null;
072                    FileVersion fileVersion = null;
073    
074                    if (type == TYPE_LATEST) {
075                            fileVersion = DLAppLocalServiceUtil.getFileVersion(classPK);
076    
077                            fileEntry = fileVersion.getFileEntry();
078                    }
079                    else {
080                            fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
081    
082                            fileVersion = fileEntry.getFileVersion();
083                    }
084    
085                    DLFileEntryAssetRenderer dlFileEntryAssetRenderer =
086                            new DLFileEntryAssetRenderer(fileEntry, fileVersion);
087    
088                    dlFileEntryAssetRenderer.setAssetRendererType(type);
089    
090                    return dlFileEntryAssetRenderer;
091            }
092    
093            @Override
094            public String getClassName() {
095                    return DLFileEntry.class.getName();
096            }
097    
098            @Override
099            public List<Tuple> getClassTypeFieldNames(
100                            long classTypeId, Locale locale, int start, int end)
101                    throws Exception {
102    
103                    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
104    
105                    DLFileEntryType dlFileEntryType =
106                            DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
107    
108                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
109    
110                    for (DDMStructure ddmStructure : ddmStructures) {
111                            classTypeFieldNames.addAll(
112                                    getDDMStructureFieldNames(ddmStructure, locale));
113                    }
114    
115                    return ListUtil.subList(classTypeFieldNames, start, end);
116            }
117    
118            @Override
119            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
120                    throws Exception {
121    
122                    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
123    
124                    DLFileEntryType dlFileEntryType =
125                            DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
126    
127                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
128    
129                    for (DDMStructure ddmStructure : ddmStructures) {
130                            classTypeFieldNames.addAll(
131                                    getDDMStructureFieldNames(ddmStructure, locale));
132                    }
133    
134                    return classTypeFieldNames.size();
135            }
136    
137            @Override
138            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
139                    throws Exception {
140    
141                    Map<Long, String> classTypes = new HashMap<Long, String>();
142    
143                    List<DLFileEntryType> dlFileEntryTypes =
144                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
145    
146                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
147                            classTypes.put(
148                                    dlFileEntryType.getFileEntryTypeId(),
149                                    dlFileEntryType.getName(locale));
150                    }
151    
152                    return classTypes;
153            }
154    
155            @Override
156            public String getType() {
157                    return TYPE;
158            }
159    
160            @Override
161            public String getTypeName(Locale locale, boolean hasSubtypes) {
162                    if (hasSubtypes) {
163                            return LanguageUtil.get(locale, "basic-document");
164                    }
165    
166                    return super.getTypeName(locale, hasSubtypes);
167            }
168    
169            @Override
170            public PortletURL getURLAdd(
171                            LiferayPortletRequest liferayPortletRequest,
172                            LiferayPortletResponse liferayPortletResponse)
173                    throws PortalException, SystemException {
174    
175                    ThemeDisplay themeDisplay =
176                            (ThemeDisplay)liferayPortletRequest.getAttribute(
177                                    WebKeys.THEME_DISPLAY);
178    
179                    if (!DLPermission.contains(
180                                    themeDisplay.getPermissionChecker(),
181                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
182    
183                            return null;
184                    }
185    
186                    long classTypeId = GetterUtil.getLong(
187                            liferayPortletRequest.getAttribute(
188                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
189    
190                    if ((classTypeId > 0) &&
191                            !DLFileEntryTypePermission.contains(
192                                    themeDisplay.getPermissionChecker(), classTypeId,
193                                    ActionKeys.VIEW)) {
194    
195                            return null;
196                    }
197    
198                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
199                            PortletKeys.DOCUMENT_LIBRARY);
200    
201                    portletURL.setParameter(
202                            "struts_action", "/document_library/edit_file_entry");
203                    portletURL.setParameter(
204                            "folderId",
205                            String.valueOf(
206                                    AssetPublisherUtil.getRecentFolderId(
207                                            liferayPortletRequest, getClassName())));
208    
209                    return portletURL;
210            }
211    
212            @Override
213            public PortletURL getURLView(
214                    LiferayPortletResponse liferayPortletResponse,
215                    WindowState windowState) {
216    
217                    LiferayPortletURL liferayPortletURL =
218                            liferayPortletResponse.createLiferayPortletURL(
219                                    PortletKeys.DOCUMENT_LIBRARY_DISPLAY,
220                                    PortletRequest.RENDER_PHASE);
221    
222                    try {
223                            liferayPortletURL.setWindowState(windowState);
224                    }
225                    catch (WindowStateException wse) {
226                    }
227    
228                    return liferayPortletURL;
229            }
230    
231            @Override
232            public boolean hasPermission(
233                            PermissionChecker permissionChecker, long classPK, String actionId)
234                    throws Exception {
235    
236                    return DLFileEntryPermission.contains(
237                            permissionChecker, classPK, actionId);
238            }
239    
240            @Override
241            public boolean isLinkable() {
242                    return _LINKABLE;
243            }
244    
245            @Override
246            protected String getIconPath(ThemeDisplay themeDisplay) {
247                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
248            }
249    
250            private static final boolean _LINKABLE = true;
251    
252    }