001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.googlecalendar;
017
018import java.io.IOException;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.Map.Entry;
023
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.generation.AbstractGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.core.util.URLEncoder;
031
032/**
033 * Google calendar iframe generator
034 * 
035 */
036public class CalendarIframeGenerator extends AbstractGenerator
037{
038    private static final String __CALENDAR_SERVER_URL = "https://calendar.google.com/calendar/embed";
039
040    private static final String __CTZ = "Europe/Paris";
041
042    private static final String __DEFAULT_COLOR = "#2952A3";
043    private static final String __DEFAULT_BGCOLOR = "#FFFFFF";
044    private static final String __DEFAULT_HEIGHT = "600";
045    private static final String __DEFAULT_MODE = "MONTH";
046    private static final String __DOES_NOT_SHOW_PARAM = "0";
047    
048    private static final String __HEIGHT_PARAM = "height";
049
050    @Override
051    public void generate() throws IOException, SAXException, ProcessingException
052    {
053        contentHandler.startDocument();
054        
055        AttributesImpl atts = new AttributesImpl();
056        atts.addCDATAAttribute("src", _getSrcAttrValue());
057        atts.addCDATAAttribute(__HEIGHT_PARAM, _getParameterValue(__HEIGHT_PARAM, __DEFAULT_HEIGHT));
058        XMLUtils.startElement(contentHandler, "iframe", atts);
059        XMLUtils.endElement(contentHandler, "iframe");
060        
061        contentHandler.endDocument();
062    }
063    
064    private String _getSrcAttrValue()
065    {
066        return _buildURL(_getParametersAsMap());
067    }
068    
069    private String _buildURL(Map<String, String> urlParams)
070    {
071        StringBuilder builder = new StringBuilder(__CALENDAR_SERVER_URL);
072        if (urlParams.size() > 0)
073        {
074            builder.append('?');
075        }
076        
077        Iterator<Entry<String, String>> entrySet = urlParams.entrySet().iterator();
078        while (entrySet.hasNext())
079        {
080            Entry<String, String> entry = entrySet.next();
081            builder.append(entry.getKey()).append('=').append(URLEncoder.encodeParameter(entry.getValue()));
082            if (entrySet.hasNext())
083            {
084                builder.append('&');
085            }
086        }
087        
088        return builder.toString();
089    }
090
091    private Map<String, String> _getParametersAsMap()
092    {
093        Map<String, String> urlParams = new HashMap<>();
094        for (String paramName : parameters.getNames())
095        {
096            if (paramName.startsWith("show"))
097            {
098                boolean paramValue = parameters.getParameterAsBoolean(paramName, false);
099                if (!paramValue)
100                {
101                    urlParams.put(paramName, __DOES_NOT_SHOW_PARAM);
102                }
103            }
104            else if ("mode".equals(paramName))
105            {
106                String paramValue = _getParameterValue(paramName, __DEFAULT_MODE);
107                if (!__DEFAULT_MODE.equals(paramValue))
108                {
109                    urlParams.put(paramName, paramValue);
110                }
111            }
112            else if ("bgcolor".equals(paramName))
113            {
114                String paramValue = _getParameterValue(paramName, __DEFAULT_BGCOLOR);
115                urlParams.put(paramName, paramValue);
116            }
117            else if ("wkst".equals(paramName) || "src".equals(paramName))
118            {
119                urlParams.put(paramName, parameters.getParameter(paramName, ""));
120            }
121        }
122        urlParams.put("ctz", __CTZ);
123        urlParams.put("color", __DEFAULT_COLOR);
124        
125        return urlParams;
126    }
127    
128    private String _getParameterValue (String name, String defaultValue)
129    {
130        String value = parameters.getParameter(name, defaultValue);
131        if (value.isEmpty())
132        {
133            value = defaultValue;
134        }
135        return value;
136    }
137}