001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    try {
073                            fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
074    
075                            if (type == TYPE_LATEST) {
076                                    fileVersion = fileEntry.getLatestFileVersion();
077                            }
078                            else if (type == TYPE_LATEST_APPROVED) {
079                                    fileVersion = fileEntry.getFileVersion();
080                            }
081                            else {
082                                    throw new IllegalArgumentException(
083                                            "Unknown asset renderer type " + type);
084                            }
085                    }
086                    catch (PortalException pe) {
087                            fileVersion = DLAppLocalServiceUtil.getFileVersion(classPK);
088    
089                            fileEntry = fileVersion.getFileEntry();
090                    }
091    
092                    DLFileEntryAssetRenderer dlFileEntryAssetRenderer =
093                            new DLFileEntryAssetRenderer(fileEntry, fileVersion);
094    
095                    dlFileEntryAssetRenderer.setAssetRendererType(type);
096    
097                    return dlFileEntryAssetRenderer;
098            }
099    
100            @Override
101            public String getClassName() {
102                    return DLFileEntry.class.getName();
103            }
104    
105            @Override
106            public List<Tuple> getClassTypeFieldNames(
107                            long classTypeId, Locale locale, int start, int end)
108                    throws Exception {
109    
110                    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
111    
112                    DLFileEntryType dlFileEntryType =
113                            DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
114    
115                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
116    
117                    for (DDMStructure ddmStructure : ddmStructures) {
118                            classTypeFieldNames.addAll(
119                                    getDDMStructureFieldNames(ddmStructure, locale));
120                    }
121    
122                    return ListUtil.subList(classTypeFieldNames, start, end);
123            }
124    
125            @Override
126            public int getClassTypeFieldNamesCount(long classTypeId, Locale locale)
127                    throws Exception {
128    
129                    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();
130    
131                    DLFileEntryType dlFileEntryType =
132                            DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);
133    
134                    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();
135    
136                    for (DDMStructure ddmStructure : ddmStructures) {
137                            classTypeFieldNames.addAll(
138                                    getDDMStructureFieldNames(ddmStructure, locale));
139                    }
140    
141                    return classTypeFieldNames.size();
142            }
143    
144            @Override
145            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
146                    throws Exception {
147    
148                    Map<Long, String> classTypes = new HashMap<Long, String>();
149    
150                    List<DLFileEntryType> dlFileEntryTypes =
151                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
152    
153                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
154                            classTypes.put(
155                                    dlFileEntryType.getFileEntryTypeId(),
156                                    dlFileEntryType.getName(locale));
157                    }
158    
159                    return classTypes;
160            }
161    
162            @Override
163            public String getType() {
164                    return TYPE;
165            }
166    
167            @Override
168            public String getTypeName(Locale locale, boolean hasSubtypes) {
169                    if (hasSubtypes) {
170                            return LanguageUtil.get(locale, "basic-document");
171                    }
172    
173                    return super.getTypeName(locale, hasSubtypes);
174            }
175    
176            @Override
177            public PortletURL getURLAdd(
178                    LiferayPortletRequest liferayPortletRequest,
179                    LiferayPortletResponse liferayPortletResponse) {
180    
181                    PortletURL portletURL = liferayPortletResponse.createRenderURL(
182                            PortletKeys.DOCUMENT_LIBRARY);
183    
184                    portletURL.setParameter(
185                            "struts_action", "/document_library/edit_file_entry");
186                    portletURL.setParameter(
187                            "folderId",
188                            String.valueOf(
189                                    AssetPublisherUtil.getRecentFolderId(
190                                            liferayPortletRequest, getClassName())));
191    
192                    return portletURL;
193            }
194    
195            @Override
196            public PortletURL getURLView(
197                    LiferayPortletResponse liferayPortletResponse,
198                    WindowState windowState) {
199    
200                    LiferayPortletURL liferayPortletURL =
201                            liferayPortletResponse.createLiferayPortletURL(
202                                    PortletKeys.DOCUMENT_LIBRARY_DISPLAY,
203                                    PortletRequest.RENDER_PHASE);
204    
205                    try {
206                            liferayPortletURL.setWindowState(windowState);
207                    }
208                    catch (WindowStateException wse) {
209                    }
210    
211                    return liferayPortletURL;
212            }
213    
214            @Override
215            public boolean hasAddPermission(
216                            PermissionChecker permissionChecker, long groupId, long classTypeId)
217                    throws Exception {
218    
219                    if ((classTypeId > 0) &&
220                            !DLFileEntryTypePermission.contains(
221                                    permissionChecker, classTypeId, ActionKeys.VIEW)) {
222    
223                            return false;
224                    }
225    
226                    return DLPermission.contains(
227                            permissionChecker, groupId, ActionKeys.ADD_DOCUMENT);
228            }
229    
230            @Override
231            public boolean hasPermission(
232                            PermissionChecker permissionChecker, long classPK, String actionId)
233                    throws Exception {
234    
235                    return DLFileEntryPermission.contains(
236                            permissionChecker, classPK, actionId);
237            }
238    
239            @Override
240            public boolean isLinkable() {
241                    return _LINKABLE;
242            }
243    
244            @Override
245            protected String getIconPath(ThemeDisplay themeDisplay) {
246                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
247            }
248    
249            private static final boolean _LINKABLE = true;
250    
251    }