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