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.common.client.DocstoreClientLocator;
7 import org.kuali.ole.docstore.common.document.Bib;
8 import org.kuali.ole.docstore.common.document.Holdings;
9 import org.kuali.ole.docstore.common.document.HoldingsTree;
10 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
11 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
12 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
13 import org.kuali.ole.docstore.common.document.content.instance.*;
14 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
15 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
16 import org.kuali.ole.factory.OverlayFileReaderFactory;
17 import org.kuali.ole.factory.OverlayOutputServiceFactory;
18 import org.kuali.ole.ingest.pojo.*;
19 import org.kuali.ole.pojo.OleOrderRecord;
20 import org.kuali.ole.pojo.edi.LineItemOrder;
21 import org.kuali.ole.service.OleOverlayActionService;
22 import org.kuali.ole.service.OverlayFileReaderService;
23 import org.kuali.ole.service.OverlayHelperService;
24 import org.kuali.ole.service.OverlayOutputService;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
27 import org.kuali.rice.krad.service.KRADServiceLocator;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32
33
34
35
36
37
38
39
40 public class OleOverlayActionServiceImpl implements OleOverlayActionService {
41
42 private static final Logger LOG = Logger.getLogger(OleOverlayActionServiceImpl.class);
43
44 private OverlayOutputService overlayDocstoreOutputService;
45 private OverlayOutputService overlayTransactionOutputService;
46 private OverlayOutputServiceFactory overlayOutputServiceFactory;
47 private OverlayFileReaderFactory overlayFileReaderFactory;
48 private BibliographicRecordHandler bibliographicRecordHandler;
49 private OverlayHelperService overlayHelperService;
50
51 private DocstoreClientLocator docstoreClientLocator;
52 private BibMarcRecordProcessor recordProcessor;
53 @Override
54 public String performOverlayLookupAction(String profileName, HashMap<String, Object> objects, String holdingId, OleOrderRecord oleOrderRecord) throws Exception {
55 String updatedResponse = null;
56 List<Item> itemList=new ArrayList<>();
57 HoldingsTree holdingsTree = getDocstoreClientLocator().getDocstoreClient().retrieveHoldingsTree(holdingId);
58 Holdings holdings=holdingsTree.getHoldings();
59 OleHoldings oleHoldings = new HoldingOlemlRecordProcessor().fromXML(holdingsTree.getHoldings().getContent());
60 List<org.kuali.ole.docstore.common.document.Item> items = holdingsTree.getItems();
61 for(org.kuali.ole.docstore.common.document.Item itemRecord:items){
62 itemList.add(new ItemOlemlRecordProcessor().fromXML(itemRecord.getContent()));
63 }
64 HashMap<String,Boolean> persistMap = new HashMap<String, Boolean>();
65 HashMap<String,String> actionMap = new HashMap<String, String>();
66 OverlayOutputService overlayOutputService = null;
67 overlayDocstoreOutputService = getOverlayDocstoreOutputService();
68 overlayTransactionOutputService = getOverlayTransactionOutputService();
69 actionMap.put("profileName",profileName);
70 List<OleOverlayAction> oleOverlayActions = (List<OleOverlayAction>) KRADServiceLocator.getBusinessObjectService().findMatching(OleOverlayAction.class,actionMap);
71 for(OleOverlayAction oleOverlayAction : oleOverlayActions){
72 OverlayFileReaderService overlayFileReaderService = null;
73 boolean isValid=true;
74 for(OleMappingField oleMappingField : oleOverlayAction.getOleMappingFields()){
75 overlayFileReaderService = getOverlayFileReaderFactory().getOverlayFileReaderService(oleMappingField.getFileFormat(),objects);
76 String incomingFieldValue=null;
77 if(oleMappingField.getIncomingField()!=null){
78 String incomingFieldFromProfile =oleMappingField.getIncomingField();
79 incomingFieldValue = overlayFileReaderService.getInputFieldValue(incomingFieldFromProfile);
80 if (LOG.isDebugEnabled()){
81 LOG.debug("###incomingFieldValue####"+incomingFieldValue);
82 }
83 isValid &= incomingFieldValue.equalsIgnoreCase(oleMappingField.getIncomingFieldValue());
84 }
85 }
86 if(isValid){
87 for(OleOutputFieldMapping oleOutputFieldMapping : oleOverlayAction.getOleOutputFieldMappings()){
88 String targetField = getTargetField(oleOutputFieldMapping);
89 String targetFieldValue = oleOutputFieldMapping.getFieldValue();
90 if(oleOutputFieldMapping.isLookUp() && overlayFileReaderService!=null){
91 StringBuffer finalValueSB = new StringBuffer();
92 List<String> dataFieldSubFieldKeyList = getDataFieldSubFieldKeyList(oleOutputFieldMapping.getFieldValue());
93 for(String dataFieldSubField : dataFieldSubFieldKeyList){
94 finalValueSB.append(overlayFileReaderService.getInputFieldValue(dataFieldSubField));
95 }
96 targetFieldValue = finalValueSB.toString();
97 }
98 performOutputMappingField(persistMap,oleOutputFieldMapping,holdings,oleHoldings,itemList,oleOrderRecord,targetField,targetFieldValue) ;
99 }
100 }
101 }
102 overlayOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT);
103 if(persistMap.get(OLEConstants.OVERLAY_OLE_HOLDINGS)!=null && persistMap.get(OLEConstants.OVERLAY_OLE_HOLDINGS)){
104 overlayOutputService.persist(oleHoldings);
105 }
106 if(persistMap.get(OLEConstants.OVERLAY_ITEM)!=null && persistMap.get(OLEConstants.OVERLAY_ITEM)){
107 overlayOutputService.persist(items);
108 }
109 return updatedResponse;
110 }
111 @Override
112 public void updateRecordExcludingGPF(HashMap<String, String> uuids, BibMarcRecord oldMarcRecord, BibMarcRecord newBibMarcRecord,LineItemOrder lineItemOrder, List<String> gpfFieldList, List<OverlayOption> overlayOptionList, String profileName)throws Exception{
113 String bibUUID = uuids.get(OLEConstants.BIBlIOGRAPHICUUID);
114 String holdingId = uuids.get(OLEConstants.OVERLAY_HOLDINGUUID);
115 String updatedResponse = null;
116 BibMarcRecord updateBibMarcRecord = null;
117 HashMap<String,Object> objects = new HashMap<String, Object>();
118 objects.put(OLEConstants.MRC,newBibMarcRecord);
119 objects.put(OLEConstants.EDI,lineItemOrder);
120 Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibUUID);
121 String bibContent=bib.getContent();
122 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
123 BibMarcRecords bibMarcRecords = recordProcessor.fromXML(bibContent);
124 BibMarcRecord bibMarcRecord=bibMarcRecords.getRecords().get(0);
125 updateBibMarcRecord = getOverlayHelperService().updateBibMarcRecordExcludingGPF(oldMarcRecord, newBibMarcRecord,gpfFieldList,overlayOptionList);
126 String updatedRecordContent = getBibMarcRecordHandler().generateXML(updateBibMarcRecord);
127 updatedResponse =performOverlayLookupAction(profileName, objects, holdingId, null);
128 bib.setContent(updatedRecordContent);
129
130 getDocstoreClientLocator().getDocstoreClient().updateBib(bib);
131
132 }
133 @Override
134 public void updateRecordIncludingGPF(HashMap<String,String> uuids,BibMarcRecord oldBibMarcRecord,BibMarcRecord newBibMarcRecord,LineItemOrder lineItemOrder,List<String> gpfFieldList,List<OverlayOption> overlayOptionList,String profileName)throws Exception{
135 String bibUUID = uuids.get(OLEConstants.BIBlIOGRAPHICUUID);
136 String holdingId = uuids.get(OLEConstants.OVERLAY_HOLDINGUUID);
137 String updatedResponse=null;
138 HashMap<String,Object> objects = new HashMap<String, Object>();
139 objects.put(OLEConstants.MRC,newBibMarcRecord);
140 objects.put(OLEConstants.EDI,lineItemOrder);
141 BibMarcRecord updateBibMarcRecord = null;
142 Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibUUID);
143 String bibContent= bib.getContent();
144 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
145 BibMarcRecords bibMarcRecords = recordProcessor.fromXML(bibContent);
146 BibMarcRecord bibMarcRecord = bibMarcRecords.getRecords().get(0);
147
148 updateBibMarcRecord = getOverlayHelperService().updateBibMarcRecordIncludingGPF(bibMarcRecord, newBibMarcRecord, gpfFieldList, overlayOptionList);
149 String updatedRecordContent = getBibMarcRecordHandler().generateXML(updateBibMarcRecord);
150 updatedResponse = performOverlayLookupAction(profileName,objects,holdingId,null);
151 bib.setContent(updatedRecordContent);
152
153 getDocstoreClientLocator().getDocstoreClient().updateBib(bib);
154
155
156 }
157
158 private List<String> getDataFieldSubFieldKeyList(String dataFieldSubfield){
159 String[] dataFieldSubfieldSplitArray = dataFieldSubfield.split("\\$");
160 List<String> dataFieldSubfieldKeyList = new ArrayList<String>();
161 StringBuffer dataFieldSubfieldKeySB = new StringBuffer();
162 String tagName = dataFieldSubfieldSplitArray[0];
163 for(int splitCount = 1;splitCount <dataFieldSubfieldSplitArray.length;splitCount++){
164 dataFieldSubfieldKeySB.append(tagName).append(OLEConstants.DELIMITER_DOLLAR).append(dataFieldSubfieldSplitArray[splitCount]);
165 dataFieldSubfieldKeyList.add(dataFieldSubfieldKeySB.toString());
166 dataFieldSubfieldKeySB = new StringBuffer();
167 }
168 return dataFieldSubfieldKeyList;
169 }
170 private String getTargetField(OleOutputFieldMapping outputFieldMapping){
171 String outputMappingTargeField = outputFieldMapping.getTargetField();
172 String[] splittedOutputMappingTargeFieldArray = outputMappingTargeField.split("\\.");
173 int splittedOutputMappingTargeFieldArrayLength = splittedOutputMappingTargeFieldArray.length;
174 outputFieldMapping.setTargetObject(splittedOutputMappingTargeFieldArrayLength>1?splittedOutputMappingTargeFieldArray[0]:null);
175 outputFieldMapping.setDetailedTargetObject(splittedOutputMappingTargeFieldArrayLength>2?splittedOutputMappingTargeFieldArray[splittedOutputMappingTargeFieldArrayLength-2]:null);
176 String targetField = splittedOutputMappingTargeFieldArrayLength>0?splittedOutputMappingTargeFieldArray[splittedOutputMappingTargeFieldArrayLength-1]:null;
177 return targetField;
178 }
179 private void performOutputMappingField(HashMap<String,Boolean> persistMap,OleOutputFieldMapping outputFieldMapping,Holdings holdings ,OleHoldings oleHoldings,List<Item> itemList, OleOrderRecord oleOrderRecord,String targetField,String targetFieldValue){
180 if(outputFieldMapping.getTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT)){
181 if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_INSTANCE)){
182 overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,holdings);
183 persistMap.put(OLEConstants.OVERLAY_INSTANCE,true);
184 }else if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_OLE_HOLDINGS)){
185 overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,oleHoldings);
186 persistMap.put(OLEConstants.OVERLAY_OLE_HOLDINGS,true);
187 }else if(outputFieldMapping.getDetailedTargetObject().equals(OLEConstants.OVERLAY_ITEM)){
188 for(Item item : itemList){
189 overlayDocstoreOutputService.setOutPutValue(targetField,targetFieldValue,item);
190 }
191 persistMap.put(OLEConstants.OVERLAY_ITEM,true);
192 }
193 }else if(outputFieldMapping.getTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_ORDERRECORD) && oleOrderRecord!=null){
194 if(outputFieldMapping.getDetailedTargetObject().equalsIgnoreCase(OLEConstants.OVERLAY_TX_RECORD)) {
195 overlayTransactionOutputService.setOutPutValue(targetField,targetFieldValue,oleOrderRecord.getOleTxRecord());
196 }
197 }
198 }
199
200
201 public OverlayOutputService getOverlayDocstoreOutputService() {
202 if(overlayDocstoreOutputService==null){
203 overlayDocstoreOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_DOCSTORE_OUTPUT_TARGET_OBJECT);
204 }
205 return overlayDocstoreOutputService;
206 }
207
208 public OverlayOutputService getOverlayTransactionOutputService() {
209 if(overlayTransactionOutputService == null){
210 overlayTransactionOutputService = getOverlayOutputServiceFactory().getOverlayOutputServiceFactory(OLEConstants.OVERLAY_ORDERRECORD);
211 }
212 return overlayTransactionOutputService;
213 }
214
215 public OverlayOutputServiceFactory getOverlayOutputServiceFactory() {
216 if(overlayOutputServiceFactory == null){
217 overlayOutputServiceFactory =GlobalResourceLoader.getService(OLEConstants.OVERLAY_OUTPUTSERVICE_FACTORY);
218 }
219 return overlayOutputServiceFactory;
220 }
221
222 public OverlayFileReaderFactory getOverlayFileReaderFactory() {
223 if(overlayFileReaderFactory == null){
224 overlayFileReaderFactory =GlobalResourceLoader.getService(OLEConstants.OVERLAY_FILE_READER_FACTORY);
225 }
226 return overlayFileReaderFactory;
227 }
228 private BibliographicRecordHandler getBibliographicRecordHandler(){
229 if(bibliographicRecordHandler == null){
230 bibliographicRecordHandler = new BibliographicRecordHandler();
231 }
232 return bibliographicRecordHandler;
233 }
234 public OverlayHelperService getOverlayHelperService() {
235 if(overlayHelperService == null){
236 overlayHelperService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_HELPER_SERVICE);
237 }
238 return overlayHelperService;
239 }
240
241 public DocstoreClientLocator getDocstoreClientLocator() {
242 if (null == docstoreClientLocator) {
243 return SpringContext.getBean(DocstoreClientLocator.class);
244 }
245 return docstoreClientLocator;
246 }
247
248 private BibMarcRecordProcessor getBibMarcRecordHandler(){
249 if(recordProcessor == null){
250 recordProcessor = new BibMarcRecordProcessor();
251 }
252 return recordProcessor;
253 }
254
255
256
257 }