001/*
002 *  Copyright 2025 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.trash;
017
018import javax.jcr.NoSuchWorkspaceException;
019import javax.jcr.RepositoryException;
020import javax.jcr.Session;
021
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.provider.AbstractRepository;
029import org.ametys.plugins.repository.provider.JackrabbitRepository;
030
031/**
032 * Init class for trash.
033 */
034public class Init extends AbstractLogEnabled implements org.ametys.runtime.plugin.Init, Serviceable
035{
036    /** The repository */
037    protected JackrabbitRepository _repository;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _repository = (JackrabbitRepository) manager.lookup(AbstractRepository.ROLE);
043    }
044    
045    @Override
046    public void init() throws Exception
047    {
048        createWorkspace();
049    }
050    
051    /**
052     * Create the workspace "trash" if not exists.
053     * @throws RepositoryException if a repository error occurred.
054     */
055    protected void createWorkspace() throws RepositoryException
056    {
057        Session session = null;
058        Session trashSession = null;
059        
060        try
061        {
062            // Try to login
063            trashSession = _repository.login(TrashConstants.TRASH_WORKSPACE);
064        }
065        catch (NoSuchWorkspaceException e)
066        {
067            getLogger().info("Trash workspace does not exist, creating it...");
068            
069            session = _repository.login();
070            session.getWorkspace().createWorkspace(TrashConstants.TRASH_WORKSPACE);
071            trashSession = _repository.login(TrashConstants.TRASH_WORKSPACE);
072
073            trashSession.getRootNode().addNode(AmetysObjectResolver.ROOT_REPO, AmetysObjectResolver.ROOT_TYPE);
074            trashSession.save();
075        }
076        finally
077        {
078            if (session != null)
079            {
080                session.logout();
081            }
082            if (trashSession != null)
083            {
084                trashSession.logout();
085            }
086        }
087    }
088}