Subversion Repositories bacoAlunos

Rev

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