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