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 */
016
017package org.ametys.plugins.userdirectory.page;
018
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.commons.lang.StringUtils;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * {@link AmetysObjectFactory} handling {@link OrgUnitPage}.
031 */
032public class OrgUnitPageFactory extends AbstractUserDirectoryPageFactory implements AmetysObjectFactory<OrgUnitPage>
033{
034    private OrganisationChartPageHandler _organisationChartPageHandler;
035
036    @Override
037    public void service(ServiceManager manager) throws ServiceException
038    {
039        super.service(manager);
040
041        _organisationChartPageHandler = (OrganisationChartPageHandler) manager.lookup(OrganisationChartPageHandler.ROLE);
042    }
043
044    /**
045     * Create a Org unit page.
046     * @param root the root page.
047     * @param syncContent the synchronized content
048     * @param path the path
049     * @return The <code>OrgUnitPage</code>
050     */
051    public OrgUnitPage createOrgUnitPage(Page root, Content syncContent, String path) 
052    {
053        return new OrgUnitPage(root, getConfiguration(), getScheme(), this, syncContent, path);
054    }
055    
056    public OrgUnitPage getAmetysObjectById(String id) throws AmetysRepositoryException
057    {
058        // E.g: udorgunit://path?rootId=...&contentId=...
059        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udorgunit://"), "?rootId=");
060        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
061        Page root = _resolver.resolveById(rootId);
062        
063        String orgUnitId = StringUtils.substringAfter(id, "&contentId=");
064        Content syncContent = _resolver.resolveById(orgUnitId);
065        
066        return createOrgUnitPage(root, syncContent, path);
067    }
068    
069    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
070    {
071        // E.g: udorgunit://path?rootId=...&contentId=...
072        String orgUnitId = StringUtils.substringAfter(id, "&contentId=");
073        return _resolver.hasAmetysObjectForId(orgUnitId);
074    }
075
076    public String getScheme()
077    {
078        return "udorgunit";
079    }
080    
081    /**
082     * Get the organisation chart page handler
083     * @return The <code>OrganisationChartPageHandler</code>
084     */
085    public OrganisationChartPageHandler getOrganisationChartPageHandler()
086    {
087        return _organisationChartPageHandler;
088    }
089}