View Javadoc

1   package org.kuali.ole.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.BibliographicRecordHandler;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
7   import org.kuali.ole.describe.service.DocstoreHelperService;
8   import org.kuali.ole.factory.OverlayFileReaderFactory;
9   import org.kuali.ole.factory.OverlayOutputServiceFactory;
10  import org.kuali.ole.ingest.pojo.*;
11  import org.kuali.ole.pojo.OleOrderRecord;
12  import org.kuali.ole.pojo.bib.BibliographicRecord;
13  import org.kuali.ole.pojo.edi.LineItemOrder;
14  import org.kuali.ole.service.OleOverlayActionService;
15  import org.kuali.ole.service.OverlayFileReaderService;
16  import org.kuali.ole.service.OverlayHelperService;
17  import org.kuali.ole.service.OverlayOutputService;
18  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: vivekb
29   * Date: 3/2/13
30   * Time: 4:52 PM
31   * To change this template use File | Settings | File Templates.
32   */
33  public class OleOverlayActionServiceImpl implements OleOverlayActionService {
34  
35      private static final Logger LOG = Logger.getLogger(OleOverlayActionServiceImpl.class);
36      private DocstoreHelperService docstoreHelperService;
37      private OverlayOutputService overlayDocstoreOutputService;
38      private OverlayOutputService overlayTransactionOutputService;
39      private OverlayOutputServiceFactory overlayOutputServiceFactory;
40      private   OverlayFileReaderFactory overlayFileReaderFactory;
41      private BibliographicRecordHandler bibliographicRecordHandler;
42      private OverlayHelperService overlayHelperService;
43      @Override
44      public String performOverlayLookupAction(String profileName, HashMap<String, Object> objects, String instanceUUID, OleOrderRecord oleOrderRecord) throws Exception {
45          String updatedResponse = null;
46          InstanceCollection oldInstanceCollection = getDocstoreHelperService().getInstanceCollection(instanceUUID);
47          List<Instance> oldInstanceList = oldInstanceCollection.getInstance();
48          Instance instance = oldInstanceList.iterator().next();
49          OleHoldings oleHoldings = instance.getOleHoldings();
50          Items items = instance.getItems();
51          List<Item> itemList = items.getItem();
52          HashMap<String,Boolean> persistMap =  new HashMap<String, Boolean>();
53          HashMap<String,String> actionMap = new HashMap<String, String>();
54          OverlayOutputService overlayOutputService = null;
55          overlayDocstoreOutputService = getOverlayDocstoreOutputService();
56          overlayTransactionOutputService = getOverlayTransactionOutputService();
57          actionMap.put("profileName",profileName);
58          List<OleOverlayAction> oleOverlayActions = (List<OleOverlayAction>) KRADServiceLocator.getBusinessObjectService().findMatching(OleOverlayAction.class,actionMap);
59          for(OleOverlayAction oleOverlayAction : oleOverlayActions){
60              OverlayFileReaderService overlayFileReaderService = null;
61              boolean isValid=true;
62              for(OleMappingField oleMappingField : oleOverlayAction.getOleMappingFields()){
63                  overlayFileReaderService = getOverlayFileReaderFactory().getOverlayFileReaderService(oleMappingField.getFileFormat(),objects);
64                  String incomingFieldValue=null;
65                      if(oleMappingField.getIncomingField()!=null){
66                          String incomingFieldFromProfile =oleMappingField.getIncomingField();
67                          incomingFieldValue = overlayFileReaderService.getInputFieldValue(incomingFieldFromProfile);
68                          LOG.info("###incomingFieldValue####"+incomingFieldValue);
69                          isValid &= incomingFieldValue.equalsIgnoreCase(oleMappingField.getIncomingFieldValue());
70                      }
71              }
72              if(isValid){
73                  for(OleOutputFieldMapping oleOutputFieldMapping : oleOverlayAction.getOleOutputFieldMappings()){
74                      String targetField = getTargetField(oleOutputFieldMapping);
75                      String targetFieldValue = oleOutputFieldMapping.getFieldValue();
76                      if(oleOutputFieldMapping.isLookUp() && overlayFileReaderService!=null){
77                          StringBuffer finalValueSB = new StringBuffer();
78                          List<String> dataFieldSubFieldKeyList = getDataFieldSubFieldKeyList(oleOutputFieldMapping.getFieldValue());
79                          for(String dataFieldSubField : dataFieldSubFieldKeyList){
80                              finalValueSB.append(overlayFileReaderService.getInputFieldValue(dataFieldSubField));
81                          }
82                          targetFieldValue = finalValueSB.toString();
83                      }
84                      performOutputMappingField(persistMap,oleOutputFieldMapping,instance,oleHoldings,itemList,oleOrderRecord,targetField,targetFieldValue)  ;
85                  }
86              }
87          }
88          overlayOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT);
89          if(persistMap.get(OLEConstants.OVERLAY_INSTANCE) !=null && persistMap.get(OLEConstants.OVERLAY_INSTANCE)){
90              overlayOutputService.persist(oldInstanceCollection);
91          }
92          if(persistMap.get(OLEConstants.OVERLAY_OLE_HOLDINGS)!=null && persistMap.get(OLEConstants.OVERLAY_OLE_HOLDINGS)){
93              overlayOutputService.persist(oleHoldings);
94          }
95          if(persistMap.get(OLEConstants.OVERLAY_ITEM)!=null && persistMap.get(OLEConstants.OVERLAY_ITEM)){
96              overlayOutputService.persist(items);
97          }
98          return updatedResponse;
99      }
100     @Override
101     public String updateRecordExcludingGPF(HashMap<String, String> uuids, BibliographicRecord oldBibliographicRecord, BibliographicRecord newBibliographicRecord,LineItemOrder lineItemOrder, List<String> gpfFieldList, List<OverlayOption> overlayOptionList, String profileName)throws Exception{
102         String bibUUID = uuids.get(OLEConstants.BIBlIOGRAPHICUUID);
103         String instanceUUID = uuids.get(OLEConstants.OVERLAY_INSTANCEUUID);
104         String updatedResponse = null;
105         BibliographicRecord updateBibliographicRecord = null;
106         HashMap<String,Object> objects = new HashMap<String, Object>();
107         objects.put(OLEConstants.MRC,newBibliographicRecord);
108         objects.put(OLEConstants.EDI,lineItemOrder);
109         oldBibliographicRecord = getDocstoreHelperService().getBibliographicRecord(bibUUID);
110         updateBibliographicRecord = getOverlayHelperService().updateBibliographicRecordExcludingGPF(oldBibliographicRecord, newBibliographicRecord,gpfFieldList,overlayOptionList);
111         String updatedRecordContent = getBibliographicRecordHandler().generateXML(updateBibliographicRecord);
112         updatedResponse = getDocstoreHelperService().persistToDocstoreForEditor(updatedRecordContent, bibUUID, "marc");
113         updatedResponse =performOverlayLookupAction(profileName, objects, instanceUUID, null);
114         return updatedResponse;
115     }
116     @Override
117     public String updateRecordIncludingGPF(HashMap<String,String> uuids,BibliographicRecord oldBibliographicRecord,BibliographicRecord newBibliographicRecord,LineItemOrder lineItemOrder,List<String> gpfFieldList,List<OverlayOption> overlayOptionList,String profileName)throws Exception{
118         String bibUUID = uuids.get(OLEConstants.BIBlIOGRAPHICUUID);
119         String instanceUUID = uuids.get(OLEConstants.OVERLAY_INSTANCEUUID);
120         String updatedResponse = null;
121         HashMap<String,Object> objects = new HashMap<String, Object>();
122         objects.put(OLEConstants.MRC,newBibliographicRecord);
123         objects.put(OLEConstants.EDI,lineItemOrder);
124         BibliographicRecord updateBibliographicRecord = null;
125         oldBibliographicRecord = getDocstoreHelperService().getBibliographicRecord(bibUUID);
126         updateBibliographicRecord = getOverlayHelperService().updateBibliographicRecordIncludingGPF(oldBibliographicRecord, newBibliographicRecord, gpfFieldList, overlayOptionList);
127         String updatedRecordContent = getBibliographicRecordHandler().generateXML(updateBibliographicRecord);
128         updatedResponse = getDocstoreHelperService().persistToDocstoreForEditor(updatedRecordContent, bibUUID, "marc");
129         updatedResponse = performOverlayLookupAction(profileName,objects,instanceUUID,null);
130         return updatedResponse;
131     }
132 
133     private List<String> getDataFieldSubFieldKeyList(String dataFieldSubfield){
134         String[] dataFieldSubfieldSplitArray = dataFieldSubfield.split("\\$");
135         List<String> dataFieldSubfieldKeyList = new ArrayList<String>();
136         StringBuffer dataFieldSubfieldKeySB = new StringBuffer();
137         String tagName = dataFieldSubfieldSplitArray[0];
138         for(int splitCount = 1;splitCount <dataFieldSubfieldSplitArray.length;splitCount++){
139             dataFieldSubfieldKeySB.append(tagName).append(OLEConstants.DELIMITER_DOLLAR).append(dataFieldSubfieldSplitArray[splitCount]);
140             dataFieldSubfieldKeyList.add(dataFieldSubfieldKeySB.toString());
141             dataFieldSubfieldKeySB = new StringBuffer();
142         }
143         return dataFieldSubfieldKeyList;
144     }
145     private String getTargetField(OleOutputFieldMapping outputFieldMapping){
146         String outputMappingTargeField = outputFieldMapping.getTargetField();
147         String[] splittedOutputMappingTargeFieldArray = outputMappingTargeField.split("\\.");
148         int splittedOutputMappingTargeFieldArrayLength = splittedOutputMappingTargeFieldArray.length;
149         outputFieldMapping.setTargetObject(splittedOutputMappingTargeFieldArrayLength>1?splittedOutputMappingTargeFieldArray[0]:null);
150         outputFieldMapping.setDetailedTargetObject(splittedOutputMappingTargeFieldArrayLength>2?splittedOutputMappingTargeFieldArray[splittedOutputMappingTargeFieldArrayLength-2]:null);
151         String targetField = splittedOutputMappingTargeFieldArrayLength>0?splittedOutputMappingTargeFieldArray[splittedOutputMappingTargeFieldArrayLength-1]:null;
152         return targetField;
153     }
154     private void performOutputMappingField(HashMap<String,Boolean> persistMap,OleOutputFieldMapping outputFieldMapping,Instance instance ,OleHoldings oleHoldings,List<Item> itemList, OleOrderRecord oleOrderRecord,String targetField,String targetFieldValue){
155         if(outputFieldMapping.getTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT)){
156             if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_INSTANCE)){
157                 overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,instance);
158                 persistMap.put(OLEConstants.OVERLAY_INSTANCE,true);
159             }else if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_OLE_HOLDINGS)){
160                 overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,oleHoldings);
161                 persistMap.put(OLEConstants.OVERLAY_OLE_HOLDINGS,true);
162             }else if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_ITEM)){
163                 for(Item item : itemList){
164                     overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,item);
165                 }
166                 persistMap.put(OLEConstants.OVERLAY_ITEM,true);
167             }
168         }else if(outputFieldMapping.getTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_ORDERRECORD) && oleOrderRecord!=null){
169             if(outputFieldMapping.getDetailedTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_TX_RECORD))     {
170                 overlayTransactionOutputService.setOutPutValue(targetField,targetFieldValue,oleOrderRecord.getOleTxRecord());
171             }
172         }
173     }
174 
175     public DocstoreHelperService getDocstoreHelperService(){
176         if (docstoreHelperService == null) {
177             docstoreHelperService = GlobalResourceLoader.getService(OLEConstants.DOCSTORE_HELPER_SERVICE);
178         }
179         return docstoreHelperService;
180     }
181     public OverlayOutputService getOverlayDocstoreOutputService() {
182         if(overlayDocstoreOutputService==null){
183            overlayDocstoreOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT);
184         }
185         return overlayDocstoreOutputService;
186     }
187 
188     public OverlayOutputService getOverlayTransactionOutputService() {
189         if(overlayTransactionOutputService == null){
190            overlayTransactionOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_ORDERRECORD);
191         }
192         return overlayTransactionOutputService;
193     }
194 
195     public OverlayOutputServiceFactory getOverlayOutputServiceFactory() {
196         if(overlayOutputServiceFactory == null){
197             overlayOutputServiceFactory =GlobalResourceLoader.getService(OLEConstants.OVERLAY_OUTPUTSERVICE_FACTORY);
198         }
199         return overlayOutputServiceFactory;
200     }
201 
202     public OverlayFileReaderFactory getOverlayFileReaderFactory() {
203         if(overlayFileReaderFactory == null){
204             overlayFileReaderFactory =GlobalResourceLoader.getService(OLEConstants.OVERLAY_FILE_READER_FACTORY);
205         }
206         return overlayFileReaderFactory;
207     }
208     private BibliographicRecordHandler getBibliographicRecordHandler(){
209         if(bibliographicRecordHandler == null){
210             bibliographicRecordHandler = new BibliographicRecordHandler();
211         }
212         return bibliographicRecordHandler;
213     }
214     public OverlayHelperService getOverlayHelperService() {
215         if(overlayHelperService == null){
216             overlayHelperService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_HELPER_SERVICE);
217         }
218         return overlayHelperService;
219     }
220 
221 }