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