001package org.kuali.ole.docstore.document.rdbms; 002 003import org.kuali.ole.DocumentUniqueIDPrefix; 004import org.kuali.ole.docstore.OleDocStoreException; 005import org.kuali.ole.docstore.common.document.content.instance.CallNumber; 006import org.kuali.ole.docstore.common.document.content.instance.OleHoldings; 007import org.kuali.ole.docstore.common.document.content.instance.ShelvingOrder; 008import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme; 009import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.*; 010import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes; 011import org.kuali.ole.docstore.model.xmlpojo.ingest.Content; 012import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 013import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument; 014import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor; 015import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor; 016import org.kuali.rice.krad.service.BusinessObjectService; 017import org.kuali.rice.krad.service.KRADServiceLocator; 018import org.slf4j.Logger; 019import org.slf4j.LoggerFactory; 020 021import javax.jcr.Node; 022import java.sql.Timestamp; 023import java.text.DateFormat; 024import java.text.SimpleDateFormat; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029/** 030 * Created with IntelliJ IDEA. 031 * User: Sreekanth 032 * Date: 7/1/13 033 * Time: 5:28 PM 034 * To change this template use File | Settings | File Templates. 035 */ 036public class RdbmsWorkHoldingsDocumentManager 037 extends RdbmsWorkInstanceDocumentManager { 038 039 private static RdbmsWorkHoldingsDocumentManager ourInstanceRdbms = null; 040 private static final Logger LOG = LoggerFactory 041 .getLogger(RdbmsWorkHoldingsDocumentManager.class); 042 private BusinessObjectService businessObjectService; 043 044 public static RdbmsWorkHoldingsDocumentManager getInstance() { 045 if (null == ourInstanceRdbms) { 046 ourInstanceRdbms = new RdbmsWorkHoldingsDocumentManager(); 047 } 048 return ourInstanceRdbms; 049 } 050 051 public BusinessObjectService getBusinessObjectService() { 052 if (null == businessObjectService) { 053 businessObjectService = KRADServiceLocator.getBusinessObjectService(); 054 } 055 return businessObjectService; 056 } 057 058 @Override 059 public void deleteDocs(RequestDocument requestDocument, Object object) { 060 ResponseDocument responseDocument = new ResponseDocument(); 061 BusinessObjectService businessObjectService = (BusinessObjectService) object; 062 HoldingsRecord holdingsRecord = new HoldingsRecord(); 063 String holdingsId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()); 064 Map itemMap = new HashMap(); 065 itemMap.put("holdingsId", holdingsId); 066 holdingsRecord.setHoldingsId(holdingsId); 067 getBusinessObjectService().delete(holdingsRecord); 068 buildResponseDocument(requestDocument, holdingsRecord, responseDocument); 069 070 } 071 072 @Override 073 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) { 074 BusinessObjectService businessObjectService = (BusinessObjectService) object; 075 ResponseDocument responseDocument = new ResponseDocument(); 076 Map parentCriteria1 = new HashMap(); 077 parentCriteria1.put("holdingsId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid())); 078 List<HoldingsRecord> holdingsRecordList = (List<HoldingsRecord>) getBusinessObjectService().findMatching(HoldingsRecord.class, parentCriteria1); 079 if(holdingsRecordList != null && holdingsRecordList.size() > 0) { 080 HoldingsRecord holdingsRecord = holdingsRecordList.get(0); 081 OleHoldings oleHoldings = buildHoldingsContent(holdingsRecord); 082 String content = new InstanceOlemlRecordProcessor().toXML(oleHoldings); 083 AdditionalAttributes additionalAttributes = new AdditionalAttributes(); 084 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, holdingsRecord.getStaffOnlyFlag().toString()); 085 Map<String, String> mapObject = new HashMap<String, String>(); 086 mapObject.put("staffOnlyFlag", holdingsRecord.getStaffOnlyFlag().toString()); 087 additionalAttributes.setAttributeMap(mapObject); 088 Content contentObj = new Content(); 089 contentObj.setContent(content); 090 responseDocument.setUuid(requestDocument.getUuid()); 091 responseDocument.setCategory(requestDocument.getCategory()); 092 responseDocument.setType(requestDocument.getType()); 093 responseDocument.setFormat(requestDocument.getFormat()); 094 responseDocument.setContent(contentObj); 095 responseDocument.setAdditionalAttributes(additionalAttributes); 096 } else { 097 responseDocument.setStatus("Failed"); 098 responseDocument.setStatusMessage("Holdings does not exist."); 099 } 100 return responseDocument; 101 } 102 103 104 @Override 105 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException { 106 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes(); 107 BusinessObjectService businessObjectService = (BusinessObjectService) object; 108 if (requestDocument.getContent().getContent() != null) { 109 modifyContent(requestDocument, businessObjectService, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId())); 110 String content = requestDocument.getContent().getContent(); 111 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor(); 112 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(content); 113 Map parentCriteria1 = new HashMap(); 114 parentCriteria1.put("holdingsId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid())); 115 HoldingsRecord holdingsRecord = getBusinessObjectService().findByPrimaryKey(HoldingsRecord.class, parentCriteria1); 116 if (attributes != null) { 117 holdingsRecord.setStaffOnlyFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.STAFFONLYFLAG))); 118 holdingsRecord.setCreatedBy(attributes.getAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY)); 119 holdingsRecord.setUpdatedBy(attributes.getAttribute(AdditionalAttributes.HOLDINGS_UPDATED_BY)); 120 //holdingsRecord.setCreatedDate(attributes.getAttribute(AdditionalAttributes.DATE_ENTERED)); 121 String createdDateForHoldings = attributes.getAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED); 122 createdDateForHoldings(holdingsRecord,requestDocument.getAdditionalAttributes()); 123 124 } 125 buildHoldingRecordForCheckIn(oleHoldings, holdingsRecord); 126 // getBusinessObjectService().save(holdingsRecord); 127 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(holdingsRecord.getUniqueIdPrefix(), holdingsRecord.getHoldingsId())); 128 respDoc.setAdditionalAttributes(attributes); 129 buildResponseDocument(requestDocument, holdingsRecord, respDoc); 130 } 131 } 132 133 @Override 134 public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) 135 throws OleDocStoreException { 136 return null; 137 } 138 139 @Override 140 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException { 141 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor(); 142 if (requestDocument.getContent() != null && requestDocument.getContent().getContent() != null) { 143 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(requestDocument.getContent().getContent()); 144 validateHoldings(oleHoldings); 145 } 146 } 147 148 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, HoldingsRecord holdingsRecord, 149 ResponseDocument responseDocument) { 150 responseDocument.setId(holdingsRecord.getHoldingsId()); 151 responseDocument.setCategory(requestDocument.getCategory()); 152 responseDocument.setType(requestDocument.getType()); 153 responseDocument.setFormat(requestDocument.getFormat()); 154 responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(holdingsRecord.getUniqueIdPrefix(), holdingsRecord.getHoldingsId())); 155 //responseDocument.setId(holdingsRecord.getInstanceId()); 156 return responseDocument; 157 } 158 159 public void validateHoldings(OleHoldings oleHoldings) throws OleDocStoreException { 160 if (oleHoldings.getCallNumber() != null) { 161 CallNumber callNumber = oleHoldings.getCallNumber(); 162 validateCallNumber(callNumber); 163 } 164 } 165 166 private void buildHoldingRecordForCheckIn(OleHoldings oleHoldings, HoldingsRecord holdingsRecord) { 167 saveHoldingsRecord(oleHoldings, holdingsRecord); 168 } 169 170 protected void modifyContent(RequestDocument reqDoc, BusinessObjectService businessObjectService, String holdingId) throws OleDocStoreException { 171 HoldingOlemlRecordProcessor holdProcessor = new HoldingOlemlRecordProcessor(); 172 String instanceId = ""; 173 if (reqDoc != null && reqDoc.getContent() != null) { 174 // getting holdings from request document 175 OleHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent()); 176 if (newHold != null && newHold.getCallNumber() != null) { 177 CallNumber cNum = newHold.getCallNumber(); 178 // validate holdings call number 179// validateCallNumber(cNum); 180 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) { 181 // add new shelving order if null 182 if (cNum.getShelvingOrder() == null) { 183 cNum.setShelvingOrder(new ShelvingOrder()); 184 } 185 //get existing holdings from rdbms 186 boolean status = true; 187 RdbmsWorkItemDocumentManager rdbmsWorkItemDocumentManager = RdbmsWorkItemDocumentManager.getInstance(); 188 Map holdingsMap = new HashMap(); 189 holdingsMap.put("holdingsId", holdingId); 190 OleHoldings existHol = null; 191 List<HoldingsRecord> holdingsRecords = (List<HoldingsRecord>) businessObjectService.findMatching(HoldingsRecord.class, holdingsMap); 192 if (holdingsRecords != null && holdingsRecords.size() > 0) { 193 HoldingsRecord holdingsRecord = holdingsRecords.get(0); 194 existHol = buildHoldingsContent(holdingsRecord); 195 //instanceId = holdingsRecord.getInstanceId(); 196 } 197 if (existHol != null) { 198 setHoldValuesFromNullToEmpty(existHol.getCallNumber()); 199 setHoldValuesFromNullToEmpty(newHold.getCallNumber()); 200 if (existHol.getCallNumber() != null && existHol.getCallNumber().getNumber() != null && !(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) && 201 existHol.getCallNumber().getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue()))) { 202 processCallNumber(newHold); 203 String newHolXml = holdProcessor.toXML(newHold); 204 reqDoc.getContent().setContent(newHolXml); 205 rdbmsWorkItemDocumentManager.updateItemCallNumberFromHoldings(instanceId, newHold, reqDoc); 206 status = false; 207 } 208 } 209 if (status && !(cNum.getShelvingOrder().getFullValue() != null && 210 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) { 211 processCallNumber(newHold); 212 String newHolXml = holdProcessor.toXML(newHold); 213 reqDoc.getContent().setContent(newHolXml); 214// rdbmsWorkItemDocumentManager.updateItemCallNumberFromHoldings(nodeByUUID, newHold, reqDoc); 215 } 216 } 217 218 } 219 } 220 } 221 222 private void setHoldValuesFromNullToEmpty(CallNumber callNumber) { 223 if (callNumber != null) { 224 if (callNumber.getNumber() == null) { 225 callNumber.setNumber(""); 226 } 227 if (callNumber.getShelvingScheme() == null) { 228 callNumber.setShelvingScheme(new ShelvingScheme()); 229 } 230 if (callNumber.getShelvingScheme().getCodeValue() == null) { 231 callNumber.getShelvingScheme().setCodeValue(""); 232 } 233 } 234 } 235 236 237 private void createdDateForHoldings(HoldingsRecord holdingsRecord, AdditionalAttributes additionalAttributes) { 238 DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); 239 Timestamp createdDate = null; 240 String createdDateForHoldings = additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED); 241 String updateDateForHoldings = additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_LAST_UPDATED); 242 holdingsRecord.setCreatedBy(additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY)); 243 holdingsRecord.setUpdatedBy(additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_UPDATED_BY)); 244 try { 245 createdDate = new Timestamp(df.parse(createdDateForHoldings).getTime()); 246 holdingsRecord.setCreatedDate(createdDate); 247 holdingsRecord.setUpdatedDate(new Timestamp(df.parse(updateDateForHoldings).getTime())); 248 249 } catch (Exception e) { 250 LOG.error("Created Date for Holdings" + e); 251 } 252 } 253 254 private void updatedDateForHoldings(HoldingsRecord holdingsRecord, String DateForHoldings) { 255 DateFormat df = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 256 Timestamp updatedDate = null; 257 try { 258 updatedDate = new Timestamp(df.parse(DateForHoldings).getTime()); 259 holdingsRecord.setUpdatedDate(updatedDate); 260 261 } catch (Exception e) { 262 LOG.error("Created Date for Holdings" + e); 263 } 264 } 265 266}