001package org.kuali.ole.docstore.repository; 002 003import org.kuali.ole.docstore.OleDocStoreException; 004import org.kuali.ole.docstore.common.document.content.instance.CallNumber; 005import org.kuali.ole.docstore.common.document.content.instance.Item; 006import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor; 007import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor; 008import org.kuali.ole.docstore.document.jcr.JcrWorkInstanceDocumentManager; 009import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 010import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection; 011import org.kuali.ole.docstore.common.document.content.instance.OleHoldings; 012import org.kuali.ole.docstore.process.ProcessParameters; 013import org.kuali.ole.repository.NodeHandler; 014import org.slf4j.Logger; 015import org.slf4j.LoggerFactory; 016 017import javax.jcr.Node; 018import javax.jcr.NodeIterator; 019import javax.jcr.RepositoryException; 020import javax.jcr.Session; 021import java.io.FileNotFoundException; 022import java.util.ArrayList; 023import java.util.List; 024 025import static org.kuali.ole.docstore.process.ProcessParameters.*; 026 027/** 028 * User: tirumalesh.b 029 * Date: 4/9/12 Time: 1:00 PM 030 */ 031public class WorkInstanceNodeManager 032 extends CustomNodeManager 033 implements NodeManager { 034 private static WorkInstanceNodeManager ourInstance = new WorkInstanceNodeManager(); 035 private static Logger LOG = LoggerFactory.getLogger(WorkInstanceNodeManager.class); 036 037 public static WorkInstanceNodeManager getInstance() { 038 return ourInstance; 039 } 040 041 private WorkInstanceNodeManager() { 042 super(); 043 numLevels = 2; 044 } 045 046 public String getParentNodePath() { 047 return "/work/instance"; 048 } 049 050 @Override 051 public Node createFileNode(RequestDocument document, String name, Node parentNode, Session session) 052 throws OleDocStoreException { 053 Node instanceNode = null; 054 JcrWorkInstanceDocumentManager resolver = JcrWorkInstanceDocumentManager.getInstance(); 055 List<String> docUUIDs = new ArrayList<String>(); 056 List<RequestDocument> resolvedDocs = resolver.getParsedHoldingsNItemDocuments(document, docUUIDs); 057 CustomNodeManager nodeManager = CustomNodeManager.getInstance(); 058 try { 059 instanceNode = initLevelNode(NODE_INSTANCE, parentNode, false, session); 060 Node holdingsNode = initNonStaticNode(NODE_HOLDINGS, instanceNode); 061 document.setUuid(instanceNode.getIdentifier()); 062 063 Node fileNode = nodeManager.createFileNode(resolvedDocs.get(0), FILE_INSTANCE, instanceNode, session); 064 resolver.modifyDocumentContent(resolvedDocs.get(0), fileNode.getIdentifier(), instanceNode.getIdentifier()); 065 createContentNode(fileNode, resolvedDocs.get(0), parentNode, session); 066 067 fileNode = nodeManager.createFileNode(resolvedDocs.get(1), FILE_HOLDINGS, holdingsNode, session); 068 resolver.modifyDocumentContent(resolvedDocs.get(1), fileNode.getIdentifier(), holdingsNode.getIdentifier()); 069 createContentNode(fileNode, resolvedDocs.get(1), parentNode, session); 070 071 fileNode = nodeManager.createFileNode(resolvedDocs.get(2), FILE_SOURCE_HOLDINGS, holdingsNode, session); 072 resolver.modifyDocumentContent(resolvedDocs.get(2), fileNode.getIdentifier(), holdingsNode.getIdentifier()); 073 createContentNode(fileNode, resolvedDocs.get(2), parentNode, session); 074 075 076 for (int i = 3; i < resolvedDocs.size(); i++) { 077 fileNode = nodeManager.createFileNode(resolvedDocs.get(i), FILE_ITEM, holdingsNode, session); 078 resolver.modifyDocumentContent(resolvedDocs.get(i), fileNode.getIdentifier(), 079 holdingsNode.getIdentifier()); 080 createContentNode(fileNode, resolvedDocs.get(i), parentNode, session); 081 } 082 083 ((InstanceCollection) document.getContent().getContentObject()).getInstance().get(0).setInstanceIdentifier( 084 instanceNode.getIdentifier()); 085 } catch (Exception e) { 086 LOG.info(e.getMessage(), e); 087 } 088 return instanceNode; 089 } 090 091 public void ingestItemRecForInstance(RequestDocument linkedItemDocument, String id, Session session) throws OleDocStoreException { 092 try { 093 094 Node nodeByUUID = getNodeByUUID(session, id); 095 Node holdingsNode = nodeByUUID.getNode(ProcessParameters.NODE_HOLDINGS); 096 NodeHandler nodeHandler = new NodeHandler(); 097 ItemOlemlRecordProcessor recordProcessor = new ItemOlemlRecordProcessor(); 098 Item item = recordProcessor.fromXML(linkedItemDocument.getContent().getContent()); 099 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}