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 java.util.Collection; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.apache.ojb.broker.query.Criteria; |
23 | |
import org.apache.ojb.broker.query.QueryByCriteria; |
24 | |
import org.apache.ojb.broker.query.QueryFactory; |
25 | |
import org.kuali.rice.core.dao.GenericDao; |
26 | |
import org.kuali.rice.ken.bo.Notification; |
27 | |
import org.kuali.rice.ken.bo.NotificationContentType; |
28 | |
import org.kuali.rice.ken.service.NotificationContentTypeService; |
29 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
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 | |
public NotificationContentType getNotificationContentType(String name) { |
51 | 0 | Criteria c = new Criteria(); |
52 | 0 | c.addEqualTo("name", name); |
53 | 0 | c.addEqualTo("current", true); |
54 | 0 | Collection<NotificationContentType> coll = businessObjectDao.findMatching(NotificationContentType.class, c); |
55 | 0 | if (coll.size() == 0) { |
56 | 0 | return null; |
57 | |
} else { |
58 | 0 | return coll.iterator().next(); |
59 | |
} |
60 | |
} |
61 | |
|
62 | |
protected int findHighestContentTypeVersion(String name) { |
63 | |
|
64 | 0 | Map<String, Object> fields = new HashMap<String, Object>(2); |
65 | 0 | fields.put("name", name); |
66 | 0 | Collection<NotificationContentType> types = businessObjectDao.findMatchingOrderBy(NotificationContentType.class, fields, "version", false); |
67 | 0 | if (types.size() > 0) { |
68 | 0 | return types.iterator().next().getVersion(); |
69 | |
} |
70 | 0 | return -1; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void saveNotificationContentType(NotificationContentType contentType) { |
77 | 0 | NotificationContentType previous = getNotificationContentType(contentType.getName()); |
78 | 0 | if (previous != null) { |
79 | 0 | previous.setCurrent(false); |
80 | 0 | businessObjectDao.save(previous); |
81 | |
} |
82 | 0 | int lastVersion = findHighestContentTypeVersion(contentType.getName()); |
83 | |
NotificationContentType next; |
84 | 0 | if (contentType.getId() == null) { |
85 | 0 | next = contentType; |
86 | |
} else { |
87 | 0 | next = new NotificationContentType(); |
88 | 0 | next.setName(contentType.getName()); |
89 | 0 | next.setDescription(contentType.getDescription()); |
90 | 0 | next.setNamespace(contentType.getNamespace()); |
91 | 0 | next.setXsd(contentType.getXsd()); |
92 | 0 | next.setXsl(contentType.getXsl()); |
93 | |
} |
94 | |
|
95 | 0 | next.setVersion(lastVersion + 1); |
96 | 0 | next.setCurrent(true); |
97 | 0 | businessObjectDao.save(next); |
98 | |
|
99 | |
|
100 | 0 | if (previous != null) { |
101 | 0 | Collection<Notification> ns = getNotificationsOfContentType(previous); |
102 | 0 | for (Notification n: ns) { |
103 | 0 | n.setContentType(next); |
104 | 0 | businessObjectDao.save(n); |
105 | |
} |
106 | |
} |
107 | 0 | } |
108 | |
|
109 | |
protected Collection<Notification> getNotificationsOfContentType(NotificationContentType ct) { |
110 | 0 | Map<String, Object> fields = new HashMap<String, Object>(1); |
111 | 0 | fields.put("contentType", ct.getId()); |
112 | 0 | return businessObjectDao.findMatching(Notification.class, fields); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
public Collection<NotificationContentType> getAllCurrentContentTypes() { |
118 | 0 | Criteria c = new Criteria(); |
119 | 0 | c.addEqualTo("current", true); |
120 | 0 | return businessObjectDao.findMatching(NotificationContentType.class, c); |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public Collection<NotificationContentType> getAllContentTypes() { |
127 | 0 | return businessObjectDao.findAll(NotificationContentType.class); |
128 | |
} |
129 | |
} |