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