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 | |
|
19 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.addVersionIndicator; |
20 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.getVersionIndicator; |
21 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.isCreated; |
22 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.isDeleted; |
23 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.isModified; |
24 | |
import static org.kuali.student.common.assembly.util.AssemblerUtils.isUpdated; |
25 | |
|
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
|
30 | |
import org.apache.log4j.Logger; |
31 | |
import org.kuali.student.common.assembly.data.AssemblyException; |
32 | |
import org.kuali.student.common.assembly.data.Data; |
33 | |
import org.kuali.student.common.assembly.data.Metadata; |
34 | |
import org.kuali.student.common.assembly.data.QueryPath; |
35 | |
import org.kuali.student.common.assembly.data.Data.Property; |
36 | |
import org.kuali.student.common.assembly.old.Assembler; |
37 | |
import org.kuali.student.common.assembly.old.data.SaveResult; |
38 | |
import org.kuali.student.common.assembly.util.AssemblerUtils; |
39 | |
import org.kuali.student.common.dto.MetaInfo; |
40 | |
import org.kuali.student.common.dto.StatusInfo; |
41 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
42 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
43 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
44 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
45 | |
import org.kuali.student.core.organization.assembly.data.server.org.OrgHelper; |
46 | |
import org.kuali.student.core.organization.assembly.data.server.org.OrgorgRelationHelper; |
47 | |
import org.kuali.student.core.organization.dto.OrgOrgRelationInfo; |
48 | |
import org.kuali.student.core.organization.service.OrganizationService; |
49 | |
|
50 | 0 | public class OrgOrgRelationAssembler implements Assembler<Data, OrgorgRelationHelper>{ |
51 | 0 | final Logger LOG = Logger.getLogger(OrgOrgRelationAssembler.class); |
52 | |
public static final String ORGORG_PATH = "orgOrgRelationInfo"; |
53 | |
private OrganizationService orgService; |
54 | |
private Metadata metadata; |
55 | 0 | private DataModel orgOrgModel = new DataModel(); |
56 | |
|
57 | |
public void setMetaData(Metadata metadata){ |
58 | 0 | this.metadata=metadata; |
59 | |
|
60 | 0 | } |
61 | |
@Override |
62 | |
public Data assemble(OrgorgRelationHelper input) throws AssemblyException { |
63 | |
|
64 | 0 | return null; |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public OrgorgRelationHelper disassemble(Data input) throws AssemblyException { |
69 | 0 | return null; |
70 | |
|
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public Data get(String id) throws AssemblyException { |
75 | |
|
76 | 0 | List<OrgOrgRelationInfo> relations = new ArrayList<OrgOrgRelationInfo>(); |
77 | 0 | List<OrgOrgRelationInfo> parentRelations = new ArrayList<OrgOrgRelationInfo>(); |
78 | 0 | Data orgOrgRelationMap = null; |
79 | |
try{ |
80 | 0 | relations = orgService.getOrgOrgRelationsByOrg(id); |
81 | 0 | parentRelations = orgService.getOrgOrgRelationsByRelatedOrg(id); |
82 | 0 | orgOrgRelationMap = buildOrgOrgRelationDataMap(relations,parentRelations); |
83 | |
|
84 | |
} |
85 | 0 | catch(DoesNotExistException dnee){ |
86 | 0 | return null; |
87 | |
|
88 | |
} |
89 | 0 | catch(Exception e){ |
90 | 0 | LOG.error(e); |
91 | 0 | throw(new AssemblyException()); |
92 | 0 | } |
93 | 0 | return orgOrgRelationMap; |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public Metadata getMetadata(String idType, String id, String type, String state) throws AssemblyException { |
98 | |
|
99 | 0 | return null; |
100 | |
} |
101 | |
|
102 | |
public void setOrgService(OrganizationService service){ |
103 | 0 | orgService = service; |
104 | 0 | } |
105 | |
|
106 | |
@Override |
107 | |
public SaveResult<Data> save(Data input) throws AssemblyException { |
108 | 0 | addOrgOrgRelations(input); |
109 | 0 | SaveResult<Data> result = new SaveResult<Data>(); |
110 | 0 | List<ValidationResultInfo> validationResults = validate(input); |
111 | 0 | result.setValue(input); |
112 | |
|
113 | 0 | return result; |
114 | |
} |
115 | |
|
116 | |
@Override |
117 | |
public List<ValidationResultInfo> validate(Data input) throws AssemblyException { |
118 | |
|
119 | 0 | return null; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
private OrgOrgRelationInfo buildOrgOrgRelationInfo(OrgorgRelationHelper orgorgRelationHelper){ |
124 | 0 | OrgOrgRelationInfo orgOrgRelationInfo = new OrgOrgRelationInfo(); |
125 | |
|
126 | 0 | if(orgorgRelationHelper.getOrgOrgRelationTypeKey().startsWith("REV_")){ |
127 | 0 | orgOrgRelationInfo.setOrgId(orgorgRelationHelper.getRelatedOrgId()); |
128 | 0 | orgOrgRelationInfo.setRelatedOrgId(orgorgRelationHelper.getOrgId()); |
129 | 0 | orgOrgRelationInfo.setType(orgorgRelationHelper.getOrgOrgRelationTypeKey().substring(4)); |
130 | |
} |
131 | |
else{ |
132 | 0 | orgOrgRelationInfo.setOrgId(orgorgRelationHelper.getOrgId()); |
133 | 0 | orgOrgRelationInfo.setRelatedOrgId(orgorgRelationHelper.getRelatedOrgId()); |
134 | 0 | orgOrgRelationInfo.setType(orgorgRelationHelper.getOrgOrgRelationTypeKey()); |
135 | |
} |
136 | |
|
137 | |
|
138 | 0 | orgOrgRelationInfo.setEffectiveDate(orgorgRelationHelper.getEffectiveDate()); |
139 | 0 | orgOrgRelationInfo.setExpirationDate(orgorgRelationHelper.getExpirationDate()); |
140 | 0 | if (isModified(orgorgRelationHelper.getData())) { |
141 | 0 | if (isUpdated(orgorgRelationHelper.getData())) { |
142 | 0 | MetaInfo metaInfo = new MetaInfo(); |
143 | 0 | orgOrgRelationInfo.setMetaInfo(metaInfo); |
144 | 0 | } |
145 | 0 | else if (isDeleted(orgorgRelationHelper.getData())) { |
146 | |
} |
147 | 0 | else if (isCreated(orgorgRelationHelper.getData())) { |
148 | |
} |
149 | |
} |
150 | 0 | if(orgOrgRelationInfo.getMetaInfo()!=null){ |
151 | 0 | orgOrgRelationInfo.getMetaInfo().setVersionInd(getVersionIndicator(orgorgRelationHelper.getData())); |
152 | |
} |
153 | |
|
154 | 0 | return orgOrgRelationInfo; |
155 | |
} |
156 | |
|
157 | |
private void addOrgOrgRelations(Data input) throws AssemblyException{ |
158 | 0 | if (input == null) { |
159 | 0 | return; |
160 | |
} |
161 | 0 | DataModelDefinition def = new DataModelDefinition(metadata); |
162 | 0 | orgOrgModel.setDefinition(def); |
163 | 0 | QueryPath metaPath = QueryPath.concat(null, ORGORG_PATH); |
164 | 0 | Metadata orgOrgMeta =orgOrgModel.getMetadata(metaPath); |
165 | 0 | for (Iterator<Property> propIter = ((Data)input.get("orgOrgRelationInfo")).iterator();propIter.hasNext();) { |
166 | 0 | Property p = propIter.next(); |
167 | 0 | OrgorgRelationHelper orgOrgRelation= OrgorgRelationHelper.wrap((Data)p.getValue()); |
168 | 0 | if (isUpdated(orgOrgRelation.getData())) { |
169 | 0 | if (orgOrgMeta.isCanEdit()) { |
170 | 0 | OrgOrgRelationInfo orgOrgRelationInfo = buildOrgOrgRelationInfo(orgOrgRelation); |
171 | 0 | orgOrgRelationInfo.setId(orgOrgRelation.getId()); |
172 | |
try { |
173 | 0 | OrgOrgRelationInfo result = orgService.updateOrgOrgRelation(orgOrgRelationInfo.getId(), orgOrgRelationInfo); |
174 | 0 | addVersionIndicator(orgOrgRelation.getData(), OrgOrgRelationInfo.class.getName(), result.getId(), result.getMetaInfo().getVersionInd()); |
175 | 0 | } catch (Exception e) { |
176 | 0 | LOG.error(e); |
177 | 0 | throw (new AssemblyException()); |
178 | 0 | } |
179 | |
} |
180 | 0 | AssemblerUtils.setUpdated(orgOrgRelation.getData(), false); |
181 | |
} |
182 | 0 | else if(isDeleted(orgOrgRelation.getData())&&orgOrgRelation.getId()!=null){ |
183 | |
|
184 | |
|
185 | |
try{ |
186 | 0 | if(orgOrgRelation.getId()!=null){ |
187 | 0 | StatusInfo result = orgService.removeOrgOrgRelation(orgOrgRelation.getId()); |
188 | 0 | propIter.remove(); |
189 | |
} |
190 | |
} |
191 | 0 | catch(Exception e ){ |
192 | 0 | LOG.error(e); |
193 | 0 | throw(new AssemblyException()); |
194 | 0 | } |
195 | |
} |
196 | 0 | else if(isCreated(orgOrgRelation.getData())){ |
197 | |
|
198 | 0 | orgOrgRelation.setOrgId((OrgHelper.wrap((Data)input.get("orgInfo")).getId())); |
199 | |
|
200 | 0 | OrgOrgRelationInfo orgOrgRelationInfo = buildOrgOrgRelationInfo(orgOrgRelation); |
201 | |
try{ |
202 | 0 | OrgOrgRelationInfo result = orgService.createOrgOrgRelation(orgOrgRelationInfo.getOrgId(), orgOrgRelationInfo.getRelatedOrgId(), orgOrgRelationInfo.getType(), orgOrgRelationInfo); |
203 | 0 | orgOrgRelation.setId(result.getId()); |
204 | 0 | addVersionIndicator(orgOrgRelation.getData(),OrgOrgRelationInfo.class.getName(),result.getId(),result.getMetaInfo().getVersionInd()); |
205 | |
} |
206 | 0 | catch(Exception e ){ |
207 | 0 | LOG.error(e); |
208 | 0 | throw(new AssemblyException()); |
209 | 0 | } |
210 | 0 | AssemblerUtils.setCreated(orgOrgRelation.getData(), false); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | 0 | } |
217 | |
|
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
private Data buildOrgOrgRelationDataMap(List<OrgOrgRelationInfo> relations,List<OrgOrgRelationInfo> parentRelations) throws AssemblyException{ |
222 | |
|
223 | 0 | Data orgOrgRelations = new Data(); |
224 | |
String relationTypeTranslation; |
225 | 0 | DataModel orgProposalModel = new DataModel(); |
226 | 0 | DataModelDefinition def = new DataModelDefinition(metadata); |
227 | 0 | orgProposalModel.setDefinition(def); |
228 | 0 | int count = 0; |
229 | |
try{ |
230 | 0 | for(OrgOrgRelationInfo relation:relations){ |
231 | 0 | Data relationMap = new Data(); |
232 | 0 | OrgorgRelationHelper orgOrgRelation= OrgorgRelationHelper.wrap(relationMap); |
233 | 0 | orgOrgRelation.setId(relation.getId()); |
234 | 0 | orgOrgRelation.setOrgId(relation.getOrgId()); |
235 | 0 | orgOrgRelation.setRelatedOrgId(relation.getRelatedOrgId()); |
236 | |
|
237 | |
|
238 | |
|
239 | 0 | QueryPath metaPath = QueryPath.concat(null, ORGORG_PATH); |
240 | 0 | Metadata orgOrgMeta =orgProposalModel.getMetadata(metaPath); |
241 | 0 | if(!orgOrgMeta.isCanEdit()){ |
242 | 0 | relationTypeTranslation =orgService.getOrgOrgRelationType(relation.getType()).getName(); |
243 | 0 | orgOrgRelation.setOrgOrgRelationTypeKey(relationTypeTranslation); |
244 | |
} |
245 | |
else{ |
246 | 0 | orgOrgRelation.setOrgOrgRelationTypeKey(relation.getType()); |
247 | |
} |
248 | 0 | orgOrgRelation.setEffectiveDate(relation.getEffectiveDate()); |
249 | 0 | orgOrgRelation.setExpirationDate(relation.getExpirationDate()); |
250 | |
|
251 | 0 | orgOrgRelations.set(count, orgOrgRelation.getData()); |
252 | 0 | addVersionIndicator(orgOrgRelation.getData(),OrgOrgRelationInfo.class.getName(),relation.getId(),relation.getMetaInfo().getVersionInd()); |
253 | 0 | count= count+1; |
254 | 0 | } |
255 | |
|
256 | 0 | for(OrgOrgRelationInfo relation:parentRelations){ |
257 | 0 | Data relationMap = new Data(); |
258 | 0 | OrgorgRelationHelper orgOrgRelation= OrgorgRelationHelper.wrap(relationMap); |
259 | 0 | orgOrgRelation.setId(relation.getId()); |
260 | 0 | orgOrgRelation.setRelatedOrgId(relation.getOrgId()); |
261 | 0 | orgOrgRelation.setOrgId(relation.getRelatedOrgId()); |
262 | |
|
263 | |
|
264 | |
|
265 | 0 | QueryPath metaPath = QueryPath.concat(null, ORGORG_PATH); |
266 | 0 | Metadata orgOrgMeta =orgProposalModel.getMetadata(metaPath); |
267 | 0 | if(!orgOrgMeta.isCanEdit()){ |
268 | 0 | relationTypeTranslation =orgService.getOrgOrgRelationType(relation.getType()).getRevName(); |
269 | 0 | orgOrgRelation.setOrgOrgRelationTypeKey(relationTypeTranslation); |
270 | |
} |
271 | |
else{ |
272 | 0 | orgOrgRelation.setOrgOrgRelationTypeKey("REV_" +relation.getType()); |
273 | |
} |
274 | 0 | orgOrgRelation.setEffectiveDate(relation.getEffectiveDate()); |
275 | 0 | orgOrgRelation.setExpirationDate(relation.getExpirationDate()); |
276 | |
|
277 | 0 | orgOrgRelations.set(count, orgOrgRelation.getData()); |
278 | 0 | addVersionIndicator(orgOrgRelation.getData(),OrgOrgRelationInfo.class.getName(),relation.getId(),relation.getMetaInfo().getVersionInd()); |
279 | 0 | count= count+1; |
280 | 0 | } |
281 | |
|
282 | |
|
283 | |
} |
284 | 0 | catch(Exception e){ |
285 | 0 | LOG.error(e); |
286 | 0 | throw(new AssemblyException()); |
287 | 0 | } |
288 | 0 | return orgOrgRelations; |
289 | |
} |
290 | |
|
291 | |
@Override |
292 | |
public Metadata getDefaultMetadata() throws AssemblyException { |
293 | |
|
294 | 0 | return null; |
295 | |
} |
296 | |
|
297 | |
} |