1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.proposal.service.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.persistence.NoResultException; |
24 | |
|
25 | |
import org.kuali.student.common.dto.ReferenceTypeInfo; |
26 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
27 | |
import org.kuali.student.common.exceptions.InvalidParameterException; |
28 | |
import org.kuali.student.common.exceptions.VersionMismatchException; |
29 | |
import org.kuali.student.common.service.impl.BaseAssembler; |
30 | |
import org.kuali.student.core.proposal.dao.ProposalDao; |
31 | |
import org.kuali.student.core.proposal.dto.ProposalInfo; |
32 | |
import org.kuali.student.core.proposal.dto.ProposalTypeInfo; |
33 | |
import org.kuali.student.core.proposal.entity.Proposal; |
34 | |
import org.kuali.student.core.proposal.entity.ProposalAttribute; |
35 | |
import org.kuali.student.core.proposal.entity.ProposalOrg; |
36 | |
import org.kuali.student.core.proposal.entity.ProposalPerson; |
37 | |
import org.kuali.student.core.proposal.entity.ProposalReference; |
38 | |
import org.kuali.student.core.proposal.entity.ProposalReferenceType; |
39 | |
import org.kuali.student.core.proposal.entity.ProposalType; |
40 | |
import org.springframework.beans.BeanUtils; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 0 | public class ProposalAssembler extends BaseAssembler { |
49 | |
|
50 | |
public static List<ProposalInfo> toProposalInfos(List<Proposal> entities) { |
51 | 9 | List<ProposalInfo> dtos = new ArrayList<ProposalInfo>(entities.size()); |
52 | 9 | for (Proposal entity : entities) { |
53 | 8 | dtos.add(toProposalInfo(entity)); |
54 | |
} |
55 | 9 | return dtos; |
56 | |
} |
57 | |
|
58 | |
public static ProposalInfo toProposalInfo(Proposal entity) { |
59 | 15 | ProposalInfo dto = new ProposalInfo(); |
60 | |
|
61 | |
|
62 | 15 | BeanUtils.copyProperties(entity, dto, |
63 | |
new String[] { "proposerPerson", "proposerOrg", "proposalReferenceType", "proposalReference", "attributes", "type" }); |
64 | 15 | if (entity.getProposerPerson() != null) { |
65 | 15 | List<String> personIds = new ArrayList<String>(entity.getProposerPerson().size()); |
66 | 15 | for (ProposalPerson person : entity.getProposerPerson()) { |
67 | 9 | personIds.add(person.getPersonId()); |
68 | |
} |
69 | 15 | dto.setProposerPerson(personIds); |
70 | |
} |
71 | 15 | if (entity.getProposerOrg() != null) { |
72 | 15 | List<String> orgIds = new ArrayList<String>(entity.getProposerOrg().size()); |
73 | 15 | for (ProposalOrg org : entity.getProposerOrg()) { |
74 | 8 | orgIds.add(org.getOrgId()); |
75 | |
} |
76 | 15 | dto.setProposerOrg(orgIds); |
77 | |
} |
78 | |
|
79 | 15 | if (entity.getProposalReference() != null){ |
80 | 15 | if (entity.getProposalReference().size() > 0){ |
81 | 15 | dto.setProposalReferenceType(entity.getProposalReference().get(0).getType().getId()); |
82 | |
} |
83 | 15 | List<String> objectIds = new ArrayList<String>(entity.getProposalReference().size()); |
84 | 15 | for (ProposalReference object : entity.getProposalReference()) { |
85 | 30 | objectIds.add(object.getObjectReferenceId()); |
86 | |
} |
87 | 15 | dto.setProposalReference(objectIds); |
88 | |
} |
89 | 15 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
90 | 15 | dto.setMetaInfo(toMetaInfo(entity.getMeta(), entity.getVersionNumber())); |
91 | 15 | dto.setType(entity.getType().getId()); |
92 | |
|
93 | 15 | return dto; |
94 | |
} |
95 | |
|
96 | |
public static List<ProposalTypeInfo> toProposalTypeInfos(List<ProposalType> entities) { |
97 | 3 | List<ProposalTypeInfo> dtos = new ArrayList<ProposalTypeInfo>(entities.size()); |
98 | 3 | for (ProposalType entity : entities) { |
99 | 4 | dtos.add(toProposalTypeInfo(entity)); |
100 | |
} |
101 | 3 | return dtos; |
102 | |
} |
103 | |
|
104 | |
public static ProposalTypeInfo toProposalTypeInfo(ProposalType entity) { |
105 | 5 | ProposalTypeInfo dto = new ProposalTypeInfo(); |
106 | |
|
107 | 5 | BeanUtils.copyProperties(entity, dto, |
108 | |
new String[] { "attributes" }); |
109 | 5 | dto.setDesc(entity.getDescr()); |
110 | 5 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
111 | 5 | return dto; |
112 | |
} |
113 | |
|
114 | |
public static List<ReferenceTypeInfo> toReferenceTypeInfos(List<ProposalReferenceType> entities) { |
115 | 1 | List<ReferenceTypeInfo> dtos = new ArrayList<ReferenceTypeInfo>(entities.size()); |
116 | 1 | for (ProposalReferenceType entity : entities) { |
117 | 1 | dtos.add(toReferenceTypeInfo(entity)); |
118 | |
} |
119 | 1 | return dtos; |
120 | |
} |
121 | |
|
122 | |
public static ReferenceTypeInfo toReferenceTypeInfo(ProposalReferenceType entity) { |
123 | 1 | ReferenceTypeInfo dto = new ReferenceTypeInfo(); |
124 | |
|
125 | 1 | BeanUtils.copyProperties(entity, dto, |
126 | |
new String[] { "attributes" }); |
127 | |
|
128 | 1 | dto.setAttributes(toAttributeMap(entity.getAttributes())); |
129 | 1 | return dto; |
130 | |
} |
131 | |
|
132 | |
public static Proposal toProposal(String proposalTypeKey, ProposalInfo proposalInfo, ProposalDao dao) throws DoesNotExistException, VersionMismatchException, InvalidParameterException { |
133 | |
Proposal proposal; |
134 | 3 | if (proposalInfo.getId() != null && !proposalInfo.getId().isEmpty()) { |
135 | 2 | proposal = dao.fetch(Proposal.class, proposalInfo.getId()); |
136 | 2 | if (!String.valueOf(proposal.getVersionNumber()).equals(proposalInfo.getMetaInfo().getVersionInd())){ |
137 | 0 | throw new VersionMismatchException("Proposal to be updated is not the current version"); |
138 | |
} |
139 | |
} else { |
140 | 1 | proposal = new Proposal(); |
141 | 1 | proposal.setProposerPerson(new ArrayList<ProposalPerson>(0)); |
142 | 1 | proposal.setProposerOrg(new ArrayList<ProposalOrg>(0)); |
143 | |
} |
144 | |
|
145 | |
|
146 | 3 | BeanUtils.copyProperties(proposalInfo, proposal, new String[] { "id", "type", |
147 | |
"attributes", "metaInfo", "proposerPerson", "proposerOrg", "proposalReference" }); |
148 | |
|
149 | |
|
150 | 3 | proposal.setAttributes(toGenericAttributes(ProposalAttribute.class, proposalInfo.getAttributes(), proposal, dao)); |
151 | |
|
152 | |
|
153 | 3 | Map<String,ProposalPerson> existingPersons = new HashMap<String,ProposalPerson>(); |
154 | 3 | if(proposal.getProposerPerson()==null){ |
155 | 0 | proposal.setProposerPerson(new ArrayList<ProposalPerson>(proposalInfo.getProposerPerson().size())); |
156 | |
} |
157 | 3 | for(ProposalPerson person:proposal.getProposerPerson()){ |
158 | 2 | existingPersons.put(person.getPersonId(), person); |
159 | |
} |
160 | 3 | proposal.getProposerPerson().clear(); |
161 | 3 | for (String proposerId : proposalInfo.getProposerPerson()) { |
162 | |
ProposalPerson person; |
163 | 2 | if(existingPersons.containsKey(proposerId)){ |
164 | 0 | person = existingPersons.get(proposerId); |
165 | 0 | existingPersons.remove(proposerId); |
166 | |
}else{ |
167 | 2 | person = new ProposalPerson(); |
168 | |
} |
169 | 2 | person.setPersonId(proposerId); |
170 | 2 | person.setProposal(proposal); |
171 | 2 | proposal.getProposerPerson().add(person); |
172 | 2 | } |
173 | 3 | for (ProposalPerson person : existingPersons.values()) { |
174 | 2 | dao.delete(person); |
175 | |
} |
176 | |
|
177 | |
|
178 | 3 | Map<String,ProposalOrg> existingOrgs = new HashMap<String,ProposalOrg>(); |
179 | 3 | if(proposal.getProposerOrg()==null){ |
180 | 0 | proposal.setProposerOrg(new ArrayList<ProposalOrg>(proposalInfo.getProposerOrg().size())); |
181 | |
} |
182 | 3 | for(ProposalOrg org:proposal.getProposerOrg()){ |
183 | 2 | existingOrgs.put(org.getOrgId(), org); |
184 | |
} |
185 | 3 | proposal.getProposerOrg().clear(); |
186 | 3 | for (String orgId: proposalInfo.getProposerOrg()) { |
187 | |
ProposalOrg org; |
188 | 4 | if(existingOrgs.containsKey(orgId)){ |
189 | 2 | org = existingOrgs.get(orgId); |
190 | 2 | existingOrgs.remove(orgId); |
191 | |
}else{ |
192 | 2 | org = new ProposalOrg(); |
193 | |
} |
194 | 4 | org.setOrgId(orgId); |
195 | 4 | org.setProposal(proposal); |
196 | 4 | proposal.getProposerOrg().add(org); |
197 | 4 | } |
198 | 3 | for (ProposalOrg proposalOrg : existingOrgs.values()) { |
199 | 0 | dao.delete(proposalOrg); |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | 3 | if (proposalInfo.getProposalReference() != null) { |
229 | |
|
230 | 3 | List<ProposalReference> references = new ArrayList<ProposalReference>(proposalInfo.getProposalReference().size()); |
231 | 3 | for (String objectReferenceId : proposalInfo.getProposalReference()) { |
232 | |
ProposalReference ref; |
233 | |
try { |
234 | 3 | ref = dao.getObjectReference(objectReferenceId, proposalInfo.getProposalReferenceType()); |
235 | 2 | } catch (NoResultException e) { |
236 | 2 | ProposalReference objectReference = new ProposalReference(); |
237 | 2 | objectReference.setObjectReferenceId(objectReferenceId); |
238 | 2 | ProposalReferenceType refType = dao.fetch(ProposalReferenceType.class, proposalInfo.getProposalReferenceType()); |
239 | 2 | objectReference.setType(refType); |
240 | 2 | ref = dao.create(objectReference); |
241 | 1 | } |
242 | 3 | references.add(ref); |
243 | 3 | } |
244 | 3 | proposal.setProposalReference(references); |
245 | |
} |
246 | |
|
247 | 3 | ProposalType proposalType = dao.fetch(ProposalType.class, proposalTypeKey); |
248 | 3 | proposal.setType(proposalType); |
249 | 3 | return proposal; |
250 | |
} |
251 | |
} |