Subversion Repositories bacoAlunos

Rev

Rev 790 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.courses;

import jomm.utils.FilesUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.Course;
import pt.estgp.estgweb.domain.DomainObjectFactory;
import pt.estgp.estgweb.domain.RepositoryFileImpl;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.domain.views.CourseView;
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
import pt.estgp.estgweb.services.data.IRepositoryFile;
import pt.estgp.estgweb.services.data.RepositoryService;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.services.expceptions.AlreadyExistsException;
import pt.estgp.estgweb.utils.DatesUtils;
import pt.estgp.estgweb.utils.Dom4jUtil;
import pt.utl.ist.berserk.logic.serviceManager.IService;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

/**
 * @author Jorge Machado
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

public class CoursesService implements IService
{
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(CoursesService.class);

    RepositoryService repositoryService = new RepositoryService();


    public CourseView loadCourse(long id, boolean initUnits) throws ServiceException
    {
        Course c = DaoFactory.getCourseDaoImpl().get(id);

        if(c != null)
        {
            CourseView cV = new CourseView(c,initUnits);
            if(c.getStudiesPlan() != null)
            {
                RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan());
                cV.setStudiesPlan(repositoryFile);
            }

            return cV;
        }
        return null;
    }

    public List<String> loadImportYears(UserSession userSession) throws ServiceException
    {
        List<String> importYears = DaoFactory.getCourseDaoImpl().loadImportYears();
        List<String> imStrings = new ArrayList<String>();
        for(1.5.0/docs/api/java/lang/String.html">String importYear: importYears)
        {
            imStrings.add(importYear);
        }
        return imStrings;
    }

    public CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException
    {
        try{
            Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code);
            if(c != null)
            {
                CourseView cV = new CourseView(c,initUnits);
                if(c.getStudiesPlan() != null)
                {
                    RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan());
                    cV.setStudiesPlan(repositoryFile);
                }
                return cV;
            }
        }
        catch(1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
        {
            logger.error(e + " loading code:" + code,e);
            throw new ServiceException("loading code: " + code  + " - " + e.toString(),e);
        }
        return null;
    }

    public CourseView submitCourse(CourseView courseView,
                                   5+0%2Fdocs%2Fapi+InputStream">InputStream stream,
                                   1.5.0/docs/api/java/lang/String.html">String name,
                                   int size,
                                   1.5.0/docs/api/java/lang/String.html">String contentType,
                                   UserSession userSession) throws ServiceException
    {
        Course c;
        if(courseView.getId() > 0)
        {
            c = DaoFactory.getCourseDaoImpl().get(courseView.getId());
        }
        else
        {
            c = DaoFactory.getCourseDaoImpl().findCourseByCodeAndYear(courseView.getCode(),courseView.getImportYear());
            if(c != null)
                throw new AlreadyExistsException(AlreadyExistsException.ALREADY_EXISTS_COURSE);      
            c = DomainObjectFactory.createCourseImpl();
            DaoFactory.getCourseDaoImpl().save(c);
        }

        1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null;

        if(stream != null && size > 0)
        {
            1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name);
            if(c.getStudiesPlan() == null)
            {
                1.5.0/docs/api/java/lang/String.html">String identifier = repositoryService.storeRepositoryFile(stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain,userSession);
                c.setStudiesPlan(identifier);
            }
            else
            {
                repositoryService.updateRepositoryFile(c.getStudiesPlan(), stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain);
            }
            IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession);
            stream = repositoryFile.getInput();
            try
            {
                5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream);
                Map<String,String> parameters = new HashMap<String,String>();
                parameters.put("COURSE_SIGES_CODE",c.getCode());
                1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(dom, Globals.TEMPLATE_COURSE_XSL_PATH);
                c.setCacheWebDocument(html);
            }
            catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
            {
                1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
                1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter printWriter = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(writer);
                e.printStackTrace(printWriter);
                htmlTrasformationResult = "<div class=\"error\"><pre>" + e.toString() + "\n" + printWriter.toString() + "</pre></div>";
                printWriter.close();
            }
            try
            {
                stream.close();
            }
            catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
            {
                logger.error(e,e);
            }
        }
        courseView.persistViewInObject(c);
        CourseView cv = loadCourse(c.getId(),false);
        cv.setHtmlResult(htmlTrasformationResult);
        return cv;
    }

    public List<CourseView> loadCourses() throws ServiceException
    {
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName();
        List<CourseView> courseViews = new ArrayList<CourseView>();
        for(Course c: courses)
        {
            CourseView courseView = new CourseView(c);
            courseViews.add(courseView);
        }
        return courseViews;
    }

    public List<CourseView> loadCoursesImportYearArea(1.5.0/docs/api/java/lang/String.html">String importYear, 1.5.0/docs/api/java/lang/String.html">String area) throws ServiceException
    {
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYear,area,null);
        List<CourseView> courseViews = new ArrayList<CourseView>();
        for(Course c: courses)
        {
            CourseView courseView = new CourseView(c);
            courseViews.add(courseView);
        }
        return courseViews;
    }

    public List<CourseView> loadCoursesImportYear() throws ServiceException
    {
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYear();
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet);
        List<CourseView> courseViews = new ArrayList<CourseView>();
        for(Course c: courses)
        {
            CourseView courseView = new CourseView(c);
            courseViews.add(courseView);
        }
        return courseViews;
    }
    public List<CourseView> loadCoursesImportYearByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException
    {
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYear();
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type);
        List<CourseView> courseViews = new ArrayList<CourseView>();
        for(Course c: courses)
        {
            CourseView courseView = new CourseView(c);
            courseViews.add(courseView);
        }
        return courseViews;
    }

    public List<CourseView> loadActiveCoursesByType(1.5.0/docs/api/java/lang/String.html">String type) throws ServiceException
    {
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYear();
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllActiveOrderByNameEvenWithoutCourseUnit(importYearIntranet,null,type);
        List<CourseView> courseViews = new ArrayList<CourseView>();
        for(Course c: courses)
        {
            CourseView courseView = new CourseView(c);
            courseViews.add(courseView);
        }
        return courseViews;
    }



}