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