1 package org.kuali.ole.ingest.action;
2
3 import org.kuali.ole.DataCarrierService;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
6 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
7 import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
8 import org.kuali.ole.editor.service.DiscoveryHelperService;
9 import org.kuali.ole.editor.service.DocstoreHelperService;
10 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
11 import org.kuali.ole.pojo.OleBibRecord;
12 import org.kuali.ole.pojo.bib.BibliographicRecord;
13 import org.kuali.ole.pojo.edi.LineItemOrder;
14 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
15 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
16 import org.kuali.rice.krms.framework.engine.Action;
17
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21
22
23
24
25
26 public class CreateBibAction implements Action {
27 private DocstoreHelperService docstoreHelperService;
28 private DiscoveryHelperService discoveryHelperService;
29
30
31
32
33
34 @Override
35 public void execute(ExecutionEnvironment executionEnvironment) {
36 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
37 List<ProfileAttributeBo> profileAttributes = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
38 BibliographicRecord bibliographicRecord = (BibliographicRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
39 LineItemOrder lineItemOrder = (LineItemOrder) dataCarrierService.getData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD);
40
41 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
42 try {
43 String responseFromDocstore = docstoreHelperService.persistNewToDocstoreForIngest(lineItemOrder,bibliographicRecord, profileAttributes);
44 ResponseHandler responseHander = new ResponseHandler();
45 Response response = responseHander.toObject(responseFromDocstore);
46 String instanceUUID = getUUID(response, OLEConstants.INSTANCE_DOC_TYPE);
47 if(null == instanceUUID){
48 throw new Exception("instance id returned from docstore is null");
49 }
50 List bibInfo = getBibInfo(instanceUUID);
51 if(bibInfo.isEmpty()){
52 Thread.sleep(2000);
53 bibInfo = getBibInfo(instanceUUID);
54 }
55 OleBibRecord oleBibRecord = new OleBibRecord();
56 Map<String, List> bibFieldValues = (Map<String, List>) bibInfo.get(0);
57 oleBibRecord.setBibAssociatedFieldsValueMap(bibFieldValues);
58 oleBibRecord.setLinkedInstanceId(instanceUUID);
59 oleBibRecord.setBibUUID(getUUID(response, OLEConstants.BIB_DOC_TYPE));
60 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, oleBibRecord);
61 executionEnvironment.getEngineResults().setAttribute(OLEConstants.BIB_CREATION_FLAG, true);
62 } catch (Exception e) {
63 e.printStackTrace();
64 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, null);
65 }
66 }
67
68 private List getBibInfo(String instanceUUID) {
69 return getDiscoveryHelperService().getBibInformationFromInsatnceId(instanceUUID);
70 }
71
72
73
74
75
76
77 @Override
78 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
79 execute(executionEnvironment);
80 }
81
82
83
84
85
86
87
88 private String getUUID(Response response, String docType) {
89 List<ResponseDocument> documents = response.getDocuments();
90 return getUUID(documents, docType);
91 }
92
93
94
95
96
97
98
99 private String getUUID(List<ResponseDocument> documents, String docType) {
100 for (Iterator<ResponseDocument> iterator = documents.iterator(); iterator.hasNext(); ) {
101 ResponseDocument responseDocument = iterator.next();
102 if (responseDocument.getType().equals(docType)) {
103 return responseDocument.getUuid();
104 } else {
105 return getUUID(responseDocument.getLinkedDocuments(), docType);
106 }
107 }
108 return null;
109 }
110
111
112
113
114
115 public DocstoreHelperService getDocstoreHelperService() {
116 return docstoreHelperService;
117 }
118
119
120
121
122
123 public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
124 this.docstoreHelperService = docstoreHelperService;
125 }
126
127
128
129
130
131 public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
132 this.discoveryHelperService = discoveryHelperService;
133 }
134
135
136
137
138
139 public DiscoveryHelperService getDiscoveryHelperService() {
140 return discoveryHelperService;
141 }
142 }