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.Set; 021import java.util.stream.Collectors; 022 023import org.ametys.cms.repository.Content; 024import org.ametys.plugins.repository.AmetysObject; 025import org.ametys.plugins.repository.AmetysObjectIterable; 026import org.ametys.plugins.repository.AmetysRepositoryException; 027import org.ametys.plugins.repository.CollectionIterable; 028import org.ametys.plugins.repository.UnknownAmetysObjectException; 029import org.ametys.plugins.repository.data.holder.ModelLessDataHolder; 030import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder; 031import org.ametys.plugins.repository.data.repositorydata.RepositoryData; 032import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage; 035import org.ametys.web.repository.page.virtual.VirtualPageConfiguration; 036 037 038/** 039 * Page representing a transitional page. 040 */ 041public class UGCTransitionalPage extends AbstractConfigurableVirtualPage<UGCTransitionalPageFactory> 042{ 043 private String _path; 044 private String _title; 045 private String _metadataValue; 046 047 /** 048 * Constructor. 049 * @param root the user directory root page. 050 * @param title the page's title. 051 * @param metadataValue the metadata value 052 * @param path the path 053 * @param configuration The abstract virtual page's configuration 054 * @param scheme The scheme 055 * @param factory The UGC transitional page factory 056 */ 057 public UGCTransitionalPage(Page root, VirtualPageConfiguration configuration, String scheme, UGCTransitionalPageFactory factory, String title, String metadataValue, String path) 058 { 059 super(root, configuration, scheme, factory); 060 061 _title = title; 062 _metadataValue = metadataValue; 063 _path = path; 064 _factory = factory; 065 } 066 067 @Override 068 public int getDepth() throws AmetysRepositoryException 069 { 070 return _root.getDepth() + 1; 071 } 072 073 @Override 074 public Set<String> getReferers() throws AmetysRepositoryException 075 { 076 return null; 077 } 078 079 @Override 080 public String getTitle() throws AmetysRepositoryException 081 { 082 return _title; 083 } 084 085 @Override 086 public String getLongTitle() throws AmetysRepositoryException 087 { 088 return _title; 089 } 090 091 @Override 092 public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException 093 { 094 List<Page> children = new ArrayList<>(); 095 096 AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue); 097 for (Content content : contentsForTransitionalPage) 098 { 099 children.add(_factory.getUGCPageFactory().createUGCPage(_root, content, _path)); 100 } 101 102 return new CollectionIterable<>(children); 103 } 104 105 @Override 106 public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException 107 { 108 if (includeInvisiblePage) 109 { 110 return getChildrenPages(); 111 } 112 else 113 { 114 List<Page> children = new ArrayList<>(); 115 return new CollectionIterable<>(children); 116 } 117 } 118 119 @Override 120 public String getPathInSitemap() throws AmetysRepositoryException 121 { 122 return _root.getPathInSitemap() + "/" + _path; 123 } 124 125 @SuppressWarnings("unchecked") 126 @Override 127 public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException 128 { 129 if (path.isEmpty()) 130 { 131 throw new AmetysRepositoryException("path must be non empty"); 132 } 133 134 String name = path; 135 AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue); 136 List<Content> contentFilters = contentsForTransitionalPage.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList()); 137 if (!contentFilters.isEmpty()) 138 { 139 Content content = contentFilters.get(0); 140 return (A) _factory.getUGCPageFactory().createUGCPage(_root, content, _path); 141 } 142 else 143 { 144 throw new UnknownAmetysObjectException("No ugc page for path " + path); 145 } 146 } 147 148 @SuppressWarnings("unchecked") 149 @Override 150 public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException 151 { 152 return getChildrenPages(); 153 } 154 155 @Override 156 public boolean hasChild(String name) throws AmetysRepositoryException 157 { 158 AmetysObjectIterable<Content> contentsForTransitionalPage = _factory.getUgcPageHandler().getContentsForTransitionalPage(_root, _metadataValue); 159 List<Content> contentFilters = contentsForTransitionalPage.stream().filter(c -> c.getName().equals(name)).collect(Collectors.toList()); 160 return !contentFilters.isEmpty(); 161 } 162 163 @Override 164 public String getId() throws AmetysRepositoryException 165 { 166 // E.g: ugctransitional://path?rootId=... 167 return "ugctransitional://" + _path + "?rootId=" + _root.getId(); 168 } 169 170 @Override 171 public String getName() throws AmetysRepositoryException 172 { 173 return _path; 174 } 175 176 @SuppressWarnings("unchecked") 177 @Override 178 public Page getParent() throws AmetysRepositoryException 179 { 180 return _root; 181 } 182 183 @Override 184 public String getParentPath() throws AmetysRepositoryException 185 { 186 return _root.getPath(); 187 } 188 189 public ModelLessDataHolder getDataHolder() 190 { 191 RepositoryData repositoryData = new MemoryRepositoryData(getName()); 192 return new DefaultModelLessDataHolder(_factory.getPageDataTypeEP(), repositoryData); 193 } 194 195 @Override 196 public boolean isVisible() throws AmetysRepositoryException 197 { 198 return _factory.getUgcPageHandler().isClassificationPagesVisible(_root); 199 } 200 201 @Override 202 public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException 203 { 204 return getChildrenPages().stream().collect(Collectors.toList()).get(index); 205 } 206}