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            // Check that parent ProgramPage exists
055            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
056            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
057            root.getChild(coursePath);
058            
059            return new CoursePage(this, root, course, program, path, null);
060        }
061        catch (UnknownAmetysObjectException e)
062        {
063            throw new UnknownAmetysObjectException("There's no object for id " + id, e);
064        }
065    }
066    
067    @Override
068    public String getScheme()
069    {
070        return "course";
071    }
072
073    @Override
074    public boolean hasAmetysObjectForId(String id) throws AmetysRepositoryException
075    {
076        String path = StringUtils.substringBetween(id, "://", "?");
077        String queryString = StringUtils.substringAfter(id, "?");
078        Map<String, String> ids = Splitter.on("&").withKeyValueSeparator("=").split(queryString);
079        
080        String rootId = ids.get("rootId");
081        String courseId = ids.get("courseId");
082        String programId = ids.get("programId");
083        
084        try
085        {
086            Page root = _resolver.resolveById(rootId);
087            
088            Program program = _resolver.resolveById(programId);
089            
090            // Check that parent ProgramPage exists
091            String levelPath = _odfPageHandler.computeLevelsPath(root, program);
092            String coursePath = StringUtils.isNotBlank(levelPath) ? levelPath + "/" + path : path;
093            root.getChild(coursePath);
094            
095            return _resolver.hasAmetysObjectForId(courseId);
096        }
097        catch (UnknownAmetysObjectException e)
098        {
099            return false;
100        }
101    }
102}