001/*
002 *  Copyright 2010 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.repository;
017
018import java.util.Map;
019
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.odf.course.Course;
023import org.ametys.odf.program.Program;
024import org.ametys.plugins.repository.AmetysObjectFactory;
025import org.ametys.plugins.repository.AmetysRepositoryException;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.web.repository.page.Page;
028
029import com.google.common.base.Splitter;
030
031/**
032 * {@link AmetysObjectFactory} handling {@link ProgramPage}.
033 */
034public class CoursePageFactory extends AbstractOdfPageFactory implements AmetysObjectFactory<CoursePage>
035{
036    /**
037     * Create a course page.
038     * @param root the odf root page.
039     * @param course the course.
040     * @param parentProgram the parent program
041     * @param path path from the parent {@link ProgramPage}
042     * @param parentPage the parent {@link Page} or null if not yet computed.
043     * @return The <code>CoursePage</code> created
044     */
045    public CoursePage createCoursePage(Page root, Course course, Program parentProgram, String path, Page parentPage)
046    {
047        return new CoursePage(root, getConfiguration(), this, course, parentProgram, path, parentPage);
048    }
049    
050    @Override
051    public CoursePage getAmetysObjectById(String id) throws AmetysRepositoryException
052    {
053        // Id is like // E.g: course://path/from/program?rootId=xxx&courseId=xxx&programId=xxxx
054        String path = StringUtils.substringBetween(id, "://", "?");
055        String queryString = StringUtils.substringAfter(id, "?");
056        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
057        
058        String rootId = ids.get("rootId");
059        String courseId = ids.get("courseId");
060        String programId = ids.get("programId");
061        
062        try
063        {
064            Page root = _resolver.resolveById(rootId);
065            Course course = _resolver.resolveById(courseId);
066            Program program = _resolver.resolveById(programId);
067            
068            // Test program restriction
069            if (!_odfPageHandler.isValidRestriction(root, program))
070            {
071                throw new UnknownAmetysObjectException("There's no course page " + courseId + " for site " + root.getSiteName());
072            }
073            
074            // Check that parent ProgramPage or CoursePage exists
075            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
076            
077            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
078            root.getChild(coursePath);
079            
080            try
081            {
082                CoursePage coursePage = createCoursePage(root, course, program, path, null);
083           
084                // Test if the virtual page really exists
085                coursePage.getPathInSitemap();
086                
087                return coursePage;
088            }
089            catch (UnknownAmetysObjectException e)
090            {
091                throw new UnknownAmetysObjectException("There's no course page " + programId + " for site " + root.getSiteName(), e);
092            }
093        }
094        catch (UnknownAmetysObjectException e)
095        {
096            throw new UnknownAmetysObjectException("There's no object for id " + id, e);
097        }
098    }
099    
100    @Override
101    public String getScheme()
102    {
103        return "course";
104    }
105
106    @Override
107    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
108    {
109        String path = StringUtils.substringBetween(id, "://", "?");
110        String queryString = StringUtils.substringAfter(id, "?");
111        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
112        
113        String rootId = ids.get("rootId");
114        String courseId = ids.get("courseId");
115        String programId = ids.get("programId");
116        
117        try
118        {
119            Page root = _resolver.resolveById(rootId);
120            
121            Program program = _resolver.resolveById(programId);
122            
123            // Check that parent ProgramPage exists
124            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
125            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
126            root.getChild(coursePath);
127            
128            return _resolver.hasAmetysObjectForId(courseId);
129        }
130        catch (UnknownAmetysObjectException e)
131        {
132            return false;
133        }
134    }
135}