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 hasAddPermission(
151                            PermissionChecker permissionChecker, long groupId, long classTypeId)
152                    throws Exception {
153    
154                    return false;
155            }
156    
157            @Override
158            public boolean hasClassTypeFieldNames(long classTypeId, Locale locale)
159                    throws Exception {
160    
161                    List<Tuple> classTypeFieldNames = getClassTypeFieldNames(
162                            classTypeId, locale, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
163    
164                    return !classTypeFieldNames.isEmpty();
165            }
166    
167            @Override
168            public boolean hasPermission(
169                            PermissionChecker permissionChecker, long classPK, String actionId)
170                    throws Exception {
171    
172                    return _PERMISSION;
173            }
174    
175            @Override
176            public boolean isActive(long companyId) {
177                    if (Validator.isNull(getPortletId())) {
178                            return true;
179                    }
180    
181                    Portlet portlet = null;
182    
183                    try {
184                            portlet = PortletLocalServiceUtil.getPortletById(
185                                    companyId, getPortletId());
186                    }
187                    catch (SystemException se) {
188                            portlet = PortletLocalServiceUtil.getPortletById(getPortletId());
189                    }
190    
191                    if (portlet == null) {
192                            return false;
193                    }
194    
195                    return portlet.isActive();
196            }
197    
198            @Override
199            public boolean isCategorizable() {
200                    return true;
201            }
202    
203            @Override
204            public boolean isLinkable() {
205                    return _LINKABLE;
206            }
207    
208            @Override
209            public boolean isListable(long classPK) throws SystemException {
210                    return true;
211            }
212    
213            @Override
214            public boolean isSelectable() {
215                    return _SELECTABLE;
216            }
217    
218            @Override
219            public void setClassName(String className) {
220                    _className = className;
221            }
222    
223            @Override
224            public void setPortletId(String portletId) {
225                    _portletId = portletId;
226            }
227    
228            protected long getControlPanelPlid(ThemeDisplay themeDisplay)
229                    throws PortalException, SystemException {
230    
231                    return PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId());
232            }
233    
234            protected List<Tuple> getDDMStructureFieldNames(
235                            DDMStructure ddmStructure, Locale locale)
236                    throws Exception {
237    
238                    List<Tuple> fields = new ArrayList<Tuple>();
239    
240                    Map<String, Map<String, String>> fieldsMap = ddmStructure.getFieldsMap(
241                            LocaleUtil.toLanguageId(locale));
242    
243                    for (Map<String, String> fieldMap : fieldsMap.values()) {
244                            String indexType = fieldMap.get("indexType");
245                            boolean privateField = GetterUtil.getBoolean(
246                                    fieldMap.get("private"));
247    
248                            String type = fieldMap.get("type");
249    
250                            if (Validator.isNull(indexType) || privateField ||
251                                    !ArrayUtil.contains(_SELECTABLE_DDM_STRUCTURE_FIELDS, type)) {
252    
253                                    continue;
254                            }
255    
256                            String label = fieldMap.get("label");
257                            String name = fieldMap.get("name");
258    
259                            fields.add(
260                                    new Tuple(label, name, type, ddmStructure.getStructureId()));
261                    }
262    
263                    return fields;
264            }
265    
266            protected String getIconPath(ThemeDisplay themeDisplay) {
267                    return themeDisplay.getPathThemeImages() + "/common/page.png";
268            }
269    
270            private static final boolean _LINKABLE = false;
271    
272            private static final boolean _PERMISSION = true;
273    
274            private static final boolean _SELECTABLE = true;
275    
276            private static final String[] _SELECTABLE_DDM_STRUCTURE_FIELDS = {
277                    "checkbox", "ddm-date", "ddm-decimal", "ddm-integer", "ddm-number",
278                    "radio", "select", "text"
279            };
280    
281            private String _className;
282            private String _portletId;
283    
284    }