Subversion Repositories bacoAlunos

Rev

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