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.orgunit.OrgUnitFactory;
032import org.ametys.odf.program.generators.ProgramContentGenerator;
033
034/**
035 * {@link Generator} for rendering raw content data for a {@link OrgUnit}.
036 */
037public class OrgUnitContentGenerator extends ProgramContentGenerator
038{
039    @Override
040    protected void _saxOtherData(Content content, Locale defaultLocale) throws SAXException, ProcessingException, IOException
041    {
042        super._saxOtherData(content, defaultLocale);
043        
044        boolean isEdition = parameters.getParameterAsBoolean("isEdition", false);
045        if (!isEdition)
046        {
047            if (content instanceof OrgUnit)
048            {
049                OrgUnit orgUnit = (OrgUnit) content;
050                
051                // Contacts
052                saxPersons(orgUnit);
053                
054                // OrgUnits
055                saxOrgUnits(orgUnit);
056            }
057        }
058        
059        Request request = ObjectModelHelper.getRequest(objectModel);
060        request.setAttribute(Content.class.getName(), content);
061    }
062    
063    @Override
064    protected Set<String> getLinkedContents(Content content, String linkedContentType)
065    {
066        // To avoid an infinite loop due to invert relations between org units
067        if (content instanceof OrgUnit && linkedContentType.equals(OrgUnitFactory.ORGUNIT_CONTENT_TYPE))
068        {
069            return new LinkedHashSet<>(((OrgUnit) content).getSubOrgUnits());
070        }
071        return super.getLinkedContents(content, linkedContentType);
072    }
073}