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