View Javadoc
1   package org.kuali.ole.docstore.repository;
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.Item;
6   import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
7   import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
8   import org.kuali.ole.docstore.document.jcr.JcrWorkInstanceDocumentManager;
9   import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
10  import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
11  import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
12  import org.kuali.ole.docstore.process.ProcessParameters;
13  import org.kuali.ole.repository.NodeHandler;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  import javax.jcr.Node;
18  import javax.jcr.NodeIterator;
19  import javax.jcr.RepositoryException;
20  import javax.jcr.Session;
21  import java.io.FileNotFoundException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import static org.kuali.ole.docstore.process.ProcessParameters.*;
26  
27  /**
28   * User: tirumalesh.b
29   * Date: 4/9/12 Time: 1:00 PM
30   */
31  public class WorkInstanceNodeManager
32          extends CustomNodeManager
33          implements NodeManager {
34      private static WorkInstanceNodeManager ourInstance = new WorkInstanceNodeManager();
35      private static Logger LOG = LoggerFactory.getLogger(WorkInstanceNodeManager.class);
36  
37      public static WorkInstanceNodeManager getInstance() {
38          return ourInstance;
39      }
40  
41      private WorkInstanceNodeManager() {
42          super();
43          numLevels = 2;
44      }
45  
46      public String getParentNodePath() {
47          return "/work/instance";
48      }
49  
50      @Override
51      public Node createFileNode(RequestDocument document, String name, Node parentNode, Session session)
52              throws OleDocStoreException {
53          Node instanceNode = null;
54          JcrWorkInstanceDocumentManager resolver = JcrWorkInstanceDocumentManager.getInstance();
55          List<String> docUUIDs = new ArrayList<String>();
56          List<RequestDocument> resolvedDocs = resolver.getParsedHoldingsNItemDocuments(document, docUUIDs);
57          CustomNodeManager nodeManager = CustomNodeManager.getInstance();
58          try {
59              instanceNode = initLevelNode(NODE_INSTANCE, parentNode, false, session);
60              Node holdingsNode = initNonStaticNode(NODE_HOLDINGS, instanceNode);
61              document.setUuid(instanceNode.getIdentifier());
62  
63              Node fileNode = nodeManager.createFileNode(resolvedDocs.get(0), FILE_INSTANCE, instanceNode, session);
64              resolver.modifyDocumentContent(resolvedDocs.get(0), fileNode.getIdentifier(), instanceNode.getIdentifier());
65              createContentNode(fileNode, resolvedDocs.get(0), parentNode, session);
66  
67              fileNode = nodeManager.createFileNode(resolvedDocs.get(1), FILE_HOLDINGS, holdingsNode, session);
68              resolver.modifyDocumentContent(resolvedDocs.get(1), fileNode.getIdentifier(), holdingsNode.getIdentifier());
69              createContentNode(fileNode, resolvedDocs.get(1), parentNode, session);
70  
71              fileNode = nodeManager.createFileNode(resolvedDocs.get(2), FILE_SOURCE_HOLDINGS, holdingsNode, session);
72              resolver.modifyDocumentContent(resolvedDocs.get(2), fileNode.getIdentifier(), holdingsNode.getIdentifier());
73              createContentNode(fileNode, resolvedDocs.get(2), parentNode, session);
74  
75  
76              for (int i = 3; i < resolvedDocs.size(); i++) {
77                  fileNode = nodeManager.createFileNode(resolvedDocs.get(i), FILE_ITEM, holdingsNode, session);
78                  resolver.modifyDocumentContent(resolvedDocs.get(i), fileNode.getIdentifier(),
79                          holdingsNode.getIdentifier());
80                  createContentNode(fileNode, resolvedDocs.get(i), parentNode, session);
81              }
82  
83              ((InstanceCollection) document.getContent().getContentObject()).getInstance().get(0).setInstanceIdentifier(
84                      instanceNode.getIdentifier());
85          } catch (Exception e) {
86              LOG.info(e.getMessage(), e);
87          }
88          return instanceNode;
89      }
90  
91      public void ingestItemRecForInstance(RequestDocument linkedItemDocument, String id, Session session) throws OleDocStoreException {
92          try {
93  
94              Node nodeByUUID = getNodeByUUID(session, id);
95              Node holdingsNode = nodeByUUID.getNode(ProcessParameters.NODE_HOLDINGS);
96              NodeHandler nodeHandler = new NodeHandler();
97              ItemOlemlRecordProcessor recordProcessor = new ItemOlemlRecordProcessor();
98              Item item = recordProcessor.fromXML(linkedItemDocument.getContent().getContent());
99              buildShelvingOrderForItem(item, holdingsNode);
100             linkedItemDocument.getContent().setContentObject(item);
101             String uuid = (nodeHandler.initFileNode(linkedItemDocument, FILE_ITEM, holdingsNode, session))
102                     .getIdentifier();
103             linkedItemDocument.setId(uuid);
104         } catch (Exception e) {
105             LOG.info(e.getMessage(),e);
106             throw new OleDocStoreException(e.getMessage(), e);
107         }
108     }
109 
110     public Node getNodeByUUID(Session newSession, String uuid) throws OleDocStoreException {
111         return CustomNodeManager.getInstance().getNodeByUUID(newSession, uuid);
112     }
113 
114     public String getInstanceData(Node instanceNode)
115             throws RepositoryException, OleDocStoreException, FileNotFoundException {
116         String instance = "";
117         String holdings = "";
118         String sourceHolding = "";
119         List<String> items = new ArrayList<String>();
120         StringBuffer stringBuffer = new StringBuffer();
121         NodeIterator nodeIterator = instanceNode.getNodes();
122         Node node = null;
123         while (nodeIterator.hasNext()) {
124             node = nodeIterator.nextNode();
125             if (node.getName().equalsIgnoreCase(ProcessParameters.FILE_INSTANCE)) {
126                 instance = getData(node);
127             } else if (node.getName().equalsIgnoreCase(ProcessParameters.NODE_HOLDINGS)) {
128                 NodeIterator nodes = node.getNodes();
129                 while (nodes.hasNext()) {
130                     Node node1 = nodes.nextNode();
131                     if (node1.getName().equalsIgnoreCase(ProcessParameters.FILE_HOLDINGS)) {
132                         holdings = getData(node1);
133                     } else if (node1.getName().equalsIgnoreCase(ProcessParameters.FILE_SOURCE_HOLDINGS)) {
134                         if (getData(node1) != null && getData(node1).length() > 0) {
135                             sourceHolding = getData(node1);
136                         }
137                     } else if (node1.getName().equalsIgnoreCase(ProcessParameters.FILE_ITEM)) {
138                         items.add(getData(node1));
139                     }
140                 }
141 
142             }
143         }
144         stringBuffer
145                 .append(instance.substring(instance.indexOf("<instanceCollection>"), instance.indexOf("</instance>")));
146         if (!holdings.equalsIgnoreCase("<null/>")) {
147             stringBuffer.append(holdings);
148         }
149         if (!sourceHolding.equalsIgnoreCase("<null/>")) {
150             stringBuffer.append(sourceHolding);
151         }
152         stringBuffer.append("<items>");
153         for (String str : items) {
154             if (!str.equalsIgnoreCase("<null/>")) {
155                 stringBuffer.append(str);
156             }
157         }
158         stringBuffer.append("</items>");
159         stringBuffer.append("</instance>");
160         stringBuffer.append("</instanceCollection>");
161         return stringBuffer.toString();
162     }
163 
164     public String getXMLOnlyForInstanceType(Node instanceNode)
165             throws RepositoryException, OleDocStoreException, FileNotFoundException {
166         NodeIterator nodeIterator = instanceNode.getNodes();
167         Node node = null;
168         String instance = "";
169         while (nodeIterator.hasNext()) {
170             node = nodeIterator.nextNode();
171             if (node.getName().equalsIgnoreCase("instanceFile")) {
172                 instance = getData(node);
173             }
174         }
175         return instance;
176     }
177 
178     private void buildShelvingOrderForItem(Item item, Node holdingsNode) throws Exception {
179         if (item != null) {
180             if (item.getCallNumber() == null) {
181                 item.setCallNumber(new CallNumber());
182             }
183             JcrWorkInstanceDocumentManager instanceManagerJcr = JcrWorkInstanceDocumentManager.getInstance();
184             Node holdingsFileNode = holdingsNode.getNode(FILE_HOLDINGS);
185             String holdingXml = getData(holdingsFileNode);
186             if (holdingXml != null) {
187                 HoldingOlemlRecordProcessor holdProcessor = new HoldingOlemlRecordProcessor();
188                 OleHoldings holdings = holdProcessor.fromXML(holdingXml);
189                 instanceManagerJcr.updateShelvingOrder(item, holdings);
190             }
191         }
192     }
193 
194 }