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