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    @Override
037    public CoursePage getAmetysObjectById(String id) throws AmetysRepositoryException
038    {
039        // Id is like // E.g: course://path/from/program?rootId=xxx&courseId=xxx&programId=xxxx
040        String path = StringUtils.substringBetween(id, "://", "?");
041        String queryString = StringUtils.substringAfter(id, "?");
042        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
043        
044        String rootId = ids.get("rootId");
045        String courseId = ids.get("courseId");
046        String programId = ids.get("programId");
047        
048        try
049        {
050            Page root = _resolver.resolveById(rootId);
051            Course course = _resolver.resolveById(courseId);
052            Program program = _resolver.resolveById(programId);
053            
054            // Test program restriction
055            if (!_odfPageHandler.isValidRestriction(root, program))
056            {
057                throw new UnknownAmetysObjectException("There's no course page " + courseId + " for site " + root.getSiteName());
058            }
059            
060            // Check that parent ProgramPage exists
061            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
062            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
063            root.getChild(coursePath);
064            
065            return new CoursePage(this, root, course, program, path, null);
066        }
067        catch (UnknownAmetysObjectException e)
068        {
069            throw new UnknownAmetysObjectException("There's no object for id " + id, e);
070        }
071    }
072    
073    @Override
074    public String getScheme()
075    {
076        return "course";
077    }
078
079    @Override
080    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
081    {
082        String path = StringUtils.substringBetween(id, "://", "?");
083        String queryString = StringUtils.substringAfter(id, "?");
084        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
085        
086        String rootId = ids.get("rootId");
087        String courseId = ids.get("courseId");
088        String programId = ids.get("programId");
089        
090        try
091        {
092            Page root = _resolver.resolveById(rootId);
093            
094            Program program = _resolver.resolveById(programId);
095            
096            // Check that parent ProgramPage exists
097            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
098            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
099            root.getChild(coursePath);
100            
101            return _resolver.hasAmetysObjectForId(courseId);
102        }
103        catch (UnknownAmetysObjectException e)
104        {
105            return false;
106        }
107    }
108}