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