View Javadoc
1   package org.kuali.ole.docstore.document.jcr;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.docstore.OleDocStoreException;
5   import org.kuali.ole.docstore.document.DocumentManager;
6   import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
7   import org.kuali.ole.docstore.repository.WorkBibNodeManager;
8   import org.kuali.ole.docstore.service.BeanLocator;
9   import org.kuali.ole.utility.Constants;
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.text.SimpleDateFormat;
17  import java.util.ArrayList;
18  import java.util.Date;
19  import java.util.List;
20  
21  /**
22   * Implements the DocumentManager interface for [Work-Bib-*] documents.
23   *
24   * @version %I%, %G%
25   * @author: tirumalesh.b
26   * Date: 31/8/12 Time: 7:04 PM
27   */
28  
29  public class JcrWorkBibDocumentManager
30          extends JcrAbstractDocumentManager {
31      private Logger logger = LoggerFactory.getLogger(this.getClass());
32      private static JcrWorkBibDocumentManager ourInstance = null;
33  
34      public static JcrWorkBibDocumentManager getInstance() {
35          if (null == ourInstance) {
36              ourInstance = new JcrWorkBibDocumentManager();
37          }
38          return ourInstance;
39      }
40  
41      protected JcrWorkBibDocumentManager() {
42          super();
43          this.nodeManager = WorkBibNodeManager.getInstance();
44      }
45  
46      /**
47       * During string ingest there may be linked instance documents in the request.
48       * In this case, the linked instance documents will also be ingested
49       *
50       * @param requestDocument
51       * @param responseDocument
52       */
53      protected void buildLinkedResponseDocuments(RequestDocument requestDocument, ResponseDocument responseDocument) {
54          if ((null == requestDocument.getLinkedRequestDocuments()) || (requestDocument.getLinkedRequestDocuments().size() == 0)) {
55              return;
56          }
57          for (RequestDocument linkedRequestDocument : requestDocument.getLinkedRequestDocuments()) {
58              DocumentManager documentManager = BeanLocator.getDocstoreFactory()
59                      .getDocumentManager(linkedRequestDocument.getCategory(), linkedRequestDocument.getType(), linkedRequestDocument.getFormat());
60              ResponseDocument linkedResponseDocument = documentManager.buildResponseDocument(linkedRequestDocument);
61              responseDocument.getLinkedDocuments().add(linkedResponseDocument);
62          }
63      }
64  
65      /**
66       * Invoked from JcrAbstractDocumentManager.java
67       * Updating BibStatus fields based on ingest/checking operation is invoked
68       *
69       * @param requestDocument
70       * @param node
71       */
72      @Override
73      protected void modifyAdditionalAttributes(RequestDocument requestDocument, Node node) {
74          Date date = new Date();
75          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
76          String dateStr = sdf.format(date);
77          String user = requestDocument.getUser();
78          String statusFromReqDoc = "";
79          String statusFromNode = "";
80          AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
81          if (additionalAttributes == null) {
82              additionalAttributes = new AdditionalAttributes();
83          }
84          if (requestDocument.getOperation() != null) {
85              if (requestDocument.getOperation().equalsIgnoreCase(Request.Operation.ingest.toString())) {
86                  statusFromReqDoc = additionalAttributes.getAttribute(AdditionalAttributes.STATUS);
87                  additionalAttributes.setAttribute(AdditionalAttributes.DATE_ENTERED, dateStr);
88                  additionalAttributes.setAttribute(AdditionalAttributes.CREATED_BY, user);
89                  //Add statusUpdatedBy and statusUpdatedOn if input request is having non empty status field
90                  if (StringUtils.isNotEmpty(statusFromReqDoc)) {
91                      additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_BY, user);
92                      additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_ON, dateStr);
93                  }
94              } else if (requestDocument.getOperation().equalsIgnoreCase(Request.Operation.checkIn.toString())) {
95                  if (requestDocument.getAdditionalAttributes() != null) {
96                      statusFromReqDoc = additionalAttributes.getAttribute(Constants.STATUS);
97                  }
98  
99                  try {
100                     if (node != null) {
101                         if (node.hasProperty(Constants.STATUS)) {
102                             statusFromNode = node.getProperty(Constants.STATUS).getString();
103                             //compare status parameter of the node and status parameter from reqDoc
104                             if (!statusFromNode.equals(statusFromReqDoc)) {
105                                 additionalAttributes.setAttribute(Constants.STATUS_UPDATED_BY, user);
106                                 additionalAttributes.setAttribute(Constants.STATUS_UPDATED_ON, dateStr);
107                             }
108                         }
109                     }
110                 } catch (RepositoryException e) {
111                     logger.error("Exception while getting node property, Cause:" + e.getMessage(), e);
112                 }
113 
114                 additionalAttributes.setAttribute(Constants.UPDATED_BY, user);
115                 additionalAttributes.setAttribute(Constants.LAST_UPDATED, dateStr);
116                 requestDocument.setAdditionalAttributes(additionalAttributes);
117             }
118         }
119     }
120 
121     @Override
122     public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
123         return null;
124     }
125 
126     @Override
127     public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
128         Session session = (Session) object;
129         ResponseDocument responseDocument = deleteVerify(requestDocument, session);
130         List<String> identifierListToDelete = new ArrayList<String>();
131         if (responseDocument.getStatus().equalsIgnoreCase("success")) {
132             Node bibNode = session.getNodeByIdentifier(requestDocument.getUuid());
133             if (bibNode.hasProperty("instanceIdentifier")) {
134                 String instanceIdentifier = bibNode.getProperty("instanceIdentifier").getString();
135                 String instanceIds[] = instanceIdentifier.split(",");
136 
137                 //Get the instance ids of Bib to delete
138                 for (String instanceId : instanceIds) {
139                     identifierListToDelete.add(instanceId);
140                 }
141             }
142             String bibIdentifier = requestDocument.getUuid();
143             identifierListToDelete.add(bibIdentifier);
144             deleteFromRepository(identifierListToDelete, session);
145             //TODO build response document
146         } else {
147             //TODO build response document
148         }
149         return new ResponseDocument();
150     }
151 
152     @Override
153     public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
154         Response response = new Response();
155         Session session = (Session) object;
156         ResponseDocument responseDocument = new ResponseDocument();
157         List<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
158         List<String> instanceIdentifierList = new ArrayList<String>();
159         Node bibNode = session.getNodeByIdentifier(requestDocument.getUuid());
160 
161         if (bibNode.hasProperty("instanceIdentifier")) {
162             String instanceIdentifier = bibNode.getProperty("instanceIdentifier").getString();
163             String instanceIds[] = instanceIdentifier.split(",");
164             for (String instanceId : instanceIds) {
165                 instanceIdentifierList.add(instanceId);
166             }
167         }
168         for (String instanceIdentifierValue : instanceIdentifierList) {
169             Node instanceNode = session.getNodeByIdentifier(instanceIdentifierValue);
170             String bibIdentifier = instanceNode.getProperty("bibIdentifier").getString();
171             String[] bibIds = bibIdentifier.split(",");
172             if (bibIds.length > 1) {
173                 responseDocument.setCategory(requestDocument.getCategory());
174                 responseDocument.setType(requestDocument.getType());
175                 responseDocument.setFormat(requestDocument.getFormat());
176                 responseDocument.setUuid(requestDocument.getUuid());
177                 responseDocument.setStatus("failure'");
178                 responseDocument.setStatusMessage("Instance is bound with more than one bid. So deletion cannot be done");
179                 return responseDocument;
180             }
181             boolean exists = checkInstancesOrItemsExistsInOLE(instanceIdentifierValue, session);
182             if (exists) {
183                 responseDocument.setId(requestDocument.getId());
184                 responseDocument.setCategory(requestDocument.getCategory());
185                 responseDocument.setType(requestDocument.getType());
186                 responseDocument.setFormat(requestDocument.getFormat());
187                 responseDocument.setUuid(requestDocument.getUuid());
188                 responseDocument.setStatus("failure");
189                 responseDocument.setStatusMessage("Instances or Items in use. So deletion cannot be done");
190                 return responseDocument;
191             }
192         }
193         responseDocument.setId(requestDocument.getId());
194         responseDocument.setCategory(requestDocument.getCategory());
195         responseDocument.setType(requestDocument.getType());
196         responseDocument.setFormat(requestDocument.getFormat());
197         responseDocument.setUuid(requestDocument.getUuid());
198         responseDocument.setStatus("success");
199         responseDocument.setStatusMessage("success");
200         return responseDocument;
201     }
202 
203     @Override
204     public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
205         for (RequestDocument linkDoc : requestDocument.getLinkedRequestDocuments()) {
206             Session session = (Session) object;
207             JcrWorkInstanceDocumentManager.getInstance().validateInput(linkDoc, session, valuesList);
208         }
209     }
210 }