Subversion Repositories bacoAlunos

Rev

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

Rev Author Line No. Line
215 jmachado 1
package pt.estgp.estgweb.services.profile;
2
 
821 jmachado 3
import jomm.utils.FilesUtils;
4
import org.apache.log4j.Logger;
215 jmachado 5
import pt.estgp.estgweb.domain.*;
821 jmachado 6
import pt.estgp.estgweb.domain.dao.DaoFactory;
215 jmachado 7
import pt.estgp.estgweb.domain.enums.RecordEnum;
821 jmachado 8
import pt.estgp.estgweb.domain.views.*;
215 jmachado 9
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
821 jmachado 10
import pt.estgp.estgweb.services.data.RepositoryService;
11
import pt.estgp.estgweb.services.expceptions.ServiceException;
12
import pt.utl.ist.berserk.logic.serviceManager.IService;
215 jmachado 13
 
14
import java.io.InputStream;
15
import java.util.*;
16
 
17
/*
18
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
19
 *
20
 *
21
 * Created at 17/Out/2003 , 23:45:24
22
 *
23
 */
24
/**
25
 * @author Jorge Machado
26
 *
27
 *
28
 * Created at 17/Out/2003 , 23:45:24
29
 *
30
 */
31
public class CurriculumService implements IService
32
{
33
 
34
    RepositoryService repositoryService = new RepositoryService();
35
 
36
    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(CurriculumService.class);
37
 
38
 
39
    public UserView deleteRecord(long recordId, UserSession userSession) throws ServiceException
40
    {
41
        Record record = DaoFactory.getRecordDaoImpl().load(recordId);
225 jmachado 42
        if(record.getRepositoryStream() != null && record.getRepositoryStream().length() > 0)
43
        {
44
            RepositoryFile repositoryFile = DaoFactory.getRepositoryFileDaoImpl().loadByIdentifier(record.getRepositoryStream());
45
            repositoryFile.setDeleted(true);
46
        }
215 jmachado 47
        DaoFactory.getRecordDaoImpl().delete(record);
225 jmachado 48
        userSession.getUser().getCreatorRecords().remove(record);
215 jmachado 49
        return loadUserViewWithRecords(record.getOwner().getId(),userSession);
50
    }
51
 
52
    public RecordView loadEditRecord(long recordId, UserSession userSession) throws ServiceException
53
    {
54
        Record record = DaoFactory.getRecordDaoImpl().load(recordId);
55
        return new RecordView(record,true);
56
    }
225 jmachado 57
 
215 jmachado 58
    public UserView createRecord(RecordView recordView,5+0%2Fdocs%2Fapi+InputStream">InputStream file, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String contentType, int size, UserSession userSession) throws ServiceException
59
    {
60
        Record r;
61
 
62
        if(recordView.getId() <= 0)
63
        {
64
            r = RecordEnum.parse(recordView.getType()).createInstance();
65
            r.setOwner(userSession.getUser());
359 jmachado 66
//            r.setCreators(new HashSet<User>());
67
//            r.getCreators().add(userSession.getUser());
68
            userSession.getUser().getCreatorRecords().add(r);
215 jmachado 69
            if(userSession.getUser().getCreatorRecords() == null)
70
                userSession.getUser().setCreatorRecords(new HashSet<Record>());
359 jmachado 71
 
215 jmachado 72
            DaoFactory.getRecordDaoImpl().save(r);
359 jmachado 73
            DaoFactory.getRecordDaoImpl().refresh(r);
74
            1.5.0/docs/api/java/lang/System.html">System.out.println(r.getCreators().size());
215 jmachado 75
        }
76
        else
77
            r = DaoFactory.getRecordDaoImpl().get(recordView.getId());
78
 
79
 
80
 
81
        recordView.persistViewInObject(r);
82
 
83
        if(file != null && size > 0)
84
        {
85
            1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name);
86
            if(recordView.getRepositoryStream() == null)
87
            {
88
                1.5.0/docs/api/java/lang/String.html">String identifier = repositoryService.storeRepositoryFile(file, contentType, extension, size, name, "profile.curriculum.record " + r.getTitle() + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.parse(recordView.getFileAccess()),userSession);
89
                r.setRepositoryStream(identifier);
90
            }
91
            else
92
            {
93
                repositoryService.updateRepositoryFile(recordView.getRepositoryStream(), file, contentType, extension, size, name, "profile.curriculum.record " + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.parse(recordView.getFileAccess()));
94
            }
95
        }
96
        return loadUserViewWithRecords(userSession.getUser().getId(), userSession);
97
    }
98
 
99
    public UserView loadUserViewWithRecordsCode(int code, UserSession userSession) throws ServiceException
100
    {
101
        User u = DaoFactory.getTeacherDaoImpl().loadBySigesCode(code);
102
        if(u == null)
103
            u = DaoFactory.getStudentDaoImpl().loadBySigesCode(code);
104
        if(u != null)
105
            return loadUserViewWithRecords(u.getId(),userSession);
106
        return null;
107
    }
225 jmachado 108
 
215 jmachado 109
    public UserView loadUserViewWithRecords(long id, UserSession userSession) throws ServiceException
110
    {
111
        User u;
112
        UserView userView;
113
        if(id <= 0)
114
        {
115
            userView = new UserView(userSession.getUser());
116
            u = userSession.getUser();
117
        }
118
        else
119
        {
120
            User user = DaoFactory.getUserDaoImpl().get(id);
121
            u = user;
122
            if(user != null)
123
                userView = new UserView(user);
124
            else
125
                return null;
126
        }
127
        if(u.getCreatorRecords() != null)
128
        {
129
            userView.setCreatorRecords(RecordView.getViews(new ArrayList<Record>(u.getCreatorRecords())));
130
        }
131
        if(u.getContributorRecords() != null)
132
        {
133
            userView.setContributorRecords(RecordView.getViews(new ArrayList<Record>(u.getContributorRecords())));
134
        }
135
 
821 jmachado 136
        Set<CourseUnit> responsableCourseUnits = null;
215 jmachado 137
        Set<CourseUnit> courseUnits = null;
138
        if(u instanceof Teacher)
139
        {
140
            Teacher teacher = (Teacher) u;
995 jmachado 141
            courseUnits = new HashSet<CourseUnit>(DaoFactory.getCourseUnitDaoImpl().loadTeachedImportYearUnits(teacher.getId(), DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear()));
142
            responsableCourseUnits = new HashSet<CourseUnit>(DaoFactory.getCourseUnitDaoImpl().loadResponsableImportYearUnits(teacher.getId(), DaoFactory.getConfigurationDaoImpl().getInterfaceImportYear()));
821 jmachado 143
            userView.setResponsableCourseUnits(new ArrayList<CourseUnitView>());
144
            for(CourseUnit c: responsableCourseUnits)
145
            {
146
                CourseUnitView courseUnitView = new CourseUnitView(c);
147
                userView.getResponsableCourseUnits().add(courseUnitView);
148
            }
215 jmachado 149
        }
150
        else if(u instanceof Student)
151
        {
152
            Student student = (Student) u;
153
            courseUnits = student.getSubscribedUnits();
154
        }
155
        if(courseUnits != null)
156
        {
157
            HashMap<Course,CourseView> courses = new HashMap<Course,CourseView>();
158
            List<CourseView> courseViews = new ArrayList<CourseView>();
159
            List<CourseUnitView> courseUnitViews = new ArrayList<CourseUnitView>();
160
            for(CourseUnit c: courseUnits)
161
            {
162
                CourseUnitView courseUnitView = new CourseUnitView(c);
163
                courseUnitViews.add(courseUnitView);
164
                CourseView courseView;
165
                if((courseView = courses.get(c.getCourse())) == null)
166
                {
167
                    courseView = new CourseView(c.getCourse());
168
                    courseViews.add(courseView);
169
                    courses.put(c.getCourse(),courseView);
170
                    courseView.setCourseUnits(new ArrayList<CourseUnitView>());
171
                }
172
                courseView.getCourseUnits().add(courseUnitView);
173
            }
174
            userView.setCourses(courseViews);
175
            userView.setCourseUnits(courseUnitViews);
176
        }
177
        return userView;
178
    }
219 jmachado 179
 
180
    public UserView loadUserViewWithGrades(long id, UserSession userSession) throws ServiceException
181
    {
182
 
183
        User u;
184
        UserView userView;
185
        if(id <= 0)
186
        {
187
            userView = new UserView(userSession.getUser());
188
            u = userSession.getUser();
189
        }
190
        else
191
        {
192
            User user = DaoFactory.getUserDaoImpl().get(id);
193
            u = user;
194
            if(user != null)
195
                userView = new UserView(user);
196
            else
197
                return null;
198
        }
199
        List<CourseUnitGradeView> grades = loadGrades(id,userSession);
200
        userView.setGrades(grades);
201
        return userView;
202
    }
203
 
204
    public List<CourseUnitGradeView> loadGrades(long userId, UserSession userSession) throws ServiceException
205
    {
206
        //todo change security
1354 jmachado 207
        if(userId != userSession.getUser().getId() && !userSession.getUser().isSuperuserOrAdmin() && !userSession.getUser().isTeacher())
219 jmachado 208
        {
209
            logger.warn("Trying see grades of other user");
210
            return null;
211
        }
212
        List<CourseUnitGradeView> gradesViews = new ArrayList<CourseUnitGradeView>();
213
        Student s = DaoFactory.getStudentDaoImpl().get(userId);
214
        if(s == null)
215
            return null;
216
        Set<CourseUnitGrade> grades = s.getCourseUnitGrades();
217
        if(grades == null)
218
            return null;
219
        for(CourseUnitGrade grade: grades)
220
        {
221
            gradesViews.add(new CourseUnitGradeView(grade));
222
        }
223
        return gradesViews;
224
    }
215 jmachado 225
}