Subversion Repositories bacoAlunos

Rev

Rev 332 | 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
 
345 jmachado 71
    public List<String> loadImportYears(UserSession userSession) throws ServiceException
72
    {
73
        List<String> importYears = DaoFactory.getCourseDaoImpl().loadImportYears();
74
        List<String> imStrings = new ArrayList<String>();
75
        for(1.5.0/docs/api/java/lang/String.html">String importYear: importYears)
76
        {
77
            imStrings.add(importYear);
78
        }
79
        return imStrings;
80
    }
81
 
214 jmachado 82
    public CourseView loadCourseByCode(1.5.0/docs/api/java/lang/String.html">String code, boolean initUnits) throws ServiceException
83
    {
84
        Course c = DaoFactory.getCourseDaoImpl().findCourseByCode(code);
85
        if(c != null)
86
        {
87
            CourseView cV = new CourseView(c,initUnits);
88
            if(c.getStudiesPlan() != null)
89
            {
90
                RepositoryFileImpl repositoryFile = repositoryService.loadView(c.getStudiesPlan());
91
                cV.setStudiesPlan(repositoryFile);
92
            }
93
            return cV;
94
        }
95
        return null;
96
    }
97
 
98
    public CourseView submitCourse(CourseView courseView,
99
                                   5+0%2Fdocs%2Fapi+InputStream">InputStream stream,
100
                                   1.5.0/docs/api/java/lang/String.html">String name,
101
                                   int size,
102
                                   1.5.0/docs/api/java/lang/String.html">String contentType,
103
                                   UserSession userSession) throws ServiceException
104
    {
105
        Course c;
106
        if(courseView.getId() > 0)
107
            c = DaoFactory.getCourseDaoImpl().get(courseView.getId());
108
        else
109
        {
110
            c = DomainObjectFactory.createCourseImpl();
111
            DaoFactory.getCourseDaoImpl().save(c);
112
        }
113
 
248 jmachado 114
        1.5.0/docs/api/java/lang/String.html">String htmlTrasformationResult = null;
115
 
214 jmachado 116
        if(stream != null && size > 0)
117
        {
268 jmachado 118
            1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name);
119
            if(c.getStudiesPlan() == null)
120
            {
332 jmachado 121
                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);
268 jmachado 122
                c.setStudiesPlan(identifier);
123
            }
124
            else
125
            {
126
                repositoryService.updateRepositoryFile(c.getStudiesPlan(), stream, contentType, extension, size, name, "course.studies.plan " + c.getName(), ResourceAccessControlEnum.publicDomain);
127
            }
128
            IRepositoryFile repositoryFile = repositoryService.load(c.getStudiesPlan(),userSession);
129
            stream = repositoryFile.getInput();
248 jmachado 130
            try
131
            {
132
                5+0%2Fdocs%2Fapi+Document">Document dom = Dom4jUtil.parse(stream);
133
                1.5.0/docs/api/java/lang/String.html">String html = Dom4jUtil.styleDocument(dom, Globals.TEMPLATE_COURSE_XSL_PATH);      
134
                c.setCacheWebDocument(html);
135
            }
136
            catch (1.5.0/docs/api/java/lang/Exception.html">Exception e)
137
            {
138
                1.5.0/docs/api/java/io/StringWriter.html">StringWriter writer = new 1.5.0/docs/api/java/io/StringWriter.html">StringWriter();
139
                1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter printWriter = new 1.5.0/docs/api/java/io/PrintWriter.html">PrintWriter(writer);
140
                e.printStackTrace(printWriter);
141
                htmlTrasformationResult = "<div class=\"error\"><pre>" + e.toString() + "\n" + printWriter.toString() + "</pre></div>";
142
                printWriter.close();
143
            }
268 jmachado 144
            try
214 jmachado 145
            {
268 jmachado 146
                stream.close();
214 jmachado 147
            }
268 jmachado 148
            catch (1.5.0/docs/api/java/io/IOException.html">IOException e)
214 jmachado 149
            {
268 jmachado 150
                logger.error(e,e);
214 jmachado 151
            }
152
        }
153
        courseView.persistViewInObject(c);
248 jmachado 154
        CourseView cv = loadCourse(c.getId(),false);
155
        cv.setHtmlResult(htmlTrasformationResult);
156
        return cv;
214 jmachado 157
    }
158
 
159
    public List<CourseView> loadCourses() throws ServiceException
160
    {
161
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName();
162
        List<CourseView> courseViews = new ArrayList<CourseView>();
163
        for(Course c: courses)
164
        {
165
            CourseView courseView = new CourseView(c);
166
            courseViews.add(courseView);
167
        }
168
        return courseViews;
169
    }
170
 
249 jmachado 171
    public List<CourseView> loadCoursesImportYear() throws ServiceException
172
    {
173
        1.5.0/docs/api/java/lang/String.html">String importYearIntranet = DatesUtils.getImportYear();
174
        List<Course> courses = DaoFactory.getCourseDaoImpl().findAllOrderByName(importYearIntranet);
175
        List<CourseView> courseViews = new ArrayList<CourseView>();
176
        for(Course c: courses)
177
        {
178
            CourseView courseView = new CourseView(c);
179
            courseViews.add(courseView);
180
        }
181
        return courseViews;
182
    }
214 jmachado 183
 
249 jmachado 184
 
185
 
214 jmachado 186
}