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.odf.program.generators;
017
018import java.util.Locale;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.generation.Generator;
025import org.apache.cocoon.xml.AttributesImpl;
026import org.apache.cocoon.xml.XMLUtils;
027import org.apache.commons.lang.ArrayUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.cms.content.ContentGenerator;
031import org.ametys.cms.repository.Content;
032import org.ametys.odf.skill.CreateSkillsComponent;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035
036/**
037 * {@link Generator} for rendering ODF content data potentially synchronized.
038 */
039public class ODFContentGenerator extends ContentGenerator
040{
041    /** The Ametys resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        super.service(serviceManager);
048        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
049    }
050    
051    /**
052     * Sax all skills units metadata
053     * @param odfContent the odf content
054     * @param defaultLocale the default locale
055     * @throws SAXException if a saxing error occurred
056     * @throws ProcessingException if a processing error occurred
057     */
058    protected void saxSkillSets(Content odfContent, Locale defaultLocale) throws SAXException, ProcessingException
059    {
060        XMLUtils.startElement(contentHandler, "skillSets");
061        
062        XMLUtils.startElement(contentHandler, "required");
063        _saxSkillSets(odfContent, CreateSkillsComponent.REQUIRED_SKILL_SETS_METADATA_NAME, defaultLocale);
064        XMLUtils.endElement(contentHandler, "required");
065
066        XMLUtils.startElement(contentHandler, "acquired");
067        _saxSkillSets(odfContent, CreateSkillsComponent.ACQUIRED_SKILL_SETS_METADATA_NAME, defaultLocale);
068        XMLUtils.endElement(contentHandler, "acquired");
069        
070        XMLUtils.endElement(contentHandler, "skillSets");
071    }
072
073    /**
074     * Sax skill sets from the content
075     * @param odfContent the ODF content
076     * @param metadataName the metadata name
077     * @param defaultLocale the default locale
078     * @throws SAXException if a saxing error occurred
079     */
080    protected void _saxSkillSets(Content odfContent, String metadataName, Locale defaultLocale) throws SAXException
081    {
082        String[] skillSetIds = odfContent.getMetadataHolder().getStringArray(metadataName, ArrayUtils.EMPTY_STRING_ARRAY);
083        for (String skillSetId : skillSetIds)
084        {
085            try
086            {
087                Content skillSetsContent = _resolver.resolveById(skillSetId);
088                _saxSkillSets(skillSetsContent, defaultLocale);
089            }
090            catch (UnknownAmetysObjectException e) 
091            {
092                getLogger().warn("Can't get skill sets content with id " + skillSetId);
093            }
094        }
095    }
096
097    /**
098     * Sax skills from the skill sets
099     * @param skillSetsContent the skill set content
100     * @param defaultLocale the default locale
101     * @throws SAXException if a saxing error occurred
102     */
103    protected void _saxSkillSets(Content skillSetsContent, Locale defaultLocale) throws SAXException
104    {
105        AttributesImpl skillSetAttr = new AttributesImpl();
106        skillSetAttr.addCDATAAttribute("id", skillSetsContent.getId());
107        XMLUtils.startElement(contentHandler, "skillSets", skillSetAttr);
108        
109        XMLUtils.createElement(contentHandler, "title", skillSetsContent.getTitle(defaultLocale));
110        _saxSkills(skillSetsContent, defaultLocale);
111        
112        XMLUtils.endElement(contentHandler, "skillSets");
113    }
114
115    /**
116     * Sax skills from the skills unit
117     * @param skillSetsContent the skills Unit content
118     * @param defaultLocale the default locale
119     * @throws SAXException if a saxing error occurred
120     */
121    protected void _saxSkills(Content skillSetsContent, Locale defaultLocale) throws SAXException
122    {
123        XMLUtils.startElement(contentHandler, "skills");
124        String[] skillIds = skillSetsContent.getMetadataHolder().getStringArray(CreateSkillsComponent.SKILLS_METADATA_NAME, ArrayUtils.EMPTY_STRING_ARRAY);
125        for (String skillId : skillIds)
126        {
127            try
128            {
129                Content skillContent = _resolver.resolveById(skillId);
130                _saxSkill(skillContent, defaultLocale);
131            }
132            catch (UnknownAmetysObjectException e) 
133            {
134                getLogger().warn("Can't get skill content with id " + skillId);
135            }
136        }
137        
138        XMLUtils.endElement(contentHandler, "skills");
139    }
140
141    /**
142     * Sax a skill
143     * @param skillContent the skill content
144     * @param defaultLocale the default locale
145     * @throws SAXException if a saxing error occurred
146     */
147    protected void _saxSkill(Content skillContent, Locale defaultLocale) throws SAXException
148    {
149        AttributesImpl skillAttr = new AttributesImpl();
150        skillAttr.addCDATAAttribute("id", skillContent.getId());
151        XMLUtils.startElement(contentHandler, "skill", skillAttr);
152        
153        XMLUtils.createElement(contentHandler, "title", skillContent.getTitle(defaultLocale));
154        
155        String[] otherNames = skillContent.getMetadataHolder().getStringArray(CreateSkillsComponent.SKILL_OTHER_NAMES_METADATA_NAME, ArrayUtils.EMPTY_STRING_ARRAY);
156        XMLUtils.startElement(contentHandler, "otherNames");
157        for (String name : otherNames)
158        {
159            XMLUtils.createElement(contentHandler, "name", name);
160        }
161        XMLUtils.endElement(contentHandler, "otherNames");
162        
163        XMLUtils.endElement(contentHandler, "skill");
164    }
165    
166    /**
167     * Sax the existing translations
168     * @param translations The translations
169     * @throws SAXException if an error occurs
170     */
171    protected void saxTranslations (Map<String, String> translations) throws SAXException
172    {
173        XMLUtils.startElement(contentHandler, "translations");
174        for (String lang : translations.keySet())
175        {
176            String contentId = translations.get(lang);
177            try
178            {
179                Content translatedContent = _resolver.resolveById(contentId);
180                XMLUtils.createElement(contentHandler, lang, translatedContent.getId());
181            }
182            catch (UnknownAmetysObjectException e)
183            {
184                // Ignore, the translation just doesn't exist in the live workspace.
185            }
186        }
187        XMLUtils.endElement(contentHandler, "translations");
188    }
189}