View Javadoc
1   package org.kuali.ole.docstore.document.jcr;
2   
3   import org.kuali.ole.docstore.OleDocStoreException;
4   import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
5   import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
6   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
7   import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
8   import org.kuali.ole.docstore.common.document.content.instance.ShelvingOrder;
9   import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import javax.jcr.Node;
14  import javax.jcr.RepositoryException;
15  import javax.jcr.Session;
16  import java.io.FileNotFoundException;
17  import java.util.List;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: Pranitha J
22   * Date: 27/2/13
23   * Time: 3:18 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  @Deprecated
27  public class JcrJcrWorkHoldingsDocumentManager extends JcrWorkInstanceDocumentManager {
28  
29      private static JcrJcrWorkHoldingsDocumentManager ourInstance = null;
30      private static Logger LOG = LoggerFactory.getLogger(JcrJcrWorkHoldingsDocumentManager.class);
31  
32  
33      public static JcrJcrWorkHoldingsDocumentManager getInstance() {
34          if (null == ourInstance) {
35              ourInstance = new JcrJcrWorkHoldingsDocumentManager();
36          }
37          return ourInstance;
38      }
39  
40      @Override
41      protected void modifyContent(RequestDocument reqDoc, Session session, Node nodeByUUID)
42              throws RepositoryException, FileNotFoundException, OleDocStoreException {
43          HoldingOlemlRecordProcessor holdProcessor = new HoldingOlemlRecordProcessor();
44          if (reqDoc != null && reqDoc.getContent() != null) {
45              // getting holdings from request document
46              OleHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent());
47              if (newHold != null && newHold.getCallNumber() != null) {
48                  CallNumber cNum = newHold.getCallNumber();
49                  // validate holdings call number
50  //                validateCallNumber(cNum);
51                  if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
52                      // add new shelving order if null
53                      if (cNum.getShelvingOrder() == null) {
54                          cNum.setShelvingOrder(new ShelvingOrder());
55                      }
56                      //get existing holdings from jackrabbit
57                      String holdingsXml = nodeManager.getData(nodeByUUID);
58                      JcrWorkItemDocumentManager workItemDocumentManager = JcrWorkItemDocumentManager.getInstance();
59                      boolean status = true;
60                      if (holdingsXml != null) {
61                          OleHoldings existHol = holdProcessor.fromXML(holdingsXml);
62                          setHoldValuesFromNullToEmpty(existHol.getCallNumber());
63                          setHoldValuesFromNullToEmpty(newHold.getCallNumber());
64                          if (!(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) &&
65                                  existHol.getCallNumber().getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue()))) {
66                              processCallNumber(newHold);
67                              String newHolXml = holdProcessor.toXML(newHold);
68                              reqDoc.getContent().setContent(newHolXml);
69                              workItemDocumentManager.updateItemCallNumberFromHoldings(nodeByUUID, newHold, reqDoc);
70                              status = false;
71                          }
72                      }
73                      if (status && !(cNum.getShelvingOrder().getFullValue() != null &&
74                              cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
75                          processCallNumber(newHold);
76  //                        workItemDocumentManager.updateItemCallNumberFromHoldings(nodeByUUID, newHold, reqDoc);
77                      }
78                  }
79  
80              }
81          }
82      }
83  
84      protected void updateContentToNode(RequestDocument reqDoc, Session session, byte[] documentBytes, Node nodeByUUID)
85              throws RepositoryException, OleDocStoreException {
86          documentBytes = convertContentToBytes(reqDoc);
87          super.updateContentToNode(reqDoc, session, documentBytes, nodeByUUID);
88          for (RequestDocument linkReqDoc : reqDoc.getLinkedRequestDocuments()) {
89              documentBytes = convertContentToBytes(linkReqDoc);
90              Node itemNode = nodeManager.getNodeByUUID(session, linkReqDoc.getId());
91              super.updateContentToNode(linkReqDoc, session, documentBytes, itemNode);
92          }
93      }
94  
95      private void setHoldValuesFromNullToEmpty(CallNumber callNumber) {
96          if (callNumber.getNumber() == null) {
97              callNumber.setNumber("");
98          }
99          if (callNumber.getShelvingScheme() == null) {
100             callNumber.setShelvingScheme(new ShelvingScheme());
101         }
102         if (callNumber.getShelvingScheme().getCodeValue() == null) {
103             callNumber.getShelvingScheme().setCodeValue("");
104         }
105     }
106 
107     @Override
108     public void validateInput(RequestDocument reqDoc, Object object, List<String> valuesList) throws OleDocStoreException {
109         HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
110         if (reqDoc.getContent() != null && reqDoc.getContent().getContent() != null) {
111             OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(reqDoc.getContent().getContent());
112             validateHoldings(oleHoldings);
113         }
114     }
115 
116     public void validateHoldings(OleHoldings oleHoldings) throws OleDocStoreException {
117         if (oleHoldings.getCallNumber() != null) {
118             CallNumber callNumber = oleHoldings.getCallNumber();
119             validateCallNumber(callNumber);
120         }
121     }
122 }
123