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