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