001/*
002 *  Copyright 2018 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.ugc.page;
018
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysObjectIterable;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.UnknownAmetysObjectException;
028import org.ametys.web.repository.page.Page;
029
030/**
031 * {@link AmetysObjectFactory} handling {@link UGCTransitionalPage}.
032 */
033public class UGCTransitionalPageFactory extends AbstractUGCPageFactory implements AmetysObjectFactory<UGCTransitionalPage>
034{
035    /**
036     * Create a UGCTransitional page.
037     *  @param root the user directory root page.
038     * @param title the page's title.
039     * @param metadataValue the metadata value
040     * @param path the path
041     * @return The <code>UGCPage</code> created
042     */
043    public UGCTransitionalPage createUGCTransitionalPage(Page root, String title, String metadataValue, String path)
044    {
045        return new UGCTransitionalPage(root, getConfiguration(root), getScheme(), this, title, metadataValue, path);
046    }
047    
048    public UGCTransitionalPage getAmetysObjectById(String id) throws AmetysRepositoryException
049    {
050        // E.g: ugctransitional://path?rootId=...
051        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugctransitional://"), "?rootId=");
052        
053        String rootId = StringUtils.substringAfter(id, "?rootId=");
054        Page root = _resolver.resolveById(rootId);
055        
056        Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root);
057        if (transitionalPageName.containsKey(path))
058        {
059            Map<String, String> attributes = transitionalPageName.get(path);
060            String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
061            String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
062            return createUGCTransitionalPage(root, title, metadataValue, path);
063        }
064        else
065        {
066            throw new UnknownAmetysObjectException("No transitional page named " + path);
067        }
068    }
069
070    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
071    {
072        try
073        {
074            // E.g: ugctransitional://path?rootId=...
075            String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugctransitional://"), "?rootId=");
076            
077            String rootId = StringUtils.substringAfter(id, "?rootId=");
078            Page root = _resolver.resolveById(rootId);
079            
080            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root);
081            
082            if (transitionalPageName.containsKey(path))
083            {
084                Map<String, String> attributes = transitionalPageName.get(path);
085                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
086                
087                AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(root, metadataValue);
088                return contentsForTransitionalPage.getSize() != 0;
089            }
090            
091            return false;
092        }
093        catch (UnknownAmetysObjectException e)
094        {
095            return false;
096        }
097        
098    }
099
100    public String getScheme()
101    {
102        return "ugctransitional";
103    }
104}