001/*
002 *  Copyright 2010 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.orgunit.generators;
017
018import java.io.IOException;
019import java.util.LinkedHashSet;
020import java.util.Locale;
021import java.util.Set;
022
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.Generator;
027import org.xml.sax.SAXException;
028
029import org.ametys.cms.repository.Content;
030import org.ametys.odf.orgunit.OrgUnit;
031import org.ametys.odf.program.generators.ProgramContentGenerator;
032
033/**
034 * {@link Generator} for rendering raw content data for a {@link OrgUnit}.
035 */
036public class OrgUnitContentGenerator extends ProgramContentGenerator
037{
038    @Override
039    protected void _saxOtherData(Content content, Locale defaultLocale) throws SAXException, ProcessingException, IOException
040    {
041        super._saxOtherData(content, defaultLocale);
042        
043        boolean isEditionMetadataSet = parameters.getParameterAsBoolean("isEditionMetadataSet", false);
044        if (!isEditionMetadataSet)
045        {
046            if (content instanceof OrgUnit)
047            {
048                OrgUnit orgUnit = (OrgUnit) content;
049                
050                // Contacts
051                saxPersons(orgUnit);
052                
053                // OrgUnits
054                saxOrgUnits(orgUnit);
055            }
056        }
057        
058        Request request = ObjectModelHelper.getRequest(objectModel);
059        request.setAttribute(Content.class.getName(), content);
060    }
061    
062    @Override
063    protected Set<String> getLinkedContents(Content content, String linkedContentType)
064    {
065        // To avoid an infinite loop due to invert relations between org units
066        if (content instanceof OrgUnit)
067        {
068            return new LinkedHashSet<>(((OrgUnit) content).getSubOrgUnits());
069        }
070        return super.getLinkedContents(content, linkedContentType);
071    }
072}