View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
7   import org.kuali.ole.batch.ingest.OLEBatchGOKBImport;
8   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9   import org.kuali.ole.docstore.common.document.Bib;
10  import org.kuali.ole.docstore.common.document.Holdings;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.*;
12  import org.kuali.ole.docstore.common.document.content.instance.Coverage;
13  import org.kuali.ole.docstore.common.document.content.instance.Link;
14  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
15  import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
16  import org.kuali.ole.docstore.common.search.SearchParams;
17  import org.kuali.ole.docstore.common.search.SearchResponse;
18  import org.kuali.ole.docstore.common.search.SearchResult;
19  import org.kuali.ole.docstore.common.search.SearchResultField;
20  import org.kuali.ole.select.OleSelectConstant;
21  import org.kuali.ole.select.bo.*;
22  import org.kuali.ole.select.businessobject.OleCopy;
23  import org.kuali.ole.select.document.*;
24  import org.kuali.ole.select.form.OLEEResourceRecordForm;
25  import org.kuali.ole.select.gokb.*;
26  import org.kuali.ole.select.gokb.service.GokbLocalService;
27  import org.kuali.ole.select.gokb.service.GokbRdbmsService;
28  import org.kuali.ole.select.gokb.service.impl.GokbLocalServiceImpl;
29  import org.kuali.ole.select.gokb.service.impl.GokbRdbmsServiceImpl;
30  import org.kuali.ole.select.gokb.util.OleGokbXmlUtil;
31  import org.kuali.ole.service.impl.OLEEResourceSearchServiceImpl;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.vnd.businessobject.*;
34  import org.kuali.ole.vnd.document.service.impl.VendorServiceImpl;
35  import org.kuali.rice.krad.service.BusinessObjectService;
36  import org.kuali.rice.krad.service.KRADServiceLocator;
37  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
38  
39  import java.sql.Timestamp;
40  import java.util.*;
41  
42  import static org.kuali.ole.OLEConstants.*;
43  
44  /**
45   * Created by sambasivam on 24/10/14.
46   */
47  public class OLEEResourceHelperService {
48  
49      private VendorServiceImpl vendorService = null;
50      private OLEEResourceSearchServiceImpl oleeResourceSearchService = new OLEEResourceSearchServiceImpl();
51      private HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
52      private DocstoreClientLocator docstoreClientLocator;
53      private static final Logger LOG = Logger.getLogger(OLEEResourceHelperService.class);
54      private BusinessObjectService businessObjectService;
55  
56      private GokbRdbmsService gokbRdbmsService;
57      private GokbLocalService gokbLocalService;
58  
59      public GokbRdbmsService getGokbRdbmsService() {
60          if (null == gokbRdbmsService) {
61              return new GokbRdbmsServiceImpl();
62          }
63          return gokbRdbmsService;
64      }
65  
66      public GokbLocalService getGokbLocalService() {
67          if (null == gokbLocalService) {
68              return new GokbLocalServiceImpl();
69          }
70          return gokbLocalService;
71      }
72  
73      public DocstoreClientLocator getDocstoreClientLocator() {
74          if (null == docstoreClientLocator) {
75              return SpringContext.getBean(DocstoreClientLocator.class);
76          }
77          return docstoreClientLocator;
78      }
79  
80      public BusinessObjectService getBusinessObjectService() {
81          if (businessObjectService == null) {
82              businessObjectService = KRADServiceLocator.getBusinessObjectService();
83          }
84          return businessObjectService;
85      }
86  
87  
88      public VendorServiceImpl getVendorService() {
89          if (vendorService == null) {
90              vendorService = new VendorServiceImpl();
91          }
92  
93          return vendorService;
94      }
95  
96      /**
97       * This method is uses to update the vendor information in the contacts tab.
98       *
99       * @param oleeResourceRecordDocument
100      */
101     public void updateVendorInfo(OLEEResourceRecordDocument oleeResourceRecordDocument) {
102 
103         List<OLEEResourceContacts> oleeResourceContactses = new ArrayList<>();
104         Map<String, VendorDetail> vendorDetailMap = new HashMap<>();
105 
106         //current vendor of the eResource
107         addVendorDetail(vendorDetailMap, oleeResourceRecordDocument.getVendorId());
108 
109         //put the vendors in the vendor map for each po item
110         List<OLEEResourcePO> oleeResourceItemPOs = oleeResourceRecordDocument.geteRSPOItems();
111 
112         addPOItemVendors(vendorDetailMap, oleeResourceItemPOs);
113 
114         //put the vendors in the vendor map for each po eResource
115         List<OLEEResourcePO> oleeResourcePOs = oleeResourceRecordDocument.getOleERSPOItems();
116 
117         addPOItemVendors(vendorDetailMap, oleeResourcePOs);
118 
119         //put the vendors in the vendor map for each invoice
120         List<OLEEResourceInvoices> oleERSInvoices = oleeResourceRecordDocument.getOleERSInvoices();
121 
122         addInvoiceVendors(vendorDetailMap, oleERSInvoices);
123 
124         //put the vendors in vendor map for child eresources
125         List<OLELinkedEresource> oleLinkedEresources = oleeResourceRecordDocument.getOleLinkedEresources();
126 
127         addChildEResources(vendorDetailMap, oleLinkedEresources);
128 
129 
130         //Iterate the map and populate the values in the List<OLEEResourceContacts>
131         Set<String> vendorNumbers = vendorDetailMap.keySet();
132 
133         for (String vendorNumber : vendorNumbers) {
134 
135             VendorDetail vendorDetail1 = vendorDetailMap.get(vendorNumber);
136 
137             StringBuffer vendorNotes = new StringBuffer();
138             //TODO : add the notes, role, format to OLEEResourceContacts
139             for (VendorContact vendorContact : vendorDetail1.getVendorContacts()) {
140                 OLEEResourceContacts oleeResourceContacts = new OLEEResourceContacts();
141                 List<OLEPhoneNumber> olePhoneNumberList = new ArrayList<>();
142                 oleeResourceContacts.setOlePhoneNumbers(olePhoneNumberList);
143                 if (StringUtils.isNotBlank(vendorContact.getVendorLine2Address())) {
144                     oleeResourceContacts.setContact(vendorContact.getVendorLine1Address() + "-" + vendorContact.getVendorLine2Address());
145                 } else {
146                     oleeResourceContacts.setContact(vendorContact.getVendorLine1Address());
147                 }
148                 oleeResourceContacts.setEmail(vendorContact.getVendorContactEmailAddress());
149                 oleeResourceContacts.setOrganization(vendorContact.getVendorDetail().getVendorName());
150 //                oleeResourceContacts.setFormat();
151 //                oleeResourceContacts.setRole();
152                 oleeResourceContacts.setVendorHeaderGeneratedIdentifier(vendorContact.getVendorHeaderGeneratedIdentifier());
153                 oleeResourceContacts.setVendorDetailAssignedIdentifier(vendorContact.getVendorDetailAssignedIdentifier());
154 
155                 for (VendorContactPhoneNumber vendorContactPhoneNumber : vendorContact.getVendorContactPhoneNumbers()) {
156                     OLEPhoneNumber olePhoneNumber = new OLEPhoneNumber();
157                     olePhoneNumber.setPhoneNumber(vendorContactPhoneNumber.getVendorPhoneNumber());
158                     olePhoneNumber.setPhoneNumberType(vendorContactPhoneNumber.getVendorPhoneType().getVendorPhoneTypeDescription());
159                     olePhoneNumberList.add(olePhoneNumber);
160                 }
161                 if (oleeResourceContacts.getOlePhoneNumbers() != null && oleeResourceContacts.getOlePhoneNumbers().size() > 0) {
162                     oleeResourceContacts.setPhone(oleeResourceContacts.getOlePhoneNumbers().get(0).getPhoneNumber());
163                 }
164 
165                 if (oleeResourceContacts.getOlePhoneNumbers() != null && oleeResourceContacts.getOlePhoneNumbers().size() > 1) {
166                     oleeResourceContacts.setHasMorePhoneNo(true);
167                 }
168                 oleeResourceContacts.setOleERSIdentifier(oleeResourceRecordDocument.getOleERSIdentifier());
169                 if (vendorDetail1.isActiveIndicator()) {
170                     oleeResourceContactses.add(oleeResourceContacts);
171                 }
172 
173             }
174 
175         }
176         oleeResourceRecordDocument.setOleERSContacts(oleeResourceContactses);
177     }
178 
179     private void addVendorDetail(Map<String, VendorDetail> vendorDetailMap, String vendorId) {
180         VendorDetail vendorDetail = getVendorService().getByVendorNumber(vendorId);
181 
182         if (vendorDetail == null) {
183             return;
184         }
185         vendorDetailMap.put(vendorDetail.getVendorNumber(), vendorDetail);
186     }
187 
188     private void addChildEResources(Map<String, VendorDetail> vendorDetailMap, List<OLELinkedEresource> oleLinkedEresources) {
189         for (OLELinkedEresource oleLinkedEresource : oleLinkedEresources) {
190             if (oleLinkedEresource.getRelationShipType() != null && oleLinkedEresource.getRelationShipType().equals("child")) {
191                 addVendorDetail(vendorDetailMap, oleLinkedEresource.getOleeResourceRecordDocument().getVendorId());
192             }
193         }
194     }
195 
196     private void addInvoiceVendors(Map<String, VendorDetail> vendorDetailMap, List<OLEEResourceInvoices> oleERSInvoices) {
197         if (oleERSInvoices != null) {
198             for (OLEEResourceInvoices oleeResourceInvoices : oleERSInvoices) {
199                 Map map = new HashMap();
200                 map.put(OLEConstants.PURAP_DOC_IDENTIFIER, oleeResourceInvoices.getInvoiceId());
201                 List<OleInvoiceDocument> oleInvoiceDocuments = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
202                 if (oleInvoiceDocuments != null && oleInvoiceDocuments.size() > 0) {
203                     Map fieldValues = new HashMap();
204                     fieldValues.put(OleSelectConstant.VENDOR_HEADER_GENERATED_ID, oleInvoiceDocuments.get(0).getVendorId());
205                     List<VendorDetail> vendorDetails = new ArrayList<>(KRADServiceLocator.getBusinessObjectService().findMatching(VendorDetail.class, fieldValues));
206                     if (vendorDetails != null) {
207                         for (VendorDetail vd : vendorDetails) {
208                             vendorDetailMap.put(vd.getVendorNumber(), vd);
209                         }
210                     }
211                 }
212             }
213         }
214     }
215 
216     private void addPOItemVendors(Map<String, VendorDetail> vendorDetailMap, List<OLEEResourcePO> oleeResourcePOs) {
217         if (oleeResourcePOs != null) {
218             for (OLEEResourcePO oleeResourcePO : oleeResourcePOs) {
219                 Map map = new HashMap();
220                 map.put(OLEConstants.PURAP_DOC_IDENTIFIER, oleeResourcePO.getOlePOItemId());
221                 List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = (List<OlePurchaseOrderDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePurchaseOrderDocument.class, map);
222                 if (olePurchaseOrderDocuments != null && olePurchaseOrderDocuments.size() > 0) {
223                     Map fieldValues = new HashMap();
224                     fieldValues.put(OleSelectConstant.VENDOR_HEADER_GENERATED_ID, olePurchaseOrderDocuments.get(0).getVendorHeaderGeneratedIdentifier());
225                     List<VendorDetail> vendorDetails = new ArrayList<>(KRADServiceLocator.getBusinessObjectService().findMatching(VendorDetail.class, fieldValues));
226                     if (vendorDetails != null) {
227                         for (VendorDetail vd : vendorDetails) {
228                             vendorDetailMap.put(vd.getVendorNumber(), vd);
229                         }
230                     }
231                 }
232             }
233         }
234     }
235 
236     public void updateEHoldingsInEResource(Holdings holdings) {
237 
238         try {
239             holdings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(holdings.getId());
240         } catch (Exception e) {
241             LOG.error(e);
242         }
243         OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(holdings.getContent());
244         String eResourceIdentifier = oleHoldings.getEResourceId();
245         if (StringUtils.isEmpty(eResourceIdentifier)) {
246             return;
247         }
248         OLEEResourceInstance oleeResourceInstance = new OLEEResourceInstance();
249         oleeResourceInstance.setInstanceTitle(holdings.getBib().getTitle());
250         oleeResourceInstance.setOleERSIdentifier(eResourceIdentifier);
251         oleeResourceSearchService.getHoldingsField(oleeResourceInstance, oleHoldings);
252         oleeResourceInstance.setInstancePublisher(oleHoldings.getPublisher());
253         if (oleHoldings.getPlatform() != null) {
254             oleeResourceInstance.setPlatformId(oleHoldings.getPlatform().getPlatformName());
255         }
256 
257         StringBuffer urls = new StringBuffer();
258         for (Link link : oleHoldings.getLink()) {
259             urls.append(link.getUrl());
260             urls.append(",");
261         }
262         if (urls.toString().contains(",")) {
263             String url = urls.substring(0, urls.lastIndexOf(","));
264             oleeResourceInstance.setUrl(url);
265         }
266         if (oleHoldings.getGokbIdentifier() != null) {
267             oleeResourceInstance.setGokbId(oleHoldings.getGokbIdentifier());
268         }
269         oleeResourceInstance.setIsbn(holdings.getBib().getIsbn());
270         oleeResourceInstance.setStatus(oleHoldings.getAccessStatus());
271         oleeResourceInstance.setSubscriptionStatus(oleHoldings.getSubscriptionStatus());
272         oleeResourceInstance.setBibId(holdings.getBib().getId());
273         oleeResourceInstance.setInstanceId(holdings.getId());
274         oleeResourceInstance.setInstanceFlag("false");
275 
276         KRADServiceLocator.getBusinessObjectService().save(oleeResourceInstance);
277 
278         OleCopy oleCopy = new OleCopy();
279         oleCopy.setBibId(holdings.getBib().getId());
280         oleCopy.setOleERSIdentifier(eResourceIdentifier != null ? eResourceIdentifier : "");
281         oleCopy.setInstanceId(holdings.getId());
282 
283         KRADServiceLocator.getBusinessObjectService().save(oleCopy);
284 
285         this.insertOrUpdateGokbDataMapping(oleHoldings, false);
286 
287     }
288 
289 
290     /**
291      * This method returns Bib Import Profile based on which we can create the bib.
292      */
293     public OLEBatchProcessProfileBo getGOKBImportProfile(String batchProcessProfileId) {
294         Map<String, String> bibImportProfileMap = new HashMap<>();
295         bibImportProfileMap.put("batchProcessProfileId", batchProcessProfileId);
296         List<OLEBatchProcessProfileBo> oleBatchProcessProfileBoList = (List<OLEBatchProcessProfileBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEBatchProcessProfileBo.class, bibImportProfileMap);
297         if (oleBatchProcessProfileBoList != null && oleBatchProcessProfileBoList.size() > 0) {
298             return oleBatchProcessProfileBoList.get(0);
299         }
300         return null;
301     }
302 
303     public List<OLEGOKbPackage> searchGokbForPackages(List<OleGokbView> oleGokbViews, OLEEResourceRecordForm oleEResourceRecordForm) {
304         OLEEResourceRecordDocument oleeResourceRecordDocument = (OLEEResourceRecordDocument) oleEResourceRecordForm.getDocument();
305         List<OLEGOKbPackage> olegoKbPackages = new ArrayList<>();
306         Map<Integer, OLEGOKbPackage> packageMap = new HashMap();
307         Set<Integer> platformIds = new HashSet<>();
308 
309         for (OleGokbView oleGokbView : oleGokbViews) {
310             Integer packageId = oleGokbView.getPackageId();
311 
312             if (packageMap.containsKey(packageId)) {
313                 OLEGOKbPackage olegoKbPackage = packageMap.get(packageId);
314                 olegoKbPackage.setTiips((olegoKbPackage.getTiips() + 1));
315 
316                 platformIds.add(oleGokbView.getPlatformId());
317 
318                 if (platformIds.size() > 1) {
319                     olegoKbPackage.setMultiplePlatform(true);
320                 }
321 
322 
323             } else {
324                 OLEGOKbPackage olegoKbPackage = new OLEGOKbPackage();
325                 Map map = new HashMap();
326                 map.put("gokbPackageId", packageId);
327                 OleGokbPackage oleGokbPackage = getBusinessObjectService().findByPrimaryKey(OleGokbPackage.class, map);
328                 olegoKbPackage.setPackageId(packageId);
329                 olegoKbPackage.setDateCreated(oleGokbPackage.getDateCreated().toString());
330                 olegoKbPackage.setDateEntered(oleGokbPackage.getDateUpdated().toString());
331                 olegoKbPackage.setPackageName(oleGokbPackage.getPackageName());
332                 olegoKbPackage.setPrimaryPlatformProvider(oleGokbView.getOrgName());
333                 olegoKbPackage.setPrimaryPlatform(oleGokbView.getPlatformName());
334                 olegoKbPackage.setTiips(1);
335                 olegoKbPackage.setGokbStatus(oleGokbView.getPackageStatus());
336                 olegoKbPackages.add(olegoKbPackage);
337                 packageMap.put(packageId, olegoKbPackage);
338 
339                 platformIds.clear();
340 
341                 platformIds.add(oleGokbView.getPlatformId());
342                 olegoKbPackage.setMultiplePlatform(false);
343                 OLEGOKbPlatform olegoKbPlatform = new OLEGOKbPlatform();
344                 olegoKbPlatform.setGoKbTIPPList(getExceptedOrCurrentTippsByPlatform(oleGokbView.getPlatformId(), oleGokbView.getTitle(), oleGokbView.getPublisherId()));
345                 olegoKbPlatform.setPlatformName(oleGokbView.getPlatformName());
346                 olegoKbPlatform.setPlatformId(oleGokbView.getPlatformId());
347                 olegoKbPlatform.setPlatformProvider(oleGokbView.getOrgName());
348                 olegoKbPlatform.setStatus(oleGokbView.getPlatformStatus());
349                 olegoKbPlatform.setPlatformProviderId(oleGokbView.getOrgId());
350                 olegoKbPlatform.setSoftwarePlatform(oleGokbView.getSoftwarePlatform());
351                 List<OLEGOKbPlatform> olegoKbPlatforms = new ArrayList<>();
352                 olegoKbPlatforms.add(olegoKbPlatform);
353 
354                 oleeResourceRecordDocument.setGoKbPlatformList(olegoKbPlatforms);
355 
356             }
357         }
358 
359         return olegoKbPackages;
360     }
361 
362 
363     public List<OLEGOKbTIPP> getExceptedOrCurrentTippsByPlatform(Integer platformId, String title, Integer publisherId) {
364         List<OLEGOKbTIPP> olegoKbTIPPs = new ArrayList<>();
365 
366         Map map = new HashMap();
367         map.put("gokbPlatformId", platformId);
368 
369         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
370 
371         for (OleGokbTipp oleGokbTipp : oleGokbTipps) {
372             if (StringUtils.isNotEmpty(oleGokbTipp.getStatus()) && (oleGokbTipp.getStatus().equals("Current") || oleGokbTipp.getStatus().equals("Excepted"))) {
373                 OLEGOKbTIPP olegoKbTIPP = new OLEGOKbTIPP();
374                 olegoKbTIPP.setTitle(title);
375                 boolean isTippExists = verifyTippExistsInOle(oleGokbTipp.getGokbTippId());
376                 olegoKbTIPP.setTippExists(isTippExists);
377                 olegoKbTIPP.setPublisherId(publisherId);
378                 olegoKbTIPP.setDateCreated(oleGokbTipp.getDateCreated().toString());
379                 olegoKbTIPP.setDateUpdated(oleGokbTipp.getDateUpdated().toString());
380                 olegoKbTIPP.setGokbStatus(oleGokbTipp.getStatus());
381                 olegoKbTIPP.setUrl(oleGokbTipp.getPlatformHostUrl());
382                 if (oleGokbTipp.getEndDate() != null) {
383                     olegoKbTIPP.setEndDate(oleGokbTipp.getEndDate().toString());
384                 }
385                 olegoKbTIPP.setStartDate(oleGokbTipp.getStartdate().toString());
386                 olegoKbTIPP.setOleGokbTipp(oleGokbTipp);
387                 olegoKbTIPPs.add(olegoKbTIPP);
388             }
389         }
390 
391         return olegoKbTIPPs;
392 
393     }
394 
395     public List<OLEGOKbTIPP> getAllTippsByPlatform(Integer platformId, String title, Integer publisherId) {
396         List<OLEGOKbTIPP> olegoKbTIPPs = new ArrayList<>();
397 
398         Map map = new HashMap();
399         map.put("gokbPlatformId", platformId);
400 
401         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
402 
403         for (OleGokbTipp oleGokbTipp : oleGokbTipps) {
404             OLEGOKbTIPP olegoKbTIPP = new OLEGOKbTIPP();
405             olegoKbTIPP.setTitle(title);
406             boolean isTippExists = verifyTippExistsInOle(oleGokbTipp.getGokbTippId());
407             olegoKbTIPP.setTippExists(isTippExists);
408             olegoKbTIPP.setPublisherId(publisherId);
409             olegoKbTIPP.setDateCreated(oleGokbTipp.getDateCreated().toString());
410             olegoKbTIPP.setDateUpdated(oleGokbTipp.getDateUpdated().toString());
411             olegoKbTIPP.setGokbStatus(oleGokbTipp.getStatus());
412             olegoKbTIPP.setUrl(oleGokbTipp.getPlatformHostUrl());
413             olegoKbTIPP.setEndDate(oleGokbTipp.getEndDate().toString());
414             olegoKbTIPP.setStartDate(oleGokbTipp.getStartdate().toString());
415             olegoKbTIPP.setOleGokbTipp(oleGokbTipp);
416 
417 
418             olegoKbTIPPs.add(olegoKbTIPP);
419         }
420 
421         return olegoKbTIPPs;
422 
423     }
424 
425     public List<OLEGOKbTIPP> getTippsByPlatform(Integer platformId) {
426         List<OLEGOKbTIPP> olegoKbTIPPs = new ArrayList<>();
427 
428         Map map = new HashMap();
429         map.put("gokbPlatformId", platformId);
430 
431         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
432 
433         for (OleGokbTipp gokbTipp : oleGokbTipps) {
434             OLEGOKbTIPP olegoKbTIPP = new OLEGOKbTIPP();
435 
436             map.clear();
437             map.put("gokbTitleId", gokbTipp.getGokbTitleId());
438 
439             OleGokbTitle oleGokbTitle = getBusinessObjectService().findByPrimaryKey(OleGokbTitle.class, map);
440             boolean isTippExists = verifyTippExistsInOle(gokbTipp.getGokbTippId());
441             olegoKbTIPP.setTippExists(isTippExists);
442             olegoKbTIPP.setTitle(oleGokbTitle.getTitleName());
443             olegoKbTIPP.setPublisherId(oleGokbTitle.getPublisherId());
444             olegoKbTIPP.setDateCreated(gokbTipp.getDateCreated().toString());
445             olegoKbTIPP.setDateUpdated(gokbTipp.getDateUpdated().toString());
446             olegoKbTIPP.setGokbStatus(gokbTipp.getStatus());
447             olegoKbTIPP.setUrl(gokbTipp.getPlatformHostUrl());
448             olegoKbTIPP.setEndDate(gokbTipp.getEndDate().toString());
449             olegoKbTIPP.setStartDate(gokbTipp.getStartdate().toString());
450             olegoKbTIPP.setOleGokbTipp(gokbTipp);
451 
452             olegoKbTIPPs.add(olegoKbTIPP);
453         }
454 
455         return olegoKbTIPPs;
456 
457     }
458 
459     private boolean verifyTippExistsInOle(Integer gokbTippId) {
460         Map map = new HashMap();
461         map.put("gokbId", gokbTippId);
462         List<OLEEResourceInstance> oleeResourceInstances = (List<OLEEResourceInstance>) getBusinessObjectService().findMatching(OLEEResourceInstance.class, map);
463         if (oleeResourceInstances != null && oleeResourceInstances.size() > 0) {
464             return true;
465         }
466         return false;
467     }
468 
469     public List<OLEGOKbPlatform> getPlatformByPlackage(String packageId) {
470         List<OLEGOKbPlatform> olegoKbPlatforms = new ArrayList<>();
471 
472         Map map = new HashMap();
473         map.put("gokbPackageId", packageId);
474 
475         Map<Integer, OLEGOKbPlatform> platformMap = new HashMap();
476 
477         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
478 
479         for (OleGokbTipp oleGokbTipp : oleGokbTipps) {
480             Integer platformId = oleGokbTipp.getGokbPlatformId();
481             if (platformMap.containsKey(platformId)) {
482 
483                 OLEGOKbPlatform olegoKbPlatform = platformMap.get(platformId);
484                 olegoKbPlatform.setNoOfTiips((olegoKbPlatform.getNoOfTiips() + 1));
485             } else {
486 
487                 map = new HashMap();
488                 map.put("gokbPlatformId", platformId);
489                 OleGokbPlatform gokbPlatform = getBusinessObjectService().findByPrimaryKey(OleGokbPlatform.class, map);
490 
491                 OLEGOKbPlatform olegoKbPlatform = new OLEGOKbPlatform();
492                 olegoKbPlatform.setPlatformName(gokbPlatform.getPlatformName());
493                 map.clear();
494                 map.put("gokbOrganizationId", gokbPlatform.getPlatformProviderId());
495                 OleGokbOrganization oleGokbOrganization = getBusinessObjectService().findByPrimaryKey(OleGokbOrganization.class, map);
496                 olegoKbPlatform.setPlatformProvider(oleGokbOrganization.getOrganizationName());
497                 olegoKbPlatform.setPlatformProviderId(gokbPlatform.getPlatformProviderId());
498                 olegoKbPlatform.setSoftwarePlatform(gokbPlatform.getSoftwarePlatform());
499                 olegoKbPlatform.setNoOfTiips(1);
500                 olegoKbPlatform.setPlatformId(platformId);
501                 olegoKbPlatform.setStatus(gokbPlatform.getStatus());
502 
503                 olegoKbPlatforms.add(olegoKbPlatform);
504                 platformMap.put(platformId, olegoKbPlatform);
505             }
506         }
507         return olegoKbPlatforms;
508     }
509 
510 
511     public List<BibMarcRecord> buildBibMarcRecords(List<OLEGOKbPlatform> olegoKbPlatforms, String eResourceId) {
512 
513         List<BibMarcRecord> bibMarcRecords = new ArrayList<>();
514 
515         Map<Integer, BibMarcRecord> bibMarcRecordMap = new HashMap<>();
516 
517         for (OLEGOKbPlatform olegoKbPlatform : olegoKbPlatforms) {
518             String platformName = olegoKbPlatform.getPlatformName();
519 
520             String imprint = "";
521             String publisher = "";
522             for (OLEGOKbTIPP olegoKbTIPP : olegoKbPlatform.getGoKbTIPPList()) {
523 
524                 if (olegoKbTIPP.isSelect() && olegoKbTIPP.getOleGokbTipp() != null) {
525 
526                     Integer titleId = olegoKbTIPP.getOleGokbTipp().getGokbTitleId();
527 
528                     if (bibMarcRecordMap.containsKey(olegoKbTIPP.getOleGokbTipp().getGokbTitleId())) {
529                         BibMarcRecord bibMarcRecord = bibMarcRecordMap.get(titleId);
530                         DataField dataField = addEHoldingsFields(olegoKbTIPP, platformName, eResourceId, imprint, publisher);
531                         bibMarcRecord.getDataFields().add(dataField);
532                     } else {
533                         OleGokbTitle oleGokbTitle = getOleGokbTitle(titleId);
534                         BibMarcRecord bibMarcRecord = buildBibMarcRecord(oleGokbTitle);
535                         bibMarcRecords.add(bibMarcRecord);
536 
537                         imprint = String.valueOf(oleGokbTitle.getImprint());
538                         publisher = String.valueOf(oleGokbTitle.getPublisherId());
539 
540                         DataField dataField = addEHoldingsFields(olegoKbTIPP, platformName, eResourceId, imprint, publisher);
541                         bibMarcRecord.getDataFields().add(dataField);
542                         bibMarcRecordMap.put(titleId, bibMarcRecord);
543                     }
544                 }
545             }
546         }
547         return bibMarcRecords;
548     }
549 
550     private DataField addEHoldingsFields(OLEGOKbTIPP olegoKbTIPP, String platformName, String eResourceId, String imprint, String publisher) {
551 
552         OleGokbTipp goKbTIPP = olegoKbTIPP.getOleGokbTipp();
553         DataField dataField = getDataFieldForTipp(platformName, eResourceId, goKbTIPP, imprint, publisher);
554         return dataField;
555 
556     }
557 
558     public DataField getDataFieldForTipp(String platformName, String eResourceId, OleGokbTipp goKbTIPP, String imprint, String publisher) {
559         DataField dataField = new DataField();
560         dataField.setTag(OLEConstants.OLEBatchProcess.CONSTANT_DATAMAPPING_FOR_EHOLDINGS);
561 
562         List<SubField> subFields = new ArrayList<>();
563 
564         subFields.add(buildSubField("u", goKbTIPP.getPlatformHostUrl()));
565 
566         if (goKbTIPP.getEndDate() != null) {
567             subFields.add(buildSubField("d", goKbTIPP.getEndDate().toString()));
568         }
569         if (goKbTIPP.getEndIssue() != null) {
570             subFields.add(buildSubField("f", goKbTIPP.getEndIssue().toString()));
571         }
572         if (goKbTIPP.getEndVolume() != null) {
573             subFields.add(buildSubField("e", goKbTIPP.getEndVolume().toString()));
574         }
575         if (goKbTIPP.getStartdate() != null) {
576             subFields.add(buildSubField("a", goKbTIPP.getStartdate().toString()));
577         }
578         if (goKbTIPP.getStartIssue() != null) {
579             subFields.add(buildSubField("c", goKbTIPP.getStartIssue().toString()));
580         }
581         if (goKbTIPP.getStartVolume() != null) {
582             subFields.add(buildSubField("b", goKbTIPP.getStartVolume().toString()));
583         }
584         if (StringUtils.isNotEmpty(platformName)) {
585             subFields.add(buildSubField("g", platformName));
586         }
587         if (StringUtils.isNotEmpty(eResourceId)) {
588             subFields.add(buildSubField("h", eResourceId));
589         }
590         if (goKbTIPP.getGokbTippId() != null) {
591             subFields.add(buildSubField("i", goKbTIPP.getGokbTippId().toString()));
592         }
593         if (imprint != null) {
594             subFields.add(buildSubField("k", imprint));
595         }
596         if (publisher != null) {
597             subFields.add(buildSubField("j", publisher));
598         }
599 
600         dataField.setSubFields(subFields);
601         return dataField;
602     }
603 
604     private SubField buildSubField(String code, String value) {
605         SubField subField = new SubField();
606         subField.setCode(code);
607         subField.setValue(value);
608         return subField;
609     }
610 
611     private BibMarcRecord buildBibMarcRecord(OleGokbTitle oleGokbTitle) {
612 
613         BibMarcRecord bibMarcRecord = buildBibMarcRecordFromTitle(oleGokbTitle);
614 
615         return bibMarcRecord;
616     }
617 
618     public BibMarcRecord buildBibMarcRecordFromTitle(OleGokbTitle oleGokbTitle) {
619         BibMarcRecord bibMarcRecord = new BibMarcRecord();
620         bibMarcRecord.setLeader("#####nam#a22######a#4500");
621         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(" + OLEConstants.GOKBID + ")" + oleGokbTitle.getGokbTitleId(), "a"));
622         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(OCLC)" + oleGokbTitle.getOclcNumber(), "a"));
623         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(DOI)" + oleGokbTitle.getDoi(), "a"));
624         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(PublisherID)" + oleGokbTitle.getPublisherId(), "a"));
625         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(ProprietaryID)" + oleGokbTitle.getProprietaryId(), "a"));
626         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_035, "(SUNCAT)" + oleGokbTitle.getSuncat(), "a"));
627         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_245, oleGokbTitle.getTitleName(), "a"));
628         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_246, oleGokbTitle.getVariantName(), "a"));
629         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_500, oleGokbTitle.getPureQa(), "a"));
630         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_022, oleGokbTitle.getIssnOnline(), "a"));
631         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_022, oleGokbTitle.getIssnPrint(), "a"));
632         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_022, oleGokbTitle.getIssnL(), "l"));
633         bibMarcRecord.getDataFields().add(buildDataField(OLEConstants.TAG_010, oleGokbTitle.getLccn(), "a"));
634         return bibMarcRecord;
635     }
636 
637     private DataField buildDataField(String tag, String data, String subfieldCode) {
638         DataField dataField = new DataField();
639         dataField.setTag(tag);
640 
641         List<SubField> subFields = new ArrayList<>();
642         SubField subField = new SubField();
643         subField.setCode(subfieldCode);
644         subField.setValue(data);
645         subFields.add(subField);
646 
647         dataField.setSubFields(subFields);
648         return dataField;
649     }
650 
651 
652     public List<String> getPlatformProvidersForInstance(List<OLEEResourceInstance> oleERSInstances) {
653 
654         List<String> platformProviders = new ArrayList<>();
655         Set<String> platformIds = new HashSet<>();
656 
657         for (OLEEResourceInstance oleeResourceInstance : oleERSInstances) {
658             platformIds.add(oleeResourceInstance.getPlatformId());
659         }
660 
661         for (String platformId : platformIds) {
662             OLEPlatformRecordDocument olePlatformRecordDocument = getBusinessObjectService().findBySinglePrimaryKey(OLEPlatformRecordDocument.class, platformId);
663             if (olePlatformRecordDocument != null) {
664                 if (olePlatformRecordDocument.getVendorHeaderGeneratedIdentifier() != null && olePlatformRecordDocument.getVendorDetailAssignedIdentifier() != null) {
665                     Map vendorMap = new HashMap();
666                     vendorMap.put(OLEConstants.VENDOR_HEADER_GENERATED_ID, olePlatformRecordDocument.getVendorHeaderGeneratedIdentifier());
667                     vendorMap.put(OLEConstants.VENDOR_DETAILED_ASSIGNED_ID, olePlatformRecordDocument.getVendorDetailAssignedIdentifier());
668                     VendorDetail vendor = getBusinessObjectService().findByPrimaryKey(VendorDetail.class, vendorMap);
669                     if (vendor != null && StringUtils.isNotEmpty(vendor.getVendorName()))
670                         platformProviders.add(vendor.getVendorName());
671                 }
672             }
673         }
674 
675         return platformProviders;
676     }
677 
678 
679     public void importTipps(OLEBatchProcessProfileBo gokbImportProfile, List<BibMarcRecord> bibMarcRecords) {
680 
681         OLEBatchGOKBImport batchGOKBImport = new OLEBatchGOKBImport();
682         batchGOKBImport.setOleBatchProcessProfileBo(gokbImportProfile);
683 
684         try {
685             batchGOKBImport.processBatch(bibMarcRecords);
686         } catch (Exception e) {
687             e.printStackTrace();
688         }
689 
690     }
691 
692     public void createOrUpdateVendorAndPlatform(OLEEResourceRecordDocument oleeResourceRecordDocument) {
693         for (OLEGOKbPlatform oleGoKbPlatform : oleeResourceRecordDocument.getGoKbPlatformList()) {
694             //create/update vendor
695             if (oleGoKbPlatform.getPlatformProviderId() != null) {
696                 createOrUpdateVendor(oleGoKbPlatform.getPlatformProviderId(), "");
697             }
698             for (OLEGOKbTIPP oleGoKbTIPP : oleGoKbPlatform.getGoKbTIPPList()) {
699                 if (oleGoKbTIPP.getPublisherId() != null) {
700                     createOrUpdateVendor(oleGoKbTIPP.getPublisherId(), "");
701                 }
702             }
703             //update publisher in E-Resource
704             oleeResourceSearchService.updatePublisher(oleGoKbPlatform.getGoKbTIPPList(), oleeResourceRecordDocument);
705 
706             //create/update platform
707             if (oleGoKbPlatform.getPlatformId() != null) {
708                 Map gokbMap = new HashMap();
709                 gokbMap.put(OLEConstants.GOKB_ID, oleGoKbPlatform.getPlatformId());
710                 List<OLEPlatformRecordDocument> platformRecordDocumentList = (List<OLEPlatformRecordDocument>) getBusinessObjectService().findMatching(OLEPlatformRecordDocument.class, gokbMap);
711                 OLEPlatformRecordDocument olePlatformRecordDocument = null;
712                 if (platformRecordDocumentList != null && platformRecordDocumentList.size() > 0) {
713                     olePlatformRecordDocument = platformRecordDocumentList.get(0);
714                     oleeResourceSearchService.updatePlatform(olePlatformRecordDocument, oleGoKbPlatform.getPlatformName(), oleGoKbPlatform.getStatus(), oleGoKbPlatform.getSoftwarePlatform(), oleGoKbPlatform.getPlatformProviderId());
715                 } else {
716                     olePlatformRecordDocument = oleeResourceSearchService.createPlatform(oleGoKbPlatform.getPlatformName(), oleGoKbPlatform.getPlatformId(), oleGoKbPlatform.getSoftwarePlatform(), oleGoKbPlatform.getStatus(), oleGoKbPlatform.getPlatformProviderId());
717                 }
718                 updatePlatformVendorAssociation(olePlatformRecordDocument);
719             }
720         }
721     }
722 
723     public void createOrUpdateVendor(Integer vendorId, String logId) {
724         OleGokbOrganization oleGokbOrganization = getBusinessObjectService().findBySinglePrimaryKey(OleGokbOrganization.class, vendorId);
725         if (oleGokbOrganization != null && oleGokbOrganization.getGokbOrganizationId() != null) {
726             Map vendorMap = new HashMap();
727             vendorMap.put(OLEConstants.GOKB_ID, oleGokbOrganization.getGokbOrganizationId());
728             List<VendorDetail> vendorDetails = (List<VendorDetail>) getBusinessObjectService().findMatching(VendorDetail.class, vendorMap);
729             if (vendorDetails != null && vendorDetails.size() > 0) {
730                 VendorDetail vendorDetail = vendorDetails.get(0);
731                 oleeResourceSearchService.updateVendor(vendorDetail, oleGokbOrganization.getOrganizationName());
732                 updateLog(logId, "", "vendorsUpdatedCount", 1);
733             } else {
734                 oleeResourceSearchService.createVendor(oleGokbOrganization.getOrganizationName(), oleGokbOrganization.getGokbOrganizationId(), oleGokbOrganization.getVariantName());
735                 updateLog(logId, "", "vendorsAddedCount", 1);
736             }
737         }
738     }
739 
740     public List<OLEPlatformRecordDocument> fetchPlatformDetailsForEResId(String eResId) {
741         OLEEResourceRecordDocument oleeResourceRecordDocument = (OLEEResourceRecordDocument) getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, eResId);
742         List<OLEPlatformRecordDocument> olePlatformRecordDocuments = new ArrayList<>();
743         for (OLEEResourceInstance oleeResourceInstance : oleeResourceRecordDocument.getOleERSInstances()) {
744             OLEPlatformRecordDocument olePlatformRecordDocument = getBusinessObjectService().findBySinglePrimaryKey(OLEPlatformRecordDocument.class, oleeResourceInstance.getPlatformId());
745             olePlatformRecordDocuments.add(olePlatformRecordDocument);
746         }
747         return olePlatformRecordDocuments;
748     }
749 
750     public void applyGokbChangesToPlatforms(String eResId, String logId) {
751         List<OLEPlatformRecordDocument> olePlatformRecordDocuments = new ArrayList<>();
752         if (eResId != null && StringUtils.isNotEmpty(eResId)) {
753             olePlatformRecordDocuments = fetchPlatformDetailsForEResId(eResId);
754         } else {
755             List<OLEPlatformRecordDocument> olePlatformRecordDocumentList = (List<OLEPlatformRecordDocument>) getBusinessObjectService().findAll(OLEPlatformRecordDocument.class);
756             for (OLEPlatformRecordDocument platformRecordDocument : olePlatformRecordDocumentList) {
757                 if (platformRecordDocument.getGokbId() != null) {
758                     olePlatformRecordDocuments.add(platformRecordDocument);
759                 }
760             }
761         }
762         for (OLEPlatformRecordDocument platformRecordDocument : olePlatformRecordDocuments) {
763             OleGokbPlatform olegoKbPlatform = getBusinessObjectService().findBySinglePrimaryKey(OleGokbPlatform.class, platformRecordDocument.getGokbId());
764             if (olegoKbPlatform != null) {
765                 if (platformRecordDocument.getGokbLastUpdatedDate() == null || olegoKbPlatform.getDateUpdated().after(platformRecordDocument.getGokbLastUpdatedDate())) {
766                     Map hashmap = new HashMap();
767                     hashmap.put("platformStatusId",platformRecordDocument.getStatusId());
768                     List<OLEPlatformStatus> olePlatformStatusList = (List<OLEPlatformStatus>) getBusinessObjectService().findMatching(OLEPlatformStatus.class, hashmap);
769                     if(olePlatformStatusList.size()>0){
770                         if (olePlatformStatusList.get(0).getPlatformStatusName().equalsIgnoreCase(olegoKbPlatform.getStatus())) {
771                             String details = "[" + olePlatformStatusList.get(0).getPlatformStatusName() + "]: GOKb status changed to[" + olegoKbPlatform.getStatus() + "]";
772                             createToDo(eResId, "Change Platform Status", details, null);
773                         }
774                     }
775                     else{
776                         String details = " GOKb status changed to[" + olegoKbPlatform.getStatus() + "]";
777                         createToDo(eResId, "Change Platform Status", details, null);
778                     }
779                     if (!platformRecordDocument.getName().equalsIgnoreCase(olegoKbPlatform.getPlatformName())) {
780                         String detail = "[" + olegoKbPlatform.getPlatformName() + "] has changed form [" + platformRecordDocument.getName() + "] to [" + olegoKbPlatform.getPlatformName() + "]";
781                         createChangeLog(eResId, "Metadata change: Platform name", detail);
782                     }
783                     // TODO :Add change log for platform provider name if not same
784                     // TODO :Add change log for authentication if not same
785                     if (!platformRecordDocument.getSoftware().equalsIgnoreCase(olegoKbPlatform.getSoftwarePlatform())) {
786                         String details = "[" + platformRecordDocument.getName() + "]: Platform software has changed from [" + platformRecordDocument.getSoftware() + "] to [" + olegoKbPlatform.getStatus() + "]";
787                         createChangeLog(eResId, "Metadata change: Platform software", details);
788                     }
789                     oleeResourceSearchService.updatePlatform(platformRecordDocument, olegoKbPlatform.getPlatformName(), olegoKbPlatform.getStatus(), olegoKbPlatform.getSoftwarePlatform(), olegoKbPlatform.getPlatformProviderId());
790                     updatePlatformVendorAssociation(platformRecordDocument);
791                     updateLog(logId, eResId, "platformsUpdatedCount", 1);
792                 }
793             }
794         }
795     }
796 
797     public List<VendorDetail> fetchVendorDetailsForEResId(String eResId) {
798         //List<String> vendorGokbList = new ArrayList<>();
799         List<VendorDetail> vendorDetails = new ArrayList<>();
800         Map eResMap = new HashMap();
801         eResMap.put("oleERSIdentifier", eResId);
802         OLEEResourceRecordDocument oleeResourceRecordDocument = (OLEEResourceRecordDocument) getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, eResId);
803         if (StringUtils.isNotEmpty(oleeResourceRecordDocument.getVendorId())) {
804             VendorDetail vendorDetail = getBusinessObjectService().findBySinglePrimaryKey(VendorDetail.class, oleeResourceRecordDocument.getVendorId());
805             vendorDetails.add(vendorDetail);
806         }
807         //vendorGokbList.add(vendorDetail.getGokbId().toString());
808         if (StringUtils.isNotEmpty(oleeResourceRecordDocument.getPublisherId())) {
809             Map vendorMap = new HashMap();
810             vendorMap.put("gokbId", oleeResourceRecordDocument.getPublisherId());
811             List<VendorDetail> publishers = (List<VendorDetail>) getBusinessObjectService().findMatching(VendorDetail.class, vendorMap);
812             //vendorGokbList.add(publisher.getGokbId().toString());
813             if (publishers.size() > 0) {
814                 vendorDetails.add(publishers.get(0));
815             }
816         }
817         for (OLEEResourceInstance oleeResourceInstance : oleeResourceRecordDocument.getOleERSInstances()) {
818             OLEPlatformRecordDocument olePlatformRecordDocument = getBusinessObjectService().findBySinglePrimaryKey(OLEPlatformRecordDocument.class, oleeResourceInstance.getPlatformId());
819             Map vendorDetailMap = new HashMap();
820             vendorDetailMap.put("gokbId", olePlatformRecordDocument.getGokbId());
821             List<VendorDetail> platformProviderList = (List<VendorDetail>) getBusinessObjectService().findMatching(VendorDetail.class, vendorDetailMap);
822             if (platformProviderList.size() > 0) {
823                 //vendorGokbList.add(olePlatformRecordDocument.getGokbId().toString());
824                 vendorDetails.add(platformProviderList.get(0));
825             }
826         }
827         return vendorDetails;
828     }
829 
830 
831     public void applyGokbChangesToVendors(String eResId, String logId) {
832         List<VendorDetail> vendorDetails = new ArrayList<>();
833         if (eResId != null && StringUtils.isNotEmpty(eResId)) {
834             vendorDetails = fetchVendorDetailsForEResId(eResId);
835         } else {
836             List<VendorDetail> vendorDetailList = (List<VendorDetail>) getBusinessObjectService().findAll(VendorDetail.class);
837             for (VendorDetail vendorDetail : vendorDetailList) {
838                 if (vendorDetail.getGokbId() != null) {
839                     vendorDetails.add(vendorDetail);
840                 }
841             }
842         }
843         for (VendorDetail vendor : vendorDetails) {
844             OleGokbOrganization oleGokbOrganization = getBusinessObjectService().findBySinglePrimaryKey(OleGokbOrganization.class, vendor.getGokbId());
845             if (oleGokbOrganization != null) {
846                 if (vendor.getGokbLastUpdated() == null || oleGokbOrganization.getDateUpdated().after(vendor.getGokbLastUpdated())) {
847                     // TODO createToDo Organization status not avaliable in gokb Organization
848                     if (!vendor.getVendorName().equalsIgnoreCase(oleGokbOrganization.getOrganizationName())) {
849                         String details = "[" + oleGokbOrganization.getOrganizationName() + "]" + " Organization name changed from [" + vendor.getVendorName() + "] to [" + oleGokbOrganization.getOrganizationName() + "]";
850                         createChangeLog(eResId, "Change: Organization name", details);
851                     }
852                     List<String> vendorAlias = new ArrayList<>();
853                     if (vendor.getVendorAliases() != null) {
854                         for (VendorAlias vendoralias : vendor.getVendorAliases()) {
855                             vendorAlias.add(vendoralias.getVendorAliasName());
856                         }
857                         if (!vendorAlias.contains(oleGokbOrganization.getVariantName())) {
858                             String details = "[" + vendor.getVendorName() + "] " + "has new variant name ";
859                             createChangeLog(eResId, "Metadata change: New organization variant name", details);
860                         }
861                     }
862                     oleeResourceSearchService.updateVendor(vendor, oleGokbOrganization.getOrganizationName());
863                     updateLog(logId, eResId, "vendorsUpdatedCount", 1);
864                 }
865             }
866         }
867     }
868 
869     public void overwriteEresourceWithPackage(OLEEResourceRecordDocument oleeResourceRecordDocument, OleGokbPackage oleGokbPackage, String logId) {
870         if (oleeResourceRecordDocument.getGokbPackageStatus() != null && !oleeResourceRecordDocument.getGokbPackageStatus().equalsIgnoreCase(oleGokbPackage.getStatus())) {
871             String details = "[" + oleeResourceRecordDocument.getGokbPackageStatus() + "]: GOKb status changed to [" + oleGokbPackage.getStatus() + "]";
872             createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Change: Package Status", details, null);
873         }
874 
875         GOKbDataElement eResourceNameDataElement = getDataElement(E_RESOURCE_NAME);
876         GOKbDataElement variantDataElement = getDataElement(VARIANT_NAME);
877         GOKbDataElement publisherDataElement = getDataElement(PUBLISHER);
878         GOKbDataElement packageScopeDataElement = getDataElement(PACKAGE_SCOPE);
879         GOKbDataElement breakableDataElement = getDataElement(BREAKABLE);
880         GOKbDataElement fixedTitleDataElement = getDataElement(FIXED_TITLE_LIST);
881         GOKbDataElement packageTypeDataElement = getDataElement(PACKAGE_TYPE);
882         GOKbDataElement eResourceDataElement = getDataElement(E_RESOURCE_GOKBID);
883 
884         Map dataMapping = new HashMap();
885         dataMapping.put("recordType", "E-Resource");
886         dataMapping.put("recordId", oleeResourceRecordDocument.getOleERSIdentifier());
887         List<OLEGOKbMappingValue> locallyModifiedElements = (List<OLEGOKbMappingValue>) getBusinessObjectService().findMatching(OLEGOKbMappingValue.class, dataMapping);
888         if (locallyModifiedElements.size() == 0) {
889             oleeResourceRecordDocument.setTitle(oleGokbPackage.getPackageName());
890             OLEEResourceVariantTitle variantTitle = new OLEEResourceVariantTitle();
891             variantTitle.setOleVariantTitle(oleGokbPackage.getVariantName());
892             oleeResourceRecordDocument.getOleEResourceVariantTitleList().add(variantTitle);
893             getPublisherIdFromPackageId(oleeResourceRecordDocument, oleGokbPackage.getGokbPackageId().toString());
894             String packId = getOlePackageScopeId(oleGokbPackage.getPackageScope());
895             if(packId != null) {
896                 oleeResourceRecordDocument.setPackageScopeId(packId);
897             }
898             oleeResourceRecordDocument.setBreakable(Boolean.valueOf(oleGokbPackage.getBreakable()));
899             oleeResourceRecordDocument.setFixedTitleList(Boolean.valueOf(oleGokbPackage.getFixed()));
900             oleeResourceRecordDocument.setGokbIdentifier(oleGokbPackage.getGokbPackageId());
901         } else {
902             for (OLEGOKbMappingValue localMappingValue : locallyModifiedElements) {
903 
904                 if (localMappingValue.getDataElementId().equalsIgnoreCase(eResourceNameDataElement.getDataElementId())) {
905                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
906                         oleeResourceRecordDocument.setTitle(oleGokbPackage.getPackageName());
907                     }
908                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(variantDataElement.getDataElementId())) {
909                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
910                         OLEEResourceVariantTitle variantTitle = new OLEEResourceVariantTitle();
911                         variantTitle.setOleVariantTitle(oleGokbPackage.getVariantName());
912                         oleeResourceRecordDocument.getOleEResourceVariantTitleList().add(variantTitle);
913                     }
914                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(publisherDataElement.getDataElementId())) {
915                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
916                         getPublisherIdFromPackageId(oleeResourceRecordDocument, oleGokbPackage.getGokbPackageId().toString());
917                     }
918                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(packageScopeDataElement.getDataElementId())) {
919                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
920                         String packId = getOlePackageScopeId(oleGokbPackage.getPackageScope());
921                         if(packId != null) {
922                             oleeResourceRecordDocument.setPackageScopeId(packId);
923                         }
924                     }
925                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(breakableDataElement.getDataElementId())) {
926                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
927                         oleeResourceRecordDocument.setBreakable(Boolean.valueOf(oleGokbPackage.getBreakable()));
928                     }
929                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(fixedTitleDataElement.getDataElementId())) {
930                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
931                         oleeResourceRecordDocument.setFixedTitleList(Boolean.valueOf(oleGokbPackage.getFixed()));
932                     }
933                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(packageTypeDataElement.getDataElementId())) {
934                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
935                         // TODO : Add package type id to E-Resource if avalibale in GokbPackage
936                         //oleeResourceRecordDocument.setPackageTypeId(oleGokbPackage.getPa);
937                     }
938                 } else if (localMappingValue.getDataElementId().equalsIgnoreCase(eResourceDataElement.getDataElementId())) {
939                     if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
940                         oleeResourceRecordDocument.setGokbIdentifier(oleGokbPackage.getGokbPackageId());
941                     }
942                 }
943             }
944         }
945         oleeResourceRecordDocument.setGokbLastUpdatedDate(oleGokbPackage.getDateUpdated());
946         oleeResourceRecordDocument.setGokbPackageStatus(oleGokbPackage.getStatus());
947         KRADServiceLocatorWeb.getDocumentService().updateDocument(oleeResourceRecordDocument);
948         updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "eResUpdatedCount", 1);
949         if (!oleeResourceRecordDocument.getTitle().equalsIgnoreCase(oleGokbPackage.getPackageName())) {
950             String details = "[" + oleeResourceRecordDocument.getTitle() + "]: E-Resource name changed from [" + oleeResourceRecordDocument.getTitle() + "] to [" + oleGokbPackage.getPackageName() + "]";
951             createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Resource name", details);
952         }
953         if (!oleeResourceRecordDocument.getPackageScopeId().equalsIgnoreCase(oleGokbPackage.getPackageScope())) {
954             String details = "[" + oleeResourceRecordDocument.getTitle() + "]: E-Resource scope changed from [" + oleeResourceRecordDocument.getPackageScopeId() + "] to [" + oleGokbPackage.getPackageScope() + "]";
955             createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Resource scope", details);
956         }
957         if (!oleGokbPackage.getBreakable().equalsIgnoreCase("Unknown")) {
958             if (!String.valueOf(oleeResourceRecordDocument.isBreakable()).equalsIgnoreCase(oleGokbPackage.getBreakable())) {
959                 String details = "[" + oleeResourceRecordDocument.getTitle() + "]: E-Resource breakable changed from [" + oleeResourceRecordDocument.isBreakable() + "] to [" + oleGokbPackage.getBreakable() + "]";
960                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Resource breakable", details);
961             }
962         }
963         if (!oleGokbPackage.getBreakable().equalsIgnoreCase("Unknown")) {
964             if (!String.valueOf(oleeResourceRecordDocument.isFixedTitleList()).equalsIgnoreCase(oleGokbPackage.getFixed())) {
965                 String details = "[" + oleeResourceRecordDocument.getTitle() + "]: E-Resource fixed changed from [" + oleeResourceRecordDocument.isFixedTitleList() + "] to [" + oleGokbPackage.getFixed() + "]";
966                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Resource fixed", details);
967             }
968         }
969     }
970 
971     private String getOlePackageScopeId(String packageScope) {
972 
973         Map map = new HashMap();
974         map.put("olePackageScopeName", packageScope);
975 
976         List<OLEPackageScope> packageScopes = (List<OLEPackageScope>) getBusinessObjectService().findMatching(OLEPackageScope.class, map);
977         if(packageScopes != null && packageScopes.size() > 0) {
978             return packageScopes.get(0).getOlePackageScopeId();
979         }
980         return null;
981     }
982 
983     public void getPublisherIdFromPackageId(OLEEResourceRecordDocument oleeResourceRecordDocument, String packageId) {
984         Map map = new HashMap();
985         map.put("gokbPackageId", packageId);
986         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
987         OleGokbTitle oleGokbTitle = getBusinessObjectService().findBySinglePrimaryKey(OleGokbTitle.class, oleGokbTipps.get(0).getGokbTitleId());
988         if (oleGokbTitle != null && oleGokbTitle.getPublisherId() != null) {
989             oleeResourceRecordDocument.setPublisher(oleGokbTitle.getPublisherId().toString());
990         }
991     }
992 
993     public void applyGokbChangesToEresources(String eResId, String logId) {
994         List<OLEEResourceRecordDocument> oleeResourceRecordDocuments = new ArrayList<>();
995         if (StringUtils.isNotEmpty(eResId)) {
996             OLEEResourceRecordDocument oleeResourceRecordDocument = getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceRecordDocument.class, eResId);
997             oleeResourceRecordDocuments.add(oleeResourceRecordDocument);
998         } else {
999             oleeResourceRecordDocuments = (List<OLEEResourceRecordDocument>) getBusinessObjectService().findAll(OLEEResourceRecordDocument.class);
1000         }
1001         for (OLEEResourceRecordDocument oleeResourceRecordDocument : oleeResourceRecordDocuments) {
1002             Map packageMap = new HashMap();
1003             packageMap.put("gokbPackageId", oleeResourceRecordDocument.getGokbIdentifier());
1004             OleGokbPackage oleGokbPackage = (OleGokbPackage) getBusinessObjectService().findBySinglePrimaryKey(OleGokbPackage.class, oleeResourceRecordDocument.getGokbIdentifier());
1005             if (oleGokbPackage != null) {
1006                 //if (oleeResourceRecordDocument.getGokbLastUpdatedDate() == null || oleGokbPackage.getDateUpdated().after(oleeResourceRecordDocument.getGokbLastUpdatedDate())) {
1007                     overwriteEresourceWithPackage(oleeResourceRecordDocument, oleGokbPackage, logId);
1008                 //}
1009             }
1010             //List<String> gokbIds = new ArrayList<>();
1011             Map eholdingsMap = new HashMap();
1012             for (OLEEResourceInstance oleeResourceInstance : oleeResourceRecordDocument.getOleERSInstances()) {
1013                 //gokbIds.add(oleeResourceInstance.getGokbId().toString());
1014                 eholdingsMap.put(oleeResourceInstance.getGokbId(), oleeResourceInstance);
1015             }
1016             Map map = new HashMap();
1017             map.put("gokbPackageId", oleGokbPackage.getGokbPackageId());
1018             List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
1019             List<OleGokbTipp> oleGokbTippTobeCreated = new ArrayList<>();
1020             for (OleGokbTipp tipp : oleGokbTipps) {
1021                 if (!eholdingsMap.containsKey(tipp.getGokbTippId())) {
1022                     oleGokbTippTobeCreated.add(tipp);
1023 //                    addTippToEresource(oleeResourceRecordDocument, tipp);
1024                 } else {
1025                     if (tipp.getStatus().equalsIgnoreCase("Retired")) {
1026                         SearchParams searchParams = new SearchParams();
1027                         searchParams.buildSearchCondition("", searchParams.buildSearchField("Biblographic", "mdf_035a", "(GOKbUID)" + tipp.getGokbTitleId()), "AND");
1028                         SearchResponse searchResponse = null;
1029                         try {
1030                             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1031                         } catch (Exception e) {
1032                             e.printStackTrace();
1033                         }
1034                         String bibId = "";
1035                         for (SearchResult searchResult : searchResponse.getSearchResults()) {
1036                             for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
1037                                 if (searchResultField.getFieldName().equalsIgnoreCase("bibIdentifier")) {
1038                                     bibId = searchResultField.getFieldValue();
1039                                 }
1040                             }
1041                         }
1042                         Bib bib = null;
1043                         try {
1044                             bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibId);
1045                         } catch (Exception e) {
1046                             e.printStackTrace();
1047                         }
1048                         Holdings eHoldings = null;
1049                         OLEEResourceInstance oleeResourceInstance = (OLEEResourceInstance) eholdingsMap.get(tipp.getGokbTippId());
1050                         try {
1051                             eHoldings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(oleeResourceInstance.getInstanceId());
1052                         } catch (Exception e) {
1053                             e.printStackTrace();
1054                         }
1055                         retireTippFromEresource(eHoldings, tipp, bib, oleeResourceRecordDocument, logId);
1056                     } else if (tipp.getStatus().equalsIgnoreCase("Deleted")) {
1057                         SearchParams searchParams = new SearchParams();
1058                         searchParams.buildSearchCondition("", searchParams.buildSearchField("Biblographic", "mdf_035a", "(GOKbUID)" + tipp.getGokbTitleId()), "AND");
1059                         SearchResponse searchResponse = null;
1060                         try {
1061                             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1062                         } catch (Exception e) {
1063                             e.printStackTrace();
1064                         }
1065                         String bibId = "";
1066                         for (SearchResult searchResult : searchResponse.getSearchResults()) {
1067                             for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
1068                                 if (searchResultField.getFieldName().equalsIgnoreCase("bibIdentifier")) {
1069                                     bibId = searchResultField.getFieldValue();
1070                                 }
1071                             }
1072                         }
1073                         Bib bib = null;
1074                         try {
1075                             bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibId);
1076                         } catch (Exception e) {
1077                             e.printStackTrace();
1078                         }
1079                         Holdings eHoldings = null;
1080                         OLEEResourceInstance oleeResourceInstance = (OLEEResourceInstance) eholdingsMap.get(tipp.getGokbTippId());
1081                         try {
1082                             eHoldings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(oleeResourceInstance.getInstanceId());
1083                         } catch (Exception e) {
1084                             e.printStackTrace();
1085                         }
1086                         deleteTippFromEresource(eHoldings, tipp, bib, oleeResourceRecordDocument, logId);
1087                     } else {
1088                         SearchParams searchParams = new SearchParams();
1089                         searchParams.buildSearchCondition("", searchParams.buildSearchField("Biblographic", "mdf_035a", "(GOKbUID)" + tipp.getGokbTitleId()), "AND");
1090                         SearchResponse searchResponse = null;
1091                         try {
1092                             searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
1093                         } catch (Exception e) {
1094                             e.printStackTrace();
1095                         }
1096                         /*String bibId = "";
1097                         for (SearchResult searchResult : searchResponse.getSearchResults()) {
1098                             for (SearchResultField searchResultField : searchResult.getSearchResultFields()) {
1099                                 if(searchResultField.getFieldName().equalsIgnoreCase("bibIdentifier")){
1100                                     bibId = searchResultField.getFieldValue();
1101                                 }
1102                             }
1103                         }
1104                         Bib bib = null;
1105                         try {
1106                             bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibId);
1107                         } catch (Exception e) {
1108                             e.printStackTrace();
1109                         }*/
1110                         Holdings eHoldings = null;
1111                         OLEEResourceInstance oleeResourceInstance = (OLEEResourceInstance) eholdingsMap.get(tipp.getGokbTippId());
1112                         try {
1113                             eHoldings = getDocstoreClientLocator().getDocstoreClient().retrieveHoldings(oleeResourceInstance.getInstanceId());
1114                         } catch (Exception e) {
1115                             e.printStackTrace();
1116                         }
1117                         //if(tipp.getDateUpdated().after(eHoldings.))
1118                         overwriteEholdingsWithTipp(eHoldings, tipp, oleeResourceRecordDocument, logId);
1119                     }
1120                 }
1121             }
1122             if(oleGokbTippTobeCreated.size()>0){
1123                 addTippToEresource(oleeResourceRecordDocument, oleGokbTippTobeCreated, logId);
1124             }
1125         }
1126     }
1127 
1128     public void addTippToEresource(OLEEResourceRecordDocument oleeResourceRecordDocument, List<OleGokbTipp> oleGokbTipps, String logId) {
1129 
1130         if (oleeResourceRecordDocument.getGokbconfig().equalsIgnoreCase("sync")) {
1131             List<BibMarcRecord> bibMarcRecords = new ArrayList<>();
1132             for (OleGokbTipp oleGokbTipp : oleGokbTipps) {
1133                 OleGokbPlatform oleGokbPlatform = fetchPlatformForTipp(oleGokbTipp);
1134                 createOrUpdateVendor(oleGokbPlatform.getPlatformProviderId(), logId);
1135                 OleGokbTitle oleGokbTitle = getOleGokbTitle(oleGokbTipp.getGokbTitleId());
1136                 if (oleGokbTitle != null) {
1137                     String details = "[" + oleGokbTitle.getTitleName() + "]: E-Instance added to OLE";
1138                     createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "New E-Instance", details);
1139                     createOrUpdateVendor(oleGokbTitle.getPublisherId(), logId);
1140                     oleeResourceSearchService.updatePublisherDetails(oleeResourceRecordDocument, oleGokbTitle.getPublisherId());
1141                 }
1142                 Map gokbMap = new HashMap();
1143                 gokbMap.put(OLEConstants.GOKB_ID, oleGokbPlatform.getGokbPlatformId());
1144                 List<OLEPlatformRecordDocument> platformRecordDocumentList = (List<OLEPlatformRecordDocument>) getBusinessObjectService().findMatching(OLEPlatformRecordDocument.class, gokbMap);
1145                 OLEPlatformRecordDocument olePlatformRecordDocument = null;
1146                 if (platformRecordDocumentList != null && platformRecordDocumentList.size() > 0) {
1147                     olePlatformRecordDocument = platformRecordDocumentList.get(0);
1148                     olePlatformRecordDocument.setGokbLastUpdatedDate(oleGokbPlatform.getDateUpdated());
1149                     oleeResourceSearchService.updatePlatform(olePlatformRecordDocument, oleGokbPlatform.getPlatformName(), oleGokbPlatform.getStatus(), oleGokbPlatform.getSoftwarePlatform(), oleGokbPlatform.getPlatformProviderId());
1150                 } else {
1151                     olePlatformRecordDocument = oleeResourceSearchService.createPlatform(oleGokbPlatform.getPlatformName(), oleGokbPlatform.getGokbPlatformId(), oleGokbPlatform.getSoftwarePlatform(), oleGokbPlatform.getStatus(), oleGokbPlatform.getPlatformProviderId());
1152                 }
1153                 if (oleeResourceRecordDocument != null) {
1154                     updatePlatformVendorAssociation(olePlatformRecordDocument);
1155                 }
1156                 String platformName = oleGokbPlatform.getPlatformName();
1157                 if (oleGokbTitle != null) {
1158                     BibMarcRecord bibMarcRecord = buildBibMarcRecordFromTitle(oleGokbTitle);
1159                     String imprint = String.valueOf(oleGokbTitle.getImprint());
1160                     String publisher = String.valueOf(oleGokbTitle.getPublisherId());
1161                     DataField dataField = getDataFieldForTipp(platformName, oleeResourceRecordDocument.getOleERSIdentifier(), oleGokbTipp, imprint, publisher);
1162                     bibMarcRecord.getDataFields().add(dataField);
1163                     bibMarcRecords.add(bibMarcRecord);
1164                 }
1165             }
1166             OLEBatchProcessProfileBo gokbImportProfile = getGOKBImportProfile(oleeResourceRecordDocument.getGokbProfile());
1167             importTipps(gokbImportProfile, bibMarcRecords);
1168             updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "eHoldingsAddedCount", oleGokbTipps.size());
1169             updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "bibAddedCount", oleGokbTipps.size());
1170         } else {
1171             for (OleGokbTipp oleGokbTipp : oleGokbTipps) {
1172                 OleGokbTitle oleGokbTitle = getBusinessObjectService().findBySinglePrimaryKey(OleGokbTitle.class, oleGokbTipp.getGokbTitleId());
1173                 String details = "[" + oleGokbTitle.getTitleName() + "]: TIPP added to GOKb package. E-Instance not created. Review TIPP and approve to create associated E-Instance";
1174                 createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "New TIPP", details, oleGokbTipp.getGokbTippId());
1175             }
1176         }
1177 
1178     }
1179 
1180     private OleGokbTitle getOleGokbTitle(Integer titleId) {
1181         Map map = new HashMap();
1182         map.put("gokbTitleId", titleId);
1183         return getBusinessObjectService().findByPrimaryKey(OleGokbTitle.class, map);
1184     }
1185 
1186     public OleGokbPlatform fetchPlatformForTipp(OleGokbTipp tipp) {
1187         OleGokbPlatform oleGokbPlatform = getBusinessObjectService().findBySinglePrimaryKey(OleGokbPlatform.class, tipp.getGokbPlatformId());
1188         return oleGokbPlatform;
1189     }
1190 
1191     public void deleteTippFromEresource(Holdings eHolding, OleGokbTipp tipp, Bib bib, OLEEResourceRecordDocument oleeResourceRecordDocument, String logId) {
1192         if (oleeResourceRecordDocument.getGokbconfig().equalsIgnoreCase("sync")) {
1193             List<OLEEResourceInstance> oleERSInstances = oleeResourceRecordDocument.getOleERSInstances();
1194             for (OLEEResourceInstance oleeResourceInstance : oleERSInstances) {
1195                 if (oleeResourceInstance.getInstanceId().equalsIgnoreCase(eHolding.getLocalId())) {
1196                     Map copyMap = new HashMap();
1197                     copyMap.put("instanceId", oleeResourceInstance.getInstanceId());
1198                     List<OleCopy> copies = (List<OleCopy>) getBusinessObjectService().findMatching(OleCopy.class, copyMap);
1199                     if (copies.get(0).getPoItemId() != null) {
1200                         String details = "[" + eHolding.getBib().getTitle() + "] TIPP deleted from GOKb package. E-Instance could not be deleted because of an attached purchase order";
1201                         createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Deleted TIPP", details, null);
1202                     } else {
1203                         try {
1204                             getDocstoreClientLocator().getDocstoreClient().deleteHoldings(eHolding.getLocalId());
1205                         } catch (Exception e) {
1206                             e.printStackTrace();
1207                         }
1208                         String details = "[" + eHolding.getBib().getTitle() + "]: E-Holding deleted from OLE";
1209                         createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Deleted E-Instance", details);
1210                     }
1211                     updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "eHoldingsDeletedCount", 1);
1212                 }
1213             }
1214         } else {
1215             String details = "[" + eHolding.getBib().getTitle() + "] TIPP deleted from GOKb package. E-Instance could not be deleted because of an attached purchase order";
1216             createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Deleted TIPP", details, null);
1217         }
1218     }
1219 
1220     public void retireTippFromEresource(Holdings eHolding, OleGokbTipp tipp, Bib bib, OLEEResourceRecordDocument oleeResourceRecordDocument, String logId) {
1221         if (oleeResourceRecordDocument.getGokbconfig().equalsIgnoreCase("sync")) {
1222             List<OLEEResourceInstance> oleERSInstances = oleeResourceRecordDocument.getOleERSInstances();
1223             for (OLEEResourceInstance oleeResourceInstance : oleERSInstances) {
1224                 if (oleeResourceInstance.getInstanceId().equalsIgnoreCase(eHolding.getLocalId())) {
1225                     Map copyMap = new HashMap();
1226                     copyMap.put("instanceId", oleeResourceInstance.getInstanceId());
1227                     List<OleCopy> copies = (List<OleCopy>) getBusinessObjectService().findMatching(OleCopy.class, copyMap);
1228                     if (copies.get(0).getPoItemId() != null) {
1229                         String details = "[" + eHolding.getBib().getTitle() + "] TIPP retired from GOKb package. E-Instance could not be deleted because of an attached purchase order";
1230                         createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Retired TIPP", details, null);
1231                     } else {
1232                         try {
1233                             getDocstoreClientLocator().getDocstoreClient().deleteHoldings(eHolding.getLocalId());
1234                         } catch (Exception e) {
1235                             e.printStackTrace();
1236                         }
1237                         String details = "[" + eHolding.getBib().getTitle() + "]: E-Holding deleted from OLE";
1238                         createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Retired E-Instance", details);
1239                     }
1240                     updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "eHoldingsRetiredCount", 1);
1241                 }
1242             }
1243 
1244         } else {
1245             String details = "[<" + eHolding.getBib().getTitle() + ">]: TIPP retired from GOKb package. No changes to E-Instance. Review E-Instance to determine if action is needed";
1246             createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Retired TIPP", details, null);
1247         }
1248     }
1249 
1250     public void overwriteEholdingsWithTipp(Holdings eHolding, OleGokbTipp tipp, OLEEResourceRecordDocument oleeResourceRecordDocument, String logId) {
1251         if (eHolding.getStatus() != null && !eHolding.getStatus().equalsIgnoreCase(tipp.getStatus())) {
1252             String details = "[" + eHolding.getStatus() + "]: GOKB status changed to [" + tipp.getStatus() + "]";
1253             createToDo(oleeResourceRecordDocument.getOleERSIdentifier(), "Change: TIPP Status", details, null);
1254         }
1255         OleHoldings oleHoldings = eHolding.getContentObject();
1256         if (oleHoldings.getExtentOfOwnership() != null && oleHoldings.getExtentOfOwnership().size()>0) {
1257             GOKbDataElement coverageStartDateElement = getDataElement(coverageStartDate);
1258             GOKbDataElement coverageStartIssueElement = getDataElement(coverageStartIssue);
1259             GOKbDataElement coverageStartVolumeeElement = getDataElement(coverageStartVolume);
1260             GOKbDataElement coverageEndDateElement = getDataElement(coverageEndDate);
1261             GOKbDataElement coverageEndIssueElement = getDataElement(coverageEndIssue);
1262             GOKbDataElement coverageEndVolumeElement = getDataElement(coverageEndVolume);
1263             Map dataMapping = new HashMap();
1264             dataMapping.put("recordType", "E-Instance");
1265             dataMapping.put("recordId", oleHoldings.getHoldingsIdentifier());
1266             List<OLEGOKbMappingValue> locallyModifiedElements = (List<OLEGOKbMappingValue>) getBusinessObjectService().findMatching(OLEGOKbMappingValue.class, dataMapping);
1267             Coverage coverage = oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().get(0);
1268             if (locallyModifiedElements.size() == 0) {
1269                 coverage.setCoverageStartDate(tipp.getStartdate().toString());
1270                 coverage.setCoverageStartIssue(tipp.getStartIssue());
1271                 coverage.setCoverageStartVolume(tipp.getStartVolume());
1272                 coverage.setCoverageEndDate(tipp.getEndDate().toString());
1273                 coverage.setCoverageEndIssue(tipp.getEndIssue());
1274                 coverage.setCoverageEndVolume(tipp.getEndVolume());
1275             } else {
1276                 for (OLEGOKbMappingValue localMappingValue : locallyModifiedElements) {
1277 
1278                     if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageStartDateElement.getDataElementId())) {
1279                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1280                             coverage.setCoverageStartDate(tipp.getStartdate().toString());
1281                         }
1282                     } else if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageStartIssueElement.getDataElementId())) {
1283                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1284                             coverage.setCoverageStartIssue(tipp.getStartIssue());
1285                         }
1286                     } else if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageStartVolumeeElement.getDataElementId())) {
1287                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1288                             coverage.setCoverageStartVolume(tipp.getStartVolume());
1289                         }
1290                     } else if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageEndDateElement.getDataElementId())) {
1291                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1292                             coverage.setCoverageEndDate(tipp.getEndDate().toString());
1293                         }
1294                     } else if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageEndIssueElement.getDataElementId())) {
1295                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1296                             coverage.setCoverageEndIssue(tipp.getEndIssue());
1297                         }
1298                     } else if (localMappingValue.getDataElementId().equalsIgnoreCase(coverageEndVolumeElement.getDataElementId())) {
1299                         if (StringUtils.isEmpty(localMappingValue.getLocalValue())) {
1300                             coverage.setCoverageEndVolume(tipp.getEndVolume());
1301                         }
1302                     }
1303                 }
1304             }
1305         }
1306         try {
1307             getDocstoreClientLocator().getDocstoreClient().updateHoldings(eHolding);
1308         } catch (Exception e) {
1309             e.printStackTrace();
1310         }
1311         updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "eHoldingsUpdatedCount", 1);
1312         OleGokbPlatform oleGokbPlatform = fetchPlatformForTipp(tipp);
1313         addorUpdatePlatform(oleGokbPlatform, oleeResourceRecordDocument, logId);
1314         OleGokbTitle oleGokbTitle = getOleGokbTitle(tipp.getGokbTitleId());
1315         oleeResourceSearchService.updatePublisherDetails(oleeResourceRecordDocument, oleGokbTitle.getPublisherId());
1316         updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "vendorsAddedCount", 1);
1317         if (oleHoldings.getExtentOfOwnership() != null && oleHoldings.getExtentOfOwnership().size()>0) {
1318             Coverage coverage = oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().get(0);
1319             if (!coverage.getCoverageStartDate().equalsIgnoreCase(tipp.getStartdate().toString())) {
1320                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance start date has changed from [" + coverage.getCoverageStartDate() + "] to [" + tipp.getStartdate() + "]";
1321                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance start date", details);
1322             }
1323             if (!coverage.getCoverageStartIssue().equalsIgnoreCase(tipp.getStartIssue())) {
1324                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance start issue has changed from [" + coverage.getCoverageStartIssue() + "] to [" + tipp.getStartIssue() + "]";
1325                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance start issue", details);
1326             }
1327             if (!coverage.getCoverageStartVolume().equalsIgnoreCase(tipp.getStartVolume())) {
1328                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance start volume has changed from [" + coverage.getCoverageStartVolume() + "] to [" + tipp.getStartVolume() + "]";
1329                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance start volume", details);
1330             }
1331             if (!coverage.getCoverageEndDate().equalsIgnoreCase(tipp.getEndDate().toString())) {
1332                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance end date has changed from [" + coverage.getCoverageEndDate() + "] to [" + tipp.getEndDate() + "]";
1333                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance end date", details);
1334             }
1335             if (!coverage.getCoverageEndVolume().equalsIgnoreCase(tipp.getEndVolume())) {
1336                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance end volume has changed from [" + coverage.getCoverageEndIssue() + "] to [" + tipp.getEndVolume() + "]";
1337                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance end volume", details);
1338             }
1339             if (!coverage.getCoverageEndIssue().equalsIgnoreCase(tipp.getEndIssue())) {
1340                 String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance end issue has changed from [" + coverage.getCoverageEndIssue() + "] to [" + tipp.getEndIssue() + "]";
1341                 createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance end issue", details);
1342             }
1343             if (oleHoldings.getLink() != null) {
1344                 if (!oleHoldings.getLink().get(0).getUrl().equalsIgnoreCase(tipp.getPlatformHostUrl())) {
1345                     String details = "[" + eHolding.getDisplayLabel() + "]]: E-Instance URL has changed from [" + oleHoldings.getLink().get(0).getUrl() + "] to [" + tipp.getPlatformHostUrl() + "]";
1346                     createChangeLog(oleeResourceRecordDocument.getOleERSIdentifier(), "Metadata change: E-Instance URL", details);
1347                 }
1348             }
1349         }
1350     }
1351 
1352     public void addorUpdatePlatform(OleGokbPlatform oleGokbPlatform, OLEEResourceRecordDocument oleeResourceRecordDocument, String logId) {
1353 
1354         Map map = new HashMap();
1355         map.put("gokbPlatformId", oleGokbPlatform.getGokbPlatformId());
1356         List<OleGokbTipp> oleGokbTipps = (List<OleGokbTipp>) getBusinessObjectService().findMatching(OleGokbTipp.class, map);
1357         OleGokbTitle oleGokbTitle = getBusinessObjectService().findBySinglePrimaryKey(OleGokbTitle.class, oleGokbTipps.get(0));
1358         oleeResourceSearchService.updatePublisherDetails(oleeResourceRecordDocument, oleGokbTitle.getPublisherId());
1359         Map gokbMap = new HashMap();
1360         gokbMap.put(OLEConstants.GOKB_ID, oleGokbPlatform.getGokbPlatformId());
1361         List<OLEPlatformRecordDocument> platformRecordDocumentList = (List<OLEPlatformRecordDocument>) getBusinessObjectService().findMatching(OLEPlatformRecordDocument.class, gokbMap);
1362         OLEPlatformRecordDocument olePlatformRecordDocument = null;
1363         if (platformRecordDocumentList != null && platformRecordDocumentList.size() > 0) {
1364             olePlatformRecordDocument = platformRecordDocumentList.get(0);
1365             oleeResourceSearchService.updatePlatform(olePlatformRecordDocument, oleGokbPlatform.getPlatformName(), oleGokbPlatform.getStatus(), oleGokbPlatform.getSoftwarePlatform(), oleGokbPlatform.getPlatformProviderId());
1366             updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "platformsUpdatedCount", 1);
1367         } else {
1368             olePlatformRecordDocument = oleeResourceSearchService.createPlatform(oleGokbPlatform.getPlatformName(), oleGokbPlatform.getGokbPlatformId(), oleGokbPlatform.getSoftwarePlatform(), oleGokbPlatform.getStatus(), oleGokbPlatform.getPlatformProviderId());
1369             updateLog(logId, oleeResourceRecordDocument.getOleERSIdentifier(), "platformsAddedCount", 1);
1370         }
1371         if (olePlatformRecordDocument != null) {
1372             updatePlatformVendorAssociation(olePlatformRecordDocument);
1373         }
1374         createOrUpdateVendor(oleGokbPlatform.getPlatformProviderId(), logId);
1375 
1376     }
1377 
1378     public void createToDo(String eResId, String type, String details, Integer tippId) {
1379         OleGokbReview oleGokbReview = new OleGokbReview();
1380         oleGokbReview.setDetails(details);
1381         oleGokbReview.setType(type);
1382         oleGokbReview.setOleERSIdentifier(eResId);
1383         oleGokbReview.setReviewDate(new Timestamp(System.currentTimeMillis()));
1384         if (tippId != null) {
1385             oleGokbReview.setGokbTippId(tippId);
1386         }
1387         getBusinessObjectService().save(oleGokbReview);
1388     }
1389 
1390     public void createChangeLog(String eResId, String type, String details) {
1391         OleGokbChangeLog oleGokbChangeLog = new OleGokbChangeLog();
1392         oleGokbChangeLog.setType(type);
1393         oleGokbChangeLog.setDetails(details);
1394         oleGokbChangeLog.setOleERSIdentifier(eResId);
1395         oleGokbChangeLog.setChangeLogDate(new Timestamp(System.currentTimeMillis()));
1396         oleGokbChangeLog.setOrigin("System");
1397     }
1398 
1399     public void updatePlatformVendorAssociation(OLEPlatformRecordDocument olePlatformRecordDocument) {
1400         if (olePlatformRecordDocument != null) {
1401             Map roleMap = new HashMap();
1402             roleMap.put("associatedEntityId", olePlatformRecordDocument.getOlePlatformId());
1403             roleMap.put("vendorRoleId", "3");
1404             List<OLEVendorAssociation> vendorAssociations = (List<OLEVendorAssociation>) getBusinessObjectService().findMatching(OLEVendorAssociation.class, roleMap);
1405             if (!(vendorAssociations != null && vendorAssociations.size() > 0)) {
1406                 if (olePlatformRecordDocument.getOlePlatformId() != null && olePlatformRecordDocument.getVendorHeaderGeneratedIdentifier() != null && olePlatformRecordDocument.getVendorDetailAssignedIdentifier() != null) {
1407                     OLEVendorAssociation oleVendorAssociation = new OLEVendorAssociation();
1408                     oleVendorAssociation.setVendorHeaderGeneratedIdentifier(olePlatformRecordDocument.getVendorHeaderGeneratedIdentifier());
1409                     oleVendorAssociation.setVendorDetailAssignedIdentifier(olePlatformRecordDocument.getVendorDetailAssignedIdentifier());
1410                     oleVendorAssociation.setVendorRoleId("3");
1411                     oleVendorAssociation.setAssociatedEntityId(olePlatformRecordDocument.getOlePlatformId());
1412                     getBusinessObjectService().save(oleVendorAssociation);
1413                 }
1414             }
1415         }
1416     }
1417 
1418 
1419     public void insertOrUpdateGokbDataMapping(OleHoldings oleHoldings, boolean isUpdate) {
1420 
1421         String holdingsId = oleHoldings.getHoldingsIdentifier();
1422 
1423         if (StringUtils.isNotEmpty(oleHoldings.getEResourceId()) && StringUtils.isNotEmpty(holdingsId) && oleHoldings.getGokbIdentifier() != null) {
1424 
1425             GOKbDataElement urlElement = getDataElement(url);
1426             GOKbDataElement gokbIdElement = getDataElement(gokbId);
1427 
1428             if (StringUtils.isNotEmpty(gokbId)) {
1429                 saveGokbMappingValue(holdingsId, gokbIdElement, String.valueOf(oleHoldings.getGokbIdentifier()), isUpdate, "E-Instance");
1430             }
1431 
1432             if (oleHoldings.getLink() != null && oleHoldings.getLink().size() > 0) {
1433                 saveGokbMappingValue(holdingsId, urlElement, oleHoldings.getLink().get(0).getUrl(), isUpdate, "E-Instance");
1434             }
1435 
1436 
1437             if (oleHoldings.getExtentOfOwnership() != null && oleHoldings.getExtentOfOwnership().get(0) != null && oleHoldings.getExtentOfOwnership().get(0).getCoverages() != null && oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage() != null && oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().get(0) != null) {
1438                 Coverage coverage = oleHoldings.getExtentOfOwnership().get(0).getCoverages().getCoverage().get(0);
1439 
1440                 GOKbDataElement coverageStartDateElement = getDataElement(coverageStartDate);
1441                 GOKbDataElement coverageStartIssueElement = getDataElement(coverageStartIssue);
1442                 GOKbDataElement coverageStartVolumeeElement = getDataElement(coverageStartVolume);
1443                 GOKbDataElement coverageEndDateElement = getDataElement(coverageEndDate);
1444                 GOKbDataElement coverageEndIssueElement = getDataElement(coverageEndIssue);
1445                 GOKbDataElement coverageEndVolumeElement = getDataElement(coverageEndVolume);
1446 
1447                 String startDate = coverage.getCoverageStartDate();
1448                 String startIssue = coverage.getCoverageStartIssue();
1449                 String startVolume = coverage.getCoverageStartVolume();
1450                 String endDate = coverage.getCoverageEndDate();
1451                 String endIssue = coverage.getCoverageEndIssue();
1452                 String endVolume = coverage.getCoverageEndVolume();
1453 
1454                 if (StringUtils.isNotEmpty(startDate)) {
1455                     saveGokbMappingValue(holdingsId, coverageStartDateElement, startDate, isUpdate, "E-Instance");
1456                 }
1457                 if (StringUtils.isNotEmpty(startDate)) {
1458                     saveGokbMappingValue(holdingsId, coverageStartIssueElement, startIssue, isUpdate, "E-Instance");
1459                 }
1460                 if (StringUtils.isNotEmpty(startDate)) {
1461                     saveGokbMappingValue(holdingsId, coverageStartVolumeeElement, startVolume, isUpdate, "E-Instance");
1462                 }
1463                 if (StringUtils.isNotEmpty(startDate)) {
1464                     saveGokbMappingValue(holdingsId, coverageEndDateElement, endDate, isUpdate, "E-Instance");
1465                 }
1466                 if (StringUtils.isNotEmpty(startDate)) {
1467                     saveGokbMappingValue(holdingsId, coverageEndIssueElement, endIssue, isUpdate, "E-Instance");
1468                 }
1469                 if (StringUtils.isNotEmpty(startDate)) {
1470                     saveGokbMappingValue(holdingsId, coverageEndVolumeElement, endVolume, isUpdate, "E-Instance");
1471                 }
1472             }
1473         }
1474     }
1475 
1476 
1477     private void saveGokbMappingValue(String recordId, GOKbDataElement goKbDataElement, String value, boolean isUpdate, String recType) {
1478         OLEGOKbMappingValue olegoKbMappingValue = null;
1479 
1480 
1481         Map map = new HashMap();
1482         map.put("recordId", recordId);
1483         map.put("dataElementId", goKbDataElement.getDataElementId());
1484         List<OLEGOKbMappingValue> olegoKbMappingValues = (List<OLEGOKbMappingValue>) getBusinessObjectService().findMatching(OLEGOKbMappingValue.class, map);
1485         if (olegoKbMappingValues != null && olegoKbMappingValues.size() > 0) {
1486             olegoKbMappingValue = olegoKbMappingValues.get(0);
1487         } else {
1488             olegoKbMappingValue = new OLEGOKbMappingValue();
1489             olegoKbMappingValue.setGokbValue(value);
1490         }
1491         if (isUpdate) {
1492             olegoKbMappingValue.setLocalValue(value);
1493         }
1494 
1495         olegoKbMappingValue.setGoKbDataElement(goKbDataElement);
1496         olegoKbMappingValue.setRecordId(recordId);
1497         olegoKbMappingValue.setRecordType(recType);
1498         getBusinessObjectService().save(olegoKbMappingValue);
1499     }
1500 
1501     public GOKbDataElement getDataElement(String dataElementName) {
1502         Map dataElementMap = new HashMap();
1503         dataElementMap.put("dataElementName", dataElementName);
1504         List<GOKbDataElement> goKbDataElements = (List<GOKbDataElement>) getBusinessObjectService().findMatching(GOKbDataElement.class, dataElementMap);
1505         if (goKbDataElements != null && goKbDataElements.size() > 0) {
1506             return goKbDataElements.get(0);
1507         }
1508         return null;
1509     }
1510 
1511     public void insertOrUpdateGokbElementsForEResource(OLEEResourceRecordDocument oleeResourceRecordDocument, boolean isUpdate) {
1512 
1513 
1514         String eResourceId = oleeResourceRecordDocument.getOleERSIdentifier();
1515 
1516         GOKbDataElement eResourceNameDataElement = getDataElement(E_RESOURCE_NAME);
1517         GOKbDataElement variantDataElement = getDataElement(VARIANT_NAME);
1518         GOKbDataElement publisherDataElement = getDataElement(PUBLISHER);
1519         GOKbDataElement packageScopeDataElement = getDataElement(PACKAGE_SCOPE);
1520         GOKbDataElement breakableDataElement = getDataElement(BREAKABLE);
1521         GOKbDataElement fixedTitleDataElement = getDataElement(FIXED_TITLE_LIST);
1522         GOKbDataElement packageTypeDataElement = getDataElement(PACKAGE_TYPE);
1523         GOKbDataElement eResourceDataElement = getDataElement(E_RESOURCE_GOKBID);
1524 
1525         if (oleeResourceRecordDocument.getGokbIdentifier() != null) {
1526 
1527             String title = oleeResourceRecordDocument.getTitle();
1528             if (StringUtils.isNotEmpty(title)) {
1529                 saveGokbMappingValue(eResourceId, eResourceNameDataElement, title, isUpdate, "E-Resource");
1530             }
1531 
1532             List<OLEEResourceVariantTitle> oleEResourceVariantTitleList = oleeResourceRecordDocument.getOleEResourceVariantTitleList();
1533 
1534             if (oleEResourceVariantTitleList != null && oleEResourceVariantTitleList.size() > 0) {
1535                 saveGokbMappingValue(eResourceId, variantDataElement, oleEResourceVariantTitleList.get(0).getOleVariantTitle(), isUpdate, "E-Resource");
1536             }
1537 
1538 
1539             String publisher = oleeResourceRecordDocument.getPublisher();
1540             if (StringUtils.isNotEmpty(publisher)) {
1541                 saveGokbMappingValue(eResourceId, publisherDataElement, publisher, isUpdate, "E-Resource");
1542             }
1543 
1544             String packageScope = oleeResourceRecordDocument.getPackageScopeId();
1545             if (StringUtils.isNotEmpty(packageScope)) {
1546                 saveGokbMappingValue(eResourceId, packageScopeDataElement, packageScope, isUpdate, "E-Resource");
1547             }
1548 
1549             if (oleeResourceRecordDocument.isBreakable()) {
1550                 saveGokbMappingValue(eResourceId, breakableDataElement, "true", isUpdate, "E-Resource");
1551             } else {
1552                 saveGokbMappingValue(eResourceId, breakableDataElement, "false", isUpdate, "E-Resource");
1553             }
1554 
1555             if (oleeResourceRecordDocument.isFixedTitleList()) {
1556                 saveGokbMappingValue(eResourceId, fixedTitleDataElement, "true", isUpdate, "E-Resource");
1557             } else {
1558                 saveGokbMappingValue(eResourceId, fixedTitleDataElement, "false", isUpdate, "E-Resource");
1559             }
1560 
1561             String packageType = oleeResourceRecordDocument.getPackageTypeId();
1562 
1563             if (StringUtils.isNotEmpty(packageType)) {
1564                 saveGokbMappingValue(eResourceId, packageTypeDataElement, packageType, isUpdate, "E-Resource");
1565             }
1566 
1567             if (oleeResourceRecordDocument.getGokbIdentifier() != null) {
1568                 saveGokbMappingValue(eResourceId, eResourceDataElement, String.valueOf(oleeResourceRecordDocument.getGokbIdentifier()), isUpdate, "E-Resource");
1569             }
1570         }
1571     }
1572 
1573     public void applyGOKbChangesToOle(String eResId) {
1574         OLEEResourceSynchronizationGokbLog oleeResourceSynchronizationGokbLog = null;
1575         if (StringUtils.isNotEmpty(eResId)) {
1576             oleeResourceSynchronizationGokbLog = new OLEEResourceSynchronizationGokbLog();
1577             oleeResourceSynchronizationGokbLog.setOleERSIdentifier(eResId);
1578         } else {
1579             oleeResourceSynchronizationGokbLog = new OLEEResourceSynchronizationGokbLog();
1580         }
1581         oleeResourceSynchronizationGokbLog.setStartTime(new Timestamp(System.currentTimeMillis()));
1582         getBusinessObjectService().save(oleeResourceSynchronizationGokbLog);
1583         applyGokbChangesToVendors(eResId, oleeResourceSynchronizationGokbLog.geteResSynchronizationGokbLogId());
1584         applyGokbChangesToPlatforms(eResId, oleeResourceSynchronizationGokbLog.geteResSynchronizationGokbLogId());
1585         applyGokbChangesToEresources(eResId, oleeResourceSynchronizationGokbLog.geteResSynchronizationGokbLogId());
1586         OLEEResourceSynchronizationGokbLog oleeResourceSynchronization = getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceSynchronizationGokbLog.class, oleeResourceSynchronizationGokbLog.geteResSynchronizationGokbLogId());
1587         oleeResourceSynchronizationGokbLog.setEndTime(new Timestamp(System.currentTimeMillis()));
1588         oleeResourceSynchronizationGokbLog.setUpdatedDate(new Timestamp(System.currentTimeMillis()));
1589         getBusinessObjectService().save(oleeResourceSynchronization);
1590     }
1591 
1592     public void updateLog(String updateId, String eResId, String column, int value) {
1593         OLEEResourceSynchronizationGokbLog oleeResourceSynchronizationGokbLog = getBusinessObjectService().findBySinglePrimaryKey(OLEEResourceSynchronizationGokbLog.class, updateId);
1594         if (oleeResourceSynchronizationGokbLog != null) {
1595             if (StringUtils.isNotEmpty(eResId)) {
1596                 oleeResourceSynchronizationGokbLog.setOleERSIdentifier(eResId);
1597             }
1598             if (column.equalsIgnoreCase("eResUpdatedCount")) {
1599                 if (oleeResourceSynchronizationGokbLog.geteHoldingsUpdatedCount() != null) {
1600                     oleeResourceSynchronizationGokbLog.seteResUpdatedCount(oleeResourceSynchronizationGokbLog.geteHoldingsUpdatedCount() + value);
1601                 } else {
1602                     oleeResourceSynchronizationGokbLog.seteHoldingsUpdatedCount(value);
1603                 }
1604             } else if (column.equalsIgnoreCase("eHoldingsAddedCount")) {
1605                 if (oleeResourceSynchronizationGokbLog.geteHoldingsAddedCount() != null) {
1606                     oleeResourceSynchronizationGokbLog.seteHoldingsAddedCount(oleeResourceSynchronizationGokbLog.geteHoldingsAddedCount() + value);
1607                 } else {
1608                     oleeResourceSynchronizationGokbLog.seteHoldingsAddedCount(value);
1609                 }
1610             } else if (column.equalsIgnoreCase("eHoldingsUpdatedCount")) {
1611                 if (oleeResourceSynchronizationGokbLog.getVendorsUpdatedCount() != null) {
1612                     oleeResourceSynchronizationGokbLog.seteHoldingsUpdatedCount(oleeResourceSynchronizationGokbLog.getVendorsUpdatedCount() + value);
1613                 } else {
1614                     oleeResourceSynchronizationGokbLog.setVendorsUpdatedCount(value);
1615                 }
1616             } else if (column.equalsIgnoreCase("eHoldingsRetiredCount")) {
1617                 if (oleeResourceSynchronizationGokbLog.geteHoldingsRetiredCount() != null) {
1618                     oleeResourceSynchronizationGokbLog.seteHoldingsRetiredCount(oleeResourceSynchronizationGokbLog.geteHoldingsRetiredCount() + value);
1619                 } else {
1620                     oleeResourceSynchronizationGokbLog.seteHoldingsRetiredCount(value);
1621                 }
1622             } else if (column.equalsIgnoreCase("eHoldingsDeletedCount")) {
1623                 if (oleeResourceSynchronizationGokbLog.geteHoldingsDeletedCount() != null) {
1624                     oleeResourceSynchronizationGokbLog.seteHoldingsDeletedCount(oleeResourceSynchronizationGokbLog.geteHoldingsDeletedCount() + value);
1625                 } else {
1626                     oleeResourceSynchronizationGokbLog.seteHoldingsDeletedCount(value);
1627                 }
1628             } else if (column.equalsIgnoreCase("eResUpdatedCount")) {
1629                 if (oleeResourceSynchronizationGokbLog.geteResUpdatedCount() != null) {
1630                     oleeResourceSynchronizationGokbLog.seteResUpdatedCount(oleeResourceSynchronizationGokbLog.geteResUpdatedCount() + value);
1631                 } else {
1632                     oleeResourceSynchronizationGokbLog.seteResUpdatedCount(value);
1633                 }
1634             } else if (column.equalsIgnoreCase("bibAddedCount")) {
1635                 if (oleeResourceSynchronizationGokbLog.getBibAddedCount() != null) {
1636                     oleeResourceSynchronizationGokbLog.setBibAddedCount(oleeResourceSynchronizationGokbLog.getBibAddedCount() + value);
1637                 } else {
1638                     oleeResourceSynchronizationGokbLog.setBibAddedCount(value);
1639                 }
1640             } else if (column.equalsIgnoreCase("vendorsAddedCount")) {
1641                 if (oleeResourceSynchronizationGokbLog.getVendorsAddedCount() != null) {
1642                     oleeResourceSynchronizationGokbLog.setVendorsAddedCount(oleeResourceSynchronizationGokbLog.getVendorsAddedCount() + value);
1643                 } else {
1644                     oleeResourceSynchronizationGokbLog.setVendorsAddedCount(value);
1645                 }
1646             } else if (column.equalsIgnoreCase("vendorsUpdatedCount")) {
1647                 if (oleeResourceSynchronizationGokbLog.getVendorsUpdatedCount() != null) {
1648                     oleeResourceSynchronizationGokbLog.setVendorsUpdatedCount(oleeResourceSynchronizationGokbLog.getVendorsUpdatedCount() + value);
1649                 } else {
1650                     oleeResourceSynchronizationGokbLog.setVendorsUpdatedCount(value);
1651                 }
1652             } else if (column.equalsIgnoreCase("platformsAddedCount")) {
1653                 if (oleeResourceSynchronizationGokbLog.getPlatformsAddedCount() != null) {
1654                     oleeResourceSynchronizationGokbLog.setPlatformsAddedCount(oleeResourceSynchronizationGokbLog.getPlatformsAddedCount() + value);
1655                 } else {
1656                     oleeResourceSynchronizationGokbLog.setPlatformsAddedCount(value);
1657                 }
1658             } else if (column.equalsIgnoreCase("platformsUpdatedCount")) {
1659                 if (oleeResourceSynchronizationGokbLog.getPlatformsUpdatedCount() != null) {
1660                     oleeResourceSynchronizationGokbLog.setPlatformsUpdatedCount(oleeResourceSynchronizationGokbLog.getPlatformsUpdatedCount() + value);
1661                 } else {
1662                     oleeResourceSynchronizationGokbLog.setPlatformsUpdatedCount(value);
1663                 }
1664             }
1665             getBusinessObjectService().save(oleeResourceSynchronizationGokbLog);
1666         }
1667     }
1668 
1669     public void retrieveAndApplyGokbChanges(){
1670         retrieveGokbChangesFromRemote();
1671         applyGOKbChangesToOle("");
1672     }
1673 
1674     public void retrieveGokbChangesFromRemote(){
1675         Timestamp lastUpdatedTime = getGokbRdbmsService().getUpdatedDate();
1676         if(lastUpdatedTime == null) {
1677             getGokbLocalService().initLocalGokb();
1678         } else {
1679             String stringFromTimeStamp = OleGokbXmlUtil.getStringFromTimeStamp(lastUpdatedTime);
1680             getGokbLocalService().updateLocalGokb(stringFromTimeStamp+"Z");
1681         }
1682     }
1683 
1684 }