001/*
002 *  Copyright 2019 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.odfweb.service.search;
017
018import java.time.ZonedDateTime;
019import java.util.Locale;
020import java.util.Optional;
021
022import org.apache.cocoon.xml.XMLUtils;
023import org.apache.commons.lang3.LocaleUtils;
024import org.slf4j.Logger;
025import org.xml.sax.ContentHandler;
026import org.xml.sax.SAXException;
027
028import org.ametys.core.util.DateUtils;
029import org.ametys.odf.course.Course;
030import org.ametys.odf.program.AbstractProgram;
031import org.ametys.plugins.odfweb.repository.CoursePage;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.web.frontoffice.search.metamodel.ReturnableSaxer;
034import org.ametys.web.frontoffice.search.metamodel.impl.PageReturnable;
035import org.ametys.web.frontoffice.search.metamodel.impl.PageSaxer;
036import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.site.Site;
039
040/**
041 * {@link ReturnableSaxer} for {@link CourseReturnable}
042 * <br>
043 * <br>Note for developpers: This Saxer extends PageSaxer to have access to its protected method #saxContent()
044 */
045public class CourseSaxer extends PageSaxer
046{
047    private CourseReturnable _courseReturnable;
048
049    /**
050     * Constructor
051     * @param pageReturnable The course returnable (needed for superclass)
052     * @param courseReturnable The associated returnable on courses
053     */
054    public CourseSaxer(PageReturnable pageReturnable, CourseReturnable courseReturnable)
055    {
056        super(pageReturnable);
057        _courseReturnable = courseReturnable;
058    }
059    
060    @Override
061    public boolean canSax(AmetysObject hit, Logger logger, SearchComponentArguments args)
062    {
063        return hit instanceof Course;
064    }
065    
066    @Override
067    public void sax(ContentHandler contentHandler, AmetysObject hit, Logger logger, SearchComponentArguments args) throws SAXException
068    {
069        Course course = (Course) hit;
070        _saxCourseContent(contentHandler, course, logger, args);
071        
072        // Get the first course page found
073        Optional<CoursePage> coursePage = _resolveCoursePage(course, args.currentSite());
074        if (coursePage.isPresent())
075        {
076            Page page = coursePage.get();
077            XMLUtils.createElement(contentHandler, "uri", page.getSitemap().getName() + "/" + page.getPathInSitemap());
078        }
079    }
080    
081    private Optional<CoursePage> _resolveCoursePage(Course course, Site currentSite)
082    {
083        return Optional.of(_courseReturnable._odfPageResolver)
084                .map(res -> res.getCoursePage(course, (AbstractProgram) null, currentSite.getName()));
085    }
086    
087    private void _saxCourseContent(ContentHandler contentHandler, Course course, Logger logger, SearchComponentArguments args) throws SAXException
088    {
089        XMLUtils.createElement(contentHandler, "title", course.getTitle());
090        ZonedDateTime lastValidationDate = course.getLastValidationDate();
091        if (lastValidationDate != null)
092        {
093            XMLUtils.createElement(contentHandler, "lastValidation", DateUtils.zonedDateTimeToString(lastValidationDate));
094        }
095        Locale locale = LocaleUtils.toLocale(args.currentPage().getSitemapName());
096        saxContent(course, "index", locale, contentHandler, logger);
097    }
098}
099