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