1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.repository;
17
18 import org.kuali.ole.RepositoryManager;
19 import org.kuali.ole.docstore.indexer.solr.IndexerService;
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
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
63
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(IndexerService.SUCCESS)) {
72 status = SUCCESS;
73 response = getResponse(request, requestDocuments, status, "Documents Deleted Successfully");
74 session.save();
75 } else {
76 status = FAILURE + " - Invalid uuid";
77 response = getResponse(request, requestDocuments, FAILURE, status);
78 }
79 LOG.debug("status" + status);
80
81 } catch (Exception e) {
82 String failOverMessage = e.getMessage();
83 if (e instanceof ItemNotFoundException) {
84 failOverMessage = "Document Not Found for uuid : " + failOverMessage;
85 }
86 response = getResponse(request, requestDocuments, FAILURE, "Delete Failed, Cause: " + failOverMessage);
87 LOG.error("Delete Failed, Cause : " + failOverMessage, e);
88 } finally {
89 RepositoryManager.getRepositoryManager().logout(session);
90 }
91 return response;
92 }
93
94 private List<String> getLinkedDocsFromRepository(String uuid, Session session, List<String> respositoryUuidList,
95 String operation) throws OleException, RepositoryException {
96
97 Node node = session.getNodeByIdentifier(uuid);
98 if (operation.equalsIgnoreCase(DELETE_WITH_LINKED_DOCS)) {
99 if (node.getPath().contains(BIBLIOGRAPHIC)) {
100 try {
101 String instanceId = node.getProperty(INSTANCE_IDENTIFIER).getString();
102 respositoryUuidList.add(instanceId);
103 } catch (RepositoryException e) {
104 LOG.error("Instance property not found ", e);
105 }
106 }
107 }
108 respositoryUuidList.add(uuid);
109 return respositoryUuidList;
110 }
111
112
113 public Response getResponse(Request req, List<RequestDocument> requestDocuments, String status,
114 String statusMessage) {
115 Response response = new Response();
116 response.setUser(req.getUser());
117 response.setOperation(req.getOperation());
118 response.setStatus(status);
119 response.setStatusMessage(statusMessage);
120 ArrayList<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
121 for (int i = 0; i < requestDocuments.size(); i++) {
122 ResponseDocument responseDocument = new ResponseDocument();
123 responseDocument.setId(requestDocuments.get(i).getId());
124 responseDocument.setCategory(requestDocuments.get(i).getCategory());
125 responseDocument.setType(requestDocuments.get(i).getType());
126 responseDocument.setFormat(requestDocuments.get(i).getFormat());
127 responseDocument.setUuid(requestDocuments.get(i).getUuid());
128 responseDocumentList.add(responseDocument);
129 }
130 response.setDocuments(responseDocumentList);
131 return response;
132 }
133
134
135 private void deleteFromRepository(List<String> uuidsList, Session session) throws Exception {
136 if (uuidsList != null && uuidsList.size() > 0) {
137 for (int i = 0; i < uuidsList.size(); i++) {
138 Node deleteNode = new NodeHandler().getNodeByUUID(session, uuidsList.get(i));
139 LOG.debug("deleteNodes.........." + deleteNode);
140 if (deleteNode != null) {
141 LOG.debug("deleteNodes from docstore.........." + deleteNode);
142 deleteNode.remove();
143 }
144 }
145 }
146 }
147
148 public void cleanUpDocStoreData() throws OleException, RepositoryException {
149 RepositoryManager repositoryManager = RepositoryManager.getRepositoryManager();
150 Session session = repositoryManager.getSession("admin", "cleanUpDocStoreData");
151 Node rootNode = session.getRootNode();
152 for (Iterator<Node> iterator = rootNode.getNodes(); iterator.hasNext(); ) {
153 Node catNode = iterator.next();
154 if (catNode != null && !catNode.getName().equals("jcr:system")) {
155 catNode.remove();
156 }
157 }
158 session.save();
159 }
160
161
162 }