View Javadoc

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   * CreateBibAction is the action class for BatchIngest(Staff upload screen) which creates a bibliographic
23   * record in Docstore.
24   */
25  public class CreateBibAction implements Action {
26      private DocstoreHelperService docstoreHelperService;
27      private DiscoveryHelperService discoveryHelperService;
28  
29      /**
30       *  This method takes the initial request when creating the BibAction.
31       * @param executionEnvironment
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       *   This method simulate the executionEnvironment.
63       * @param executionEnvironment
64       */
65  
66      @Override
67      public void executeSimulation(ExecutionEnvironment executionEnvironment) {
68          execute(executionEnvironment);
69      }
70  
71      /**
72       *      This method gets the uuid based on docType
73       * @param response
74       * @param docType
75       * @return  uuid
76       */
77      private String getUUID(Response response, String docType) {
78          List<ResponseDocument> documents = response.getDocuments();
79          return getUUID(documents, docType);
80      }
81  
82      /**
83     *  This method gets the uuid based on List of documents and docType.
84       * @param documents
85       * @param docType
86       * @return   uuid
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      *     Gets the docstoreHelperService attribute.
102      * @return  Returns the docstoreHelperService.
103      */
104     public DocstoreHelperService getDocstoreHelperService() {
105         return docstoreHelperService;
106     }
107 
108     /**
109      * Sets the docstoreHelperService attribute value.
110      * @param docstoreHelperService .The docstoreHelperService to set.
111      */
112     public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
113         this.docstoreHelperService = docstoreHelperService;
114     }
115 
116     /**
117      *   Sets the discoveryHelperService attribute value.
118      * @param discoveryHelperService .The discoveryHelperService to set.
119      */
120     public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
121         this.discoveryHelperService = discoveryHelperService;
122     }
123 
124     /**
125      * Gets the discoveryHelperService attribute.
126      * @return  Returns discoveryHelperService.
127      */
128     public DiscoveryHelperService getDiscoveryHelperService() {
129         return discoveryHelperService;
130     }
131 }