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.avalon.framework.service.Serviceable;
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDAO;
026import org.ametys.plugins.repository.AmetysObjectFactory;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.userdirectory.OrganisationChartPageHandler;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.skin.SkinsManager;
032
033/**
034 * {@link AmetysObjectFactory} handling {@link OrgUnitPage}.
035 */
036public class OrgUnitPageFactory implements AmetysObjectFactory<OrgUnitPage>, Serviceable
037{
038    private AmetysObjectResolver _resolver;
039    private OrganisationChartPageHandler _organisationChartPageHandler;
040    private SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
041    private SkinsManager _skinManager;
042    
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
047        _organisationChartPageHandler = (OrganisationChartPageHandler) manager.lookup(OrganisationChartPageHandler.ROLE);
048        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.ROLE);
049        _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
050    }
051
052    public OrgUnitPage getAmetysObjectById(String id) throws AmetysRepositoryException
053    {
054        // E.g: udorgunit://path?rootId=...&contentId=...
055        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "udorgunit://"), "?rootId=");
056        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
057        Page root = _resolver.resolveById(rootId);
058        
059        String orgUnitId = StringUtils.substringAfter(id, "&contentId=");
060        Content syncContent = _resolver.resolveById(orgUnitId);
061        return new OrgUnitPage(root, syncContent, path, _resolver, _organisationChartPageHandler, _synchronizableContentsCollectionDAO, _skinManager);
062    }
063    
064    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
065    {
066        // E.g: udorgunit://path?rootId=...&contentId=...
067        String orgUnitId = StringUtils.substringAfter(id, "&contentId=");
068        return _resolver.hasAmetysObjectForId(orgUnitId);
069    }
070
071    public String getScheme()
072    {
073        return "udorgunit";
074    }
075    
076    SynchronizableContentsCollectionDAO _getSynchronizableContentsCollectionDAO()
077    {
078        return _synchronizableContentsCollectionDAO;
079    }
080}