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.userdirectory.transformation.xslt;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
023import org.ametys.plugins.userdirectory.page.OrgUnitPage;
024import org.ametys.plugins.userdirectory.page.OrganisationChartPageResolver;
025
026/**
027 * Helper component to be used from XSL stylesheets.
028 */
029public class OrgUnitXSLTHelper extends org.ametys.web.transformation.xslt.AmetysXSLTHelper
030{
031    /** The DAO for synchronizable contents collections */
032    protected static SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
033    /** The resolver for organisation chart directory pages */
034    protected static OrganisationChartPageResolver _organisationChartPageResolver;
035
036    @Override
037    public void service(ServiceManager manager) throws ServiceException
038    {
039        super.service(manager);
040        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
041        _organisationChartPageResolver = (OrganisationChartPageResolver) manager.lookup(OrganisationChartPageResolver.ROLE);
042    }
043    
044    
045    /**
046     * Gets the id of the page of the orgUnit content
047     * @param contentId The orgUnit content id
048     * @return the id of the page of the orgUnit content
049     */
050    public static String getOrgUnitPage(String contentId)
051    {
052        if (contentId == null)
053        {
054            return null;
055        }
056        
057        Content content = _ametysObjectResolver.resolveById(contentId);
058        if (content == null)
059        {
060            return null;
061        }
062        
063        String currentSitemap = lang();
064        String siteName = site();
065            
066        OrgUnitPage orgUnitPage = _organisationChartPageResolver.getOrgUnitPage(content, siteName, currentSitemap);
067        if (orgUnitPage != null)
068        {
069            return orgUnitPage.getId();
070        }
071        
072        return null;
073    }
074}