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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
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    
032    import java.util.Locale;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class DDMTemplateImpl extends DDMTemplateBaseImpl {
038    
039            public DDMTemplateImpl() {
040            }
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, SystemException {
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            /**
076             * Returns the WebDAV URL to access the template.
077             *
078             * @param  themeDisplay the theme display needed to build the URL. It can
079             *         set HTTPS access, the server name, the server port, the path
080             *         context, and the scope group.
081             * @param  webDAVToken the WebDAV token for the URL
082             * @return the WebDAV URL
083             */
084            @Override
085            public String getWebDavURL(ThemeDisplay themeDisplay, String webDAVToken) {
086                    StringBundler sb = new StringBundler(11);
087    
088                    boolean secure = false;
089    
090                    if (themeDisplay.isSecure() ||
091                            PropsValues.WEBDAV_SERVLET_HTTPS_REQUIRED) {
092    
093                            secure = true;
094                    }
095    
096                    String portalURL = PortalUtil.getPortalURL(
097                            themeDisplay.getServerName(), themeDisplay.getServerPort(), secure);
098    
099                    sb.append(portalURL);
100    
101                    sb.append(themeDisplay.getPathContext());
102                    sb.append(StringPool.SLASH);
103                    sb.append("webdav");
104    
105                    Group group = themeDisplay.getScopeGroup();
106    
107                    sb.append(group.getFriendlyURL());
108    
109                    sb.append(StringPool.SLASH);
110                    sb.append(webDAVToken);
111                    sb.append(StringPool.SLASH);
112                    sb.append("Templates");
113                    sb.append(StringPool.SLASH);
114                    sb.append(getTemplateId());
115    
116                    return sb.toString();
117            }
118    
119            @Override
120            public void setSmallImageType(String smallImageType) {
121                    _smallImageType = smallImageType;
122            }
123    
124            private String _smallImageType;
125    
126    }