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