1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.organization.assembly; |
17 | |
|
18 | |
import static org.kuali.student.core.assembly.util.AssemblerUtils.addVersionIndicator; |
19 | |
import static org.kuali.student.core.assembly.util.AssemblerUtils.getVersionIndicator; |
20 | |
import static org.kuali.student.core.assembly.util.AssemblerUtils.isModified; |
21 | |
import static org.kuali.student.core.assembly.util.AssemblerUtils.setCreated; |
22 | |
import static org.kuali.student.core.assembly.util.AssemblerUtils.setUpdated; |
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
29 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
30 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
31 | |
import org.kuali.student.core.assembly.data.AssemblyException; |
32 | |
import org.kuali.student.core.assembly.data.Data; |
33 | |
import org.kuali.student.core.assembly.data.Metadata; |
34 | |
import org.kuali.student.core.assembly.data.QueryPath; |
35 | |
import org.kuali.student.core.assembly.old.BaseAssembler; |
36 | |
import org.kuali.student.core.assembly.old.data.SaveResult; |
37 | |
import org.kuali.student.core.dto.MetaInfo; |
38 | |
import org.kuali.student.core.exceptions.AlreadyExistsException; |
39 | |
import org.kuali.student.core.exceptions.DataValidationErrorException; |
40 | |
import org.kuali.student.core.exceptions.DoesNotExistException; |
41 | |
import org.kuali.student.core.exceptions.InvalidParameterException; |
42 | |
import org.kuali.student.core.exceptions.MissingParameterException; |
43 | |
import org.kuali.student.core.exceptions.OperationFailedException; |
44 | |
import org.kuali.student.core.exceptions.PermissionDeniedException; |
45 | |
import org.kuali.student.core.exceptions.VersionMismatchException; |
46 | |
import org.kuali.student.core.organization.assembly.data.server.OrgInfoData; |
47 | |
import org.kuali.student.core.organization.assembly.data.server.OrgInfoData.ModificationState; |
48 | |
import org.kuali.student.core.organization.assembly.data.server.org.OrgHelper; |
49 | |
import org.kuali.student.core.organization.dto.OrgInfo; |
50 | |
import org.kuali.student.core.organization.dto.OrgOrgRelationInfo; |
51 | |
import org.kuali.student.core.organization.dto.OrgPositionRestrictionInfo; |
52 | |
import org.kuali.student.core.organization.service.OrganizationService; |
53 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
54 | |
import org.springframework.transaction.annotation.Transactional; |
55 | |
|
56 | 0 | @Transactional(rollbackFor={Throwable.class}) |
57 | |
public class OrgProposalAssembler extends BaseAssembler<Data, OrgHelper>{ |
58 | 0 | final Logger LOG = Logger.getLogger(OrgProposalAssembler.class); |
59 | |
private OrganizationService orgService; |
60 | 0 | public static String PROPOSAL_TYPE_CREATE_ORG = "kuali.proposal.type.org.create"; |
61 | 0 | public static String ORG_PROPOSAL_DATA_TYPE = "OrgProposal"; |
62 | |
public static final String ORG_INFO_PATH = "orgInfo"; |
63 | |
public static final String QUALIFICATION_ORG_ID = "orgId"; |
64 | |
public static final String PERM_DTO_NAME = "Organization"; |
65 | |
private Metadata metadata; |
66 | 0 | private DataModel orgProposalModel = new DataModel(); |
67 | 0 | public OrgProposalAssembler(){ |
68 | |
|
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private void setMetadata(String orgId){ |
76 | |
try { |
77 | 0 | this.metadata=getMetadata(QUALIFICATION_ORG_ID, orgId, null, null); |
78 | 0 | } catch (AssemblyException e) { |
79 | 0 | LOG.error(e); |
80 | 0 | } |
81 | 0 | } |
82 | |
@Override |
83 | |
public Data assemble(OrgHelper input) throws AssemblyException { |
84 | 0 | if (input == null) { |
85 | 0 | return null; |
86 | |
} |
87 | 0 | Data result = new Data(); |
88 | |
|
89 | 0 | return result; |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public OrgHelper disassemble(Data input) throws AssemblyException { |
94 | 0 | if (input == null) { |
95 | 0 | return null; |
96 | |
} |
97 | |
|
98 | |
|
99 | 0 | return null; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public Data get(String id) throws AssemblyException { |
104 | 0 | if(metadata==null){ |
105 | 0 | setMetadata(id); |
106 | |
} |
107 | 0 | OrgInfo orgInfo = new OrgInfo(); |
108 | 0 | List<OrgPositionRestrictionInfo> positions = new ArrayList<OrgPositionRestrictionInfo>(); |
109 | 0 | List<OrgOrgRelationInfo> relations = new ArrayList<OrgOrgRelationInfo>(); |
110 | 0 | Data result = new Data(); |
111 | |
|
112 | |
try{ |
113 | 0 | orgInfo = orgService.getOrganization(id); |
114 | 0 | OrgInfoData orgInfoData = new OrgInfoData(); |
115 | 0 | orgInfoData.setOrgInfo(orgInfo); |
116 | 0 | OrgHelper resultOrg = buildOrgDataMap(orgInfoData); |
117 | 0 | OrgOrgRelationAssembler orgOrgRelationAssembler = new OrgOrgRelationAssembler(); |
118 | 0 | orgOrgRelationAssembler.setMetaData(metadata); |
119 | 0 | orgOrgRelationAssembler.setOrgService(orgService); |
120 | 0 | OrgPositionRestrictionAssembler orgPositionRestrictionAssembler= new OrgPositionRestrictionAssembler(); |
121 | 0 | orgPositionRestrictionAssembler.setOrgService(orgService); |
122 | 0 | orgPositionRestrictionAssembler.setMetaData(metadata); |
123 | 0 | OrgPersonRelationAssembler orgPersonRelationAssembler = new OrgPersonRelationAssembler(); |
124 | 0 | orgPersonRelationAssembler.setMetaData(metadata); |
125 | 0 | orgPersonRelationAssembler.setOrgService(orgService); |
126 | 0 | Data orgOrgRelationMap = orgOrgRelationAssembler.get(id); |
127 | 0 | Data orgPositionMap = orgPositionRestrictionAssembler.get(id); |
128 | 0 | Data orgRelationMap = orgPersonRelationAssembler.get(id); |
129 | 0 | result.set("orgInfo", resultOrg.getData()); |
130 | 0 | result.set("orgOrgRelationInfo", orgOrgRelationMap); |
131 | 0 | result.set("OrgPositionRestrictionInfo", orgPositionMap); |
132 | 0 | result.set("orgPersonRelationInfo", orgRelationMap); |
133 | |
|
134 | |
} |
135 | 0 | catch(Exception e){ |
136 | 0 | LOG.error(e); |
137 | 0 | } |
138 | |
|
139 | 0 | return result; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
@Override |
144 | |
public SaveResult<Data> save(Data input) throws AssemblyException { |
145 | |
|
146 | 0 | OrgHelper orgHelper = OrgHelper.wrap((Data)input.get("orgInfo")); |
147 | 0 | if(metadata==null){ |
148 | 0 | setMetadata(orgHelper.getId()); |
149 | |
} |
150 | 0 | OrgInfoData orgInfoData = buildOrgInfo(orgHelper); |
151 | 0 | SaveResult<Data> result = new SaveResult<Data>(); |
152 | 0 | List<ValidationResultInfo> validationResults = validate(input); |
153 | |
|
154 | |
try { |
155 | 0 | saveOrg(orgInfoData); |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 0 | String orgId = orgInfoData.getOrgInfo().getId(); |
161 | 0 | addVersionIndicator(orgHelper.getData(),OrgInfo.class.getName(),orgId,orgInfoData.getOrgInfo().getMetaInfo().getVersionInd()); |
162 | |
|
163 | 0 | orgHelper.setId(orgId); |
164 | 0 | if(orgId!=null&&input.get("orgOrgRelationInfo")!=null){ |
165 | |
|
166 | 0 | OrgOrgRelationAssembler orgOrgRelationAssembler = new OrgOrgRelationAssembler(); |
167 | 0 | orgOrgRelationAssembler.setMetaData(metadata); |
168 | 0 | orgOrgRelationAssembler.setOrgService(orgService); |
169 | 0 | Data relationData = orgOrgRelationAssembler.save(input).getValue(); |
170 | |
|
171 | |
} |
172 | 0 | if(orgId!=null&&input.get("OrgPositionRestrictionInfo")!=null){ |
173 | 0 | OrgPositionRestrictionAssembler orgPositionRestrictionAssembler= new OrgPositionRestrictionAssembler(); |
174 | 0 | orgPositionRestrictionAssembler.setMetaData(metadata); |
175 | 0 | orgPositionRestrictionAssembler.setOrgService(orgService); |
176 | 0 | Data positionData = orgPositionRestrictionAssembler.save(input).getValue(); |
177 | |
} |
178 | 0 | if(orgId!=null&&input.get("orgPersonRelationInfo")!=null){ |
179 | 0 | OrgPersonRelationAssembler orgPersonRelationAssembler= new OrgPersonRelationAssembler(); |
180 | 0 | orgPersonRelationAssembler.setMetaData(metadata); |
181 | 0 | orgPersonRelationAssembler.setOrgService(orgService); |
182 | 0 | Data relationData = orgPersonRelationAssembler.save(input).getValue(); |
183 | |
} |
184 | |
|
185 | 0 | result.setValue((orgId == null) ? null : get(orgId)); |
186 | |
|
187 | 0 | } catch (Exception e) { |
188 | 0 | LOG.error(e); |
189 | 0 | throw(new AssemblyException()); |
190 | 0 | } |
191 | |
|
192 | 0 | return result; |
193 | |
} |
194 | |
|
195 | |
public void setOrgService(OrganizationService service){ |
196 | 0 | orgService = service; |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
private OrgInfoData buildOrgInfo(OrgHelper org){ |
201 | 0 | OrgInfo orgInfo = new OrgInfo(); |
202 | 0 | OrgInfoData result = new OrgInfoData(); |
203 | |
|
204 | 0 | orgInfo.setType(org.getType()); |
205 | 0 | orgInfo.setLongDesc(org.getDescription()); |
206 | 0 | orgInfo.setShortName(org.getAbbreviation()); |
207 | 0 | orgInfo.setLongName(org.getName()); |
208 | 0 | orgInfo.setEffectiveDate(org.getEffectiveDate()); |
209 | 0 | orgInfo.setExpirationDate(org.getExpirationDate()); |
210 | 0 | if(org.getId()!=null){ |
211 | 0 | orgInfo.setId(org.getId()); |
212 | |
} |
213 | |
|
214 | 0 | if (isModified(org.getData())) { |
215 | |
|
216 | 0 | MetaInfo metaInfo = new MetaInfo(); |
217 | 0 | orgInfo.setMetaInfo(metaInfo); |
218 | 0 | result.setModificationState(ModificationState.UPDATED); |
219 | |
|
220 | |
|
221 | |
|
222 | 0 | } |
223 | |
else{ |
224 | 0 | setCreated(org.getData(), true); |
225 | 0 | result.setModificationState(ModificationState.CREATED); |
226 | |
} |
227 | 0 | if(orgInfo.getMetaInfo()!=null){ |
228 | 0 | orgInfo.getMetaInfo().setVersionInd(getVersionIndicator(org.getData())); |
229 | |
} |
230 | |
|
231 | 0 | result.setOrgInfo(orgInfo); |
232 | 0 | return result; |
233 | |
} |
234 | |
|
235 | |
private void saveOrg(OrgInfoData input) throws AlreadyExistsException, DataValidationErrorException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException, DoesNotExistException, VersionMismatchException { |
236 | 0 | OrgInfo result = null; |
237 | 0 | OrgInfo orgInfo = input.getOrgInfo(); |
238 | 0 | DataModelDefinition def = new DataModelDefinition(metadata); |
239 | 0 | orgProposalModel.setDefinition(def); |
240 | 0 | QueryPath metaPath = QueryPath.concat(null, ORG_INFO_PATH); |
241 | 0 | Metadata orgProposalMeta =orgProposalModel.getMetadata(metaPath); |
242 | 0 | if (input.getModificationState() != null) { |
243 | 0 | if (orgProposalMeta.isCanEdit()) { |
244 | 0 | switch (input.getModificationState()) { |
245 | |
case CREATED: |
246 | 0 | result = orgService.createOrganization(orgInfo.getType(), orgInfo); |
247 | 0 | break; |
248 | |
case UPDATED: |
249 | 0 | result = orgService.updateOrganization(orgInfo.getId(), orgInfo); |
250 | |
default: |
251 | |
} |
252 | |
} |
253 | |
} |
254 | 0 | if (result != null) { |
255 | 0 | input.setOrgInfo(result); |
256 | |
} |
257 | 0 | } |
258 | |
|
259 | |
@Override |
260 | |
public List<ValidationResultInfo> validate(Data input) throws AssemblyException { |
261 | |
|
262 | 0 | return null; |
263 | |
} |
264 | |
|
265 | |
private <T extends Data> T copyIfExists(Data source, T target, String key) { |
266 | 0 | Data d = source.get(key); |
267 | 0 | if (d == null) { |
268 | 0 | return null; |
269 | |
} else { |
270 | 0 | d.copy(target, false); |
271 | 0 | source.set(key, target); |
272 | 0 | return target; |
273 | |
} |
274 | |
} |
275 | |
|
276 | |
private OrgHelper buildOrgDataMap(OrgInfoData orgInfoData){ |
277 | 0 | OrgHelper org = OrgHelper.wrap(new Data()); |
278 | 0 | org.setId(orgInfoData.getOrgInfo().getId()); |
279 | 0 | org.setType(orgInfoData.getOrgInfo().getType()); |
280 | 0 | org.setName(orgInfoData.getOrgInfo().getLongName()); |
281 | 0 | org.setAbbreviation(orgInfoData.getOrgInfo().getShortName()); |
282 | 0 | org.setDescription(orgInfoData.getOrgInfo().getLongDesc()); |
283 | 0 | org.setEffectiveDate(orgInfoData.getOrgInfo().getEffectiveDate()); |
284 | 0 | org.setExpirationDate(orgInfoData.getOrgInfo().getExpirationDate()); |
285 | 0 | setUpdated(org.getData(), true); |
286 | 0 | addVersionIndicator(org.getData(),OrgInfo.class.getName(),orgInfoData.getOrgInfo().getId(),orgInfoData.getOrgInfo().getMetaInfo().getVersionInd()); |
287 | |
|
288 | 0 | return org; |
289 | |
} |
290 | |
|
291 | |
@Override |
292 | |
protected String getDataType() { |
293 | 0 | return ORG_PROPOSAL_DATA_TYPE; |
294 | |
} |
295 | |
|
296 | |
@Override |
297 | |
protected String getDocumentPropertyName() { |
298 | 0 | return "course"; |
299 | |
} |
300 | |
|
301 | |
@Override |
302 | |
protected String getDtoName() { |
303 | 0 | return PERM_DTO_NAME; |
304 | |
} |
305 | |
|
306 | |
@Override |
307 | |
protected AttributeSet getQualification(String idType, String id) { |
308 | 0 | AttributeSet qualification = null; |
309 | 0 | if(id!=null&&!id.isEmpty()){ |
310 | 0 | qualification = new AttributeSet(); |
311 | |
|
312 | |
|
313 | |
|
314 | 0 | qualification.put(idType, id); |
315 | |
} |
316 | |
|
317 | 0 | return qualification; |
318 | |
} |
319 | |
|
320 | |
} |