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