View Javadoc
1   package org.kuali.ole.docstore.document.rdbms;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.DocumentUniqueIDPrefix;
5   import org.kuali.ole.docstore.OleDocStoreException;
6   import org.kuali.ole.docstore.model.rdbms.bo.*;
7   import org.kuali.ole.docstore.model.enums.DocCategory;
8   import org.kuali.ole.docstore.model.enums.DocFormat;
9   import org.kuali.ole.docstore.model.enums.DocType;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
12  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
13  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.*;
14  import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEInstanceOlemlRecordProcessor;
15  import org.kuali.rice.krad.service.BusinessObjectService;
16  import org.kuali.rice.krad.service.KRADServiceLocator;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  import javax.jcr.Node;
21  import java.sql.Timestamp;
22  import java.text.SimpleDateFormat;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * Created with IntelliJ IDEA.
30   * User: sambasivam
31   * Date: 7/22/13
32   * Time: 3:35 PM
33   * To change this template use File | Settings | File Templates.
34   */
35  @Deprecated
36  public class RdbmsWorkEInstanceDocumentManager extends RdbmsAbstarctDocumentManager {
37  
38      private static RdbmsWorkEInstanceDocumentManager ourInstanceRdbms = null;
39      private static final Logger LOG = LoggerFactory.getLogger(RdbmsWorkEInstanceDocumentManager.class);
40      private BusinessObjectService businessObjectService;
41  
42      public static RdbmsWorkEInstanceDocumentManager getInstance() {
43          if (null == ourInstanceRdbms) {
44              ourInstanceRdbms = new RdbmsWorkEInstanceDocumentManager();
45          }
46          return ourInstanceRdbms;
47      }
48  
49      public BusinessObjectService getBusinessObjectService() {
50          if (null == businessObjectService) {
51              businessObjectService = KRADServiceLocator.getBusinessObjectService();
52          }
53          return businessObjectService;
54      }
55  
56  
57      @Override
58      public void deleteDocs(RequestDocument requestDocument, Object object) {
59          ResponseDocument responseDocument = new ResponseDocument();
60          BusinessObjectService businessObjectService = (BusinessObjectService) object;
61          EInstanceRecord eInstanceRecord = new EInstanceRecord();
62          EHoldingsRecord eHoldingsRecord = new EHoldingsRecord();
63          String eInstanceId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid());
64          Map itemMap = new HashMap();
65          itemMap.put("eInstanceIdentifier", eInstanceId);
66          eInstanceRecord.seteInstanceIdentifier(eInstanceId);
67          eHoldingsRecord.seteHoldingsIdentifier(eInstanceId);
68          deleteEHoldingNChildRecords(eHoldingsRecord);
69          getBusinessObjectService().delete(eInstanceRecord);
70          buildResponseDocument(requestDocument, eInstanceRecord, responseDocument);
71      }
72  
73      @Override
74      public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
75          ResponseDocument respDoc = new ResponseDocument();
76          Map eInstanceMap = new HashMap();
77          eInstanceMap.put("eInstanceIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
78          EInstanceRecord eInstanceRecord = getBusinessObjectService().findByPrimaryKey(EInstanceRecord.class, eInstanceMap);
79          String content = buildEInstanceContent(eInstanceRecord);
80          Content contentObj = new Content();
81          contentObj.setContent(content);
82          respDoc.setUuid(requestDocument.getUuid());
83          respDoc.setCategory(requestDocument.getCategory());
84          respDoc.setType(requestDocument.getType());
85          respDoc.setFormat(requestDocument.getFormat());
86          respDoc.setContent(contentObj);
87          return respDoc;
88      }
89  
90      @Override
91      public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException {
92          //To change body of implemented methods use File | Settings | File Templates.
93      }
94  
95      @Override
96      public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException {
97          businessObjectService = (BusinessObjectService) object;
98          String bibUUid = null;
99          WorkEInstanceOlemlRecordProcessor workEInstanceOlemlRecordProcessor = new WorkEInstanceOlemlRecordProcessor();
100         InstanceCollection instanceCollection = workEInstanceOlemlRecordProcessor.fromXML(requestDocument.getContent().getContent());
101         EInstance eInstance = instanceCollection.getEInstance().get(0);
102         EHoldings eHoldings = eInstance.getEHoldings();
103 
104         for (EInstance inst : instanceCollection.getEInstance()) {
105             List<String> resIdList = new ArrayList<String>();
106             resIdList.addAll(inst.getResourceIdentifier());
107             List<String> resIdList1 = new ArrayList<String>();
108             resIdList1.addAll(resIdList);
109             for (String resId : inst.getResourceIdentifier()) {
110                 Map bibMap = new HashMap();
111                 bibMap.put("bibId", DocumentUniqueIDPrefix.getDocumentId(resId));
112                 List<BibRecord> bibRecords = (List<BibRecord>) getBusinessObjectService().findMatching(BibRecord.class, bibMap);
113                 if (bibRecords != null && bibRecords.size() == 0) {
114                     resIdList1.remove(resId);
115                 }
116             }
117             inst.getResourceIdentifier().clear();
118             inst.getResourceIdentifier().addAll(resIdList1);
119         }
120         if (eInstance.getResourceIdentifier() != null && eInstance.getResourceIdentifier().size() > 0) {
121             bibUUid = DocumentUniqueIDPrefix.getDocumentId(eInstance.getResourceIdentifier().get(0));
122         }
123         String eInstancePrefix = DocumentUniqueIDPrefix.getPrefix(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
124         EInstanceRecord eInstanceRecord = saveEInstanceRecord(eInstance, bibUUid, eInstancePrefix);
125 
126         String eHoldingsPrefix = DocumentUniqueIDPrefix.getPrefix(requestDocument.getCategory(), DocType.EHOLDINGS.getCode(), requestDocument.getFormat());
127         EHoldingsRecord eHoldingsRecord = saveEHoldingsRecord(eHoldings, eInstanceRecord.geteInstanceIdentifier(), eHoldingsPrefix);
128         eInstance.setInstanceIdentifier(DocumentUniqueIDPrefix.getPrefixedId(eInstanceRecord.getUniqueIdPrefix(), eInstanceRecord.geteInstanceIdentifier()));
129         eHoldings.setHoldingsIdentifier(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
130 
131         requestDocument.getContent().setContentObject(instanceCollection);
132         requestDocument.getContent().setContent(workEInstanceOlemlRecordProcessor.toXML(instanceCollection));
133         requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eInstanceRecord.getUniqueIdPrefix(), eInstanceRecord.geteInstanceIdentifier()));
134         buildResponseDocument(responseDocument, eInstanceRecord, eHoldingsRecord);
135         return null;
136     }
137 
138     @Override
139     public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
140         ResponseDocument responseDocument = new ResponseDocument();
141         responseDocument.setStatus("success");
142         return responseDocument;
143     }
144 
145     private void buildResponseDocument(ResponseDocument responseDocument, EInstanceRecord eInstanceRecord, EHoldingsRecord eHoldingsRecord) {
146         responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eInstanceRecord.getUniqueIdPrefix(), eInstanceRecord.geteInstanceIdentifier()));
147         responseDocument.setCategory(DocCategory.WORK.getCode());
148         responseDocument.setType(DocType.EINSTANCE.getCode());
149         responseDocument.setFormat(DocFormat.OLEML.getCode());
150         responseDocument.setStatus("Success");
151 
152         List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
153 
154         ResponseDocument holdingResponseDocument = new ResponseDocument();
155         holdingResponseDocument.setCategory(DocCategory.WORK.getCode());
156         holdingResponseDocument.setType(DocType.EHOLDINGS.getCode());
157         holdingResponseDocument.setFormat(DocFormat.OLEML.getCode());
158         // holdingResponseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
159         holdingResponseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
160         responseDocuments.add(holdingResponseDocument);
161 
162         responseDocument.setLinkedDocuments(responseDocuments);
163     }
164 
165     public ResponseDocument buildResponseDocument(RequestDocument requestDocument, EInstanceRecord eInstanceRecord, ResponseDocument responseDocument) {
166         responseDocument.setId(eInstanceRecord.geteInstanceIdentifier());
167         responseDocument.setCategory(requestDocument.getCategory());
168         responseDocument.setType(requestDocument.getType());
169         responseDocument.setFormat(requestDocument.getFormat());
170         responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eInstanceRecord.getUniqueIdPrefix(),
171                 eInstanceRecord.geteInstanceIdentifier()));
172         responseDocument.setId(eInstanceRecord.geteInstanceIdentifier());
173         return responseDocument;
174     }
175 
176     private EHoldingsRecord saveEHoldingsRecord(EHoldings eHoldings, String eInstanceId, String eHoldingsPrefix) {
177 
178         StringBuffer locationLevel = new StringBuffer("");
179 
180         EHoldingsRecord eHoldingsRecord = new EHoldingsRecord();
181         eHoldingsRecord.seteInstanceIdentifier(eInstanceId);
182         eHoldingsRecord.setUniqueIdPrefix(eHoldingsPrefix);
183 
184         if (eHoldings.getLocation() != null) {
185             eHoldingsRecord.setLocation(getLocation(eHoldings.getLocation(), locationLevel));
186             eHoldingsRecord.setLocationLevel(locationLevel.toString());
187         }
188 
189         if (eHoldings.getAccessInformation() != null) {
190             eHoldingsRecord.setAccessLocation(eHoldings.getAccessInformation().getAccessLocation());
191             eHoldingsRecord.setAccessPassword(eHoldings.getAccessInformation().getAccessPassword());
192             eHoldingsRecord.setAccessUsername(eHoldings.getAccessInformation().getAccessUsername());
193             eHoldingsRecord.setAuthenticationType(eHoldings.getAccessInformation().getAuthenticationType());
194             eHoldingsRecord.setNumberSimultaneousUsers(eHoldings.getAccessInformation().getNumberOfSimultaneousUser());
195             eHoldingsRecord.setProxiedResource(eHoldings.getAccessInformation().getProxiedResource());
196         }
197         if (eHoldings.getCallNumber() != null) {
198             CallNumber callNumber = eHoldings.getCallNumber();
199             eHoldingsRecord.setCallNumberPrefix(callNumber.getPrefix());
200             eHoldingsRecord.setCallNumber(callNumber.getNumber());
201             if (callNumber.getShelvingOrder() != null) {
202                 eHoldingsRecord.setShelvingOrder(callNumber.getShelvingOrder().getFullValue());
203             }
204             CallNumberTypeRecord callNumberTypeRecord = saveCallNumberTypeRecord(callNumber.getCallNumberType());
205             eHoldingsRecord.setCallNumberTypeId(callNumberTypeRecord == null ? null : callNumberTypeRecord.getCallNumberTypeId());
206 
207         }
208         eHoldingsRecord.setAccessStatus(eHoldings.getAccessStatus());
209         eHoldingsRecord.setDonorNote(eHoldings.getDonorNote());
210         eHoldingsRecord.setDonorPublicDisplayNote(eHoldings.getDonorPublicDisplay());
211         if (eHoldings.getLink() != null) {
212             eHoldingsRecord.setLink(eHoldings.getLink().getUrl() != null ? eHoldings.getLink().getUrl() : "");
213             eHoldingsRecord.setLinkText(eHoldings.getLink().getText() != null ? eHoldings.getLink().getText() : "");
214         }
215         eHoldingsRecord.setOrderFormat(eHoldings.getOrderFormat() != null ? eHoldings.getOrderFormat() : "");
216         eHoldingsRecord.setOrderType(eHoldings.getOrderType() != null ? eHoldings.getOrderType() : "");
217         eHoldingsRecord.setImprint(eHoldings.getImprint() != null ? eHoldings.getImprint() : "");
218 
219         //eHoldingsRecord.setStatusDate(eHoldings.getStatusDate().toGregorianCalendar() != null ? eHoldings.getStatusDate(): "");
220 
221         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
222         Timestamp statusDate = null;
223         try {
224             if (eHoldings.getStatusDate() != null) {
225                 statusDate = new Timestamp(sdf.parse(eHoldings.getStatusDate()).getTime());
226 
227             }
228         } catch (Exception e) {
229             LOG.info("Exception :", e);
230         }
231         eHoldingsRecord.setStatusDate(statusDate);
232 
233         eHoldingsRecord.seteResourceId(eHoldings.getEResourceId());
234         eHoldingsRecord.seteResourceTitle(eHoldings.getEResourceTitle());
235         eHoldingsRecord.setPublisher(eHoldings.getPublisher());
236         eHoldingsRecord.setLocalPersistentLink(eHoldings.getLocalPersistentLink());
237         eHoldingsRecord.setVendor(eHoldings.getVendor());
238         eHoldingsRecord.setPurchaseOrderId(eHoldings.getPurchaseOrderId());
239 
240         eHoldingsRecord.setiLLAllowed(eHoldings.isInterLibraryLoanAllowed());
241         eHoldingsRecord.setStaffOnly(eHoldings.isStaffOnlyFlag());
242         eHoldingsRecord.setStatisticalSearchCode(eHoldings.getStatisticalSearchingCode());
243         eHoldingsRecord.setSubscriptionStatus(eHoldings.getSubscriptionStatus());
244         if (eHoldings.getPlatform() != null) {
245             eHoldingsRecord.setPlatform(eHoldings.getPlatform().getPlatformName());
246             eHoldingsRecord.setAdminURL(eHoldings.getPlatform().getAdminUrl());
247             eHoldingsRecord.setAdminPassword(eHoldings.getPlatform().getAdminPassword());
248             eHoldingsRecord.setAdminUsername(eHoldings.getPlatform().getAdminUserName());
249         }
250 
251         if (eHoldings.getInvoice() != null) {
252             eHoldingsRecord.setPaymentStatus(eHoldings.getInvoice().getPaymentStatus());
253             eHoldingsRecord.setFundCode(eHoldings.getInvoice().getFundCode());
254         }
255 
256         getBusinessObjectService().save(eHoldingsRecord);
257 
258         if (eHoldings.getExtentOfOwnership() != null) {
259             if (eHoldings.getExtentOfOwnership().getCoverages() != null) {
260                 eHoldingsRecord.seteInstanceCoverageRecordList(saveCoverageRecord(eHoldings.getExtentOfOwnership().getCoverages(), eHoldingsRecord.geteHoldingsIdentifier()));
261             }
262             if (eHoldings.getExtentOfOwnership().getPerpetualAccesses() != null) {
263                 eHoldingsRecord.seteInstancePerpetualAccessRecordList(savePerpetualAccessRecord(eHoldings.getExtentOfOwnership().getPerpetualAccesses(), eHoldingsRecord.geteHoldingsIdentifier()));
264             }
265         }
266         if (eHoldings.getNote() != null && eHoldings.getNote().size() > 0) {
267             eHoldingsRecord.seteHoldingsNoteList(saveHoldingsNotes(eHoldings.getNote(), eHoldingsRecord.geteHoldingsIdentifier()));
268         }
269         getBusinessObjectService().save(eHoldingsRecord);
270 
271         return eHoldingsRecord;
272     }
273 
274     //Coverages
275     private List<EInstanceCoverageRecord> saveCoverageRecord(Coverages coverages, String eHoldingsIdentifier) {
276 
277         if (eHoldingsIdentifier != null) {
278             Map parentCriteria1 = new HashMap();
279             parentCriteria1.put("eHoldingsIdentifier", eHoldingsIdentifier);
280             getBusinessObjectService().deleteMatching(EInstanceCoverageRecord.class, parentCriteria1);
281         }
282 
283         List<EInstanceCoverageRecord> coverageList = new ArrayList<>();
284         for (Coverage coverage : coverages.getCoverage()) {
285             EInstanceCoverageRecord eInstanceCoverageRecord = new EInstanceCoverageRecord();
286             eInstanceCoverageRecord.setEHoldingsIdentifier(eHoldingsIdentifier);
287             if(coverage.getCoverageStartDate() != null) {
288                 eInstanceCoverageRecord.setCoverageStartDate(getTimeStampFromString(coverage.getCoverageStartDate()));
289             }
290             eInstanceCoverageRecord.setCoverageStartVolume(coverage.getCoverageStartVolume());
291             eInstanceCoverageRecord.setCoverageStartIssue(coverage.getCoverageStartIssue());
292             if(coverage.getCoverageEndDate() != null) {
293                 eInstanceCoverageRecord.setCoverageEndDate(getTimeStampFromString(coverage.getCoverageEndDate()));
294             }
295             eInstanceCoverageRecord.setCoverageEndVolume(coverage.getCoverageEndVolume());
296             eInstanceCoverageRecord.setCoverageEndIssue(coverage.getCoverageEndIssue());
297             coverageList.add(eInstanceCoverageRecord);
298         }
299         return coverageList;
300     }
301 
302     //Perpetual Access
303     private List<EInstancePerpetualAccessRecord> savePerpetualAccessRecord(PerpetualAccesses perpetualAccesses, String eHoldingsIdentifier) {
304 
305         if (eHoldingsIdentifier != null) {
306             Map parentCriteria1 = new HashMap();
307             parentCriteria1.put("eHoldingsIdentifier", eHoldingsIdentifier);
308             getBusinessObjectService().deleteMatching(EInstancePerpetualAccessRecord.class, parentCriteria1);
309         }
310 
311         List<EInstancePerpetualAccessRecord> perpetualAccessList = new ArrayList<>();
312         for (PerpetualAccess perpetualAccess : perpetualAccesses.getPerpetualAccess()) {
313             EInstancePerpetualAccessRecord eInstancePerpetualAccess = new EInstancePerpetualAccessRecord();
314             eInstancePerpetualAccess.setEHoldingsIdentifier(eHoldingsIdentifier);
315             if(perpetualAccess.getPerpetualAccessStartDate() != null) {
316                 eInstancePerpetualAccess.setPerpetualAccessStartDate(getTimeStampFromString(perpetualAccess.getPerpetualAccessStartDate()));
317             }
318             eInstancePerpetualAccess.setPerpetualAccessStartVolume(perpetualAccess.getPerpetualAccessStartVolume());
319             eInstancePerpetualAccess.setPerpetualAccessStartIssue(perpetualAccess.getPerpetualAccessStartIssue());
320             if(perpetualAccess.getPerpetualAccessEndDate() != null) {
321                 eInstancePerpetualAccess.setPerpetualAccessEndDate(getTimeStampFromString(perpetualAccess.getPerpetualAccessEndDate()));
322             }
323             eInstancePerpetualAccess.setPerpetualAccessEndVolume(perpetualAccess.getPerpetualAccessEndVolume());
324             eInstancePerpetualAccess.setPerpetualAccessEndIssue(perpetualAccess.getPerpetualAccessEndIssue());
325             perpetualAccessList.add(eInstancePerpetualAccess);
326         }
327         return perpetualAccessList;
328     }
329 
330     //Notes
331     private List<EHoldingsNote> saveHoldingsNotes(List<Note> notes, String eHoldingsIdentifier) {
332 
333         if (eHoldingsIdentifier != null) {
334             Map parentCriteria1 = new HashMap();
335             parentCriteria1.put("eHoldingsIdentifier", eHoldingsIdentifier);
336             getBusinessObjectService().deleteMatching(EHoldingsNote.class, parentCriteria1);
337         }
338 
339         List<EHoldingsNote> noteList = new ArrayList<>();
340         for (Note note : notes) {
341             EHoldingsNote eHoldingsNote = new EHoldingsNote();
342             EHoldingsNoteType eHoldingsNoteType = new EHoldingsNoteType();
343             eHoldingsNote.seteHoldingsIdentifier(eHoldingsIdentifier);
344             eHoldingsNote.seteHoldingsNoteText(note.getValue());
345             noteList.add(eHoldingsNote);
346         }
347         return noteList;
348     }
349 
350     private EInstanceRecord saveEInstanceRecord(EInstance eInstance, String bibId, String eInstancePrefix) {
351         EInstanceRecord eInstanceRecord = new EInstanceRecord();
352         eInstanceRecord.setUniqueIdPrefix(eInstancePrefix);
353         eInstanceRecord.setBibId(bibId);
354         eInstanceRecord = getBusinessObjectService().save(eInstanceRecord);
355         return eInstanceRecord;
356     }
357 
358     private String buildEInstanceContent(EInstanceRecord eInstanceRecord) {
359         EInstance eInstance = new EInstance();
360         eInstance.getResourceIdentifier().add(DocumentUniqueIDPrefix.getPrefixedId(DocumentUniqueIDPrefix.PREFIX_WORK_BIB_MARC, eInstanceRecord.getBibId()));
361         InstanceCollection eInstanceCollection = new InstanceCollection();
362         eInstanceRecord.seteHoldingsRecord(eInstanceRecord.geteHoldingsRecord());
363         eInstance.setInstanceIdentifier(DocumentUniqueIDPrefix.getPrefixedId(eInstanceRecord.getUniqueIdPrefix(), eInstanceRecord.geteInstanceIdentifier()));
364         EHoldingsRecord holdingsRecord = eInstanceRecord.geteHoldingsRecord();
365         EHoldings oleHoldings = buildEHoldingsContent(holdingsRecord);
366         eInstance.setEHoldings(oleHoldings);
367         eInstanceCollection.getEInstance().add(eInstance);
368         String content = new WorkEInstanceOlemlRecordProcessor().toXML(eInstanceCollection);
369         return content;
370     }
371 
372     protected EHoldings buildEHoldingsContent(EHoldingsRecord eHoldingsRecord) {
373         EHoldings oleHoldings = new EHoldings();
374         ExtentOfOwnership extentOfOwnership = new ExtentOfOwnership();
375 
376         oleHoldings.setHoldingsIdentifier(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
377         oleHoldings.setAccessStatus(eHoldingsRecord.getAccessStatus());
378 
379         oleHoldings.setEResourceId(eHoldingsRecord.geteResourceId());
380         oleHoldings.setEResourceTitle(eHoldingsRecord.geteResourceTitle());
381 
382         AccessInformation accessInformation = new AccessInformation();
383         accessInformation.setAccessLocation(eHoldingsRecord.getAccessLocation());
384         accessInformation.setAccessPassword(eHoldingsRecord.getAccessPassword());
385         accessInformation.setAccessUsername(eHoldingsRecord.getAccessUsername());
386         accessInformation.setAuthenticationType(eHoldingsRecord.getAuthenticationType());
387         accessInformation.setNumberOfSimultaneousUser(eHoldingsRecord.getNumberSimultaneousUsers());
388         accessInformation.setProxiedResource(eHoldingsRecord.getProxiedResource());
389         oleHoldings.setAccessInformation(accessInformation);
390 
391         Link link = new Link();
392         link.setUrl(eHoldingsRecord.getLink());
393         link.setText(eHoldingsRecord.getLinkText());
394         oleHoldings.setLink(link);
395 
396         oleHoldings.setDonorNote(eHoldingsRecord.getDonorNote());
397         oleHoldings.setDonorPublicDisplay(eHoldingsRecord.getDonorPublicDisplayNote());
398 
399         if (eHoldingsRecord.getStatusDate() != null) {
400             String statusDate = new SimpleDateFormat("dd-MM-yyyy").format(eHoldingsRecord.getStatusDate());
401             oleHoldings.setStatusDate(statusDate);
402         }
403 
404 
405         oleHoldings.setStaffOnlyFlag(Boolean.valueOf(eHoldingsRecord.getStaffOnly()));
406         oleHoldings.setStatisticalSearchingCode(eHoldingsRecord.getStatisticalSearchCode());
407         oleHoldings.setSubscriptionStatus(eHoldingsRecord.getSubscriptionStatus());
408         oleHoldings.setPublisher(eHoldingsRecord.getPublisher());
409 
410         Platform platform = new Platform();
411         platform.setAdminPassword(eHoldingsRecord.getAdminPassword());
412         platform.setAdminUrl(eHoldingsRecord.getAdminURL());
413         platform.setAdminUserName(eHoldingsRecord.getAdminUsername());
414         platform.setPlatformName(eHoldingsRecord.getPlatform());
415         oleHoldings.setPlatform(platform);
416 
417         oleHoldings.setImprint(eHoldingsRecord.getImprint());
418         Invoice invoice = new Invoice();
419         invoice.setCurrentFYCost(eHoldingsRecord.getCurrentFYCost());
420         invoice.setFundCode(eHoldingsRecord.getFundCode());
421         invoice.setPaymentStatus(eHoldingsRecord.getPaymentStatus());
422         oleHoldings.setInvoice(invoice);
423 
424         oleHoldings.setStaffOnlyFlag(Boolean.valueOf(eHoldingsRecord.getStaffOnly()));
425         oleHoldings.setInterLibraryLoanAllowed(Boolean.valueOf(eHoldingsRecord.getiLLAllowed()));
426         oleHoldings.setStatisticalSearchingCode(eHoldingsRecord.getStatisticalSearchCode());
427 
428 
429         oleHoldings.setPurchaseOrderId(eHoldingsRecord.getPurchaseOrderId());
430         oleHoldings.setLocalPersistentLink(eHoldingsRecord.getLocalPersistentLink());
431 
432         if (eHoldingsRecord.getLocation() != null) {
433             Location location = getLocationDetails(eHoldingsRecord.getLocation(), eHoldingsRecord.getLocationLevel());
434             oleHoldings.setLocation(location);
435         }
436         CallNumber callNumber = new CallNumber();
437         if (eHoldingsRecord.getCallNumberTypeRecord() != null) {
438             callNumber.setNumber(eHoldingsRecord.getCallNumber());
439             CallNumberType callNumberType = new CallNumberType();
440             callNumberType.setCodeValue(eHoldingsRecord.getCallNumberTypeRecord().getCode());
441             callNumber.setCallNumberType(callNumberType);
442             ShelvingOrder shelvingOrder = new ShelvingOrder();
443             shelvingOrder.setFullValue(eHoldingsRecord.getShelvingOrder());
444             callNumber.setShelvingOrder(shelvingOrder);
445             callNumber.setPrefix(eHoldingsRecord.getCallNumberPrefix());
446             oleHoldings.setCallNumber(callNumber);
447         }
448         if (eHoldingsRecord.geteInstanceCoverageRecordList() != null && eHoldingsRecord.geteInstanceCoverageRecordList().size() > 0) {
449             Coverages coverages = new Coverages();
450             for (EInstanceCoverageRecord holdingCoverage : eHoldingsRecord.geteInstanceCoverageRecordList()) {
451                 Coverage coverage = new Coverage();
452                 if (holdingCoverage != null) {
453                     if (holdingCoverage.getCoverageStartDate() != null) {
454                         coverage.setCoverageStartDate(new SimpleDateFormat("MM/dd/yyyy").format(holdingCoverage.getCoverageStartDate()));
455                     }
456                     if (holdingCoverage.getCoverageEndDate() != null) {
457                         coverage.setCoverageEndDate(new SimpleDateFormat("MM/dd/yyyy").format(holdingCoverage.getCoverageEndDate()));
458                     }
459                     coverage.setCoverageStartVolume(holdingCoverage.getCoverageStartVolume());
460                     coverage.setCoverageStartIssue(holdingCoverage.getCoverageStartIssue());
461                     coverage.setCoverageEndVolume(holdingCoverage.getCoverageEndVolume());
462                     coverage.setCoverageEndIssue(holdingCoverage.getCoverageEndIssue());
463                     coverages.getCoverage().add(coverage);
464                 }
465             }
466             extentOfOwnership.setCoverages(coverages);
467         }
468         if (eHoldingsRecord.geteInstancePerpetualAccessRecordList() != null && eHoldingsRecord.geteInstancePerpetualAccessRecordList().size() > 0) {
469             PerpetualAccesses perpetualAccesses = new PerpetualAccesses();
470             for (EInstancePerpetualAccessRecord holdingPerpetualAccess : eHoldingsRecord.geteInstancePerpetualAccessRecordList()) {
471                 PerpetualAccess perpetualAccess = new PerpetualAccess();
472                 if (holdingPerpetualAccess != null) {
473                     if (holdingPerpetualAccess.getPerpetualAccessStartDate() != null) {
474                         perpetualAccess.setPerpetualAccessStartDate(new SimpleDateFormat("MM/dd/yyyy").format(holdingPerpetualAccess.getPerpetualAccessStartDate()));
475                     }
476                     if (holdingPerpetualAccess.getPerpetualAccessEndDate() != null) {
477                         perpetualAccess.setPerpetualAccessEndDate(new SimpleDateFormat("MM/dd/yyyy").format(holdingPerpetualAccess.getPerpetualAccessEndDate()));
478                     }
479                     perpetualAccess.setPerpetualAccessStartVolume(holdingPerpetualAccess.getPerpetualAccessStartVolume());
480                     perpetualAccess.setPerpetualAccessStartIssue(holdingPerpetualAccess.getPerpetualAccessStartIssue());
481                     perpetualAccess.setPerpetualAccessEndVolume(holdingPerpetualAccess.getPerpetualAccessEndVolume());
482                     perpetualAccess.setPerpetualAccessEndIssue(holdingPerpetualAccess.getPerpetualAccessEndIssue());
483                     perpetualAccesses.getPerpetualAccess().add(perpetualAccess);
484                 }
485             }
486             extentOfOwnership.setPerpetualAccesses(perpetualAccesses);
487         }
488         oleHoldings.setExtentOfOwnership(extentOfOwnership);
489         if (eHoldingsRecord.geteHoldingsNoteList() != null && eHoldingsRecord.geteHoldingsNoteList().size() > 0) {
490             List<EHoldingsNote> holdingsNoteRecords = eHoldingsRecord.geteHoldingsNoteList();
491             if (holdingsNoteRecords != null && holdingsNoteRecords.size() > 0) {
492                 List<Note> notes = new ArrayList<Note>();
493                 for (EHoldingsNote holdingsNoteRecord : holdingsNoteRecords) {
494                     Note note = new Note();
495                     if (holdingsNoteRecord.geteHoldingsNoteType() != null) {
496                         note.setType(holdingsNoteRecord.geteHoldingsNoteType().geteHoldingsNoteTypeName());
497                     }
498                     note.setValue(holdingsNoteRecord.geteHoldingsNoteText());
499                     oleHoldings.getNote().add(note);
500                 }
501 
502             }
503         }
504         return oleHoldings;
505     }
506 
507     private Location getLocationDetails(String locationName, String locationLevelName) {
508         Location location = new Location();
509         //LocationLevel locationLevel = new LocationLevel();
510         LocationLevel locationLevel = createLocationLevel(locationName, locationLevelName);
511         location.setLocationLevel(locationLevel);
512         return location;
513     }
514 
515     public LocationLevel createLocationLevel(String locationName, String locationLevelName) {
516         LocationLevel locationLevel = null;
517         if (StringUtils.isNotEmpty(locationName) && StringUtils.isNotEmpty(locationLevelName)) {
518             String[] locations = locationName.split("/");
519             String[] locationLevels = locationLevelName.split("/");
520             String locName = "";
521             String levelName = "";
522             if (locations.length > 0) {
523                 locName = locations[0];
524                 levelName = locationLevels[0];
525                 if (locationName.contains("/")) {
526                     locationName = locationName.replaceFirst(locations[0] + "/", "");
527                 } else {
528                     locationName = locationName.replace(locations[0], "");
529                 }
530 
531                 if (locationLevelName.contains("/")) {
532                     locationLevelName = locationLevelName.replaceFirst(locationLevels[0] + "/", "");
533                 } else {
534                     locationLevelName = locationLevelName.replace(locationLevels[0], "");
535                 }
536                 if (locName != null && locations.length != 0) {
537                     locationLevel = new LocationLevel();
538                     locationLevel.setLevel(levelName);
539                     locationLevel.setName(locName);
540                     locationLevel.setLocationLevel(createLocationLevel(locationName, locationLevelName));
541                 }
542             }
543         }
544         return locationLevel;
545     }
546 
547     protected void saveEHoldingsRecord(EHoldings eHoldings, EHoldingsRecord eHoldingsRecord) {
548 
549         StringBuffer locationLevel = new StringBuffer("");
550 
551         eHoldingsRecord.setLocation(getLocation(eHoldings.getLocation(), locationLevel));
552         eHoldingsRecord.setLocationLevel(locationLevel.toString());
553 
554         eHoldingsRecord.setAccessStatus(eHoldings.getAccessStatus());
555         eHoldingsRecord.setDonorNote(eHoldings.getDonorNote());
556         eHoldingsRecord.setDonorPublicDisplayNote(eHoldings.getDonorPublicDisplay());
557 
558         if (eHoldings.getAccessInformation() != null) {
559             eHoldingsRecord.setAccessLocation(eHoldings.getAccessInformation().getAccessLocation());
560             eHoldingsRecord.setAccessPassword(eHoldings.getAccessInformation().getAccessPassword());
561             eHoldingsRecord.setAccessUsername(eHoldings.getAccessInformation().getAccessUsername());
562             eHoldingsRecord.setAuthenticationType(eHoldings.getAccessInformation().getAuthenticationType());
563             eHoldingsRecord.setNumberSimultaneousUsers(eHoldings.getAccessInformation().getNumberOfSimultaneousUser());
564             eHoldingsRecord.setProxiedResource(eHoldings.getAccessInformation().getProxiedResource());
565         }
566 
567         eHoldingsRecord.setOrderFormat(eHoldings.getOrderFormat() != null ? eHoldings.getOrderFormat() : "");
568         eHoldingsRecord.setOrderType(eHoldings.getOrderType() != null ? eHoldings.getOrderType() : "");
569         eHoldingsRecord.setImprint(eHoldings.getImprint() != null ? eHoldings.getImprint() : "");
570         //eHoldingsRecord.setStatusDate(eHoldings.getStatusDate() != null ? eHoldings.getStatusDate() : "");
571 
572         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
573         Timestamp statusDate = null;
574         try {
575 
576             if (eHoldings.getStatusDate() != null) {
577                 statusDate = new Timestamp(sdf.parse(eHoldings.getStatusDate()).getTime());
578 
579             }
580         } catch (Exception e) {
581             LOG.info("Exception :", e);
582         }
583         eHoldingsRecord.setStatusDate(statusDate);
584         eHoldingsRecord.seteResourceId(eHoldings.getEResourceId());
585         eHoldingsRecord.seteResourceTitle(eHoldings.getEResourceTitle());
586         eHoldingsRecord.setPublisher(eHoldings.getPublisher());
587         eHoldingsRecord.setLocalPersistentLink(eHoldings.getLocalPersistentLink());
588 
589         if (eHoldings.getPlatform() != null) {
590             eHoldingsRecord.setPlatform(eHoldings.getPlatform().getPlatformName());
591             eHoldingsRecord.setAdminURL(eHoldings.getPlatform().getAdminUrl());
592             eHoldingsRecord.setAdminPassword(eHoldings.getPlatform().getAdminPassword());
593             eHoldingsRecord.setAdminUsername(eHoldings.getPlatform().getAdminUserName());
594         }
595 
596         if (eHoldings.getCallNumber() != null) {
597             CallNumber callNumber = eHoldings.getCallNumber();
598             eHoldingsRecord.setCallNumberPrefix(callNumber.getPrefix());
599             eHoldingsRecord.setCallNumber(callNumber.getNumber());
600             if (callNumber.getShelvingOrder() != null) {
601                 eHoldingsRecord.setShelvingOrder(callNumber.getShelvingOrder().getFullValue());
602             }
603             CallNumberTypeRecord callNumberTypeRecord = saveCallNumberTypeRecord(callNumber.getCallNumberType());
604             eHoldingsRecord.setCallNumberTypeId(callNumberTypeRecord == null ? null : callNumberTypeRecord.getCallNumberTypeId());
605 
606         }
607         if (eHoldings.getExtentOfOwnership() != null) {
608             if (eHoldings.getExtentOfOwnership().getCoverages() != null) {
609                 eHoldingsRecord.seteInstanceCoverageRecordList(saveCoverageRecord(eHoldings.getExtentOfOwnership().getCoverages(), eHoldingsRecord.geteHoldingsIdentifier()));
610             }
611             if (eHoldings.getExtentOfOwnership().getPerpetualAccesses() != null) {
612                 eHoldingsRecord.seteInstancePerpetualAccessRecordList(savePerpetualAccessRecord(eHoldings.getExtentOfOwnership().getPerpetualAccesses(), eHoldingsRecord.geteHoldingsIdentifier()));
613             }
614         }
615         if (eHoldings.getNote() != null && eHoldings.getNote().size() > 0) {
616             eHoldingsRecord.seteHoldingsNoteList(saveHoldingsNotes(eHoldings.getNote(), eHoldingsRecord.geteHoldingsIdentifier()));
617         }
618         if (eHoldings.getLink() != null) {
619             eHoldingsRecord.setLink(eHoldings.getLink().getUrl());
620             eHoldingsRecord.setLinkText(eHoldings.getLink().getText());
621         }
622         eHoldingsRecord.setiLLAllowed(eHoldings.isInterLibraryLoanAllowed());
623         eHoldingsRecord.setStaffOnly(eHoldings.isStaffOnlyFlag());
624         eHoldingsRecord.setStatisticalSearchCode(eHoldings.getStatisticalSearchingCode());
625         eHoldingsRecord.setSubscriptionStatus(eHoldings.getSubscriptionStatus());
626         getBusinessObjectService().save(eHoldingsRecord);
627     }
628 
629     protected CallNumberTypeRecord saveCallNumberTypeRecord(CallNumberType scheme) {
630 
631         Map callMap = new HashMap();
632         callMap.put("code", scheme.getCodeValue());
633         List<CallNumberTypeRecord> callNumberTypeRecords = (List<CallNumberTypeRecord>) getBusinessObjectService().findMatching(CallNumberTypeRecord.class, callMap);
634         if (callNumberTypeRecords.size() == 0) {
635             if (scheme.getCodeValue() != null && !"".equals(scheme.getCodeValue())) {
636                 CallNumberTypeRecord callNumberTypeRecord = new CallNumberTypeRecord();
637                 callNumberTypeRecord.setCode(scheme.getCodeValue());
638                 callNumberTypeRecord.setName(scheme.getFullValue());
639                 getBusinessObjectService().save(callNumberTypeRecord);
640                 return callNumberTypeRecord;
641             } else
642                 return null;
643         }
644         return callNumberTypeRecords.get(0);
645     }
646 
647     protected String getLocation(Location location, StringBuffer locationLevel) {
648         StringBuffer locationName = new StringBuffer("");
649         //StringBuffer locationLevel = new StringBuffer("");
650         if (location != null && location.getLocationLevel() != null) {
651             locationName = locationName.append(location.getLocationLevel().getName());
652             locationLevel = locationLevel.append(location.getLocationLevel().getLevel());
653 
654             if (location.getLocationLevel().getLocationLevel() != null) {
655                 locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getName());
656                 locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLevel());
657 
658                 if (location.getLocationLevel().getLocationLevel().getLocationLevel() != null) {
659                     locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getName());
660                     locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
661 
662                     if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
663                         locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName());
664                         locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
665 
666                         if (location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel() != null) {
667                             locationName = locationName.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getName());
668                             locationLevel = locationLevel.append("/").append(location.getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLocationLevel().getLevel());
669                         }
670                     }
671                 }
672             }
673         }
674         return locationName.toString();
675     }
676 
677     protected void deleteEHoldingNChildRecords(EHoldingsRecord eHoldingsRecord) {
678         String eHoldingsId = eHoldingsRecord.geteHoldingsIdentifier();
679 
680         Map eHoldingsMap = new HashMap();
681         eHoldingsMap.put("eHoldingsIdentifier", eHoldingsId);
682 
683         getBusinessObjectService().deleteMatching(EInstanceCoverageRecord.class, eHoldingsMap);
684         getBusinessObjectService().deleteMatching(EInstancePerpetualAccessRecord.class, eHoldingsMap);
685         getBusinessObjectService().deleteMatching(EHoldingsNote.class, eHoldingsMap);
686         getBusinessObjectService().delete(eHoldingsRecord);
687     }
688 
689     public void addResourceId(RequestDocument linkedRequestDocument, ResponseDocument respDoc) {
690         String eInstanceContent = linkedRequestDocument.getContent().getContent();
691         InstanceCollection instanceCollection = new WorkEInstanceOlemlRecordProcessor().fromXML(eInstanceContent);
692         EInstance eInstance = instanceCollection.getEInstance().get(0);
693         eInstance.getResourceIdentifier().clear();
694         eInstance.getResourceIdentifier().add(DocumentUniqueIDPrefix.getPrefixedId(DocumentUniqueIDPrefix.PREFIX_WORK_BIB_MARC, respDoc.getUuid()));
695         linkedRequestDocument.getContent().setContent(new WorkEInstanceOlemlRecordProcessor().toXML(instanceCollection));
696     }
697 
698 }