1 package org.kuali.ole.ingest.action;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.describe.service.MockDiscoveryHelperService;
5 import org.kuali.ole.describe.service.MockDocstoreHelperService;
6 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
7 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
8
9 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
10 import org.kuali.rice.krms.framework.engine.Action;
11
12 import java.util.Iterator;
13 import java.util.List;
14
15 /**
16 * Created with IntelliJ IDEA.
17 * User: ?
18 * Date: 12/18/12
19 * Time: 5:43 PM
20 * To change this template use File | Settings | File Templates.
21 */
22 public class MockCreateBibAction implements Action {
23 private MockDocstoreHelperService docstoreHelperService;
24 private MockDiscoveryHelperService discoveryHelperService;
25
26 private static final Logger LOG = Logger.getLogger(MockCreateBibAction.class);
27
28 /**
29 * This method takes the initial request when creating the BibAction.
30 * @param executionEnvironment
31 */
32 @Override
33 public void execute(ExecutionEnvironment executionEnvironment) {
34 LOG.info(" Called CreateBibAction ---------------> ");
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 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, null);
57 }*/
58 }
59
60 /**
61 * This method simulate the executionEnvironment.
62 * @param executionEnvironment
63 */
64
65 @Override
66 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
67 execute(executionEnvironment);
68 }
69
70 /**
71 * This method gets the uuid based on docType
72 * @param response
73 * @param docType
74 * @return uuid
75 */
76 private String getUUID(Response response, String docType) {
77 List<ResponseDocument> documents = response.getDocuments();
78 return getUUID(documents, docType);
79 }
80
81 /**
82 * This method gets the uuid based on List of documents and docType.
83 * @param documents
84 * @param docType
85 * @return uuid
86 */
87 private String getUUID(List<ResponseDocument> documents, String docType) {
88 for (Iterator<ResponseDocument> iterator = documents.iterator(); iterator.hasNext(); ) {
89 ResponseDocument responseDocument = iterator.next();
90 if (responseDocument.getType().equals(docType)) {
91 return responseDocument.getUuid();
92 } else {
93 return getUUID(responseDocument.getLinkedDocuments(), docType);
94 }
95 }
96 return null;
97 }
98
99 public MockDocstoreHelperService getDocstoreHelperService() {
100 return docstoreHelperService;
101 }
102
103 public void setDocstoreHelperService(MockDocstoreHelperService docstoreHelperService) {
104 this.docstoreHelperService = docstoreHelperService;
105 }
106
107 public MockDiscoveryHelperService getDiscoveryHelperService() {
108 return discoveryHelperService;
109 }
110
111 public void setDiscoveryHelperService(MockDiscoveryHelperService discoveryHelperService) {
112 this.discoveryHelperService = discoveryHelperService;
113 }
114 }