001/*
002 *  Copyright 2017 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.linkdirectory.dynamic;
017
018import java.io.IOException;
019import java.util.Date;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.core.user.CurrentUserProvider;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.runtime.parameter.ParameterHelper;
034
035/**
036 * Abstract generator for saxing dynamic information.
037 */
038public abstract class AbstractInternalDynamicInformationGenerator extends ServiceableGenerator
039{
040    /** Constant for root tag */
041    public static final String DYNAMIC_INFORMATION = "dynamic-information";
042    /** Constant for short value tag */
043    public static final String SHORT_VALUE = "short-value";
044    /** Constant for long value tag */
045    public static final String LONG_VALUE = "long-value";
046    /** Constant for tooltip tag */
047    public static final String TOOLTIP = "tooltip";
048    /** Constant for item tag */
049    public static final String ITEM = "item";
050    /** Constant for item's title tag */
051    public static final String ITEM_TITLE = "title";
052    /** Constant for item's summary tag */
053    public static final String ITEM_SUMMARY = "summary";
054    /** Constant for item's footer tag */
055    public static final String ITEM_FOOTER = "footer";
056
057    /** The current user provider **/
058    protected CurrentUserProvider _currentUserProvider;
059
060    @Override
061    public void service(ServiceManager serviceManager) throws ServiceException
062    {
063        super.service(serviceManager);
064        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
065    }
066
067    public void generate() throws IOException, SAXException, ProcessingException
068    {
069        contentHandler.startDocument();
070
071        XMLUtils.startElement(contentHandler, DYNAMIC_INFORMATION);
072
073        saxShortValue();
074        saxLongValue();
075
076        XMLUtils.startElement(contentHandler, TOOLTIP);
077        saxTooltips();
078        XMLUtils.endElement(contentHandler, TOOLTIP);
079
080        XMLUtils.endElement(contentHandler, DYNAMIC_INFORMATION);
081
082        contentHandler.endDocument();
083    }
084    
085    /** 
086     * Get the current connected user
087     * @return A user identity or <code>null</code> if there is no connected user
088     */
089    protected UserIdentity getCurrentUser()
090    {
091        return _currentUserProvider.getUser();
092    }
093    
094    /**
095     * Get the current language
096     * @return the current language
097     */
098    protected String getCurrentLanguage()
099    {
100        Request request = ObjectModelHelper.getRequest(objectModel);
101        return request.getParameter("lang");
102    }
103
104    /**
105     * Get the number of requested max items
106     * @return the number of max items. -1 if there is no limitations
107     */
108    protected int getMaxItems() 
109    {
110        Request request = ObjectModelHelper.getRequest(objectModel);
111        try
112        {
113            return Integer.parseInt(request.getParameter("maxItems"));
114        }
115        catch (NumberFormatException e)
116        {
117            return -1;
118        }
119    }
120    
121    /**
122     * Create a short value node.
123     * 
124     * @throws SAXException If a SAXException error occurs
125     */
126    protected abstract void saxShortValue() throws SAXException;
127
128    /**
129     * Create a long value node.
130     * 
131     * @throws SAXException If a error occurs while saxing
132     */
133    protected abstract void saxLongValue() throws SAXException;
134
135    /**
136     * Create a tooltip node.
137     * 
138     * @throws SAXException If a error occurs while saxing
139     */
140    protected abstract void saxTooltips() throws SAXException;
141
142    /**
143     * Create an item node.
144     * 
145     * @param title The title of item.
146     * @param summary The summary of item.
147     * @param footer The footer of item.
148     * @throws SAXException If a error occurs while saxing
149     */
150    protected void saxItem(String title, String summary, String footer) throws SAXException
151    {
152        XMLUtils.startElement(contentHandler, ITEM);
153        XMLUtils.createElement(contentHandler, ITEM_TITLE, title == null ? "" : title);
154        XMLUtils.createElement(contentHandler, ITEM_SUMMARY, summary == null ? "" : summary);
155        XMLUtils.createElement(contentHandler, ITEM_FOOTER, footer == null ? "" : footer);
156        XMLUtils.endElement(contentHandler, ITEM);
157    }
158    
159    /**
160     * Create an item node when the title is a {@link Date}
161     * @param title The title of item as Date.
162     * @param summary The summary of item.
163     * @param footer The footer of item.
164     * @throws SAXException If a error occurs while saxing
165     */
166    protected void saxItem(Date title, String summary, String footer) throws SAXException
167    {
168        XMLUtils.startElement(contentHandler, ITEM);
169        AttributesImpl attr = new AttributesImpl();
170        attr.addCDATAAttribute("type", "date");
171        XMLUtils.createElement(contentHandler, ITEM_TITLE, attr, ParameterHelper.valueToString(title));
172        XMLUtils.createElement(contentHandler, ITEM_SUMMARY, summary == null ? "" : summary);
173        XMLUtils.createElement(contentHandler, ITEM_FOOTER, footer == null ? "" : footer);
174        XMLUtils.endElement(contentHandler, ITEM);
175    }
176
177}