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.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.cms.repository.Content;
029import org.ametys.plugins.repository.AmetysObjectFactory;
030import org.ametys.plugins.repository.AmetysObjectIterable;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.AmetysRepositoryException;
033import org.ametys.plugins.repository.CollectionIterable;
034import org.ametys.plugins.repository.UnknownAmetysObjectException;
035import org.ametys.plugins.repository.jcr.JCRAmetysObject;
036import org.ametys.plugins.repository.virtual.VirtualAmetysObjectFactory;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038import org.ametys.web.data.type.ModelItemTypeExtensionPoint;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.service.ServiceExtensionPoint;
041import org.ametys.web.skin.SkinsManager;
042
043/**
044 * {@link AmetysObjectFactory} for handling "virtual" ugc page
045 */
046public class VirtualUGCPageFactory extends AbstractLogEnabled implements VirtualAmetysObjectFactory<Page>, Serviceable
047{
048    /** The ametys object resolver */
049    protected AmetysObjectResolver _resolver;
050    
051    /** The UGC page handler */
052    protected UGCPageHandler _ugcPageHandler;
053
054    /** The skin manager */
055    protected SkinsManager _skinManager;
056    
057    /** The extension point with available data types for pages */
058    protected ModelItemTypeExtensionPoint _pageDataTypeExtensionPoint;
059    
060    /** The service extension point */
061    protected ServiceExtensionPoint _serviceExtensionPoint;
062    
063    @Override
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
067        _ugcPageHandler = (UGCPageHandler) manager.lookup(UGCPageHandler.ROLE);
068        _skinManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
069        _pageDataTypeExtensionPoint = (ModelItemTypeExtensionPoint) manager.lookup(ModelItemTypeExtensionPoint.ROLE_PAGE_DATA);
070        _serviceExtensionPoint = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
071    }
072
073    @Override
074    public Page getAmetysObjectById(String id) throws AmetysRepositoryException
075    {
076        // No page object linked to this factory
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
082    {
083        // No page object linked to this factory
084        throw new UnsupportedOperationException();
085    }
086
087    @Override
088    public String getScheme()
089    {
090        return "ugcroot";
091    }
092
093    @Override
094    public AmetysObjectIterable<Page> getChildren(JCRAmetysObject parent)
095    {
096        if (!(parent instanceof Page))
097        {
098            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
099        }
100        
101        List<Page> children = new ArrayList<>();
102        Page rootPage = (Page) parent;
103        
104        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
105        if (StringUtils.isNotBlank(classificationMetadata))
106        {
107            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
108            for (String name : transitionalPageName.keySet())
109            {
110                Map<String, String> attributes = transitionalPageName.get(name);
111                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
112                AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, metadataValue);
113                if (contentsForTransitionalPage.getSize() != 0)
114                {
115                    String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
116                    children.add(new UGCTransitionalPage(rootPage, title, metadataValue, name, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint));
117                }
118            }
119        }
120        else
121        {
122            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
123            for (Content content : contents)
124            {
125                children.add(new UGCPage(rootPage, content, "_root", _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint));
126            }
127        }
128        
129        return new CollectionIterable<>(children);
130    }
131
132    @Override
133    public Page getChild(JCRAmetysObject parent, String childName)
134    {
135        if (!(parent instanceof Page))
136        {
137            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
138        }
139        
140        Page rootPage = (Page) parent;
141        
142        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
143        if (StringUtils.isNotBlank(classificationMetadata))
144        {
145            Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
146            if (transitionalPageName.containsKey(childName))
147            {
148                Map<String, String> attributes = transitionalPageName.get(childName);
149                String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE);
150                
151                AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, metadataValue);
152                if (contentsForTransitionalPage.getSize() != 0)
153                {
154                    String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE);
155                    return new UGCTransitionalPage(rootPage, title, metadataValue, childName, _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
156                }
157                else
158                {
159                    throw new UnknownAmetysObjectException("No transitional ugc page named " + childName);
160                }
161            }
162            else
163            {
164                throw new UnknownAmetysObjectException("No transitional ugc page named " + childName);
165            }
166        }
167        else
168        {
169            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
170            List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
171            
172            if (!contentFilters.isEmpty())
173            {
174                Content content = contentFilters.get(0);
175                return new UGCPage(rootPage, content, "_root", _resolver, _ugcPageHandler, _skinManager, _pageDataTypeExtensionPoint, _pageDataTypeExtensionPoint, _serviceExtensionPoint, _pageDataTypeExtensionPoint);
176            }
177            else
178            {
179                throw new UnknownAmetysObjectException("No ugc page named " + childName);
180            }
181        }
182    }
183
184    @Override
185    public boolean hasChild(JCRAmetysObject parent, String childName)
186    {
187        if (!(parent instanceof Page))
188        {
189            throw new IllegalArgumentException("The holder of the ugc virtual pages should be a page.");
190        }
191
192        Page rootPage = (Page) parent;
193        
194        String classificationMetadata = _ugcPageHandler.getClassificationAttribute(rootPage);
195        if (StringUtils.isNotBlank(classificationMetadata))
196        {
197            AmetysObjectIterable<Content> contentsForTransitionalPage = _ugcPageHandler.getContentsForTransitionalPage(rootPage, childName);
198            if (contentsForTransitionalPage.getSize() != 0)
199            {
200                Map<String, Map<String, String>> transitionalPageName = _ugcPageHandler.getTransitionalPage(rootPage);
201                return transitionalPageName.containsKey(childName);
202            }
203            
204            return false;
205        }
206        else
207        {
208            AmetysObjectIterable<Content> contents = _ugcPageHandler.getContentsForRootPage(rootPage);
209            List<Content> contentFilters = contents.stream().filter(c -> c.getName().equals(childName)).collect(Collectors.toList());
210            
211            return !contentFilters.isEmpty();
212        }
213    }
214}