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