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