Subversion Repositories bacoAlunos

Rev

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