001/*
002 *  Copyright 2021 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.forms.dao;
017
018import java.time.LocalDate;
019import java.time.ZonedDateTime;
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024import java.util.Optional;
025import java.util.Set;
026import java.util.stream.Collectors;
027
028import javax.jcr.Node;
029import javax.jcr.Repository;
030import javax.jcr.RepositoryException;
031
032import org.apache.avalon.framework.component.Component;
033import org.apache.avalon.framework.service.ServiceException;
034import org.apache.avalon.framework.service.ServiceManager;
035import org.apache.avalon.framework.service.Serviceable;
036import org.apache.commons.lang.StringUtils;
037
038import org.ametys.core.observation.Event;
039import org.ametys.core.observation.ObservationManager;
040import org.ametys.core.right.RightManager;
041import org.ametys.core.right.RightManager.RightResult;
042import org.ametys.core.ui.Callable;
043import org.ametys.core.user.CurrentUserProvider;
044import org.ametys.core.user.UserIdentity;
045import org.ametys.core.util.DateUtils;
046import org.ametys.core.util.I18nUtils;
047import org.ametys.plugins.core.user.UserHelper;
048import org.ametys.plugins.forms.FormEvents;
049import org.ametys.plugins.forms.dao.FormEntryDAO.Sort;
050import org.ametys.plugins.forms.helper.ScheduleOpeningHelper;
051import org.ametys.plugins.forms.repository.CopyFormUpdater;
052import org.ametys.plugins.forms.repository.CopyFormUpdaterExtensionPoint;
053import org.ametys.plugins.forms.repository.Form;
054import org.ametys.plugins.forms.repository.Form.ExpirationPolicy;
055import org.ametys.plugins.forms.repository.FormDirectory;
056import org.ametys.plugins.forms.repository.FormEntry;
057import org.ametys.plugins.forms.repository.FormFactory;
058import org.ametys.plugins.forms.repository.FormQuestion;
059import org.ametys.plugins.forms.rights.FormsDirectoryRightAssignmentContext;
060import org.ametys.plugins.repository.AmetysObject;
061import org.ametys.plugins.repository.AmetysObjectIterable;
062import org.ametys.plugins.repository.AmetysObjectResolver;
063import org.ametys.plugins.repository.ModifiableAmetysObject;
064import org.ametys.plugins.repository.UnknownAmetysObjectException;
065import org.ametys.plugins.repository.jcr.NameHelper;
066import org.ametys.plugins.repository.provider.AbstractRepository;
067import org.ametys.plugins.workflow.support.WorkflowHelper;
068import org.ametys.runtime.authentication.AccessDeniedException;
069import org.ametys.runtime.i18n.I18nizableText;
070import org.ametys.runtime.model.ElementDefinition;
071import org.ametys.runtime.plugin.component.AbstractLogEnabled;
072import org.ametys.web.parameters.view.ViewParametersManager;
073import org.ametys.web.repository.page.ModifiableZoneItem;
074import org.ametys.web.repository.page.Page;
075import org.ametys.web.repository.page.SitemapElement;
076import org.ametys.web.repository.page.ZoneDAO;
077import org.ametys.web.repository.page.ZoneItem;
078import org.ametys.web.repository.site.SiteManager;
079import org.ametys.web.service.Service;
080import org.ametys.web.service.ServiceExtensionPoint;
081
082/**
083 * The form DAO
084 */
085public class FormDAO extends AbstractLogEnabled implements Serviceable, Component
086{
087    /** The Avalon role */
088    public static final String ROLE = FormDAO.class.getName();
089    /** The right id to handle forms */
090    public static final String HANDLE_FORMS_RIGHT_ID = "Plugins_Forms_Right_Handle";
091
092    private static final String __FORM_NAME_PREFIX = "form-";
093    
094    /** The Ametys object resolver */
095    protected AmetysObjectResolver _resolver;
096    /** The current user provider */
097    protected CurrentUserProvider _userProvider;
098    /** I18n Utils */
099    protected I18nUtils _i18nUtils;
100    /** The form directory DAO */
101    protected FormDirectoryDAO _formDirectoryDAO;
102    /** The form page DAO */
103    protected FormPageDAO _formPageDAO;
104    /** The form entry DAO */
105    protected FormEntryDAO _formEntryDAO;
106    /** The user helper */
107    protected UserHelper _userHelper;
108    /** The JCR repository. */
109    protected Repository _repository;
110    /** The right manager */
111    protected RightManager _rightManager;
112    /** The service extension point */
113    protected ServiceExtensionPoint _serviceEP;
114    /** The zone DAO */
115    protected ZoneDAO _zoneDAO;
116    /** The schedule opening helper */
117    protected ScheduleOpeningHelper _scheduleOpeningHelper;
118    /** The workflow helper */
119    protected WorkflowHelper _workflowHelper;
120    /** The site manager */
121    protected SiteManager _siteManager;
122    /** Observer manager. */
123    protected ObservationManager _observationManager;
124    /** The current user provider. */
125    protected CurrentUserProvider _currentUserProvider;
126    
127    /** The copy form updater extension point */
128    protected CopyFormUpdaterExtensionPoint _copyFormEP;
129    
130    public void service(ServiceManager manager) throws ServiceException
131    {
132        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
133        _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
134        _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE);
135        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
136        _formDirectoryDAO = (FormDirectoryDAO) manager.lookup(FormDirectoryDAO.ROLE);
137        _formPageDAO = (FormPageDAO) manager.lookup(FormPageDAO.ROLE);
138        _formEntryDAO = (FormEntryDAO) manager.lookup(FormEntryDAO.ROLE);
139        _repository = (Repository) manager.lookup(AbstractRepository.ROLE);
140        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
141        _serviceEP = (ServiceExtensionPoint) manager.lookup(ServiceExtensionPoint.ROLE);
142        _zoneDAO = (ZoneDAO) manager.lookup(ZoneDAO.ROLE);
143        _scheduleOpeningHelper = (ScheduleOpeningHelper) manager.lookup(ScheduleOpeningHelper.ROLE);
144        _workflowHelper = (WorkflowHelper) manager.lookup(WorkflowHelper.ROLE);
145        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
146        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
147        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
148        _copyFormEP = (CopyFormUpdaterExtensionPoint) manager.lookup(CopyFormUpdaterExtensionPoint.ROLE);
149    }
150
151    /**
152     * Check if a user have read rights on a form
153     * @param userIdentity the user
154     * @param form the form
155     * @return true if the user have read rights on a form
156     */
157    public boolean hasReadRightOnForm(UserIdentity userIdentity, Form form)
158    {
159        return _rightManager.hasReadAccess(userIdentity, form);
160    }
161    
162    /**
163     * Check if a user have write rights on a form element
164     * @param userIdentity the user
165     * @param formElement the form element
166     * @return true if the user have write rights on a form element
167     */
168    public boolean hasWriteRightOnForm(UserIdentity userIdentity, AmetysObject formElement)
169    {
170        return _rightManager.hasRight(userIdentity, HANDLE_FORMS_RIGHT_ID, formElement) == RightResult.RIGHT_ALLOW;
171    }
172    
173    /**
174     * Check if a user have write rights on a form
175     * @param userIdentity the user
176     * @param form the form
177     * @return true if the user have write rights on a form
178     */
179    public boolean hasRightAffectationRightOnForm(UserIdentity userIdentity, Form form)
180    {
181        return hasWriteRightOnForm(userIdentity, form) || _rightManager.hasRight(userIdentity, "Runtime_Rights_Rights_Handle", "/cms") == RightResult.RIGHT_ALLOW;
182    }
183    
184    /**
185     * Check rights for a form element as ametys object
186     * @param formElement the form element as ametys object
187     */
188    public void checkHandleFormRight(AmetysObject formElement)
189    {
190        UserIdentity user = _userProvider.getUser();
191        if (!hasWriteRightOnForm(user, formElement))
192        {
193            throw new AccessDeniedException("User '" + user + "' tried to handle forms without convenient right [" + HANDLE_FORMS_RIGHT_ID + "]");
194        }
195    }
196    
197    /**
198     * Get all forms from a site
199     * @param siteName the site name
200     * @return the list of form
201     */
202    public List<Form> getForms(String siteName)
203    {
204        String xpathQuery = "//element(" + siteName + ", ametys:site)//element(*, ametys:form)";
205        return _resolver.query(xpathQuery)
206            .stream()
207            .filter(Form.class::isInstance)
208            .map(Form.class::cast)
209            .collect(Collectors.toList());
210    }
211    
212    /**
213     * Get the form properties
214     * @param formId The form's id
215     * @param full <code>true</code> to get full information on form
216     * @param withRights <code>true</code> to have rights in the properties
217     * @return The form properties
218     */
219    @Callable (rights = Callable.NO_CHECK_REQUIRED)
220    public Map<String, Object> getFormProperties (String formId, boolean full, boolean withRights)
221    {
222        // Assume that no read access is checked (required for bus message target)
223        try
224        {
225            Form form = _resolver.resolveById(formId);
226            return getFormProperties(form, full, true);
227        }
228        catch (UnknownAmetysObjectException e)
229        {
230            getLogger().warn("Can't find form with id: {}. It probably has just been deleted", formId, e);
231            Map<String, Object> infos = new HashMap<>();
232            infos.put("id", formId);
233            return infos;
234        }
235    }
236    
237    /**
238     * Get the form properties
239     * @param form The form
240     * @param full <code>true</code> to get full information on form
241     * @param withRights <code>true</code> to have rights in the properties
242     * @return The form properties
243     */
244    public Map<String, Object> getFormProperties (Form form, boolean full, boolean withRights)
245    {
246        Map<String, Object> infos = new HashMap<>();
247        
248        List<FormEntry> entries = _formEntryDAO.getFormEntries(form, false, List.of(new Sort(FormEntry.ATTRIBUTE_SUBMIT_DATE, "descending")));
249        List<SitemapElement> pages = getFormPage(form.getId(), form.getSiteName());
250        String workflowName = form.getWorkflowName();
251
252        infos.put("type", "root");
253        infos.put("isForm", true);
254        infos.put("author", _userHelper.user2json(form.getAuthor(), true));
255        infos.put("contributor", _userHelper.user2json(form.getContributor()));
256        infos.put("lastModificationDate", DateUtils.zonedDateTimeToString(form.getLastModificationDate()));
257        infos.put("creationDate", DateUtils.zonedDateTimeToString(form.getCreationDate()));
258        infos.put("entriesAmount", entries.size());
259        infos.put("lastEntry", _getLastSubmissionDate(entries));
260        infos.put("workflowLabel", StringUtils.isNotBlank(workflowName) ? _workflowHelper.getWorkflowLabel(workflowName) : new I18nizableText("plugin.forms", "PLUGINS_FORMS_FORMS_EDITOR_WORKFLOW_NO_WORKFLOW"));
261        
262        /** Use in the bus message */
263        infos.put("id", form.getId());
264        infos.put("name", form.getName());
265        infos.put("title", form.getTitle());
266        infos.put("fullPath", getFormFullPath(form.getId()));
267        infos.put("pages", _getPagesInfos(pages));
268        infos.put("hasChildren", form.getPages().size() > 0);
269        infos.put("workflowName", workflowName);
270        
271        infos.put("isConfigured", isFormConfigured(form));
272        
273        UserIdentity currentUser = _userProvider.getUser();
274        if (withRights)
275        {
276            Set<String> userRights = _getUserRights(form);
277            infos.put("rights", userRights);
278            infos.put("canEditRight", userRights.contains(HANDLE_FORMS_RIGHT_ID) || _rightManager.hasRight(currentUser, "Runtime_Rights_Rights_Handle", "/cms") == RightResult.RIGHT_ALLOW);
279        }
280        else
281        {
282            boolean canWrite = hasWriteRightOnForm(currentUser, form);
283            infos.put("canWrite", canWrite);
284            infos.put("canEditRight", canWrite || _rightManager.hasRight(currentUser, "Runtime_Rights_Rights_Handle", "/cms") == RightResult.RIGHT_ALLOW);
285            infos.put("canRead", hasReadRightOnForm(currentUser, form));
286        }
287        
288        if (full)
289        {
290            infos.put("isPublished", !pages.isEmpty());
291            infos.put("hasEntries", !entries.isEmpty());
292            infos.put("nbEntries", form.getActiveEntries().size());
293            
294            FormDirectory formDirectoriesRoot = _formDirectoryDAO.getFormDirectoriesRootNode(form.getSiteName());
295            String parentId = form.getParent().getId().equals(formDirectoriesRoot.getId()) ? FormDirectoryDAO.ROOT_FORM_DIRECTORY_ID : form.getParent().getId();
296            infos.put("parentId", parentId);
297            
298            infos.put("isLimitedToOneEntryByUser", form.isLimitedToOneEntryByUser());
299            infos.put("isEntriesLimited", form.isEntriesLimited());
300            Optional<Long> maxEntries = form.getMaxEntries();
301            if (maxEntries.isPresent())
302            {
303                infos.put("maxEntries", maxEntries.get());
304            }
305            infos.put("isQueueEnabled", form.isQueueEnabled());
306            Optional<Long> queueSize = form.getQueueSize();
307            if (queueSize.isPresent())
308            {
309                infos.put("queueSize", queueSize.get());
310            }
311            
312            infos.put("expirationEnabled", form.isExpirationEnabled());
313            
314            LocalDate startDate = form.getStartDate();
315            LocalDate endDate = form.getEndDate();
316            if (startDate != null || endDate != null)
317            {
318                infos.put("scheduleStatus", _scheduleOpeningHelper.getStatus(form));
319                if (startDate != null)
320                {
321                    infos.put("startDate", DateUtils.localDateToString(startDate));
322                }
323                if (endDate != null)
324                {
325                    infos.put("endDate", DateUtils.localDateToString(endDate));
326                }
327            }
328
329            infos.put("adminEmails", form.hasValue(Form.ADMIN_EMAIL_SUBJECT));
330            infos.put("receiptAcknowledgement", form.hasValue(Form.RECEIPT_SENDER));
331            infos.put("isAnonymous", _rightManager.hasAnonymousReadAccess(form));
332        }
333        
334        return infos;
335    }
336    
337    /**
338     * Get the form title
339     * @param formId the form id
340     * @return the form title
341     */
342    @Callable (rights = Callable.NO_CHECK_REQUIRED)
343    public String getFormTitle(String formId)
344    {
345        Form form = _resolver.resolveById(formId);
346        return form.getTitle();
347    }
348    
349    /**
350     * Get the form full path
351     * @param formId the form id
352     * @return the form full path
353     */
354    @Callable (rights = Callable.NO_CHECK_REQUIRED)
355    public String getFormFullPath(String formId)
356    {
357        Form form = _resolver.resolveById(formId);
358        
359        String separator = " > ";
360        String fullPath = form.getTitle();
361        
362        FormDirectory parent = form.getParent();
363        if (!_formDirectoryDAO.isRoot(parent))
364        {
365            fullPath = _formDirectoryDAO.getFormDirectoryPath(parent, separator) + separator + fullPath;
366        }
367        
368        return fullPath;
369    }
370    
371    /**
372     * Get user rights for the given form
373     * @param form the form
374     * @return the set of rights
375     */
376    protected Set<String> _getUserRights (Form form)
377    {
378        UserIdentity user = _userProvider.getUser();
379        return _rightManager.getUserRights(user, form);
380    }
381    
382    /**
383     * Creates a {@link Form}.
384     * @param siteName The site name
385     * @param parentId The id of the parent.
386     * @param name  name The desired name for the new {@link Form}
387     * @return The id of the created form
388     * @throws Exception if an error occurs during the form creation process
389     */
390    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
391    public Map<String, String> createForm (String siteName, String parentId, String name) throws Exception
392    {
393        Map<String, String> result = new HashMap<>();
394        
395        FormDirectory parentDirectory = _formDirectoryDAO.getFormDirectory(siteName, parentId);
396        _formDirectoryDAO.checkHandleFormDirectoriesRight(parentDirectory);
397        
398        String uniqueName = NameHelper.getUniqueAmetysObjectName(parentDirectory, __FORM_NAME_PREFIX + name);
399        Form form = parentDirectory.createChild(uniqueName, FormFactory.FORM_NODETYPE);
400        
401        form.setTitle(name);
402        form.setAuthor(_userProvider.getUser());
403        form.setCreationDate(ZonedDateTime.now());
404        form.setLastModificationDate(ZonedDateTime.now());
405        
406        parentDirectory.saveChanges();
407        String formId = form.getId();
408        
409        _formPageDAO.createPage(formId, _i18nUtils.translate(new I18nizableText("plugin.forms", "PLUGINS_FORMS_CREATE_PAGE_DEFAULT_NAME")));
410        
411        result.put("id", formId);
412        result.put("name", form.getTitle());
413        
414        return result;
415    }
416    
417    /**
418     * Rename a {@link Form}
419     * @param id The id of the form
420     * @param newName The new name for the form
421     * @return A result map
422     */
423    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
424    public Map<String, String> renameForm (String id, String newName)
425    {
426        Map<String, String> results = new HashMap<>();
427        
428        Form form = _resolver.resolveById(id);
429        checkHandleFormRight(form);
430        
431        String uniqueName = NameHelper.getUniqueAmetysObjectName(form.getParent(), __FORM_NAME_PREFIX + newName);
432        Node node = form.getNode();
433        try
434        {
435            form.setTitle(newName);
436            form.setContributor(_userProvider.getUser());
437            form.setLastModificationDate(ZonedDateTime.now());
438            
439            node.getSession().move(node.getPath(), node.getParent().getPath() + '/' + uniqueName);
440            node.getSession().save();
441            
442            results.put("newName", form.getTitle());
443        }
444        catch (RepositoryException e)
445        {
446            getLogger().warn("Form renaming failed.", e);
447            results.put("message", "cannot-rename");
448        }
449        
450        results.put("id", id);
451        return results;
452    }
453    
454    /**
455     * Copies and pastes a form.
456     * @param formDirectoryId The id of the form directory target of the copy
457     * @param formId The id of the form to copy
458     * @return The results
459     */
460    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
461    public Map<String, String> copyForm(String formDirectoryId, String formId)
462    {
463        Map<String, String> result = new HashMap<>();
464        
465        Form originalForm = _resolver.resolveById(formId);
466        FormDirectory parentFormDirectory = _resolver.resolveById(formDirectoryId);
467        _formDirectoryDAO.checkHandleFormDirectoriesRight(parentFormDirectory);
468        
469        String uniqueName = NameHelper.getUniqueAmetysObjectName(parentFormDirectory, __FORM_NAME_PREFIX + originalForm.getTitle());
470        
471        Form cForm = originalForm.copyTo(parentFormDirectory, uniqueName);
472        originalForm.copyTo(cForm);
473        
474        String copyTitle = _i18nUtils.translate(new I18nizableText("plugin.forms", "PLUGIN_FORMS_TREE_COPY_NAME_PREFIX")) + originalForm.getTitle();
475        cForm.setTitle(copyTitle);
476        cForm.setAuthor(_userProvider.getUser());
477        cForm.setCreationDate(ZonedDateTime.now());
478        cForm.setLastModificationDate(ZonedDateTime.now());
479        
480        for (String epId : _copyFormEP.getExtensionsIds())
481        {
482            CopyFormUpdater copyFormUpdater = _copyFormEP.getExtension(epId);
483            copyFormUpdater.updateForm(originalForm, cForm);
484        }
485        
486        result.put("id", cForm.getId());
487        
488        return result;
489    }
490    
491    /**
492     * Deletes a {@link Form}.
493     * @param id The id of the form to delete
494     * @return The id of the form
495     */
496    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
497    public Map<String, String> deleteForm (String id)
498    {
499        Map<String, String> result = new HashMap<>();
500        
501        Form form = _resolver.resolveById(id);
502        checkHandleFormRight(form);
503        
504        List<SitemapElement> pages = getFormPage(form.getId(), form.getSiteName());
505        if (!pages.isEmpty())
506        {
507            throw new AccessDeniedException("Can't delete form ('" + form.getId() + "') which contains pages");
508        }
509
510        ModifiableAmetysObject parent = form.getParent();
511        form.remove();
512        parent.saveChanges();
513        
514        result.put("id", id);
515        return result;
516    }
517    
518    /**
519     * Moves a {@link Form}
520     * @param siteName name of the site
521     * @param id The id of the form
522     * @param newParentId The id of the new parent directory of the form.
523     * @return A result map
524     */
525    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
526    public Map<String, Object> moveForm(String siteName, String id, String newParentId)
527    {
528        Map<String, Object> results = new HashMap<>();
529        Form form = _resolver.resolveById(id);
530        FormDirectory directory = _formDirectoryDAO.getFormDirectory(siteName, newParentId);
531        
532        if (hasWriteRightOnForm(_userProvider.getUser(), form) && _formDirectoryDAO.hasWriteRightOnFormDirectory(_userProvider.getUser(), directory))
533        {
534            _formDirectoryDAO.move(form, siteName, newParentId, results);
535        }
536        else
537        {
538            results.put("message", "not-allowed");
539        }
540
541        results.put("id", form.getId());
542        return results;
543    }
544    
545    /**
546     * Change workflow of a {@link Form}
547     * @param formId The id of the form
548     * @param workflowName The name of new workflow
549     * @return A result map
550     */
551    @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION)
552    public Map<String, String> setWorkflow (String formId, String workflowName)
553    {
554        Map<String, String> results = new HashMap<>();
555        
556        Form form = _resolver.resolveById(formId);
557        checkHandleFormRight(form);
558        
559        form.setWorkflowName(workflowName);
560        form.setContributor(_userProvider.getUser());
561        form.setLastModificationDate(ZonedDateTime.now());
562        
563        form.saveChanges();
564        
565        results.put("id", formId);
566
567        Map<String, Object> eventParams = new HashMap<>();
568        eventParams.put("form", form);
569        _observationManager.notify(new Event(FormEvents.FORM_MODIFIED, _currentUserProvider.getUser(), eventParams));
570        
571        return results;
572    }
573
574    /**
575     * Get the submission date of the last entry to the form
576     * @param entries A list of form entry ordered by submission date
577     * @return the date of the last submission
578     */
579    protected ZonedDateTime _getLastSubmissionDate(List<FormEntry> entries)
580    {
581        return entries.isEmpty() ? null : entries.get(0).getSubmitDate();
582    }
583
584    /**
585     * Get all zone items which contains the form
586     * @param formId the form id
587     * @param siteName the site name
588     * @return the zone items
589     */
590    public AmetysObjectIterable<ModifiableZoneItem> getFormZoneItems(String formId, String siteName)
591    {
592        String xpathQuery = "//element(" + siteName + ", ametys:site)//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.forms.service.Display' and ametys:service_parameters/@ametys:formId = '" + formId + "']";
593        return _resolver.query(xpathQuery);
594    }
595    
596    /**
597     * Get the locale to use for a given form
598     * @param form the form
599     * @return the locale to use, can be null if the form is not published on a page
600     */
601    public String getFormLocale(Form form)
602    {
603        List<SitemapElement> zoneItems = getFormPage(form.getId(), form.getSiteName());
604        
605        return zoneItems.stream()
606                .findFirst()
607                .map(SitemapElement::getSitemapName)
608                .orElse(null);
609    }
610    
611    /**
612     * Get all the page where the form is published
613     * @param formId the form id
614     * @param siteName the site name
615     * @return the list of page
616     */
617    public List<SitemapElement> getFormPage(String formId, String siteName)
618    {
619        AmetysObjectIterable<ModifiableZoneItem> zoneItems = getFormZoneItems(formId, siteName);
620        
621        return zoneItems.stream()
622                .map(z -> z.getZone().getSitemapElement())
623                .collect(Collectors.toList());
624    }
625    
626    /**
627     * Get the page names
628     * @param pages the list of page
629     * @return the list of page name
630     */
631    protected List<Map<String, Object>> _getPagesInfos(List<SitemapElement> pages)
632    {
633        List<Map<String, Object>> pagesInfos = new ArrayList<>();
634        for (SitemapElement sitemapElement : pages)
635        {
636            Map<String, Object> info = new HashMap<>();
637            info.put("id", sitemapElement.getId());
638            info.put("title", sitemapElement.getTitle());
639            info.put("isPage", sitemapElement instanceof Page);
640            
641            pagesInfos.add(info);
642        }
643        
644        return pagesInfos;
645    }
646    
647    /**
648     * Get all the view available for the form display service
649     * @param formId the form identifier
650     * @param siteName the site name
651     * @param language the language
652     * @return the views as json
653     * @throws Exception if an error occurred
654     */
655    @Callable (rights = Callable.NO_CHECK_REQUIRED)
656    public List<Map<String, Object>> getFormDisplayViews(String formId, String siteName, String language) throws Exception
657    {
658        List<Map<String, Object>> jsonifiedViews = new ArrayList<>();
659    
660        Service service = _serviceEP.getExtension("org.ametys.forms.service.Display");
661        ElementDefinition viewElementDefinition = (ElementDefinition) service.getParameters().getOrDefault(ViewParametersManager.SERVICE_VIEW_DEFAULT_MODEL_ITEM_NAME, null);
662        
663        String xpathQuery = "//element(" + siteName + ", ametys:site)/ametys-internal:sitemaps/" + language
664                + "//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.forms.service.Display' and ametys:service_parameters/@ametys:formId = '" + formId + "']";
665        AmetysObjectIterable<ModifiableZoneItem> zoneItems = _resolver.query(xpathQuery);
666        
667        Optional<Object> existedServiceView = zoneItems.stream()
668            .map(ZoneItem::getServiceParameters)
669            .map(sp -> sp.getValue(ViewParametersManager.SERVICE_VIEW_DEFAULT_MODEL_ITEM_NAME))
670            .findFirst();
671        
672        Map<String, I18nizableText> typedEntries = viewElementDefinition.getEnumerator().getEntries();
673        for (String id : typedEntries.keySet())
674        {
675            Map<String, Object> viewAsJson = new HashMap<>();
676            viewAsJson.put("id", id);
677            viewAsJson.put("label", typedEntries.get(id));
678            
679            Boolean isServiceView = existedServiceView.map(s -> s.equals(id)).orElse(false);
680            if (isServiceView || existedServiceView.isEmpty() && id.equals(viewElementDefinition.getDefaultValue()))
681            {
682                viewAsJson.put("isDefault", true);
683            }
684            
685            jsonifiedViews.add(viewAsJson);
686        }
687        
688        return jsonifiedViews;
689    }
690    
691    /**
692     * <code>true</code> if the form is well configured
693     * @param form the form
694     * @return <code>true</code> if the form is well configured
695     */
696    public boolean isFormConfigured(Form form)
697    {
698        List<FormQuestion> questions = form.getQuestions();
699        return !questions.isEmpty() && !questions.stream().anyMatch(q -> !q.getType().isQuestionConfigured(q));
700    }
701    
702    /**
703     * Get the dashboard URI
704     * @param siteName the site name
705     * @return the dashboard URI
706     */
707    public String getDashboardUri(String siteName)
708    {
709        String xpathQuery = "//element(" + siteName + ", ametys:site)//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.plugins.forms.workflow.service.dashboard']";
710        AmetysObjectIterable<ModifiableZoneItem> zoneItems = _resolver.query(xpathQuery);
711        
712        Optional<Page> dashboardPage = zoneItems.stream()
713                .map(z -> z.getZone().getSitemapElement())
714                .filter(Page.class::isInstance)
715                .map(Page.class::cast)
716                .findAny();
717        
718        if (dashboardPage.isPresent())
719        {
720            Page page = dashboardPage.get();
721            String pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html";
722            
723            String url = _siteManager.getSite(siteName).getUrl();
724            return url + "/" + pagePath;
725        }
726        
727        return StringUtils.EMPTY;
728    }
729    
730    /**
731     * Get the admin dashboard URI
732     * @param siteName the site name
733     * @return the admin dashboard URI
734     */
735    public String getAdminDashboardUri(String siteName)
736    {
737        String xpathQuery = "//element(" + siteName + ", ametys:site)//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.plugins.forms.workflow.service.admin.dashboard']";
738        AmetysObjectIterable<ModifiableZoneItem> zoneItems = _resolver.query(xpathQuery);
739        
740        Optional<Page> dashboardPage = zoneItems.stream()
741                .map(z -> z.getZone().getSitemapElement())
742                .filter(Page.class::isInstance)
743                .map(Page.class::cast)
744                .findAny();
745        
746        if (dashboardPage.isPresent())
747        {
748            Page page = dashboardPage.get();
749            String pagePath = page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html";
750            
751            String url = _siteManager.getSite(siteName).getUrl();
752            return url + "/" + pagePath;
753        }
754        
755        return StringUtils.EMPTY;
756    }
757
758    /**
759     * Get the form expiration values
760     * @param formId the id of the form
761     * @return the values as a JSON map for edition
762     */
763    @Callable (rights = HANDLE_FORMS_RIGHT_ID, rightContext = FormsDirectoryRightAssignmentContext.ID, paramIndex = 0)
764    public Map<String, Object> getFormExpiration(String formId)
765    {
766        Form form = _resolver.resolveById(formId);
767        
768        return Map.of(Form.EXPIRATION_ENABLED, form.isExpirationEnabled(),
769                Form.EXPIRATION_PERIOD, form.getExpirationPeriod(),
770                Form.EXPIRATION_POLICY, form.getExpirationPolicy().name());
771    }
772    
773    /**
774     * Set the form expiration policy
775     * @param formId the id of the form
776     * @param expirationPeriod the expiration period in months
777     * @param expirationPolicy the expiration policy
778     */
779    @Callable (rights = HANDLE_FORMS_RIGHT_ID, rightContext = FormsDirectoryRightAssignmentContext.ID, paramIndex = 0)
780    public void setExpirationPolicy(String formId, long expirationPeriod, String expirationPolicy)
781    {
782        Form form = _resolver.resolveById(formId);
783        
784        form.setExpirationPolicy(true, expirationPeriod, ExpirationPolicy.valueOf(expirationPolicy));
785        form.saveChanges();
786        
787        _observationManager.notify(new Event(FormEvents.FORM_MODIFIED, _currentUserProvider.getUser(), Map.of("form", form)));
788    }
789    
790    /**
791     * Remove the form expiration policy
792     * @param formId the id of the form
793     */
794    public void removeExpirationPolicy(String formId)
795    {
796        Form form = _resolver.resolveById(formId);
797        
798        form.setExpirationPolicy(false, -1, null);
799        form.saveChanges();
800        
801        _observationManager.notify(new Event(FormEvents.FORM_MODIFIED, _currentUserProvider.getUser(), Map.of("form", form)));
802    }
803    
804}