001package org.kuali.ole.docstore.document.rdbms; 002 003import org.apache.commons.lang.StringUtils; 004import org.kuali.ole.DocumentUniqueIDPrefix; 005import org.kuali.ole.docstore.OleDocStoreException; 006import org.kuali.ole.docstore.model.rdbms.bo.*; 007import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes; 008import org.kuali.ole.docstore.model.xmlpojo.ingest.Content; 009import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 010import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument; 011import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumber; 012import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumberType; 013import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EHoldings; 014import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.ShelvingOrder; 015import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEHoldingOlemlRecordProcessor; 016import org.kuali.ole.utility.callnumber.CallNumberFactory; 017import org.kuali.rice.krad.service.BusinessObjectService; 018import org.slf4j.Logger; 019import org.slf4j.LoggerFactory; 020 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026/** 027 * Created with IntelliJ IDEA. 028 * User: sambasivam 029 * Date: 7/24/13 030 * Time: 4:19 PM 031 * To change this template use File | Settings | File Templates. 032 */ 033public class RdbmsWorkEHoldingsDocumentManager extends RdbmsWorkEInstanceDocumentManager { 034 035 private static RdbmsWorkEHoldingsDocumentManager ourInstanceRdbms = null; 036 private static final Logger LOG = LoggerFactory.getLogger(RdbmsWorkEHoldingsDocumentManager.class); 037 private BusinessObjectService businessObjectService; 038 039 public static RdbmsWorkEHoldingsDocumentManager getInstance() { 040 if (null == ourInstanceRdbms) { 041 ourInstanceRdbms = new RdbmsWorkEHoldingsDocumentManager(); 042 } 043 return ourInstanceRdbms; 044 } 045 046 @Override 047 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) { 048 ResponseDocument respDoc = new ResponseDocument(); 049 Map eInstanceMap = new HashMap(); 050 eInstanceMap.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid())); 051 EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, eInstanceMap); 052 EHoldings eHoldings = buildEHoldingsContent(eHoldingsRecord); 053 String content = new WorkEHoldingOlemlRecordProcessor().toXML(eHoldings); 054 Content contentObj = new Content(); 055 contentObj.setContent(content); 056 respDoc.setUuid(requestDocument.getUuid()); 057 respDoc.setCategory(requestDocument.getCategory()); 058 respDoc.setType(requestDocument.getType()); 059 respDoc.setFormat(requestDocument.getFormat()); 060 respDoc.setContent(contentObj); 061 return respDoc; 062 } 063 064 @Override 065 public void deleteDocs(RequestDocument requestDocument, Object object) { 066 ResponseDocument responseDocument = new ResponseDocument(); 067 BusinessObjectService businessObjectService = (BusinessObjectService) object; 068 EInstanceRecord eInstanceRecord = new EInstanceRecord(); 069 EHoldingsRecord eHoldingsRecord = new EHoldingsRecord(); 070 String eHoldingsId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()); 071 Map itemMap = new HashMap(); 072 itemMap.put("eHoldingsIdentifier", eHoldingsId); 073 eHoldingsRecord.seteHoldingsIdentifier(eHoldingsId); 074 eInstanceRecord.seteInstanceIdentifier(eHoldingsId); 075 deleteEHoldingNChildRecords(eHoldingsRecord); 076 getBusinessObjectService().delete(eInstanceRecord); 077 buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument); 078 } 079 080 @Override 081 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException { 082 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes(); 083 BusinessObjectService businessObjectService = (BusinessObjectService) object; 084 if (requestDocument.getContent().getContent() != null) { 085 modifyContent(requestDocument, businessObjectService, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId())); 086 String content = requestDocument.getContent().getContent(); 087 WorkEHoldingOlemlRecordProcessor workEHoldingOlemlRecordProcessor = new WorkEHoldingOlemlRecordProcessor(); 088 EHoldings eHoldings = workEHoldingOlemlRecordProcessor.fromXML(content); 089 Map parentCriteria1 = new HashMap(); 090 parentCriteria1.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid())); 091 EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, parentCriteria1); 092 buildHoldingRecordForCheckIn(eHoldings, eHoldingsRecord); 093 // getBusinessObjectService().save(holdingsRecord); 094 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier())); 095 responseDocument.setAdditionalAttributes(attributes); 096 buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument); 097 } 098 } 099 100 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, EHoldingsRecord eHoldingsRecord, 101 ResponseDocument responseDocument) { 102 responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier()); 103 responseDocument.setCategory(requestDocument.getCategory()); 104 responseDocument.setType(requestDocument.getType()); 105 responseDocument.setFormat(requestDocument.getFormat()); 106 responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier())); 107 responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier()); 108 return responseDocument; 109 } 110 111 private void buildHoldingRecordForCheckIn(EHoldings eHoldings, EHoldingsRecord eHoldingsRecord) { 112 saveEHoldingsRecord(eHoldings, eHoldingsRecord); 113 } 114 115 protected void modifyContent(RequestDocument reqDoc, BusinessObjectService businessObjectService, String holdingId) throws OleDocStoreException { 116 WorkEHoldingOlemlRecordProcessor holdProcessor = new WorkEHoldingOlemlRecordProcessor(); 117 String instanceId = ""; 118 if (reqDoc != null && reqDoc.getContent() != null) { 119 // getting holdings from request document 120 EHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent()); 121 if (newHold != null && newHold.getCallNumber() != null) { 122 CallNumber cNum = newHold.getCallNumber(); 123 // validate holdings call number 124// validateCallNumber(cNum); 125 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) { 126 // add new shelving order if null 127 if (cNum.getShelvingOrder() == null) { 128 cNum.setShelvingOrder(new ShelvingOrder()); 129 } 130 //get existing holdings from rdbms 131 boolean status = true; 132 Map holdingsMap = new HashMap(); 133 holdingsMap.put("eHoldingsIdentifier", holdingId); 134 EHoldings existHol = null; 135 List<EHoldingsRecord> eHoldingsRecords = (List<EHoldingsRecord>) businessObjectService.findMatching(EHoldingsRecord.class, holdingsMap); 136 if (eHoldingsRecords != null && eHoldingsRecords.size() > 0) { 137 EHoldingsRecord holdingsRecord = eHoldingsRecords.get(0); 138 existHol = buildEHoldingsContent(holdingsRecord); 139 instanceId = holdingsRecord.geteInstanceIdentifier(); 140 } 141 if (existHol != null) { 142 setHoldValuesFromNullToEmpty(existHol.getCallNumber()); 143 setHoldValuesFromNullToEmpty(newHold.getCallNumber()); 144 if (existHol.getCallNumber() != null && existHol.getCallNumber().getNumber() != null && !(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) && 145 existHol.getCallNumber().getCallNumberType().getCodeValue().equalsIgnoreCase(cNum.getCallNumberType().getCodeValue()))) { 146 processCallNumber(newHold); 147 String newHolXml = holdProcessor.toXML(newHold); 148 reqDoc.getContent().setContent(newHolXml); 149 status = false; 150 } 151 } 152 if (status && !(cNum.getShelvingOrder().getFullValue() != null && 153 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) { 154 processCallNumber(newHold); 155 } 156 } 157 158 } 159 } 160 } 161 162 private void setHoldValuesFromNullToEmpty(CallNumber callNumber) { 163 if (callNumber != null) { 164 if (callNumber.getNumber() == null) { 165 callNumber.setNumber(""); 166 } 167 if (callNumber.getCallNumberType() == null) { 168 callNumber.setCallNumberType(new CallNumberType()); 169 } 170 if (callNumber.getCallNumberType().getCodeValue() == null) { 171 callNumber.getCallNumberType().setCodeValue(""); 172 } 173 } 174 } 175 176 protected void processCallNumber(EHoldings eHolding) throws OleDocStoreException { 177 if (eHolding != null && eHolding.getCallNumber() != null) { 178 //validateCallNumber(oleHolding.getCallNumber()); 179 CallNumber cNum = eHolding.getCallNumber(); 180 computeCallNumberType(cNum); 181 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) { 182 //Build sortable key if a valid call number exists 183 boolean isValid = validateCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue()); 184 String value = ""; 185 if (isValid) { 186 value = buildSortableCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue()); 187 } else { 188 value = cNum.getNumber(); 189 } 190 if (cNum.getShelvingOrder() == null) { 191 cNum.setShelvingOrder(new ShelvingOrder()); 192 } 193 cNum.getShelvingOrder().setFullValue(value); 194 } 195 } 196 } 197 198 /** 199 * Compute 'call number type name' if a valid 'call number type code' is available 200 * 201 * @param callNumber 202 */ 203 public void computeCallNumberType(CallNumber callNumber) { 204 Set<String> validCallNumberTypeSet = org.kuali.ole.utility.callnumber.CallNumberType.validCallNumberTypeCodeSet; 205 if (callNumber != null) { 206 if (callNumber.getCallNumberType() != null) { 207 String callNumberTypeCode = callNumber.getCallNumberType().getCodeValue(); 208 String callNumberTypeName = ""; 209 //If call number type code is valid 210 if ((StringUtils.isNotEmpty(callNumberTypeCode)) && (validCallNumberTypeSet 211 .contains(callNumberTypeCode))) { 212 callNumberTypeName = org.kuali.ole.utility.callnumber.CallNumberType.valueOf(callNumberTypeCode).getDescription(); 213 callNumber.getCallNumberType().setFullValue(callNumberTypeName); 214 } 215 } 216 } 217 } 218 219 protected boolean validateCallNumber(String callNumber, String codeValue) throws OleDocStoreException { 220 boolean isValid = false; 221 if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) { 222 org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue); 223 if (callNumberObj != null) { 224 isValid = callNumberObj.isValid(callNumber); 225 } 226 } 227 return isValid; 228 } 229 230 protected String buildSortableCallNumber(String callNumber, String codeValue) throws OleDocStoreException { 231 String shelvingOrder = ""; 232 if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) { 233 org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue); 234 if (callNumberObj != null) { 235 shelvingOrder = callNumberObj.getSortableKey(callNumber); 236 //shelvingOrder = shelvingOrder.replaceAll(" ", "_"); 237 } 238 } 239 return shelvingOrder; 240 } 241}