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