View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.repository;
17  
18  import java.io.ByteArrayInputStream;
19  import java.util.ArrayList;
20  import java.util.Calendar;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.jcr.Binary;
25  import javax.jcr.Node;
26  import javax.jcr.RepositoryException;
27  import javax.jcr.Session;
28  import javax.jcr.version.VersionHistory;
29  import javax.jcr.version.VersionIterator;
30  import javax.jcr.version.VersionManager;
31  
32  import org.kuali.ole.RepositoryManager;
33  import org.kuali.ole.docstore.DocStoreConstants;
34  import org.kuali.ole.docstore.model.enums.DocType;
35  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
36  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleHolding;
37  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleItem;
38  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkHoldingOlemlRecordProcessor;
39  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
40  import org.kuali.ole.docstore.service.ServiceLocator;
41  import org.kuali.ole.logger.DocStoreLogger;
42  import org.kuali.ole.pojo.OleException;
43  
44  import static org.kuali.ole.docstore.process.ProcessParameters.FILE_ITEM;
45  
46  public class CheckinManager {
47      private static final String XML_DECL = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
48      DocStoreLogger docStoreLogger = new DocStoreLogger(this.getClass().getName());
49       private static final org.apache.log4j.Logger LOG  = org.apache.log4j.Logger.getLogger(CheckinManager.class);
50  
51      public String updateContent(RequestDocument requestDocument) throws OleException {
52          List<String> ingestDocs = new ArrayList<String>();
53          List<String> uuids = new ArrayList<String>();
54          StringBuilder sb = new StringBuilder();
55          String uuid = null;
56          String content = null;
57          String latestVersion = null;
58          content = requestDocument.getContent().getContent();
59          requestDocument.setUuid(requestDocument.getId());
60          uuid = requestDocument.getId();
61          ingestDocs.add(sb.toString());
62          uuids.add(uuid);
63          if (DocType.INSTANCE.getCode().equalsIgnoreCase(requestDocument.getType())
64              && content == null && requestDocument.getId() != null ) {
65              for(RequestDocument linkedItemDocument : requestDocument.getLinkedRequestDocuments()){
66                  if(DocType.ITEM.getCode().equalsIgnoreCase(linkedItemDocument.getType())
67                       && linkedItemDocument.getContent().getContent() != null){
68                       ingestNIndexItemRecForInstance(requestDocument);
69                  }
70              }
71          }
72          try {
73              ServiceLocator.getIndexerService().indexDocument(requestDocument);
74              try {
75                  latestVersion = updateRecordInDocStore(requestDocument);
76              }
77              catch (Exception e) {
78                  ServiceLocator.getIndexerService().deleteDocument(uuid, requestDocument.getCategory());
79                  e.printStackTrace();
80                  throw new OleException(e);
81              }
82          }
83          catch (Exception e) {
84              docStoreLogger.log(e.getMessage());
85              throw new OleException(e.getMessage());
86          }
87          return latestVersion;
88      }
89  
90      private void ingestNIndexItemRecForInstance(RequestDocument reqDoc) throws OleException {
91          try {
92              Session session = RepositoryManager.getRepositoryManager().getSession("CheckinManager","ingestNIndexItemRecForInstance");
93              Node nodeByUUID = getNodeByUUID(session, reqDoc.getId());
94              Node holdingsNode = nodeByUUID.getNode("holdings");
95              NodeHandler nodeHandler = new NodeHandler();
96  
97              for(RequestDocument linkedItemDocument : reqDoc.getLinkedRequestDocuments()){
98               WorkItemOlemlRecordProcessor recordProcessor = new WorkItemOlemlRecordProcessor();
99               OleItem item = recordProcessor.fromXML(linkedItemDocument.getContent().getContent());
100              linkedItemDocument.getContent().setContentObject(item);
101             String uuid = (nodeHandler.initFileNode(linkedItemDocument, FILE_ITEM, holdingsNode,
102                                                     session)).getIdentifier();
103             linkedItemDocument.setId(uuid);
104             }
105             session.save();
106         }
107         catch (Exception e) {
108            docStoreLogger.log(e.getMessage());
109            throw new OleException(e.getMessage(), e);
110         }
111     }
112 
113 
114     private String updateRecordInDocStore(RequestDocument reqDoc) throws OleException, RepositoryException {
115         Session session = RepositoryManager.getRepositoryManager().getSession("CheckinManager","updateRecordInDocStore");
116         String charset = "UTF-8";
117         byte[] documentBytes = null;
118         String currentVersion = null;
119         try {
120             if(reqDoc.getContent().getContent() != null){
121                 setIdentifierValueInContent(reqDoc);
122                 documentBytes = reqDoc.getContent().getContent().getBytes(charset);
123             }
124         }
125         catch (Exception e) {
126             getDocStoreLogger().log("Failed to convert input string to byte[] with charset " + charset, e);
127             throw new OleException(e.getMessage());
128         }
129         RequestDocument linkReqInfo = new RequestDocument();
130          if (reqDoc.getLinkedRequestDocuments() != null && reqDoc.getLinkedRequestDocuments().size() > 0) {
131             for (Iterator<RequestDocument> linkIterator = reqDoc.getLinkedRequestDocuments().iterator(); linkIterator.hasNext(); ) {
132                 linkReqInfo = linkIterator.next();
133             }
134         }
135         Node nodeByUUID = getNodeByUUID(session, reqDoc.getId());
136         try {
137             Binary binary = null;
138             if (documentBytes != null) {
139                 binary = session.getValueFactory().createBinary(new ByteArrayInputStream(documentBytes));
140                 nodeByUUID.getNode("jcr:content").setProperty("jcr:data", binary);
141             }
142             if (linkReqInfo != null && linkReqInfo.getId() != null && linkReqInfo.getType().equalsIgnoreCase(DocType.INSTANCE.getCode())) {
143                 nodeByUUID.setProperty("instanceIdentifier", linkReqInfo.getId());
144             }
145             Calendar lastModified = Calendar.getInstance();
146             lastModified.setTimeInMillis(lastModified.getTimeInMillis());
147             if(!reqDoc.getType().equalsIgnoreCase(DocType.INSTANCE.getCode())){
148                nodeByUUID.getNode("jcr:content").setProperty("jcr:lastModified", lastModified);
149             }
150             session.save();
151             if (DocStoreConstants.isVersioningEnabled) {
152                 VersionManager versionManager = getVersionManager(session);
153                 versionManager.checkpoint(nodeByUUID.getPath());
154                 VersionHistory versionHistory = versionManager.getVersionHistory(nodeByUUID.getPath());
155                 VersionIterator allVersions = versionHistory.getAllVersions();
156                 while (allVersions.hasNext()) {
157                     currentVersion = allVersions.nextVersion().getName();
158                 }
159                 getDocStoreLogger().log("Version updated for UUID:" + reqDoc.getUuid() + "  ====  version:" + currentVersion);
160             }
161         }
162         catch (Exception e) {
163             docStoreLogger.log(e.getMessage());
164             throw new OleException(e.getMessage());
165         }
166         finally {
167             RepositoryManager.getRepositoryManager().logout(session);
168         }
169         return currentVersion;
170     }
171 
172     private void setIdentifierValueInContent(RequestDocument reqDoc) {
173         if (reqDoc.getType().equalsIgnoreCase(DocType.ITEM.getCode())) {
174             WorkItemOlemlRecordProcessor recordProcessor = new WorkItemOlemlRecordProcessor();
175             OleItem item = recordProcessor.fromXML(reqDoc.getContent().getContent());
176             item.setItemIdentifier(reqDoc.getId());
177             reqDoc.getContent().setContent(recordProcessor.toXML(item));
178         }
179         if (reqDoc.getType().equalsIgnoreCase(DocType.HOLDINGS.getDescription())) {
180             WorkHoldingOlemlRecordProcessor recordProcessor = new WorkHoldingOlemlRecordProcessor();
181             OleHolding holdings = recordProcessor.fromXML(reqDoc.getContent().getContent());
182             holdings.setHoldingsIdentifier(reqDoc.getId());
183             reqDoc.getContent().setContent(recordProcessor.toXML(holdings));
184         }
185     }
186 
187     private Node getNodeByUUID(Session newSession, String uuid)
188             throws OleException {
189         return new NodeHandler().getNodeByUUID(newSession, uuid);
190     }
191 
192     public DocStoreLogger getDocStoreLogger() {
193         return docStoreLogger;
194     }
195 
196     public VersionManager getVersionManager(Session session)
197             throws OleException, RepositoryException {
198         return session.getWorkspace().getVersionManager();
199     }
200 }