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.xmlpojo.ingest.AdditionalAttributes;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
10  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
11  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumber;
12  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumberType;
13  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EHoldings;
14  import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.ShelvingOrder;
15  import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEHoldingOlemlRecordProcessor;
16  import org.kuali.ole.utility.callnumber.CallNumberFactory;
17  import org.kuali.rice.krad.service.BusinessObjectService;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  
26  /**
27   * Created with IntelliJ IDEA.
28   * User: sambasivam
29   * Date: 7/24/13
30   * Time: 4:19 PM
31   * To change this template use File | Settings | File Templates.
32   */
33  @Deprecated
34  public class RdbmsWorkEHoldingsDocumentManager extends RdbmsWorkEInstanceDocumentManager {
35  
36      private static RdbmsWorkEHoldingsDocumentManager ourInstanceRdbms = null;
37      private static final Logger LOG = LoggerFactory.getLogger(RdbmsWorkEHoldingsDocumentManager.class);
38      private BusinessObjectService businessObjectService;
39  
40      public static RdbmsWorkEHoldingsDocumentManager getInstance() {
41          if (null == ourInstanceRdbms) {
42              ourInstanceRdbms = new RdbmsWorkEHoldingsDocumentManager();
43          }
44          return ourInstanceRdbms;
45      }
46  
47      @Override
48      public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
49          ResponseDocument respDoc = new ResponseDocument();
50          Map eInstanceMap = new HashMap();
51          eInstanceMap.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
52          EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, eInstanceMap);
53          EHoldings eHoldings = buildEHoldingsContent(eHoldingsRecord);
54          String content = new WorkEHoldingOlemlRecordProcessor().toXML(eHoldings);
55          Content contentObj = new Content();
56          contentObj.setContent(content);
57          respDoc.setUuid(requestDocument.getUuid());
58          respDoc.setCategory(requestDocument.getCategory());
59          respDoc.setType(requestDocument.getType());
60          respDoc.setFormat(requestDocument.getFormat());
61          respDoc.setContent(contentObj);
62          return respDoc;
63      }
64  
65      @Override
66      public void deleteDocs(RequestDocument requestDocument, Object object) {
67          ResponseDocument responseDocument = new ResponseDocument();
68          BusinessObjectService businessObjectService = (BusinessObjectService) object;
69          EInstanceRecord eInstanceRecord = new EInstanceRecord();
70          EHoldingsRecord eHoldingsRecord = new EHoldingsRecord();
71          String eHoldingsId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid());
72          Map itemMap = new HashMap();
73          itemMap.put("eHoldingsIdentifier", eHoldingsId);
74          eHoldingsRecord.seteHoldingsIdentifier(eHoldingsId);
75          eInstanceRecord.seteInstanceIdentifier(eHoldingsId);
76          deleteEHoldingNChildRecords(eHoldingsRecord);
77          getBusinessObjectService().delete(eInstanceRecord);
78          buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument);
79      }
80  
81      @Override
82      public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException {
83          AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
84          BusinessObjectService businessObjectService = (BusinessObjectService) object;
85          if (requestDocument.getContent().getContent() != null) {
86              modifyContent(requestDocument, businessObjectService, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId()));
87              String content = requestDocument.getContent().getContent();
88              WorkEHoldingOlemlRecordProcessor workEHoldingOlemlRecordProcessor = new WorkEHoldingOlemlRecordProcessor();
89              EHoldings eHoldings = workEHoldingOlemlRecordProcessor.fromXML(content);
90              Map parentCriteria1 = new HashMap();
91              parentCriteria1.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
92              EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, parentCriteria1);
93              buildHoldingRecordForCheckIn(eHoldings, eHoldingsRecord);
94              // getBusinessObjectService().save(holdingsRecord);
95              requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
96              responseDocument.setAdditionalAttributes(attributes);
97              buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument);
98          }
99      }
100 
101     public ResponseDocument buildResponseDocument(RequestDocument requestDocument, EHoldingsRecord eHoldingsRecord,
102                                                   ResponseDocument responseDocument) {
103         responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier());
104         responseDocument.setCategory(requestDocument.getCategory());
105         responseDocument.setType(requestDocument.getType());
106         responseDocument.setFormat(requestDocument.getFormat());
107         responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
108         responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier());
109         return responseDocument;
110     }
111 
112     private void buildHoldingRecordForCheckIn(EHoldings eHoldings, EHoldingsRecord eHoldingsRecord) {
113         saveEHoldingsRecord(eHoldings, eHoldingsRecord);
114     }
115 
116     protected void modifyContent(RequestDocument reqDoc, BusinessObjectService businessObjectService, String holdingId) throws OleDocStoreException {
117         WorkEHoldingOlemlRecordProcessor holdProcessor = new WorkEHoldingOlemlRecordProcessor();
118         String instanceId = "";
119         if (reqDoc != null && reqDoc.getContent() != null) {
120             // getting holdings from request document
121             EHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent());
122             if (newHold != null && newHold.getCallNumber() != null) {
123                 CallNumber cNum = newHold.getCallNumber();
124                 // validate holdings call number
125 //                validateCallNumber(cNum);
126                 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
127                     // add new shelving order if null
128                     if (cNum.getShelvingOrder() == null) {
129                         cNum.setShelvingOrder(new ShelvingOrder());
130                     }
131                     //get existing holdings from rdbms
132                     boolean status = true;
133                     Map holdingsMap = new HashMap();
134                     holdingsMap.put("eHoldingsIdentifier", holdingId);
135                     EHoldings existHol = null;
136                     List<EHoldingsRecord> eHoldingsRecords = (List<EHoldingsRecord>) businessObjectService.findMatching(EHoldingsRecord.class, holdingsMap);
137                     if (eHoldingsRecords != null && eHoldingsRecords.size() > 0) {
138                         EHoldingsRecord holdingsRecord = eHoldingsRecords.get(0);
139                         existHol = buildEHoldingsContent(holdingsRecord);
140                         instanceId = holdingsRecord.geteInstanceIdentifier();
141                     }
142                     if (existHol != null) {
143                         setHoldValuesFromNullToEmpty(existHol.getCallNumber());
144                         setHoldValuesFromNullToEmpty(newHold.getCallNumber());
145                         if (existHol.getCallNumber() != null && existHol.getCallNumber().getNumber() != null && !(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) &&
146                                 existHol.getCallNumber().getCallNumberType().getCodeValue().equalsIgnoreCase(cNum.getCallNumberType().getCodeValue()))) {
147                             processCallNumber(newHold);
148                             String newHolXml = holdProcessor.toXML(newHold);
149                             reqDoc.getContent().setContent(newHolXml);
150                             status = false;
151                         }
152                     }
153                     if (status && !(cNum.getShelvingOrder().getFullValue() != null &&
154                             cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
155                         processCallNumber(newHold);
156                     }
157                 }
158 
159             }
160         }
161     }
162 
163     private void setHoldValuesFromNullToEmpty(CallNumber callNumber) {
164         if (callNumber != null) {
165             if (callNumber.getNumber() == null) {
166                 callNumber.setNumber("");
167             }
168             if (callNumber.getCallNumberType() == null) {
169                 callNumber.setCallNumberType(new CallNumberType());
170             }
171             if (callNumber.getCallNumberType().getCodeValue() == null) {
172                 callNumber.getCallNumberType().setCodeValue("");
173             }
174         }
175     }
176 
177     protected void processCallNumber(EHoldings eHolding) throws OleDocStoreException {
178         if (eHolding != null && eHolding.getCallNumber() != null) {
179             //validateCallNumber(oleHolding.getCallNumber());
180             CallNumber cNum = eHolding.getCallNumber();
181             computeCallNumberType(cNum);
182             if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
183                 //Build sortable key if a valid call number exists
184                 boolean isValid = validateCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue());
185                 String value = "";
186                 if (isValid) {
187                     value = buildSortableCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue());
188                 } else {
189                     value = cNum.getNumber();
190                 }
191                 if (cNum.getShelvingOrder() == null) {
192                     cNum.setShelvingOrder(new ShelvingOrder());
193                 }
194                 cNum.getShelvingOrder().setFullValue(value);
195             }
196         }
197     }
198 
199     /**
200      * Compute 'call number type name' if a valid 'call number type code' is available
201      *
202      * @param callNumber
203      */
204     public void computeCallNumberType(CallNumber callNumber) {
205         Set<String> validCallNumberTypeSet = org.kuali.ole.utility.callnumber.CallNumberType.validCallNumberTypeCodeSet;
206         if (callNumber != null) {
207             if (callNumber.getCallNumberType() != null) {
208                 String callNumberTypeCode = callNumber.getCallNumberType().getCodeValue();
209                 String callNumberTypeName = "";
210                 //If call number type code is valid
211                 if ((StringUtils.isNotEmpty(callNumberTypeCode)) && (validCallNumberTypeSet
212                         .contains(callNumberTypeCode))) {
213                     callNumberTypeName = org.kuali.ole.utility.callnumber.CallNumberType.valueOf(callNumberTypeCode).getDescription();
214                     callNumber.getCallNumberType().setFullValue(callNumberTypeName);
215                 }
216             }
217         }
218     }
219 
220     protected boolean validateCallNumber(String callNumber, String codeValue) throws OleDocStoreException {
221         boolean isValid = false;
222        /* if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) {
223             org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue);
224             if (callNumberObj != null) {
225                 isValid = callNumberObj.isValid(callNumber);
226             }
227         }*/
228         return isValid;
229     }
230 
231     protected String buildSortableCallNumber(String callNumber, String codeValue) throws OleDocStoreException {
232         String shelvingOrder = "";
233       /*  if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) {
234             org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue);
235             if (callNumberObj != null) {
236                 shelvingOrder = callNumberObj.getSortableKey(callNumber);
237                 //shelvingOrder = shelvingOrder.replaceAll(" ", "_");
238             }
239         }*/
240 
241         return shelvingOrder;
242     }
243 }