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 org.kuali.ole.RepositoryManager;
19  import org.kuali.ole.docstore.discovery.service.IndexerServiceImpl;
20  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
21  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
22  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
24  import org.kuali.ole.docstore.service.ServiceLocator;
25  import org.kuali.ole.pojo.OleException;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.jcr.ItemNotFoundException;
30  import javax.jcr.Node;
31  import javax.jcr.RepositoryException;
32  import javax.jcr.Session;
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * @author Sreekanth
39   */
40  public class DeleteManager {
41      private static final Logger LOG                     = LoggerFactory.getLogger(DeleteManager.class);
42      private static final String DELETE_WITH_LINKED_DOCS = "deleteWithLinkedDocs";
43      private static final String BIBLIOGRAPHIC           = "bibliographic";
44      private static final String INSTANCE_IDENTIFIER     = "instanceIdentifier";
45      private static final String SUCCESS                 = "Success";
46      private static final String FAILURE                 = "Failure";
47  
48      public Response deleteDocs(Request request) throws OleException {
49          Session session = null;
50          List<RequestDocument> requestDocuments = request.getRequestDocuments();
51          List<String> uuidsList = new ArrayList<String>();
52          String status = null;
53          String category = null;
54          Response response = new Response();
55          session = RepositoryManager.getRepositoryManager().getSession(request.getUser(), request.getOperation());
56          try {
57              List<String> respositoryUuidList = new ArrayList<String>();
58              for (Iterator<RequestDocument> iterator = requestDocuments.iterator(); iterator.hasNext(); ) {
59                  RequestDocument document = iterator.next();
60                  category = document.getCategory();
61                  String uuid = document.getUuid();
62                  /* if (uuid == null)
63                uuid = document.getId();*/
64                  String operation = request.getOperation();
65                  LOG.debug("operation-->" + operation);
66                  respositoryUuidList = getLinkedDocsFromRepository(uuid, session, respositoryUuidList, operation);
67              }
68              LOG.debug("respository UuidList size-->" + respositoryUuidList.size());
69              deleteFromRepository(respositoryUuidList, session);
70              String statusValue = ServiceLocator.getIndexerService().deleteDocuments(category, respositoryUuidList);
71              if (statusValue.equalsIgnoreCase(IndexerServiceImpl.SUCCESS)) {
72                  status = SUCCESS;
73                  response = getResponse(request, requestDocuments, status, "Documents Deleted Successfully");
74                  session.save();
75              }
76              else {
77                  status = FAILURE + " - Invalid uuid";
78                  response = getResponse(request, requestDocuments, FAILURE, status);
79              }
80              LOG.debug("status" + status);
81  
82          }
83          catch (Exception e) {
84              String failOverMessage = e.getMessage();
85              if (e instanceof ItemNotFoundException) {
86                  failOverMessage = "Document Not Found for uuid : " + failOverMessage;
87              }
88              response = getResponse(request, requestDocuments, FAILURE, "Delete Failed, Cause: " + failOverMessage);
89              LOG.error("Delete Failed, Cause : " + failOverMessage, e);
90          }
91          finally {
92              RepositoryManager.getRepositoryManager().logout(session);
93          }
94          return response;
95      }
96  
97      private List<String> getLinkedDocsFromRepository(String uuid, Session session, List<String> respositoryUuidList,
98                                                       String operation) throws OleException, RepositoryException {
99  
100         Node node = session.getNodeByIdentifier(uuid);
101         if (operation.equalsIgnoreCase(DELETE_WITH_LINKED_DOCS)) {
102             if (node.getPath().contains(BIBLIOGRAPHIC)) {
103                 try {
104                     String instanceId = node.getProperty(INSTANCE_IDENTIFIER).getString();
105                     respositoryUuidList.add(instanceId);
106                 }
107                 catch (RepositoryException e) {
108                     LOG.error("Instance property not found ", e);
109                 }
110             }
111         }
112         respositoryUuidList.add(uuid);
113         return respositoryUuidList;
114     }
115 
116 
117     public Response getResponse(Request req, List<RequestDocument> requestDocuments, String status,
118                                 String statusMessage) {
119         Response response = new Response();
120         response.setUser(req.getUser());
121         response.setOperation(req.getOperation());
122         response.setStatus(status);
123         response.setStatusMessage(statusMessage);
124         ArrayList<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
125         for (int i = 0; i < requestDocuments.size(); i++) {
126             ResponseDocument responseDocument = new ResponseDocument();
127             responseDocument.setId(requestDocuments.get(i).getId());
128             responseDocument.setCategory(requestDocuments.get(i).getCategory());
129             responseDocument.setType(requestDocuments.get(i).getType());
130             responseDocument.setFormat(requestDocuments.get(i).getFormat());
131             responseDocument.setUuid(requestDocuments.get(i).getUuid());
132             responseDocumentList.add(responseDocument);
133         }
134         response.setDocuments(responseDocumentList);
135         return response;
136     }
137 
138 
139     private void deleteFromRepository(List<String> uuidsList, Session session) throws Exception {
140         if (uuidsList != null && uuidsList.size() > 0) {
141             for (int i = 0; i < uuidsList.size(); i++) {
142                 Node deleteNode = new NodeHandler().getNodeByUUID(session, uuidsList.get(i));
143                 LOG.debug("deleteNodes.........." + deleteNode);
144                 if (deleteNode != null) {
145                     LOG.debug("deleteNodes from docstore.........." + deleteNode);
146                     deleteNode.remove();
147                 }
148             }
149         }
150     }
151 
152     public void cleanUpDocStoreData() throws OleException, RepositoryException {
153         RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
154         Session session = repositoryManager.getSession("admin", "cleanUpDocStoreData");
155         Node rootNode = session.getRootNode();
156         for (Iterator<Node> iterator = rootNode.getNodes(); iterator.hasNext(); ) {
157             Node catNode = iterator.next();
158             if (catNode != null && !catNode.getName().equals("jcr:system")) {
159                 catNode.remove();
160             }
161         }
162         session.save();
163     }
164 
165 
166 }