001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 isEnableSelectStructureLink(
283                    DDMStructure structure, long classPK) {
284    
285                    if (structure.getStructureId() == classPK) {
286                            return false;
287                    }
288    
289                    if (classPK == 0) {
290                            return true;
291                    }
292    
293                    if (structure.getParentStructureId() == 0) {
294                            return true;
295                    }
296    
297                    if (structure.getParentStructureId() != classPK) {
298                            return true;
299                    }
300    
301                    return false;
302            }
303    
304            @Override
305            public boolean isShowAddStructureButton() {
306                    String portletId = getPortletId();
307    
308                    if (portletId.equals(PortletKeys.DYNAMIC_DATA_MAPPING)) {
309                            return false;
310                    }
311    
312                    return true;
313            }
314    
315            @Override
316            public boolean isShowStructureSelector() {
317                    return false;
318            }
319    
320            protected long getControlPanelPlid(
321                            LiferayPortletRequest liferayPortletRequest)
322                    throws PortalException, SystemException {
323    
324                    return PortalUtil.getControlPanelPlid(liferayPortletRequest);
325            }
326    
327            protected String getDefaultEditTemplateTitle(Locale locale) {
328                    return LanguageUtil.get(locale, "new-template");
329            }
330    
331            protected String getDefaultViewTemplateTitle(Locale locale) {
332                    return LanguageUtil.get(locale, "templates");
333            }
334    
335            protected String getViewTemplatesURL(
336                            LiferayPortletRequest liferayPortletRequest,
337                            LiferayPortletResponse liferayPortletResponse, long classNameId,
338                            long classPK)
339                    throws Exception {
340    
341                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
342                            getControlPanelPlid(liferayPortletRequest),
343                            PortletKeys.DYNAMIC_DATA_MAPPING, PortletRequest.RENDER_PHASE);
344    
345                    portletURL.setParameter(
346                            "struts_action", "/dynamic_data_mapping/view_template");
347                    portletURL.setParameter("classNameId", String.valueOf(classNameId));
348                    portletURL.setParameter("classPK", String.valueOf(classPK));
349    
350                    return portletURL.toString();
351            }
352    
353            private static Set<String> _templateLanguageTypes =
354                    SetUtil.fromArray(
355                            new String[] {
356                                    TemplateConstants.LANG_TYPE_FTL, TemplateConstants.LANG_TYPE_VM
357                            });
358            private static Set<String> _viewTemplateExcludedColumnNames =
359                    SetUtil.fromArray(new String[] {"structure"});
360    
361    }