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 */
016package org.ametys.plugins.odfweb.catalog;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.ui.Callable;
026import org.ametys.odf.catalog.Catalog;
027import org.ametys.plugins.odfweb.repository.OdfPageHandler;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.web.repository.page.Page;
031
032/**
033 * DAO for ODF catalog.
034 * Override {@link Catalog} to handle web specificities.
035 */
036public class CatalogDAO extends org.ametys.odf.catalog.CatalogDAO
037{
038    private OdfPageHandler _odfPageHandler;
039
040    @Override
041    public void service(ServiceManager smanager) throws ServiceException
042    {
043        super.service(smanager);
044        _odfPageHandler = (OdfPageHandler) smanager.lookup(OdfPageHandler.ROLE);
045    }
046
047    @Callable
048    @Override
049    public Map<String, Object> removeCatalog(String catalogId, boolean forceDeletion)
050    {
051        Catalog catalog = null;
052        try
053        {
054            catalog = _resolver.resolveById(catalogId);
055        }
056        catch (UnknownAmetysObjectException e)
057        {
058            return Map.of("error", "unknown-catalog");
059        }
060
061        Page page = _odfPageHandler.getOdfRootPage(null, null, catalog.getName());
062        
063        if (page != null)
064        {
065            Map<String, Object> result = new HashMap<>();
066            result.put("error", "error-with-message");
067            result.put("message", new I18nizableText("plugin.odf-web", "PLUGINS_ODFWEB_CATALOG_DELETE_ERROR_ODFROOT", Collections.singletonList(page.getTitle())));
068            return result;
069        }
070        
071        return super.removeCatalog(catalogId, forceDeletion);
072        
073    }
074}