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