001/*
002 *  Copyright 2012 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.cms.repository;
017
018import javax.jcr.Node;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.content.ContentExtractor;
024import org.ametys.cms.trash.element.TrashElementDAO;
025import org.ametys.plugins.repository.AmetysObjectFactory;
026import org.ametys.plugins.repository.AmetysObjectResolver;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.jcr.LockComponent;
029
030/**
031 * {@link AmetysObjectFactory} handling {@link Content}.
032 */
033public class ModifiableContentFactory extends ContentFactory
034{
035    private LockComponent _lockComponent;
036    private ServiceManager _serviceManager;
037    private ContentExtractor _contentExtractor;
038    private TrashElementDAO _trashElementDAO;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _serviceManager = manager;
045        _trashElementDAO = (TrashElementDAO) manager.lookup(TrashElementDAO.ROLE);
046    }
047    
048    @Override
049    public ModifiableDefaultContent getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
050    {
051        return new ModifiableDefaultContent<>(node, parentPath, this);
052    }
053    
054    LockComponent getLockComponent()
055    {
056        // lazy initialization to prevent chicken/egg scenario on startup
057        if (_lockComponent == null)
058        {
059            try
060            {
061                _lockComponent = (LockComponent) _serviceManager.lookup(LockComponent.ROLE);
062            }
063            catch (ServiceException e)
064            {
065                throw new AmetysRepositoryException("Caught an exception while retrieving LockComponent", e);
066            }
067        }
068        
069        return _lockComponent;
070    }
071    
072    ContentExtractor getContentExtractor()
073    {
074        if (_contentExtractor == null)
075        {
076            try
077            {
078                _contentExtractor = (ContentExtractor) _manager.lookup(ContentExtractor.CMS_CONTENT_EXTACTOR_ROLE);
079            }
080            catch (ServiceException e)
081            {
082                throw new IllegalStateException("Unable to get ContentExtractor", e);
083            }
084        }
085        
086        return _contentExtractor;
087    }
088    
089    TrashElementDAO _getTrashElementDAO()
090    {
091        return _trashElementDAO;
092    }
093    
094    AmetysObjectResolver _getResolver()
095    {
096        return _resolver;
097    }
098}