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