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