/
MergingCoursesDaoImpl.java
207 lines (163 loc) · 6.52 KB
/
MergingCoursesDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portlet.courses.dao;
import java.util.List;
import javax.annotation.Resource;
import javax.portlet.PortletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jasig.portlet.courses.model.xml.Course;
import org.jasig.portlet.courses.model.xml.CourseSummary;
import org.jasig.portlet.courses.model.xml.Term;
import org.jasig.portlet.courses.model.xml.TermSummary;
/**
* MergingCoursesDaoImpl merges together information from multiple data sources.
* This implementation will merge together matching terms and courses that
* contain identical IDs. In the case that two sources specify information for
* a single-value property (e.g. GPA), the last DAO in the list will be given
* priority. In the case of multi-valued list properties (instructors), the
* lists from each source will be merged together.
*
* @author Jen Bourey, jennifer.bourey@gmail.com
* @version $Revision$
*/
public class MergingCoursesDaoImpl implements ICoursesDao {
protected final Log log = LogFactory.getLog(getClass());
private List<ICoursesDao> courseDaos;
/**
* Set the list of course DAOs to be called. This list will be called in
* order, and conflicting information will be resolved by prioritizing
* information from the last DAO.
*
* @param courseDaos
*/
@Resource(name="courseDaos")
public void setCourseDaos(List<ICoursesDao> courseDaos) {
this.courseDaos = courseDaos;
}
@Override
public TermSummary getTermSummary(PortletRequest request) {
TermSummary summary = null;
// iterate over the list of course DAOs
for (ICoursesDao dao : courseDaos) {
try {
TermSummary daoSummary = dao.getTermSummary(request);
if (summary == null) {
summary = daoSummary;
} else {
mergeTermSummaries(summary, daoSummary);
}
} catch (Exception e) {
log.error("Exception reading course dao", e);
}
}
return summary;
}
@Override
public CourseSummary getCourseSummary(PortletRequest request, String termCode) {
CourseSummary summary = null;
// iterate over the list of course DAOs
for (ICoursesDao dao : courseDaos) {
try {
CourseSummary daoSummary = dao.getCourseSummary(request, termCode);
if (summary == null) {
summary = daoSummary;
} else {
mergeCourseSummaries(summary, daoSummary);
}
} catch (Exception e) {
log.error("Exception reading course dao", e);
}
}
return summary;
}
protected void mergeTermSummaries(TermSummary original, TermSummary additional) {
final List<Term> originalTerms = original.getTerms();
for (Term t : additional.getTerms()) {
// if this term already exists in the summary, merge
// information from this DAO into the existing entry
final Term originalTerm = original.getTerm(t.getCode());
if (original.getTerm(t.getCode()) != null) {
//TODO nothing to merge as of yet
}
// if we haven't seen this term before, just add it to the
// list
else {
originalTerms.add(t);
}
}
}
protected void mergeCourseSummaries(CourseSummary original, CourseSummary additional) {
// overall credit total
if (additional.getCredits() != null) {
original.setCredits(additional.getCredits());
}
// overall GPA
if (additional.getGpa() != null) {
original.setGpa(additional.getGpa());
}
// merge the course lists for the existing entry
// and new DAO
for (Course c : additional.getCourses()) {
Course course = original.getCourse(c.getCode());
if (course != null) {
mergeCourse(course, c);
}
else {
original.getCourses().add(c);
}
}
}
/**
* Merge together information from the additional course into the provided
* original course.
*
* @param original
* @param additional
*/
protected void mergeCourse(Course original, Course additional) {
if (additional.getCredits() != null) {
original.setCredits(additional.getCredits());
}
if (additional.getGrade() != null) {
original.setGrade(additional.getGrade());
}
if (additional.getLocation() != null) {
original.setLocation(additional.getLocation());
}
if (additional.getCourseUpdates() != null) {
original.getCourseUpdates().addAll(additional.getCourseUpdates());
}
if (additional.getInstructors() != null) {
original.getInstructors().addAll(additional.getInstructors());
}
if (additional.getSchool() != null) {
original.setSchool(additional.getSchool());
}
if (additional.getMeetingTimes() != null) {
original.setMeetingTimes(additional.getMeetingTimes());
}
if (additional.getTitle() != null) {
original.setTitle(additional.getTitle());
}
if (additional.getUrl() != null) {
original.setUrl(additional.getUrl());
}
}
}