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