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.dynamicdatamapping.util;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
022    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
023    import com.liferay.portal.kernel.template.TemplateConstants;
024    import com.liferay.portal.kernel.template.TemplateHandler;
025    import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
026    import com.liferay.portal.kernel.util.ArrayUtil;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.SetUtil;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.security.permission.ActionKeys;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
038    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    import java.util.Locale;
043    import java.util.Set;
044    
045    import javax.portlet.PortletRequest;
046    import javax.portlet.PortletURL;
047    
048    /**
049     * @author Eduardo Garcia
050     */
051    @ProviderType
052    public abstract class BaseDDMDisplay implements DDMDisplay {
053    
054            @Override
055            public String getAddStructureActionId() {
056                    return ActionKeys.ADD_STRUCTURE;
057            }
058    
059            @Override
060            public String getAddTemplateActionId() {
061                    return ActionKeys.ADD_TEMPLATE;
062            }
063    
064            @Override
065            public String getAvailableFields() {
066                    return "Liferay.FormBuilder.AVAILABLE_FIELDS.DDM_STRUCTURE";
067            }
068    
069            @Override
070            public String getEditStructureDefaultValuesURL(
071                            LiferayPortletRequest liferayPortletRequest,
072                            LiferayPortletResponse liferayPortletResponse,
073                            DDMStructure structure, String redirectURL, String backURL)
074                    throws Exception {
075    
076                    return null;
077            }
078    
079            @Override
080            public String getEditTemplateBackURL(
081                            LiferayPortletRequest liferayPortletRequest,
082                            LiferayPortletResponse liferayPortletResponse, long classNameId,
083                            long classPK, String portletResource)
084                    throws Exception {
085    
086                    String redirect = ParamUtil.getString(
087                            liferayPortletRequest, "redirect");
088    
089                    if (Validator.isNull(redirect) || Validator.isNull(portletResource)) {
090                            return getViewTemplatesURL(
091                                    liferayPortletRequest, liferayPortletResponse, classNameId,
092                                    classPK);
093                    }
094    
095                    return redirect;
096            }
097    
098            @Override
099            public String getEditTemplateTitle(
100                    DDMStructure structure, DDMTemplate template, Locale locale) {
101    
102                    if ((structure != null) && (template != null)) {
103                            StringBundler sb = new StringBundler(5);
104    
105                            sb.append(template.getName(locale));
106                            sb.append(StringPool.SPACE);
107                            sb.append(StringPool.OPEN_PARENTHESIS);
108                            sb.append(structure.getName(locale));
109                            sb.append(StringPool.CLOSE_PARENTHESIS);
110    
111                            return sb.toString();
112                    }
113                    else if (structure != null) {
114                            return LanguageUtil.format(
115                                    locale, "new-template-for-structure-x",
116                                    structure.getName(locale), false);
117                    }
118                    else if (template != null) {
119                            return template.getName(locale);
120                    }
121    
122                    return getDefaultEditTemplateTitle(locale);
123            }
124    
125            @Override
126            public String getEditTemplateTitle(long classNameId, Locale locale) {
127                    if (classNameId > 0) {
128                            TemplateHandler templateHandler =
129                                    TemplateHandlerRegistryUtil.getTemplateHandler(classNameId);
130    
131                            if (templateHandler != null) {
132                                    return LanguageUtil.get(locale, "new") + StringPool.SPACE +
133                                            templateHandler.getName(locale);
134                            }
135                    }
136    
137                    return getDefaultEditTemplateTitle(locale);
138            }
139    
140            @Override
141            public String getResourceName(long classNameId) {
142                    if (classNameId > 0) {
143                            TemplateHandler templateHandler =
144                                    TemplateHandlerRegistryUtil.getTemplateHandler(classNameId);
145    
146                            if (templateHandler != null) {
147                                    return templateHandler.getResourceName();
148                            }
149                    }
150    
151                    return getResourceName();
152            }
153    
154            @Override
155            public String getStorageType() {
156                    return StringPool.BLANK;
157            }
158    
159            @Override
160            public String getStructureName(Locale locale) {
161                    return LanguageUtil.get(locale, "structure");
162            }
163    
164            @Override
165            public String getStructureType() {
166                    return StringPool.BLANK;
167            }
168    
169            @Override
170            public long[] getTemplateClassNameIds(long classNameId) {
171                    if (classNameId > 0) {
172                            return new long[] {classNameId};
173                    }
174    
175                    return TemplateHandlerRegistryUtil.getClassNameIds();
176            }
177    
178            @Override
179            public long[] getTemplateClassPKs(
180                            long companyId, long classNameId, long classPK)
181                    throws Exception {
182    
183                    if (classPK > 0) {
184                            return new long[] {classPK};
185                    }
186    
187                    List<Long> classPKs = new ArrayList<Long>();
188    
189                    classPKs.add(0L);
190    
191                    List<DDMStructure> structures =
192                            DDMStructureLocalServiceUtil.getClassStructures(
193                                    companyId, PortalUtil.getClassNameId(getStructureType()));
194    
195                    for (DDMStructure structure : structures) {
196                            classPKs.add(structure.getPrimaryKey());
197                    }
198    
199                    return ArrayUtil.toLongArray(classPKs);
200            }
201    
202            @Override
203            public long[] getTemplateGroupIds(
204                            ThemeDisplay themeDisplay, boolean includeAncestorTemplates)
205                    throws Exception {
206    
207                    if (includeAncestorTemplates) {
208                            return PortalUtil.getCurrentAndAncestorSiteGroupIds(
209                                    themeDisplay.getScopeGroupId());
210                    }
211    
212                    return new long[] {themeDisplay.getScopeGroupId()};
213            }
214    
215            @Override
216            public long getTemplateHandlerClassNameId(
217                    DDMTemplate template, long classNameId) {
218    
219                    if (template != null) {
220                            return template.getClassNameId();
221                    }
222    
223                    return classNameId;
224            }
225    
226            @Override
227            public Set<String> getTemplateLanguageTypes() {
228                    return _templateLanguageTypes;
229            }
230    
231            @Override
232            public String getTemplateMode() {
233                    return StringPool.BLANK;
234            }
235    
236            @Override
237            public String getTemplateType() {
238                    return StringPool.BLANK;
239            }
240    
241            @Override
242            public String getTemplateType(DDMTemplate template, Locale locale) {
243                    return LanguageUtil.get(locale, template.getType());
244            }
245    
246            @Override
247            public String getViewTemplatesBackURL(
248                            LiferayPortletRequest liferayPortletRequest,
249                            LiferayPortletResponse liferayPortletResponse, long classPK)
250                    throws Exception {
251    
252                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
253                            getControlPanelPlid(liferayPortletRequest),
254                            PortletKeys.DYNAMIC_DATA_MAPPING, PortletRequest.RENDER_PHASE);
255    
256                    portletURL.setParameter("struts_action", "/dynamic_data_mapping/view");
257    
258                    return portletURL.toString();
259            }
260    
261            @Override
262            public Set<String> getViewTemplatesExcludedColumnNames() {
263                    return _viewTemplateExcludedColumnNames;
264            }
265    
266            @Override
267            public String getViewTemplatesTitle(
268                    DDMStructure structure, boolean controlPanel, boolean search,
269                    Locale locale) {
270    
271                    if (structure != null) {
272                            return LanguageUtil.format(
273                                    locale, "templates-for-structure-x", structure.getName(locale),
274                                    false);
275                    }
276    
277                    return getDefaultViewTemplateTitle(locale);
278            }
279    
280            /**
281             * @deprecated As of 7.0.0
282             */
283            @Deprecated
284            @Override
285            public String getViewTemplatesTitle(
286                    DDMStructure structure, boolean controlPanel, Locale locale) {
287    
288                    return getViewTemplatesTitle(structure, controlPanel, false, locale);
289            }
290    
291            @Override
292            public String getViewTemplatesTitle(DDMStructure structure, Locale locale) {
293                    return getViewTemplatesTitle(structure, false, false, locale);
294            }
295    
296            @Override
297            public boolean isShowAddStructureButton() {
298                    String portletId = getPortletId();
299    
300                    if (portletId.equals(PortletKeys.DYNAMIC_DATA_MAPPING)) {
301                            return false;
302                    }
303    
304                    return true;
305            }
306    
307            @Override
308            public boolean isShowStructureSelector() {
309                    return false;
310            }
311    
312            protected long getControlPanelPlid(
313                            LiferayPortletRequest liferayPortletRequest)
314                    throws PortalException {
315    
316                    return PortalUtil.getControlPanelPlid(liferayPortletRequest);
317            }
318    
319            protected String getDefaultEditTemplateTitle(Locale locale) {
320                    return LanguageUtil.get(locale, "new-template");
321            }
322    
323            protected String getDefaultViewTemplateTitle(Locale locale) {
324                    return LanguageUtil.get(locale, "templates");
325            }
326    
327            protected String getViewTemplatesURL(
328                            LiferayPortletRequest liferayPortletRequest,
329                            LiferayPortletResponse liferayPortletResponse, long classNameId,
330                            long classPK)
331                    throws Exception {
332    
333                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
334                            getControlPanelPlid(liferayPortletRequest),
335                            PortletKeys.DYNAMIC_DATA_MAPPING, PortletRequest.RENDER_PHASE);
336    
337                    portletURL.setParameter(
338                            "struts_action", "/dynamic_data_mapping/view_template");
339                    portletURL.setParameter("classNameId", String.valueOf(classNameId));
340                    portletURL.setParameter("classPK", String.valueOf(classPK));
341    
342                    return portletURL.toString();
343            }
344    
345            private static final Set<String> _templateLanguageTypes =
346                    SetUtil.fromArray(
347                            new String[] {
348                                    TemplateConstants.LANG_TYPE_FTL, TemplateConstants.LANG_TYPE_VM
349                            });
350            private static final Set<String> _viewTemplateExcludedColumnNames =
351                    SetUtil.fromArray(new String[] {"structure"});
352    
353    }