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.survey.generators;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.generation.ServiceableGenerator;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.xml.sax.SAXException;
031
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.plugins.survey.repository.SurveyPage;
036import org.ametys.plugins.survey.repository.SurveyQuestion;
037import org.ametys.plugins.survey.repository.SurveyRule;
038
039/**
040 * SAX rules of a {@link SurveyQuestion}
041 *
042 */
043public class QuestionRulesGenerator extends ServiceableGenerator
044{
045    /** The Ametys object resolver */
046    protected AmetysObjectResolver _resolver;
047    
048    @Override
049    public void service(ServiceManager serviceManager) throws ServiceException
050    {
051        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
052    }
053    
054    @Override
055    public void generate() throws IOException, SAXException, ProcessingException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        String id = request.getParameter("id");
059        
060        AmetysObject ao = _resolver.resolveById(id);
061        
062        contentHandler.startDocument();
063        
064        if (ao instanceof SurveyQuestion)
065        {
066            saxRules((SurveyQuestion) ao, 0);
067        }
068        else if (ao instanceof SurveyPage)
069        {
070            saxRule ((SurveyPage) ao);
071        }
072        
073        contentHandler.endDocument();
074    }
075    
076    /**
077     * SAX rules
078     * @param question the question
079     * @param number the question number 
080     * @throws SAXException if an error occurs while saxing
081     */
082    public void saxRules (SurveyQuestion question, int number) throws SAXException
083    {
084        Map<String, String> options = question.getOptions();
085        
086        AttributesImpl attr = new AttributesImpl();
087        attr = new AttributesImpl();
088        attr.addCDATAAttribute("id", question.getId());
089        attr.addCDATAAttribute("number", String.valueOf(number));
090        
091        XMLUtils.startElement(contentHandler, "question", attr);
092        XMLUtils.createElement(contentHandler, "title", question.getTitle());
093        
094        XMLUtils.startElement(contentHandler, "rules");
095        List<SurveyRule> rules = question.getRules();
096        for (SurveyRule rule : rules)
097        {
098            AttributesImpl atts = new AttributesImpl();
099            
100            String option = rule.getOption();
101            atts.addCDATAAttribute("option", option);
102            atts.addCDATAAttribute("optionLabel", options.get(option));
103            
104            atts.addCDATAAttribute("type", rule.getType().name());
105            String page = rule.getPage();
106            if (page != null)
107            {
108                try
109                {
110                    SurveyPage pageAO = _resolver.resolveById(page);
111                    atts.addCDATAAttribute("page", page);
112                    atts.addCDATAAttribute("pageName", pageAO.getLabel());
113                }
114                catch (UnknownAmetysObjectException e)
115                {
116                    // Page does not exist anymore
117                }
118            }
119            XMLUtils.createElement(contentHandler, "rule", atts);
120        }
121        XMLUtils.endElement(contentHandler, "rules");
122        
123        XMLUtils.endElement(contentHandler, "question");
124    }
125    
126    /**
127     * SAX page rule
128     * @param surveyPage the survey page
129     * @throws SAXException if an error occurs while saxing
130     */
131    public void saxRule (SurveyPage surveyPage) throws SAXException
132    {
133        SurveyRule rule = surveyPage.getRule();
134        
135        if (rule != null)
136        {
137            AttributesImpl atts = new AttributesImpl();
138            atts.addCDATAAttribute("type", rule.getType().name());
139            String page = rule.getPage();
140            if (page != null)
141            {
142                try
143                { 
144                    atts.addCDATAAttribute("page", page);
145                    SurveyPage pageAO = _resolver.resolveById(page);
146                    atts.addCDATAAttribute("pageName", pageAO.getLabel());
147                }
148                catch (UnknownAmetysObjectException e)
149                {
150                    // The page does not exist anymore
151                }
152            }
153            
154            XMLUtils.createElement(contentHandler, "rule", atts);
155        }
156    }
157
158}