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.asset.model;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.util.ArrayUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.Tuple;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
033    
034    import java.util.ArrayList;
035    import java.util.Collections;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    import javax.portlet.PortletRequest;
041    import javax.portlet.PortletURL;
042    
043    /**
044     * @author Jorge Ferrer
045     * @author Juan Fernández
046     * @author Raymond Augé
047     * @author Sergio González
048     */
049    public abstract class BaseAssetRendererFactory implements AssetRendererFactory {
050    
051            public AssetEntry getAssetEntry(long assetEntryId)
052                    throws PortalException, SystemException {
053    
054                    return AssetEntryLocalServiceUtil.getEntry(assetEntryId);
055            }
056    
057            public AssetEntry getAssetEntry(String className, long classPK)
058                    throws PortalException, SystemException {
059    
060                    return AssetEntryLocalServiceUtil.getEntry(className, classPK);
061            }
062    
063            public AssetRenderer getAssetRenderer(long classPK)
064                    throws PortalException, SystemException {
065    
066                    return getAssetRenderer(classPK, TYPE_LATEST_APPROVED);
067            }
068    
069            @SuppressWarnings("unused")
070            public AssetRenderer getAssetRenderer(long groupId, String urlTitle)
071                    throws PortalException, SystemException {
072    
073                    return null;
074            }
075    
076            public long getClassNameId() {
077                    return PortalUtil.getClassNameId(_className);
078            }
079    
080            public List<Tuple> getClassTypeFieldNames(long classTypeId, Locale locale)
081                    throws Exception {
082    
083                    return Collections.emptyList();
084            }
085    
086            public Map<Long, String> getClassTypes(long[] groupId, Locale locale)
087                    throws Exception {
088    
089                    return Collections.emptyMap();
090            }
091    
092            public String getIconPath(PortletRequest portletRequest) {
093                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
094                            WebKeys.THEME_DISPLAY);
095    
096                    return getIconPath(themeDisplay);
097            }
098    
099            public String getPortletId() {
100                    return _portletId;
101            }
102    
103            public String getTypeName(Locale locale, boolean hasSubtypes) {
104                    return ResourceActionsUtil.getModelResource(locale, getClassName());
105            }
106    
107            @SuppressWarnings("unused")
108            public PortletURL getURLAdd(
109                            LiferayPortletRequest liferayPortletRequest,
110                            LiferayPortletResponse liferayPortletResponse)
111                    throws PortalException, SystemException {
112    
113                    return null;
114            }
115    
116            public boolean hasClassTypeFieldNames(long classTypeId, Locale locale)
117                    throws Exception {
118    
119                    List<Tuple> classTypeFieldNames = getClassTypeFieldNames(
120                            classTypeId, locale);
121    
122                    return !classTypeFieldNames.isEmpty();
123            }
124    
125            public boolean hasPermission(
126                            PermissionChecker permissionChecker, long classPK, String actionId)
127                    throws Exception {
128    
129                    return _PERMISSION;
130            }
131    
132            public boolean isCategorizable() {
133                    return true;
134            }
135    
136            public boolean isLinkable() {
137                    return _LINKABLE;
138            }
139    
140            public boolean isSelectable() {
141                    return _SELECTABLE;
142            }
143    
144            public void setClassName(String className) {
145                    _className = className;
146            }
147    
148            public void setPortletId(String portletId) {
149                    _portletId = portletId;
150            }
151    
152            protected long getControlPanelPlid(ThemeDisplay themeDisplay)
153                    throws PortalException, SystemException {
154    
155                    return PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId());
156            }
157    
158            protected List<Tuple> getDDMStructureFieldNames(
159                            DDMStructure ddmStructure, Locale locale)
160                    throws Exception {
161    
162                    List<Tuple> fields = new ArrayList<Tuple>();
163    
164                    Map<String, Map<String, String>> fieldsMap = ddmStructure.getFieldsMap(
165                            LocaleUtil.toLanguageId(locale));
166    
167                    for (Map<String, String> fieldMap : fieldsMap.values()) {
168                            String indexType = fieldMap.get("indexType");
169                            boolean privateField = GetterUtil.getBoolean(
170                                    fieldMap.get("private"));
171    
172                            String type = fieldMap.get("type");
173    
174                            if (Validator.isNull(indexType) || privateField ||
175                                    !ArrayUtil.contains(_SELECTABLE_DDM_STRUCTURE_FIELDS, type)) {
176    
177                                    continue;
178                            }
179    
180                            String label = fieldMap.get("label");
181                            String name = fieldMap.get("name");
182    
183                            fields.add(new Tuple(label, name));
184                    }
185    
186                    return fields;
187            }
188    
189            protected String getIconPath(ThemeDisplay themeDisplay) {
190                    return themeDisplay.getPathThemeImages() + "/common/page.png";
191            }
192    
193            private static final boolean _LINKABLE = false;
194    
195            private static final boolean _PERMISSION = true;
196    
197            private static final boolean _SELECTABLE = true;
198    
199            private static final String[] _SELECTABLE_DDM_STRUCTURE_FIELDS = {
200                    "checkbox", "ddm-date", "ddm-decimal", "ddm-integer", "ddm-number",
201                    "radio", "select", "text"
202            };
203    
204            private String _className;
205            private String _portletId;
206    
207    }