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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    import com.liferay.portal.model.Group;
026    import com.liferay.portal.model.Image;
027    import com.liferay.portal.service.ImageLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.webserver.WebServerServletTokenUtil;
032    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateVersion;
033    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateVersionLocalServiceUtil;
034    
035    import java.util.Locale;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class DDMTemplateImpl extends DDMTemplateBaseImpl {
041    
042            @Override
043            public String getDefaultLanguageId() {
044                    Document document = null;
045    
046                    try {
047                            document = SAXReaderUtil.read(getName());
048    
049                            if (document != null) {
050                                    Element rootElement = document.getRootElement();
051    
052                                    return rootElement.attributeValue("default-locale");
053                            }
054                    }
055                    catch (Exception e) {
056                    }
057    
058                    Locale locale = LocaleUtil.getSiteDefault();
059    
060                    return locale.toString();
061            }
062    
063            @Override
064            public String getSmallImageType() throws PortalException {
065                    if ((_smallImageType == null) && isSmallImage()) {
066                            Image smallImage = ImageLocalServiceUtil.getImage(
067                                    getSmallImageId());
068    
069                            _smallImageType = smallImage.getType();
070                    }
071    
072                    return _smallImageType;
073            }
074    
075            @Override
076            public String getTemplateImageURL(ThemeDisplay themeDisplay) {
077                    if (!isSmallImage()) {
078                            return null;
079                    }
080    
081                    if (Validator.isNotNull(getSmallImageURL())) {
082                            return getSmallImageURL();
083                    }
084    
085                    return themeDisplay.getPathImage() + "/template?img_id=" +
086                            getSmallImageId() + "&t=" +
087                                    WebServerServletTokenUtil.getToken(getSmallImageId());
088            }
089    
090            @Override
091            public DDMTemplateVersion getTemplateVersion() throws PortalException {
092                    return DDMTemplateVersionLocalServiceUtil.getTemplateVersion(
093                            getTemplateId(), getVersion());
094            }
095    
096            /**
097             * Returns the WebDAV URL to access the template.
098             *
099             * @param  themeDisplay the theme display needed to build the URL. It can
100             *         set HTTPS access, the server name, the server port, the path
101             *         context, and the scope group.
102             * @param  webDAVToken the WebDAV token for the URL
103             * @return the WebDAV URL
104             */
105            @Override
106            public String getWebDavURL(ThemeDisplay themeDisplay, String webDAVToken) {
107                    StringBundler sb = new StringBundler(11);
108    
109                    boolean secure = false;
110    
111                    if (themeDisplay.isSecure() ||
112                            PropsValues.WEBDAV_SERVLET_HTTPS_REQUIRED) {
113    
114                            secure = true;
115                    }
116    
117                    String portalURL = PortalUtil.getPortalURL(
118                            themeDisplay.getServerName(), themeDisplay.getServerPort(), secure);
119    
120                    sb.append(portalURL);
121    
122                    sb.append(themeDisplay.getPathContext());
123                    sb.append(StringPool.SLASH);
124                    sb.append("webdav");
125    
126                    Group group = themeDisplay.getScopeGroup();
127    
128                    sb.append(group.getFriendlyURL());
129    
130                    sb.append(StringPool.SLASH);
131                    sb.append(webDAVToken);
132                    sb.append(StringPool.SLASH);
133                    sb.append("Templates");
134                    sb.append(StringPool.SLASH);
135                    sb.append(getTemplateId());
136    
137                    return sb.toString();
138            }
139    
140            @Override
141            public void setSmallImageType(String smallImageType) {
142                    _smallImageType = smallImageType;
143            }
144    
145            private String _smallImageType;
146    
147    }