001/* 002 * Copyright 2023 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.Set; 022 023import org.apache.commons.lang3.LocaleUtils; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.plugins.repository.AmetysObject; 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.data.holder.ModelLessDataHolder; 032import org.ametys.plugins.repository.data.holder.impl.DefaultModelLessDataHolder; 033import org.ametys.plugins.repository.data.repositorydata.RepositoryData; 034import org.ametys.plugins.repository.data.repositorydata.impl.MemoryRepositoryData; 035import org.ametys.web.repository.page.Page; 036import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage; 037import org.ametys.web.repository.page.virtual.VirtualPageConfiguration; 038 039/** 040 * Page representing an UGC page. 041 */ 042public class UGCPage extends AbstractConfigurableVirtualPage<UGCPageFactory> 043{ 044 private String _title; 045 private String _path; 046 private Content _ugcContent; 047 048 /** 049 * Constructor. 050 * @param root the root page. 051 * @param syncContent the synchronized content 052 * @param path the path 053 * @param configuration The abstract virtual page's configuration 054 * @param scheme The scheme 055 * @param factory The UGC page factory 056 */ 057 public UGCPage(Page root, VirtualPageConfiguration configuration, String scheme, UGCPageFactory factory, Content syncContent, String path) 058 { 059 super(root, configuration, scheme, factory); 060 061 _path = path; 062 _ugcContent = syncContent; 063 _title = _ugcContent.getTitle(LocaleUtils.toLocale(root.getSitemapName())); 064 } 065 066 /** 067 * Returns the associated UGC {@link Content}. 068 * @return the associated UGC {@link Content}. 069 */ 070 @SuppressWarnings("unchecked") 071 @Override 072 public Content getContent() 073 { 074 return _ugcContent; 075 } 076 077 public int getDepth() throws AmetysRepositoryException 078 { 079 return _root.getDepth() + (_path.equals("_root") ? 1 : 2); 080 } 081 082 @Override 083 public Set<String> getReferers() throws AmetysRepositoryException 084 { 085 return null; 086 } 087 088 public String getTitle() throws AmetysRepositoryException 089 { 090 return _title; 091 } 092 093 public String getLongTitle() throws AmetysRepositoryException 094 { 095 return _title; 096 } 097 098 public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException 099 { 100 List<Page> children = new ArrayList<>(); 101 return new CollectionIterable<>(children); 102 } 103 104 public String getPathInSitemap() throws AmetysRepositoryException 105 { 106 if (_path.equals("_root")) 107 { 108 return _root.getPathInSitemap() + "/" + getName(); 109 } 110 else 111 { 112 return _root.getPathInSitemap() + "/" + _path + "/" + getName(); 113 } 114 } 115 116 public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException 117 { 118 if (path.isEmpty()) 119 { 120 throw new AmetysRepositoryException("path must be non empty"); 121 } 122 123 return null; 124 } 125 126 @SuppressWarnings("unchecked") 127 public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException 128 { 129 return getChildrenPages(); 130 } 131 132 public boolean hasChild(String name) throws AmetysRepositoryException 133 { 134 return false; 135 } 136 137 public String getId() throws AmetysRepositoryException 138 { 139 return _factory.getUgcPageHandler().computePageId(_path, _root, _ugcContent); 140 } 141 142 public String getName() throws AmetysRepositoryException 143 { 144 return _ugcContent.getName(); 145 } 146 147 @SuppressWarnings("unchecked") 148 public Page getParent() throws AmetysRepositoryException 149 { 150 if (!_path.equals("_root")) 151 { 152 String name = _path; 153 Map<String, Map<String, String>> transitionalPageName = _factory.getUgcPageHandler().getTransitionalPage(_root); 154 Map<String, String> attributes = transitionalPageName.get(name); 155 String title = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_TITLE); 156 String metadataValue = attributes.get(UGCPageHandler.ATTRIBUTE_TRANSITIONAL_PAGE_METADATA_VALUE); 157 158 return _factory.getTransitionalPageFactory().createUGCTransitionalPage(_root, title, metadataValue, name); 159 } 160 else 161 { 162 return _root; 163 } 164 } 165 166 public String getParentPath() throws AmetysRepositoryException 167 { 168 if (!_path.equals("_root")) 169 { 170 return _root.getPath() + "/" + _path; 171 } 172 else 173 { 174 return _root.getPath(); 175 } 176 } 177 178 public ModelLessDataHolder getDataHolder() 179 { 180 RepositoryData repositoryData = new MemoryRepositoryData(getName()); 181 return new DefaultModelLessDataHolder(_factory.getPageDataTypeEP(), repositoryData); 182 } 183 184 public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException 185 { 186 List<Page> children = new ArrayList<>(); 187 return new CollectionIterable<>(children); 188 } 189 190 public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException 191 { 192 throw new UnknownAmetysObjectException("There is no child for ugc page"); 193 } 194}