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            public String getDefaultLanguageId() {
043                    Document document = null;
044    
045                    try {
046                            document = SAXReaderUtil.read(getName());
047    
048                            if (document != null) {
049                                    Element rootElement = document.getRootElement();
050    
051                                    return rootElement.attributeValue("default-locale");
052                            }
053                    }
054                    catch (Exception e) {
055                    }
056    
057                    Locale locale = LocaleUtil.getDefault();
058    
059                    return locale.toString();
060            }
061    
062            public String getSmallImageType() throws PortalException, SystemException {
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            /**
074             * Returns the WebDAV URL to access the template.
075             *
076             * @param  themeDisplay the theme display needed to build the URL. It can
077             *         set HTTPS access, the server name, the server port, the path
078             *         context, and the scope group.
079             * @param  webDAVToken the WebDAV token for the URL
080             * @return the WebDAV URL
081             */
082            public String getWebDavURL(ThemeDisplay themeDisplay, String webDAVToken) {
083                    StringBundler sb = new StringBundler(11);
084    
085                    boolean secure = false;
086    
087                    if (themeDisplay.isSecure() ||
088                            PropsValues.WEBDAV_SERVLET_HTTPS_REQUIRED) {
089    
090                            secure = true;
091                    }
092    
093                    String portalURL = PortalUtil.getPortalURL(
094                            themeDisplay.getServerName(), themeDisplay.getServerPort(), secure);
095    
096                    sb.append(portalURL);
097    
098                    sb.append(themeDisplay.getPathContext());
099                    sb.append(StringPool.SLASH);
100                    sb.append("webdav");
101    
102                    Group group = themeDisplay.getScopeGroup();
103    
104                    sb.append(group.getFriendlyURL());
105    
106                    sb.append(StringPool.SLASH);
107                    sb.append(webDAVToken);
108                    sb.append(StringPool.SLASH);
109                    sb.append("Templates");
110                    sb.append(StringPool.SLASH);
111                    sb.append(getTemplateId());
112    
113                    return sb.toString();
114            }
115    
116            public void setSmallImageType(String smallImageType) {
117                    _smallImageType = smallImageType;
118            }
119    
120            private String _smallImageType;
121    
122    }