1 package org.kuali.ole.ingest.action;
2
3 import org.apache.log4j.Logger;
4 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
5 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
6 import org.kuali.ole.editor.service.MockDiscoveryHelperService;
7 import org.kuali.ole.editor.service.MockDocstoreHelperService;
8 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
9 import org.kuali.rice.krms.framework.engine.Action;
10
11 import java.util.Iterator;
12 import java.util.List;
13
14 /**
15 * Created with IntelliJ IDEA.
16 * User: ?
17 * Date: 12/18/12
18 * Time: 5:43 PM
19 * To change this template use File | Settings | File Templates.
20 */
21 public class MockCreateBibAction implements Action {
22 private MockDocstoreHelperService docstoreHelperService;
23 private MockDiscoveryHelperService discoveryHelperService;
24
25 private static final Logger LOG = Logger.getLogger(MockCreateBibAction.class);
26
27 /**
28 * This method takes the initial request when creating the BibAction.
29 * @param executionEnvironment
30 */
31 @Override
32 public void execute(ExecutionEnvironment executionEnvironment) {
33 LOG.info(" Called CreateBibAction ---------------> ");
34 /*DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
35 List<ProfileAttributeBo> profileAttributes = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
36 BibliographicRecord bibliographicRecord = (BibliographicRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
37
38 DocstoreHelperService docstoreHelperService = new DocstoreHelperService();
39 try {
40 String responseFromDocstore = docstoreHelperService.persistNewToDocstoreForIngest(bibliographicRecord, profileAttributes);
41 ResponseHandler responseHander = new ResponseHandler();
42 Response response = responseHander.toObject(responseFromDocstore);
43 String instanceUUID = getUUID(response, OLEConstants.INSTANCE_DOC_TYPE);
44
45 List bibInfo = getDiscoveryHelperService().getBibInformationFromInsatnceId(instanceUUID);
46
47 OleBibRecord oleBibRecord = new OleBibRecord();
48 Map<String, List> bibFieldValues = (Map<String, List>) bibInfo.get(0);
49 oleBibRecord.setBibAssociatedFieldsValueMap(bibFieldValues);
50 oleBibRecord.setLinkedInstanceId(instanceUUID);
51 oleBibRecord.setBibUUID(getUUID(response, OLEConstants.BIB_DOC_TYPE));
52 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, oleBibRecord);
53 executionEnvironment.getEngineResults().setAttribute(OLEConstants.BIB_CREATION_FLAG, true);
54 } catch (Exception e) {
55 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, null);
56 }*/
57 }
58
59 /**
60 * This method simulate the executionEnvironment.
61 * @param executionEnvironment
62 */
63
64 @Override
65 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
66 execute(executionEnvironment);
67 }
68
69 /**
70 * This method gets the uuid based on docType
71 * @param response
72 * @param docType
73 * @return uuid
74 */
75 private String getUUID(Response response, String docType) {
76 List<ResponseDocument> documents = response.getDocuments();
77 return getUUID(documents, docType);
78 }
79
80 /**
81 * This method gets the uuid based on List of documents and docType.
82 * @param documents
83 * @param docType
84 * @return uuid
85 */
86 private String getUUID(List<ResponseDocument> documents, String docType) {
87 for (Iterator<ResponseDocument> iterator = documents.iterator(); iterator.hasNext(); ) {
88 ResponseDocument responseDocument = iterator.next();
89 if (responseDocument.getType().equals(docType)) {
90 return responseDocument.getUuid();
91 } else {
92 return getUUID(responseDocument.getLinkedDocuments(), docType);
93 }
94 }
95 return null;
96 }
97
98 public MockDocstoreHelperService getDocstoreHelperService() {
99 return docstoreHelperService;
100 }
101
102 public void setDocstoreHelperService(MockDocstoreHelperService docstoreHelperService) {
103 this.docstoreHelperService = docstoreHelperService;
104 }
105
106 public MockDiscoveryHelperService getDiscoveryHelperService() {
107 return discoveryHelperService;
108 }
109
110 public void setDiscoveryHelperService(MockDiscoveryHelperService discoveryHelperService) {
111 this.discoveryHelperService = discoveryHelperService;
112 }
113 }