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 exists
075            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
076            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
077            root.getChild(coursePath);
078            
079            try
080            {
081                CoursePage coursePage = createCoursePage(root, course, program, path, null);
082           
083                // Test if the virtual page really exists
084                coursePage.getPathInSitemap();
085                
086                return coursePage;
087            }
088            catch (UnknownAmetysObjectException e)
089            {
090                throw new UnknownAmetysObjectException("There's no course page " + programId + " for site " + root.getSiteName(), e);
091            }
092        }
093        catch (UnknownAmetysObjectException e)
094        {
095            throw new UnknownAmetysObjectException("There's no object for id " + id, e);
096        }
097    }
098    
099    @Override
100    public String getScheme()
101    {
102        return "course";
103    }
104
105    @Override
106    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
107    {
108        String path = StringUtils.substringBetween(id, "://", "?");
109        String queryString = StringUtils.substringAfter(id, "?");
110        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
111        
112        String rootId = ids.get("rootId");
113        String courseId = ids.get("courseId");
114        String programId = ids.get("programId");
115        
116        try
117        {
118            Page root = _resolver.resolveById(rootId);
119            
120            Program program = _resolver.resolveById(programId);
121            
122            // Check that parent ProgramPage exists
123            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
124            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
125            root.getChild(coursePath);
126            
127            return _resolver.hasAmetysObjectForId(courseId);
128        }
129        catch (UnknownAmetysObjectException e)
130        {
131            return false;
132        }
133    }
134}