Subversion Repositories bacoAlunos

Rev

Rev 249 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
214 jmachado 1
package pt.estgp.estgweb.services.courses;
2
 
3
import pt.utl.ist.berserk.logic.serviceManager.IService;
4
import pt.estgp.estgweb.services.data.RepositoryService;
268 jmachado 5
import pt.estgp.estgweb.services.data.IRepositoryFile;
214 jmachado 6
import pt.estgp.estgweb.services.courseunits.LoadCourseUnitAnnouncements;
7
import pt.estgp.estgweb.services.courseunits.LoadCourseUnitService;
8
import pt.estgp.estgweb.services.expceptions.ServiceException;
9
import pt.estgp.estgweb.domain.views.CourseUnitView;
10
import pt.estgp.estgweb.domain.views.AnnouncementView;
11
import pt.estgp.estgweb.domain.views.CourseView;
12
import pt.estgp.estgweb.domain.*;
13
import pt.estgp.estgweb.domain.dao.DaoFactory;
14
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
248 jmachado 15
import pt.estgp.estgweb.utils.Dom4jUtil;
249 jmachado 16
import pt.estgp.estgweb.utils.DatesUtils;
248 jmachado 17
import pt.estgp.estgweb.Globals;
214 jmachado 18
import org.apache.log4j.Logger;
248 jmachado 19
import org.dom4j.Document;
20
import org.dom4j.DocumentException;
214 jmachado 21
 
22
import java.util.List;
23
import java.util.ArrayList;
24
import java.io.InputStream;
248 jmachado 25
import java.io.PrintWriter;
26
import java.io.StringWriter;
268 jmachado 27
import java.io.IOException;
248 jmachado 28
import java.net.MalformedURLException;
214 jmachado 29
 
30
import jomm.utils.FilesUtils;
31
 
32
/*
33
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
34
 *
35
 *
36
 * Created at 17/Out/2003 , 23:45:24
37
 *
38
 */
39
/**
40
 * @author Jorge Machado
41
 *
42
 *
43
 * Created at 17/Out/2003 , 23:45:24
44
 *
45
 */
46
public class CoursesService implements IService
47
{
48
    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);
49
 
50
    RepositoryService repositoryService = new RepositoryService();
51
 
52
 
53
    public CourseView loadCourse(long id, boolean initUnits) throws ServiceException
54
    {
55
        Course c = DaoFactory.getCourseDaoImpl().get(id);
56
 
57
        if(c != null)
58
        {
59
            CourseView cV = new CourseView(c,initUnits);
60
            if(c.getStudiesPlan() != null)
61
            {
62
                RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan());
63
                cV.setStudiesPlan(repositoryFile);
64
            }
65
 
66
            return cV;
67
        }
68
        return null;
69
    }
70
 
71
    public CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException
72
    {
73
        Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code);
74
        if(c != null)
75
        {
76
            CourseView cV = new CourseView(c,initUnits);
77
            if(c.getStudiesPlan() != null)
78
            {
79
                RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan());
80
                cV.setStudiesPlan(repositoryFile);
81
            }
82
            return cV;
83
        }
84
        return null;
85
    }
86
 
87
    public CourseView submitCourse(CourseView courseView,
88
                                   5+0%2Fdocs%2Fapi+InputStream">InputStream stream,
89
                                   1.5.0/docs/api/java/lang/String.html">String name,
90
                                   int size,
91
                                   1.5.0/docs/api/java/lang/String.html">String contentType,
92
                                   UserSession userSession) throws ServiceException
93
    {
94
        Course c;
95
        if(courseView.getId() > 0)
96
            c = DaoFactory.getCourseDaoImpl().get(courseView.getId());
97
        else
98
        {
99
            c = DomainObjectFactory.createCourseImpl();
100
            DaoFactory.getCourseDaoImpl().save(c);
101
        }
102
 
248 jmachado 103
        1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null;
104
 
214 jmachado 105
        if(stream != null && size > 0)
106
        {
268 jmachado 107
            1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name);
108
            if(c.getStudiesPlan() == null)
109
            {
110
                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);
111
                c.setStudiesPlan(identifier);
112
            }
113
            else
114
            {
115
                repositoryService.updateRepositoryFile(c.getStudiesPlan(), stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain);
116
            }
117
            IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession);
118
            stream = repositoryFile.getInput();
248 jmachado 119
            try
120
            {
121
                5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream);
122
                1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(dom, Globals.TEMPLATE_COURSE_XSL_PATH);      
123
                c.setCacheWebDocument(html);
124
            }
125
            catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
126
            {
127
                1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
128
                1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter printWriter = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(writer);
129
                e.printStackTrace(printWriter);
130
                htmlTrasformationResult = "<div class=\"error\"><pre>" + e.toString() + "\n" + printWriter.toString() + "</pre></div>";
131
                printWriter.close();
132
            }
268 jmachado 133
            try
214 jmachado 134
            {
268 jmachado 135
                stream.close();
214 jmachado 136
            }
268 jmachado 137
            catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
214 jmachado 138
            {
268 jmachado 139
                logger.error(e,e);
214 jmachado 140
            }
141
        }
142
        courseView.persistViewInObject(c);
248 jmachado 143
        CourseView cv = loadCourse(c.getId(),false);
144
        cv.setHtmlResult(htmlTrasformationResult);
145
        return cv;
214 jmachado 146
    }
147
 
148
    public List<CourseView> loadCourses() throws ServiceException
149
    {
150
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName();
151
        List<CourseView> courseViews = new ArrayList<CourseView>();
152
        for(Course c: courses)
153
        {
154
            CourseView courseView = new CourseView(c);
155
            courseViews.add(courseView);
156
        }
157
        return courseViews;
158
    }
159
 
249 jmachado 160
    public List<CourseView> loadCoursesImportYear() throws ServiceException
161
    {
162
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYear();
163
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet);
164
        List<CourseView> courseViews = new ArrayList<CourseView>();
165
        for(Course c: courses)
166
        {
167
            CourseView courseView = new CourseView(c);
168
            courseViews.add(courseView);
169
        }
170
        return courseViews;
171
    }
214 jmachado 172
 
249 jmachado 173
 
174
 
214 jmachado 175
}