1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.comment.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.dto.ReferenceTypeInfo; |
22 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
23 | |
import org.kuali.student.common.exceptions.InvalidParameterException; |
24 | |
import org.kuali.student.common.service.impl.BaseAssembler; |
25 | |
import org.kuali.student.core.comment.dao.CommentDao; |
26 | |
import org.kuali.student.core.comment.dto.CommentInfo; |
27 | |
import org.kuali.student.core.comment.dto.CommentTypeInfo; |
28 | |
import org.kuali.student.core.comment.dto.TagInfo; |
29 | |
import org.kuali.student.core.comment.dto.TagTypeInfo; |
30 | |
import org.kuali.student.core.comment.entity.Comment; |
31 | |
import org.kuali.student.core.comment.entity.CommentAttribute; |
32 | |
import org.kuali.student.core.comment.entity.CommentRichText; |
33 | |
import org.kuali.student.core.comment.entity.CommentType; |
34 | |
import org.kuali.student.core.comment.entity.Reference; |
35 | |
import org.kuali.student.core.comment.entity.ReferenceType; |
36 | |
import org.kuali.student.core.comment.entity.Tag; |
37 | |
import org.kuali.student.core.comment.entity.TagAttribute; |
38 | |
import org.kuali.student.core.comment.entity.TagType; |
39 | |
import org.springframework.beans.BeanUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | 0 | public class CommentServiceAssembler extends BaseAssembler { |
48 | |
|
49 | |
public static CommentInfo toCommentInfo(Comment entity) { |
50 | 11 | CommentInfo dto = new CommentInfo(); |
51 | |
|
52 | 11 | BeanUtils.copyProperties(entity, dto, |
53 | |
new String[] { "commentText", "attributes", "type", "metaInfo" }); |
54 | |
|
55 | 11 | dto.setCommentText(toRichTextInfo(entity.getCommentText())); |
56 | 11 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
57 | 11 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
58 | 11 | dto.setType(entity.getType().getId()); |
59 | |
|
60 | 11 | return dto; |
61 | |
} |
62 | |
|
63 | |
public static List<CommentInfo> toCommentInfos(List<Comment> entities) { |
64 | 2 | List<CommentInfo> dtos = new ArrayList<CommentInfo>(entities.size()); |
65 | 2 | for (Comment entity : entities) { |
66 | 3 | dtos.add(toCommentInfo(entity)); |
67 | |
} |
68 | 2 | return dtos; |
69 | |
} |
70 | |
|
71 | |
public static TagInfo toTagInfo(Tag entity) { |
72 | 11 | TagInfo dto = new TagInfo(); |
73 | 11 | BeanUtils.copyProperties(entity, dto, new String[]{"attributes","type","reference"}); |
74 | 11 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
75 | 11 | dto.setType(entity.getType().getId()); |
76 | 11 | dto.setReferenceId(entity.getReferennce().getReferenceId()); |
77 | 11 | dto.setReferenceTypeKey(entity.getReferennce().getReferenceType().getId()); |
78 | 11 | return dto; |
79 | |
|
80 | |
} |
81 | |
|
82 | |
public static List<TagInfo> toTagInfos(List<Tag> entries){ |
83 | 3 | List<TagInfo> dtos = new ArrayList<TagInfo>(entries.size()); |
84 | 3 | for(Tag entry: entries){ |
85 | 5 | dtos.add(toTagInfo(entry)); |
86 | |
} |
87 | 3 | return dtos; |
88 | |
} |
89 | |
|
90 | |
public static TagTypeInfo toTagTypeInfo(TagType entity){ |
91 | 1 | TagTypeInfo dto = new TagTypeInfo(); |
92 | 1 | BeanUtils.copyProperties(entity, dto,new String[]{"attributes"}); |
93 | 1 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
94 | 1 | return dto; |
95 | |
} |
96 | |
|
97 | |
public static List<TagTypeInfo> toTagTypeInfos(List<TagType> entries){ |
98 | 1 | List<TagTypeInfo> dtos = new ArrayList<TagTypeInfo>(entries.size()); |
99 | 1 | for(TagType entity: entries){ |
100 | 1 | dtos.add(toTagTypeInfo(entity)); |
101 | |
} |
102 | |
|
103 | 1 | return dtos; |
104 | |
} |
105 | |
|
106 | |
public static List<CommentTypeInfo> toCommentTypeInfos(List<CommentType> entities) { |
107 | 1 | List<CommentTypeInfo> dtos = new ArrayList<CommentTypeInfo>(entities.size()); |
108 | 1 | for (CommentType entity : entities) { |
109 | 2 | dtos.add(toCommentTypeInfo(entity)); |
110 | |
} |
111 | 1 | return dtos; |
112 | |
} |
113 | |
|
114 | |
private static CommentTypeInfo toCommentTypeInfo(CommentType entity) { |
115 | 2 | CommentTypeInfo dto = new CommentTypeInfo(); |
116 | 2 | BeanUtils.copyProperties(entity, dto,new String[]{"attributes"}); |
117 | 2 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
118 | 2 | return dto; |
119 | |
} |
120 | |
|
121 | |
public static Comment toComment(boolean isUpdate,CommentInfo dto,CommentDao dao) throws InvalidParameterException, DoesNotExistException{ |
122 | |
|
123 | 4 | Comment entity = new Comment(); |
124 | 4 | BeanUtils.copyProperties(dto,entity,new String[]{"reference","commentText", "attributes", "type", "metaInfo"}); |
125 | 4 | entity.setAttributes(toGenericAttributes(CommentAttribute.class,dto.getAttributes(),entity,dao)); |
126 | 4 | Reference reference = dao.getReference(dto.getReferenceId(), dto.getReferenceTypeKey()); |
127 | 4 | if (reference == null) { |
128 | 0 | throw new InvalidParameterException( |
129 | |
"Reference does not exist for id: " + dto.getReferenceId()); |
130 | |
} |
131 | 4 | entity.setReference(reference); |
132 | 4 | CommentType type = dao.fetch(CommentType.class,dto.getType()); |
133 | 4 | if (type == null) { |
134 | 0 | throw new InvalidParameterException( |
135 | |
"Tag Type does not exist for id: " + dto.getType()); |
136 | |
} |
137 | 4 | entity.setType(type); |
138 | 4 | entity.setCommentText(toRichText(CommentRichText.class, dto.getCommentText())); |
139 | 4 | entity.setAttributes(toGenericAttributes(CommentAttribute.class, dto.getAttributes(), entity, dao)); |
140 | 4 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
141 | 4 | return entity; |
142 | |
} |
143 | |
public static Tag toTag(boolean isUpdate,TagInfo dto, CommentDao dao) throws InvalidParameterException, DoesNotExistException{ |
144 | |
|
145 | 4 | Tag entity = new Tag(); |
146 | 4 | BeanUtils.copyProperties(dto,entity,new String[]{"reference","type","attributes"}); |
147 | 4 | entity.setAttributes(toGenericAttributes(TagAttribute.class,dto.getAttributes(),entity,dao)); |
148 | |
|
149 | 4 | Reference reference = dao.getReference(dto.getReferenceId(), dto.getReferenceTypeKey()); |
150 | 4 | if (reference == null) { |
151 | 0 | throw new InvalidParameterException( |
152 | |
"Reference does not exist for id: " + dto.getReferenceId()); |
153 | |
} |
154 | 4 | entity.setReference(reference); |
155 | 4 | TagType type = dao.fetch(TagType.class,dto.getType()); |
156 | 4 | if (type == null) { |
157 | 0 | throw new InvalidParameterException( |
158 | |
"Tag Type does not exist for id: " + dto.getType()); |
159 | |
} |
160 | 4 | entity.setType(type); |
161 | 4 | return entity; |
162 | |
} |
163 | |
|
164 | |
public static Comment toComment(String referenceId, |
165 | |
String referenceTypeKey, CommentInfo dto, |
166 | |
CommentDao commentDao) throws InvalidParameterException, DoesNotExistException { |
167 | 0 | Comment entity = new Comment(); |
168 | |
|
169 | 0 | return toComment(entity, referenceId, referenceTypeKey, dto, commentDao); |
170 | |
} |
171 | |
|
172 | |
public static Comment toComment(Comment entity, String referenceId, |
173 | |
String referenceTypeKey, CommentInfo dto, CommentDao commentDao) |
174 | |
throws InvalidParameterException, DoesNotExistException { |
175 | 1 | BeanUtils.copyProperties(dto, entity, new String[] { "id, reference", |
176 | |
"commentText", "attributes", "type", "metaInfo" }); |
177 | 1 | entity.setCommentText(toRichText(CommentRichText.class, dto.getCommentText())); |
178 | |
|
179 | 1 | entity.setAttributes(toGenericAttributes(CommentAttribute.class, dto |
180 | |
.getAttributes(), entity, commentDao)); |
181 | 1 | CommentType type = commentDao.fetch(CommentType.class, dto.getType()); |
182 | 1 | if (type == null) { |
183 | 0 | throw new InvalidParameterException( |
184 | |
"Tag Type does not exist for id: " + dto.getType()); |
185 | |
} |
186 | 1 | entity.setType(type); |
187 | 1 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
188 | |
|
189 | 1 | Reference reference = commentDao.getReference(referenceId, |
190 | |
referenceTypeKey); |
191 | 1 | if (reference == null) { |
192 | 1 | reference = new Reference(); |
193 | 1 | reference.setReferenceId(referenceId); |
194 | |
try { |
195 | 1 | ReferenceType referenceType = commentDao.fetch(ReferenceType.class, referenceTypeKey); |
196 | 1 | reference.setReferenceType(referenceType); |
197 | 1 | commentDao.create(reference); |
198 | 0 | } catch (DoesNotExistException e) { |
199 | 0 | throw new InvalidParameterException(e.getMessage()); |
200 | 1 | } |
201 | |
|
202 | |
} |
203 | 1 | entity.setReference(reference); |
204 | |
|
205 | 1 | return entity; |
206 | |
} |
207 | |
|
208 | |
public static List<ReferenceTypeInfo> toReferenceTypeInfos( |
209 | |
List<ReferenceType> entities) { |
210 | 1 | List<ReferenceTypeInfo> dtos = new ArrayList<ReferenceTypeInfo>(entities.size()); |
211 | 1 | for (ReferenceType entity : entities) { |
212 | 2 | dtos.add(toReferenceType(entity)); |
213 | |
} |
214 | 1 | return dtos; |
215 | |
|
216 | |
} |
217 | |
|
218 | |
private static ReferenceTypeInfo toReferenceType(ReferenceType entity) { |
219 | 2 | ReferenceTypeInfo dto = new ReferenceTypeInfo(); |
220 | |
|
221 | 2 | BeanUtils.copyProperties(entity, dto,new String[]{"attributes"}); |
222 | 2 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
223 | 2 | return dto; |
224 | |
} |
225 | |
|
226 | |
} |