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.AmetysRepositoryException;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * {@link AmetysObjectFactory} handling {@link UGCPage}.
031 */
032public class UGCPageFactory extends AbstractUGCPageFactory implements AmetysObjectFactory<UGCPage>
033{
034    /**
035     * Create a UGC page.
036     * @param root the root page.
037     * @param syncContent the synchronized content
038     * @param path the path
039     * @return The <code>UGCPage</code> created
040     */
041    public UGCPage createUGCPage(Page root, Content syncContent, String path)
042    {
043        return new UGCPage(root, getConfiguration(root), getScheme(), this, syncContent, path);
044    }
045    
046    public UGCPage getAmetysObjectById(String id) throws AmetysRepositoryException
047    {
048        // E.g: ugccontent://path?rootId=...&contentId=...
049        String path = StringUtils.substringBefore(StringUtils.substringAfter(id, "ugccontent://"), "?rootId=");
050        String rootId = StringUtils.substringBefore(StringUtils.substringAfter(id, "?rootId="), "&contentId=");
051        Page root = _resolver.resolveById(rootId);
052        
053        String contentId = StringUtils.substringAfter(id, "&contentId=");
054        Content ugcContent = _resolver.resolveById(contentId);
055        
056        if ("_root".equals(path) && _ugcPageHandler.hasContentForRootPage(root, ugcContent))
057        {
058            return createUGCPage(root, ugcContent, path);
059        }
060        else
061        {
062            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(root);
063            if (transitionalPageName.containsKey(path))
064            {
065                Map<String, String> attributes = transitionalPageName.get(path);
066                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
067                if (_ugcPageHandler.hasContentForTransitionalPage(root, metadataValue, ugcContent))
068                {
069                    return createUGCPage(root, ugcContent, path);
070                }
071            }
072        }
073        
074        throw new UnknownAmetysObjectException("The UGC page with id '" + id  + "' does not match a existing UGC page for content with id " + contentId);
075    }
076
077    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
078    {
079        // E.g: ugccontent://path?rootId=...&contentId=...
080        String contentId = StringUtils.substringAfter(id, "&contentId=");
081        
082        if (!_resolver.hasAmetysObjectForId(contentId))
083        {
084            return false;
085        }
086        else
087        {
088            // Related content exists, check its page is the same
089            UGCPage ugcPage = _ugcPageHandler.getUgcPage(contentId, null);
090            return ugcPage != null ? id.equals(ugcPage.getId()) : false;
091        }
092    }
093
094    public String getScheme()
095    {
096        return "ugccontent";
097    }
098}