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.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import javax.jcr.Node;
027import javax.jcr.RepositoryException;
028import javax.jcr.Value;
029
030import org.apache.avalon.framework.service.ServiceException;
031import org.apache.avalon.framework.service.ServiceManager;
032import org.apache.commons.lang3.StringUtils;
033
034import org.ametys.cms.contenttype.ContentType;
035import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
036import org.ametys.core.observation.Event;
037import org.ametys.core.observation.ObservationManager;
038import org.ametys.core.ui.Callable;
039import org.ametys.core.util.LambdaUtils;
040import org.ametys.plugins.repository.AmetysObjectResolver;
041import org.ametys.plugins.repository.jcr.JCRAmetysObject;
042import org.ametys.plugins.ugc.observation.ObservationConstants;
043import org.ametys.plugins.ugc.page.UGCPageHandler;
044import org.ametys.plugins.ugc.page.VirtualUGCPageFactory;
045import org.ametys.runtime.i18n.I18nizableText;
046import org.ametys.runtime.i18n.I18nizableTextParameter;
047import org.ametys.web.clientsideelement.AbstractPageClientSideElement;
048import org.ametys.web.repository.page.LockablePage;
049import org.ametys.web.repository.page.ModifiablePage;
050import org.ametys.web.repository.page.Page;
051
052/**
053 * Client side element for a controller which set/remove the root page of a ugc.
054 */
055public class SetUGCRootClientSideElement extends AbstractPageClientSideElement
056{
057    /** Observer manager. */
058    protected ObservationManager _observationManager;
059    /** The extension point for content types */
060    protected ContentTypeExtensionPoint _contentTypeEP;
061    /** The UGC page handler */
062    protected UGCPageHandler _ugcPageHandler;
063
064    @Override
065    public void service(ServiceManager smanager) throws ServiceException
066    {
067        super.service(smanager);
068        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
069        _contentTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
070        _ugcPageHandler = (UGCPageHandler) smanager.lookup(UGCPageHandler.ROLE);
071        
072    }
073    
074    /**
075     * Gets the status of the given page
076     * @param pageId The page id
077     * @return the status of the given page
078     */
079    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
080    public Map<String, Object> getStatus(String pageId)
081    {
082        Map<String, Object> result = new HashMap<>();
083        
084        Map<String, Object> parameters = this._script.getParameters();
085        
086        Page page = _resolver.resolveById(pageId);
087        
088        if (!hasRight(getRights(Map.of())))
089        {
090            throw new IllegalStateException("User " + _currentUserProvider.getUser() + " try to access privileges feature without sufficient rights");
091        }
092        
093        if (page instanceof JCRAmetysObject)
094        {
095            if (_isUGCRootPage((JCRAmetysObject) page))
096            {
097                List<String> i18nParameters = new ArrayList<>();
098                i18nParameters.add(page.getTitle());
099    
100                I18nizableText ed = (I18nizableText) parameters.get("ugc-page-description");
101                I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
102                result.put("ugc-page-title", msg);
103                
104                ed = (I18nizableText) parameters.get("remove-ugc-page-description");
105                msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
106                result.put("remove-ugc-page-title", msg);
107                
108                String contentTypeId = page.getValue(UGCPageHandler.CONTENT_TYPE_DATA_NAME, StringUtils.EMPTY);
109                if (StringUtils.isNotEmpty(contentTypeId))
110                {
111                    I18nizableText contentTypeText = _contentTypeEP.hasExtension(contentTypeId) ? _contentTypeEP.getExtension(contentTypeId).getLabel() : new I18nizableText(contentTypeId);
112                    
113                    Map<String, I18nizableTextParameter> contentTypeI18nParameters = new HashMap<>();
114                    contentTypeI18nParameters.put("0", contentTypeText);
115                    
116                    ed = (I18nizableText) parameters.get("contenttype-ugc-page-description");
117                    msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), contentTypeI18nParameters);
118                    result.put("contenttype-ugc-page-description", msg);
119                }
120                
121                result.put("ugc-page-id", new I18nizableText(page.getId()));
122            }
123            else if (!_ugcPageHandler.getUGCRootPages(page.getSiteName(), page.getSitemapName()).isEmpty())
124            {
125                I18nizableText ed = (I18nizableText) parameters.get("ugc-page-already-exist");
126                I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey());
127                result.put("ugc-page-already-exist", msg);
128            }
129            else
130            {
131                List<String> i18nParameters = new ArrayList<>();
132                i18nParameters.add(page.getTitle());
133    
134                I18nizableText ed = (I18nizableText) parameters.get("add-ugc-page-description");
135                I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
136                
137                result.put("add-ugc-page-id", new I18nizableText(page.getId()));
138                result.put("add-ugc-page-title", msg);
139            }
140        }
141        else
142        {
143            List<String> noJcrI18nParameters = new ArrayList<>();
144            noJcrI18nParameters.add(page.getTitle());
145
146            I18nizableText ed = (I18nizableText) parameters.get("no-jcr-page-description");
147            I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), noJcrI18nParameters);
148            
149            result.put("no-jcr-page-id", new I18nizableText(page.getId()));
150            result.put("no-jcr-page-title", msg);
151        }
152        
153        return result;
154    }
155    
156    /**
157     * Sets the given page as the root of a ugc
158     * @param pageId The id of the page
159     * @param contentTypeId The id of the content type
160     * @param attributePath The classification attribute path
161     * @param classificationPageVisible the visibility of transitional pages
162     * @return A result map
163     * @throws RepositoryException if a repository error occurred
164     */
165    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
166    public Map<String, Object> setUGCRoot(String pageId, String contentTypeId, String attributePath, boolean classificationPageVisible) throws RepositoryException
167    {
168        Map<String, Object> result = new HashMap<>();
169        
170        Page page = _resolver.resolveById(pageId);
171        
172        if (!hasRight(page))
173        {
174            throw new IllegalStateException("User " + _currentUserProvider.getUser() + " try to access privileges feature without sufficient rights");
175        }
176        
177        if (page instanceof LockablePage lockablePage && lockablePage.isLocked())
178        {
179            throw new IllegalStateException("Cannot set the locked page '/" + page.getSitemapName() + "/" + page.getPathInSitemap() + "' as UGC root");
180        }
181        
182        ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
183        if (contentType == null)
184        {
185            result.put("error", "wrong-content-type");
186            return result;
187        }
188        else if (StringUtils.isNotBlank(attributePath))
189        {
190            if (!contentType.hasModelItem(attributePath))
191            {
192                result.put("error", "wrong-metadata");
193                return result;
194            }
195        }
196        
197        _ugcPageHandler.setUGCRoot(page, contentTypeId, attributePath, classificationPageVisible);
198        
199        return result;
200    }
201
202    
203    
204    /**
205     * Remove the ugc root status to the given page
206     * @param pageId The id of the page
207     * @return A result map
208     * @throws RepositoryException if a repository error occured
209     */
210    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
211    public Map<String, Object> removeUGCRoot(String pageId) throws RepositoryException
212    {
213        Map<String, Object> result = new HashMap<>();
214        
215        Page page = _resolver.resolveById(pageId);
216        
217        if (page instanceof JCRAmetysObject)
218        {
219            if (!hasRight(page))
220            {
221                throw new IllegalStateException("User '" + _currentUserProvider.getUser() + "' tried to access a privileged feature without convenient right");
222            }
223            
224            if (page instanceof LockablePage lockablePage && lockablePage.isLocked())
225            {
226                throw new IllegalStateException("Cannot unset the locked page '/" + page.getSitemapName() + "/" + page.getPathInSitemap() + "' as UGC root");
227            }
228
229            if (!_isUGCRootPage((JCRAmetysObject) page))
230            {
231                result.put("error", "no-root");
232                return result;
233            }
234            
235            Map<String, Object> eventParams = new HashMap<>();
236            eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page);
237            
238            // Unindex pages for all workspaces before the properties were removed
239            _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_DELETING, _currentUserProvider.getUser(), eventParams));
240            
241            _removeUGCRootProperty(page);
242            
243            _observationManager.notify(new Event(org.ametys.web.ObservationConstants.EVENT_PAGE_UPDATED, _currentUserProvider.getUser(), eventParams));
244            
245            // After live synchronization
246            _observationManager.notify(new Event(ObservationConstants.EVENT_UGC_ROOT_DELETED, _currentUserProvider.getUser(), eventParams));
247        }
248        else
249        {
250            result.put("error", "no-root");
251        }
252        return result;
253    }
254    
255    /**
256     * Gets information about ugc root status on the given.
257     * @param pageId The id of the page
258     * @return information about ugc root status on the given.
259     */
260    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
261    public Map<String, Object> getRootPageInfo(String pageId)
262    {
263        Map<String, Object> result = new HashMap<>();
264        
265        Page page = _resolver.resolveById(pageId);
266        
267        if (!hasRight(page))
268        {
269            throw new IllegalStateException("User " + _currentUserProvider.getUser() + " try to access privileges feature without sufficient rights");
270        }
271        
272        Set<Page> currentUGCPages = _ugcPageHandler.getUGCRootPages(page.getSiteName(), page.getSitemapName());
273        
274        if (currentUGCPages.contains(page))
275        {
276            result.put("isRoot", true);
277            result.put("contentType", page.getValue(UGCPageHandler.CONTENT_TYPE_DATA_NAME));
278            result.put("metadata", page.getValue(UGCPageHandler.CLASSIFICATION_ATTRIBUTE_DATA_NAME));
279            result.put("is-visible", page.getValue(UGCPageHandler.CLASSIFICATION_PAGE_VISIBLE_DATA_NAME, false));
280        }
281        else
282        {
283            result.put("isRoot", false);
284        }
285        
286        return result;
287    }
288    
289    private void _removeUGCRootProperty(Page page) throws RepositoryException
290    {
291        if (page instanceof JCRAmetysObject)
292        {
293            JCRAmetysObject jcrPage = (JCRAmetysObject) page;
294            Node node = jcrPage.getNode();
295            
296            if (node.hasProperty(AmetysObjectResolver.VIRTUAL_PROPERTY))
297            {
298                List<Value> values = new ArrayList<>(Arrays.asList(node.getProperty(AmetysObjectResolver.VIRTUAL_PROPERTY).getValues()));
299                int index = values.stream()
300                        .map(LambdaUtils.wrap(Value::getString))
301                        .collect(Collectors.toList())
302                        .indexOf(VirtualUGCPageFactory.class.getName());
303                if (index != -1)
304                {
305                    values.remove(index);
306                    node.setProperty(AmetysObjectResolver.VIRTUAL_PROPERTY, values.toArray(new Value[values.size()]));
307                }
308
309                // Remove the ugc root property
310                if (page instanceof ModifiablePage)
311                {
312                    ModifiablePage modifiablePage = (ModifiablePage) page;
313                    modifiablePage.removeValue(UGCPageHandler.CONTENT_TYPE_DATA_NAME);
314                    modifiablePage.removeValue(UGCPageHandler.CLASSIFICATION_ATTRIBUTE_DATA_NAME);
315                    modifiablePage.removeValue(UGCPageHandler.CLASSIFICATION_PAGE_VISIBLE_DATA_NAME);
316                }
317                
318                jcrPage.saveChanges();
319            }
320        }
321    }
322    
323    private boolean _isUGCRootPage (JCRAmetysObject jcrPage)
324    {
325        try
326        {
327            Node node = jcrPage.getNode();
328            
329            if (node.hasProperty(AmetysObjectResolver.VIRTUAL_PROPERTY))
330            {
331                List<Value> values = Arrays.asList(node.getProperty(AmetysObjectResolver.VIRTUAL_PROPERTY).getValues());
332                
333                return values.stream()
334                        .map(LambdaUtils.wrap(Value::getString))
335                        .anyMatch(v -> VirtualUGCPageFactory.class.getName().equals(v));
336            }
337            else
338            {
339                return false;
340            }
341        }
342        catch (RepositoryException e)
343        {
344            return false;
345        }
346    }
347}