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.page;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.logger.AbstractLogEnabled;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * Resolves an organisation chart page path from the associated orgUnit content.
031 */
032public class OrganisationChartPageResolver extends AbstractLogEnabled implements Component, Serviceable
033{
034    /** The avalon role. */
035    public static final String ROLE = OrganisationChartPageResolver.class.getName();
036    
037    /** The ametys object resolver. */
038    protected AmetysObjectResolver _ametysResolver;
039    /** The organisation chart page handler */
040    protected OrganisationChartPageHandler _pageHandler;
041    
042    
043    @Override
044    public void service(ServiceManager serviceManager) throws ServiceException
045    {
046        _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
047        _pageHandler = (OrganisationChartPageHandler) serviceManager.lookup(OrganisationChartPageHandler.ROLE);
048    }
049    
050    /**
051     * Return the orgUnit page
052     * @param orgUnitRootPage the orgUnit root page
053     * @param orgUnitContent the orgUnit content
054     * @return the orgUnit page or null
055     */
056    public OrgUnitPage getOrgUnitPage(Page orgUnitRootPage, Content orgUnitContent)
057    {
058        // E.g: udorgunit://path?rootId=...&contentId=...
059        String orgUnitPath = _getOrgUnitPath(orgUnitContent);
060        String pageId =  "udorgunit://" + orgUnitPath + "?rootId=" + orgUnitRootPage.getId() + "&contentId=" + orgUnitContent.getId();
061        return _ametysResolver.resolveById(pageId);
062    }
063    
064    /**
065     * Get the path of a orgUnit content
066     * @param orgUnitContent The orgUnit content
067     * @return The path
068     */
069    private String _getOrgUnitPath(Content orgUnitContent) 
070    {
071        Content parentContent = _pageHandler.getParentContent(orgUnitContent);
072        if (parentContent != null)
073        {
074            return _getOrgUnitPath(parentContent) + "/" + orgUnitContent.getName();
075        }
076        else
077        {
078            return orgUnitContent.getName();
079        }
080    }
081    
082    /**
083     * Return the orgUnit page
084     * @param userContent the orgUnit page content
085     * @param siteName The current site name.
086     * @param sitemapName The current sitemap name.
087     * @return the orgUnit page or null
088     */
089    public OrgUnitPage getOrgUnitPage(Content userContent, String siteName, String sitemapName)
090    {
091        String language = userContent.getLanguage();
092        if (language == null)
093        {
094            language = sitemapName;
095        }
096        
097        Page organisationChartRootPage = _pageHandler.getOrganisationChartRootPages(siteName, language);
098        
099        if (organisationChartRootPage == null)
100        {
101            return null;
102        }
103        
104        return getOrgUnitPage(organisationChartRootPage, userContent);
105    }
106}