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