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