001    /**
002     * Copyright (c) 2000-present 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.template;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.portletdisplaytemplate.BasePortletDisplayTemplateHandler;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.template.TemplateVariableGroup;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.documentlibrary.service.DLAppLocalService;
028    import com.liferay.portlet.documentlibrary.service.DLAppService;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService;
030    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeService;
031    import com.liferay.portlet.documentlibrary.util.DLUtil;
032    import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplateConstants;
033    
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Locale;
037    import java.util.Map;
038    
039    /**
040     * @author Eduardo Garcia
041     */
042    public class DocumentLibraryPortletDisplayTemplateHandler
043            extends BasePortletDisplayTemplateHandler {
044    
045            @Override
046            public String getClassName() {
047                    return FileEntry.class.getName();
048            }
049    
050            @Override
051            public Map<String, Object> getCustomContextObjects() {
052                    Map<String, Object> contextObjects = new HashMap<>();
053    
054                    try {
055                            contextObjects.put("dlUtil", DLUtil.getDL());
056                    }
057                    catch (SecurityException se) {
058                            _log.error(se, se);
059                    }
060    
061                    return contextObjects;
062            }
063    
064            @Override
065            public String getName(Locale locale) {
066                    String portletTitle = PortalUtil.getPortletTitle(
067                            PortletKeys.DOCUMENT_LIBRARY, locale);
068    
069                    return portletTitle.concat(StringPool.SPACE).concat(
070                            LanguageUtil.get(locale, "template"));
071            }
072    
073            @Override
074            public String getResourceName() {
075                    return PortletKeys.DOCUMENT_LIBRARY;
076            }
077    
078            @Override
079            public Map<String, TemplateVariableGroup> getTemplateVariableGroups(
080                            long classPK, String language, Locale locale)
081                    throws Exception {
082    
083                    Map<String, TemplateVariableGroup> templateVariableGroups =
084                            super.getTemplateVariableGroups(classPK, language, locale);
085    
086                    String[] restrictedVariables = getRestrictedVariables(language);
087    
088                    TemplateVariableGroup documentUtilTemplateVariableGroup =
089                            new TemplateVariableGroup("document-util", restrictedVariables);
090    
091                    documentUtilTemplateVariableGroup.addVariable(
092                            "dl-util", DLUtil.class, "dlUtil");
093    
094                    templateVariableGroups.put(
095                            "document-util", documentUtilTemplateVariableGroup);
096    
097                    TemplateVariableGroup documentServicesTemplateVariableGroup =
098                            new TemplateVariableGroup("document-services", restrictedVariables);
099    
100                    documentServicesTemplateVariableGroup.setAutocompleteEnabled(false);
101    
102                    documentServicesTemplateVariableGroup.addServiceLocatorVariables(
103                            DLAppLocalService.class, DLAppService.class,
104                            DLFileEntryTypeLocalService.class, DLFileEntryTypeService.class);
105    
106                    templateVariableGroups.put(
107                            documentServicesTemplateVariableGroup.getLabel(),
108                            documentServicesTemplateVariableGroup);
109    
110                    TemplateVariableGroup fieldsTemplateVariableGroup =
111                            templateVariableGroups.get("fields");
112    
113                    fieldsTemplateVariableGroup.empty();
114    
115                    fieldsTemplateVariableGroup.addCollectionVariable(
116                            "documents", List.class, PortletDisplayTemplateConstants.ENTRIES,
117                            "document", FileEntry.class, "curFileEntry", "title");
118    
119                    return templateVariableGroups;
120            }
121    
122            @Override
123            protected String getTemplatesConfigPath() {
124                    return PropsValues.DL_DISPLAY_TEMPLATES_CONFIG;
125            }
126    
127            private static final Log _log = LogFactoryUtil.getLog(
128                    DocumentLibraryPortletDisplayTemplateHandler.class);
129    
130    }