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