1 package org.kuali.ole.select.document;
2
3 import org.kuali.ole.OLEConstants;
4 import org.kuali.ole.alert.document.OleTransactionalDocumentBase;
5 import org.kuali.ole.select.bo.*;
6 import org.kuali.ole.service.OLEPlatformService;
7 import org.kuali.ole.vnd.businessobject.VendorDetail;
8 import org.kuali.rice.core.api.config.property.ConfigContext;
9 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
10 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
11 import org.kuali.rice.kew.api.document.search.DocumentSearchResult;
12 import org.kuali.rice.kew.api.exception.WorkflowException;
13 import org.kuali.rice.krad.document.Copyable;
14 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16 import org.kuali.rice.krad.util.GlobalVariables;
17 import org.springframework.util.StringUtils;
18
19 import java.lang.reflect.Method;
20 import java.sql.Timestamp;
21 import java.util.*;
22
23 import java.sql.Date;
24
25
26
27
28
29
30 public class OLEPlatformRecordDocument extends OleTransactionalDocumentBase implements Copyable {
31
32 private OLEPlatformService olePlatformService;
33
34 public OLEPlatformService getOlePlatformService() {
35 if (olePlatformService == null) {
36 olePlatformService = GlobalResourceLoader.getService(OLEConstants.PLATFORM_SERVICE);
37 }
38 return olePlatformService;
39 }
40
41 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OLEPlatformRecordDocument.class);
42
43 private String name;
44
45 private String olePlatformId ;
46
47 private String statusId;
48
49 private String statusName;
50
51 private Integer gokbId;
52
53 private String platformProviderName;
54
55 private Integer platformProviderId;
56
57 private String vendorLink;
58
59 private String vendorId;
60
61 private Integer vendorHeaderGeneratedIdentifier;
62
63 private Integer vendorDetailAssignedIdentifier;
64
65 private boolean active;
66
67 private String software;
68
69 private boolean branded;
70
71 private String brandingNote;
72
73 private boolean linkResolver;
74
75 private String linkResolverNote;
76
77 private boolean gokbFlag;
78
79 private boolean saveValidationFlag;
80
81 private boolean platformProviderFlag;
82
83 private VendorDetail vendorDetail;
84
85 private OLEPlatformStatus olePlatformStatus;
86
87 private String gokbPlatformStatus;
88
89 private List<OLEPlatformGeneralNote> generalNotes = new ArrayList<>();
90
91 private List<OLEPlatformAdminUrl> adminUrls = new ArrayList<>();
92
93 private List<OLEPlatformEventLog> eventLogs = new ArrayList<>();
94
95 private List<OLEPlatformEventLog> filterEventLogs = new ArrayList<>();
96
97 private List<OLEPlatformVariantTitle> variantTitles = new ArrayList<>();
98
99 private List<OLEEResourceRecordDocument> linkedEResources = new ArrayList<>();
100
101 private Timestamp gokbLastUpdatedDate;
102
103 public OLEPlatformRecordDocument() {
104 this.setActive(true);
105 this.setBranded(true);
106 this.setLinkResolver(true);
107 getGeneralNotes().add(new OLEPlatformGeneralNote());
108 getVariantTitles().add(new OLEPlatformVariantTitle());
109 }
110
111 public String getOlePlatformId() {
112 return olePlatformId;
113 }
114
115 public void setOlePlatformId(String olePlatformId) {
116 this.olePlatformId = olePlatformId;
117 }
118
119 public String getStatusId() {
120 return statusId;
121 }
122
123 public void setStatusId(String statusId) {
124 this.statusId = statusId;
125 }
126
127 public String getStatusName() {
128 if (olePlatformStatus != null) {
129 return olePlatformStatus.getPlatformStatusName();
130 } else if (getStatusId() != null) {
131 OLEPlatformStatus platformStatus = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OLEPlatformStatus.class, statusId);
132 if (platformStatus != null) {
133 return platformStatus.getPlatformStatusName();
134 }
135 }
136 return statusName;
137 }
138
139 public void setStatusName(String statusName) {
140 this.statusName = statusName;
141 }
142
143 public Integer getGokbId() {
144 return gokbId;
145 }
146
147 public void setGokbId(Integer gokbId) {
148 this.gokbId = gokbId;
149 }
150
151 public String getPlatformProviderName() {
152 if (vendorDetail != null) {
153 return vendorDetail.getVendorName();
154 }
155 return platformProviderName;
156 }
157
158 public void setPlatformProviderName(String platformProviderName) {
159 this.platformProviderName = platformProviderName;
160 }
161
162 public Integer getPlatformProviderId() {
163 return platformProviderId;
164 }
165
166 public void setPlatformProviderId(Integer platformProviderId) {
167 this.platformProviderId = platformProviderId;
168 }
169
170 public String getVendorLink() {
171 String oleurl = ConfigContext.getCurrentContextConfig().getProperty("ole.url");
172 String url = oleurl + "/ole-kr-krad/inquiry?methodToCall=start&dataObjectClassName=org.kuali.ole.vnd.businessobject.VendorDetail&vendorHeaderGeneratedIdentifier=" + vendorHeaderGeneratedIdentifier + "&vendorDetailAssignedIdentifier="
173 + vendorDetailAssignedIdentifier;
174 return url;
175 }
176
177 public void setVendorLink(String vendorLink) {
178 this.vendorLink = vendorLink;
179 }
180
181 public String getVendorId() {
182 return vendorId;
183 }
184
185 public void setVendorId(String vendorId) {
186 this.vendorId = vendorId;
187 }
188
189 public Integer getVendorHeaderGeneratedIdentifier() {
190 return vendorHeaderGeneratedIdentifier;
191 }
192
193 public void setVendorHeaderGeneratedIdentifier(Integer vendorHeaderGeneratedIdentifier) {
194 this.vendorHeaderGeneratedIdentifier = vendorHeaderGeneratedIdentifier;
195 }
196
197 public Integer getVendorDetailAssignedIdentifier() {
198 return vendorDetailAssignedIdentifier;
199 }
200
201 public void setVendorDetailAssignedIdentifier(Integer vendorDetailAssignedIdentifier) {
202 this.vendorDetailAssignedIdentifier = vendorDetailAssignedIdentifier;
203 }
204
205 public boolean isActive() {
206 return active;
207 }
208
209 public void setActive(boolean active) {
210 this.active = active;
211 }
212
213 public String getSoftware() {
214 return software;
215 }
216
217 public void setSoftware(String software) {
218 this.software = software;
219 }
220
221 public boolean isBranded() {
222 return branded;
223 }
224
225 public void setBranded(boolean branded) {
226 this.branded = branded;
227 }
228
229 public String getBrandingNote() {
230 return brandingNote;
231 }
232
233 public void setBrandingNote(String brandingNote) {
234 this.brandingNote = brandingNote;
235 }
236
237 public boolean isLinkResolver() {
238 return linkResolver;
239 }
240
241 public void setLinkResolver(boolean linkResolver) {
242 this.linkResolver = linkResolver;
243 }
244
245 public String getLinkResolverNote() {
246 return linkResolverNote;
247 }
248
249 public void setLinkResolverNote(String linkResolverNote) {
250 this.linkResolverNote = linkResolverNote;
251 }
252
253 public VendorDetail getVendorDetail() {
254 return vendorDetail;
255 }
256
257 public void setVendorDetail(VendorDetail vendorDetail) {
258 this.vendorDetail = vendorDetail;
259 }
260
261 public OLEPlatformStatus getOlePlatformStatus() {
262 return olePlatformStatus;
263 }
264
265 public void setOlePlatformStatus(OLEPlatformStatus olePlatformStatus) {
266 this.olePlatformStatus = olePlatformStatus;
267 }
268
269 public List<OLEPlatformGeneralNote> getGeneralNotes() {
270 return generalNotes;
271 }
272
273 public void setGeneralNotes(List<OLEPlatformGeneralNote> generalNotes) {
274 this.generalNotes = generalNotes;
275 }
276
277 public List<OLEPlatformAdminUrl> getAdminUrls() {
278 return adminUrls;
279 }
280
281 public void setAdminUrls(List<OLEPlatformAdminUrl> adminUrls) {
282 this.adminUrls = adminUrls;
283 }
284
285 public List<OLEPlatformEventLog> getEventLogs() {
286 return eventLogs;
287 }
288
289 public void setEventLogs(List<OLEPlatformEventLog> eventLogs) {
290 this.eventLogs = eventLogs;
291 }
292
293 public List<OLEPlatformEventLog> getFilterEventLogs() {
294 return filterEventLogs;
295 }
296
297 public void setFilterEventLogs(List<OLEPlatformEventLog> filterEventLogs) {
298 this.filterEventLogs = filterEventLogs;
299 }
300
301 public List<OLEEResourceRecordDocument> getLinkedEResources() {
302 return linkedEResources;
303 }
304
305 public void setLinkedEResources(List<OLEEResourceRecordDocument> linkedEResources) {
306 this.linkedEResources = linkedEResources;
307 }
308
309 public boolean isGokbFlag() {
310 if (org.apache.commons.lang3.StringUtils.isNotBlank(this.getOlePlatformId())) {
311 return getOlePlatformService().getGokbLinkFlag(this.olePlatformId);
312 }
313 return false;
314 }
315
316 public void setGokbFlag(boolean gokbFlag) {
317 this.gokbFlag = gokbFlag;
318 }
319
320 public String getName() {
321 return name;
322 }
323
324 public void setName(String name) {
325 this.name = name;
326 }
327
328 public boolean isSaveValidationFlag() {
329 return saveValidationFlag;
330 }
331
332 public void setSaveValidationFlag(boolean saveValidationFlag) {
333 this.saveValidationFlag = saveValidationFlag;
334 }
335
336 public List<OLEPlatformVariantTitle> getVariantTitles() {
337 return variantTitles;
338 }
339
340 public void setVariantTitles(List<OLEPlatformVariantTitle> variantTitles) {
341 this.variantTitles = variantTitles;
342 }
343
344 public void setResultDetails(DocumentSearchResult searchResult, List<OLESearchCondition> oleSearchEresources) {
345 List<DocumentAttribute> documentAttributes = searchResult.getDocumentAttributes();
346 for (DocumentAttribute docAttribute : documentAttributes) {
347 String name = docAttribute.getName();
348 if (OLEConstants.PLATFORM_RESULT_FIELDS.contains(name)) {
349 if (name.equals(OLEConstants.PLATFORM_NAME)) {
350 name = OLEConstants.PLATFORM_NAME;
351 }
352 Method getMethod;
353 try {
354 if (name.equals(OLEConstants.GOKB_ID)) {
355 getMethod = getSetMethod(OLEPlatformRecordDocument.class, name, new Class[]{Integer.class});
356 getMethod.invoke(this, Integer.parseInt(docAttribute.getValue().toString()));
357 } else {
358 getMethod = getSetMethod(OLEPlatformRecordDocument.class, name, new Class[]{String.class});
359 getMethod.invoke(this, docAttribute.getValue().toString());
360 }
361 } catch (Exception ex) {
362 ex.printStackTrace();
363 }
364 }
365 }
366 }
367
368 private Method getSetMethod(Class targetClass, String attr, Class[] objectAttributes) throws Exception {
369 Method method = targetClass.getMethod("set" + StringUtils.capitalize(attr), objectAttributes);
370 return method;
371 }
372
373 public String getGokbPlatformStatus() {
374 return gokbPlatformStatus;
375 }
376
377 public void setGokbPlatformStatus(String gokbPlatformStatus) {
378 this.gokbPlatformStatus = gokbPlatformStatus;
379 }
380
381 @Override
382 public void processAfterRetrieve() {
383 super.processAfterRetrieve();
384 LOG.debug("Inside OLEPlatformRecordDocument processAfterRetrieve");
385 if (this.getVendorHeaderGeneratedIdentifier() != null && this.getVendorDetailAssignedIdentifier() != null) {
386 this.setVendorId(this.getVendorHeaderGeneratedIdentifier().toString() + "-" + this.getVendorDetailAssignedIdentifier().toString());
387 }
388 if (this.getOlePlatformId() != null) {
389 Map<String, String> platformMap = new HashMap<String, String>();
390 platformMap.put(OLEConstants.PLATFORM_ID, this.getOlePlatformId());
391 List<OLEEResourceInstance> oleeResourceInstances = (List<OLEEResourceInstance>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEEResourceInstance.class, platformMap);
392 if (oleeResourceInstances != null && oleeResourceInstances.size() > 0) {
393 Map avoidingDuplicateMap = new HashMap<>();
394 for (OLEEResourceInstance oleeResourceInstance : oleeResourceInstances) {
395 if (oleeResourceInstance.getStatus() != null && oleeResourceInstance.getStatus().equals(OLEConstants.OleHoldings.ACTIVE)) {
396 oleeResourceInstance.getOleERSDocument().setActiveTitlesCount(oleeResourceInstance.getOleERSDocument().getActiveTitlesCount() + 1);
397 avoidingDuplicateMap.put(oleeResourceInstance.getOleERSIdentifier(), oleeResourceInstance.getOleERSDocument());
398 }
399 }
400 getLinkedEResources().addAll(avoidingDuplicateMap.values());
401 }
402 }
403 }
404
405
406 @Override
407 public boolean getAllowsCopy() {
408 boolean allowsCopy = true;
409 return allowsCopy;
410 }
411
412 @Override
413 public void toCopy() throws WorkflowException {
414 super.toCopy();
415 this.setOlePlatformId(null);
416 this.setGokbId(null);
417 this.setGokbFlag(false);
418 this.setLinkedEResources(new ArrayList<OLEEResourceRecordDocument>());
419 this.setObjectId(null);
420 this.setVersionNumber(null);
421 }
422
423 @Override
424 public void prepareForSave(KualiDocumentEvent event) {
425
426
427 super.prepareForSave(event);
428 Map platformMap = new HashMap<>();
429 platformMap.put(OLEConstants.OLE_PLATFORM_ID, this.getOlePlatformId());
430 OLEPlatformRecordDocument platformRecordDocument = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEPlatformRecordDocument.class, platformMap);
431 String status = null;
432 Integer gokbId = null;
433 if (platformRecordDocument != null) {
434 status = platformRecordDocument.getStatusName();
435 gokbId = platformRecordDocument.getGokbId();
436 }
437 OLEPlatformEventLog platformEventLog = new OLEPlatformEventLog();
438 if (status == null) {
439 platformEventLog.setSaveFlag(true);
440 platformEventLog.setLogTypeId("3");
441 platformEventLog.setEventReportedDate(new Date(System.currentTimeMillis()));
442 platformEventLog.setEventUserId(GlobalVariables.getUserSession().getPrincipalId());
443 platformEventLog.setEventNote(OLEConstants.OLEEResourceRecord.STATUS_IS + getStatusName());
444 this.getEventLogs().add(platformEventLog);
445 } else if (!status.equals(getStatusName())) {
446 platformEventLog.setSaveFlag(true);
447 platformEventLog.setLogTypeId("3");
448 platformEventLog.setEventReportedDate(new Date(System.currentTimeMillis()));
449 platformEventLog.setEventUserId(GlobalVariables.getUserSession().getPrincipalId());
450 platformEventLog.setEventNote(OLEConstants.OLEEResourceRecord.STATUS_FROM + status + OLEConstants.OLEEResourceRecord.STATUS_TO + getStatusName());
451 this.getEventLogs().add(platformEventLog);
452 }
453 if ((this.getGokbId() != null && gokbId == null) || (this.getGokbId() != null && gokbId != null && !gokbId.equals(this.getGokbId()))) {
454 platformEventLog = new OLEPlatformEventLog();
455 platformEventLog.setSaveFlag(true);
456 platformEventLog.setLogTypeId("3");
457 platformEventLog.setEventReportedDate(new Date(System.currentTimeMillis()));
458 platformEventLog.setEventUserId(GlobalVariables.getUserSession().getPrincipalId());
459 platformEventLog.setEventNote("Platform Record is linked with gokb " + this.getGokbId() + " manually.");
460 this.getEventLogs().add(platformEventLog);
461 }
462 }
463
464 public Timestamp getGokbLastUpdatedDate() {
465 return gokbLastUpdatedDate;
466 }
467
468 public void setGokbLastUpdatedDate(Timestamp gokbLastUpdatedDate) {
469 this.gokbLastUpdatedDate = gokbLastUpdatedDate;
470 }
471
472 public boolean isPlatformProviderFlag() {
473 return platformProviderFlag;
474 }
475
476 public void setPlatformProviderFlag(boolean platformProviderFlag) {
477 this.platformProviderFlag = platformProviderFlag;
478 }
479 }