1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ken.service.impl; |
17 | |
|
18 | |
import org.kuali.rice.core.framework.persistence.dao.GenericDao; |
19 | |
import org.kuali.rice.ken.bo.Notification; |
20 | |
import org.kuali.rice.ken.bo.NotificationContentType; |
21 | |
import org.kuali.rice.ken.service.NotificationContentTypeService; |
22 | |
|
23 | |
import java.util.Collection; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class NotificationContentTypeServiceImpl implements NotificationContentTypeService { |
37 | |
private GenericDao businessObjectDao; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public NotificationContentTypeServiceImpl(GenericDao businessObjectDao) { |
44 | 0 | this.businessObjectDao = businessObjectDao; |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public NotificationContentType getNotificationContentType(String name) { |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | Map<String, Object> c = new HashMap<String, Object>(); |
59 | 0 | c.put("name", name); |
60 | 0 | c.put("current", new Boolean(true)); |
61 | |
|
62 | 0 | Collection<NotificationContentType> coll = businessObjectDao.findMatching(NotificationContentType.class, c); |
63 | 0 | if (coll.size() == 0) { |
64 | 0 | return null; |
65 | |
} else { |
66 | 0 | return coll.iterator().next(); |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
protected int findHighestContentTypeVersion(String name) { |
71 | |
|
72 | 0 | Map<String, Object> fields = new HashMap<String, Object>(2); |
73 | 0 | fields.put("name", name); |
74 | 0 | Collection<NotificationContentType> types = businessObjectDao.findMatchingOrderBy(NotificationContentType.class, fields, "version", false); |
75 | 0 | if (types.size() > 0) { |
76 | 0 | return types.iterator().next().getVersion(); |
77 | |
} |
78 | 0 | return -1; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public void saveNotificationContentType(NotificationContentType contentType) { |
85 | 0 | NotificationContentType previous = getNotificationContentType(contentType.getName()); |
86 | 0 | if (previous != null) { |
87 | 0 | previous.setCurrent(false); |
88 | 0 | businessObjectDao.save(previous); |
89 | |
} |
90 | 0 | int lastVersion = findHighestContentTypeVersion(contentType.getName()); |
91 | |
NotificationContentType next; |
92 | 0 | if (contentType.getId() == null) { |
93 | 0 | next = contentType; |
94 | |
} else { |
95 | 0 | next = new NotificationContentType(); |
96 | 0 | next.setName(contentType.getName()); |
97 | 0 | next.setDescription(contentType.getDescription()); |
98 | 0 | next.setNamespace(contentType.getNamespace()); |
99 | 0 | next.setXsd(contentType.getXsd()); |
100 | 0 | next.setXsl(contentType.getXsl()); |
101 | |
} |
102 | |
|
103 | 0 | next.setVersion(lastVersion + 1); |
104 | 0 | next.setCurrent(true); |
105 | 0 | businessObjectDao.save(next); |
106 | |
|
107 | |
|
108 | 0 | if (previous != null) { |
109 | 0 | Collection<Notification> ns = getNotificationsOfContentType(previous); |
110 | 0 | for (Notification n: ns) { |
111 | 0 | n.setContentType(next); |
112 | 0 | businessObjectDao.save(n); |
113 | |
} |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
protected Collection<Notification> getNotificationsOfContentType(NotificationContentType ct) { |
118 | 0 | Map<String, Object> fields = new HashMap<String, Object>(1); |
119 | 0 | fields.put("contentType", ct.getId()); |
120 | 0 | return businessObjectDao.findMatching(Notification.class, fields); |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
public Collection<NotificationContentType> getAllCurrentContentTypes() { |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | 0 | Map<String, Boolean> c = new HashMap<String, Boolean>(); |
132 | 0 | c.put("current", new Boolean(true)); |
133 | |
|
134 | 0 | return businessObjectDao.findMatching(NotificationContentType.class, c); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public Collection<NotificationContentType> getAllContentTypes() { |
141 | 0 | return businessObjectDao.findAll(NotificationContentType.class); |
142 | |
} |
143 | |
} |