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 */
016package org.ametys.plugins.ugc.page;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.repository.AmetysObjectFactory;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.CollectionIterable;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.plugins.repository.jcr.JCRAmetysObject;
032import org.ametys.plugins.repository.virtual.VirtualAmetysObjectFactory;
033import org.ametys.web.repository.page.Page;
034
035/**
036 * {@link AmetysObjectFactory} for handling "virtual" ugc page
037 */
038public class VirtualUGCPageFactory extends AbstractUGCPageFactory implements VirtualAmetysObjectFactory<Page>
039{
040    @Override
041    public Page getAmetysObjectById(String id) throws AmetysRepositoryException
042    {
043        // No page object linked to this factory
044        throw new UnsupportedOperationException();
045    }
046
047    @Override
048    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
049    {
050        // No page object linked to this factory
051        throw new UnsupportedOperationException();
052    }
053
054    @Override
055    public String getScheme()
056    {
057        return "ugcroot";
058    }
059
060    @Override
061    public AmetysObjectIterable<Page> getChildren(JCRAmetysObject parent)
062    {
063        if (!(parent instanceof Page))
064        {
065            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
066        }
067        
068        List<Page> children = new ArrayList<>();
069        Page rootPage = (Page) parent;
070        
071        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
072        if (StringUtils.isNotBlank(classificationMetadata))
073        {
074            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
075            for (String name : transitionalPageName.keySet())
076            {
077                Map<String, String> attributes = transitionalPageName.get(name);
078                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
079                AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, metadataValue);
080                if (contentsForTransitionalPage.getSize() != 0)
081                {
082                    String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
083                    children.add(getTransitionalPageFactory().createUGCTransitionalPage(rootPage, title, metadataValue, name));
084                }
085            }
086        }
087        else
088        {
089            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
090            for (Content content : contents)
091            {
092                children.add(getUGCPageFactory().createUGCPage(rootPage, content, "_root"));
093            }
094        }
095        
096        return new CollectionIterable<>(children);
097    }
098
099    @Override
100    public Page getChild(JCRAmetysObject parent, String childName)
101    {
102        if (!(parent instanceof Page))
103        {
104            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
105        }
106        
107        Page rootPage = (Page) parent;
108        
109        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
110        if (StringUtils.isNotBlank(classificationMetadata))
111        {
112            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
113            if (transitionalPageName.containsKey(childName))
114            {
115                Map<String, String> attributes = transitionalPageName.get(childName);
116                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
117                
118                AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, metadataValue);
119                if (contentsForTransitionalPage.getSize() != 0)
120                {
121                    String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
122                    return getTransitionalPageFactory().createUGCTransitionalPage(rootPage, title, metadataValue, childName);
123                }
124                else
125                {
126                    throw new UnknownAmetysObjectException("No transitional ugc page named " + childName);
127                }
128            }
129            else
130            {
131                throw new UnknownAmetysObjectException("No transitional ugc page named " + childName);
132            }
133        }
134        else
135        {
136            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
137            List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
138            
139            if (!contentFilters.isEmpty())
140            {
141                Content content = contentFilters.get(0);
142                return getUGCPageFactory().createUGCPage(rootPage, content, "_root");
143            }
144            else
145            {
146                throw new UnknownAmetysObjectException("No ugc page named " + childName);
147            }
148        }
149    }
150
151    @Override
152    public boolean hasChild(JCRAmetysObject parent, String childName)
153    {
154        if (!(parent instanceof Page))
155        {
156            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
157        }
158
159        Page rootPage = (Page) parent;
160        
161        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
162        if (StringUtils.isNotBlank(classificationMetadata))
163        {
164            AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, childName);
165            if (contentsForTransitionalPage.getSize() != 0)
166            {
167                Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
168                return transitionalPageName.containsKey(childName);
169            }
170            
171            return false;
172        }
173        else
174        {
175            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
176            List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
177            
178            return !contentFilters.isEmpty();
179        }
180    }
181}