View Javadoc
1   package org.kuali.ole.repository;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.solr.client.solrj.SolrQuery;
5   import org.apache.solr.client.solrj.SolrServer;
6   import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
7   import org.apache.solr.client.solrj.response.QueryResponse;
8   import org.apache.solr.common.SolrDocumentList;
9   import org.junit.Ignore;
10  import org.junit.Test;
11  import org.kuali.ole.RepositoryManager;
12  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
13  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
14  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
15  import org.kuali.rice.core.api.config.property.ConfigContext;
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  import javax.jcr.Node;
20  import javax.jcr.Session;
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import static org.junit.Assert.*;
27  
28  /**
29   * @author PJ7789
30   */
31  @Ignore
32  @Deprecated
33  public class DeleteManage_AT {
34  
35      private static final Logger LOG = LoggerFactory.getLogger(DeleteManage_AT.class);
36  
37      @Test
38      public void deleteLinkedDocs() throws Exception {
39          Session session = null;
40  
41          File file = new File(getClass().getResource("/org/kuali/ole/repository/request.xml").toURI());
42          String input = FileUtils.readFileToString(file);
43          RequestHandler rh = new RequestHandler();
44          Request request = rh.toObject(input);
45          //            IngestNIndexHandlerService ingestNIndexHandlerService = new IngestNIndexHandlerService();
46          //            Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request);
47          //            for (ResponseDocument resDoc : response.getDocuments()) {
48          //                bibIds.add(resDoc.getUuid());
49          //                for (ResponseDocument linkedDoc : resDoc.getLinkedDocuments()) {
50          //                    instanceIds.add(linkedDoc.getUuid());
51          //                }
52          //            }
53          //
54  
55  
56          List<RequestDocument> docStoreDocuments = null;
57          List<String> uuidsList = new ArrayList<String>();
58          String status = null;
59          List<String> categoryList = new ArrayList<String>();
60          List<String> typeList = new ArrayList<String>();
61          List<String> formatList = new ArrayList<String>();
62          String category = null;
63          String type = null;
64          String format = null;
65          session = RepositoryManager.getRepositoryManager().getSession();
66          try {
67              for (Iterator<RequestDocument> iterator = docStoreDocuments.iterator(); iterator.hasNext(); ) {
68                  RequestDocument document = iterator.next();
69                  category = document.getCategory();
70                  type = document.getType();
71                  format = document.getFormat();
72                  LOG.info("UUIDs...." + document.getUuid() + "category...." + category);
73                  String uuid = document.getUuid();
74                  uuidsList.add(uuid);
75                  categoryList.add(category);
76                  typeList.add(type);
77                  formatList.add(format);
78              }
79              String deleteUuidXml = deleteuuidsXml(uuidsList, categoryList, typeList, formatList);
80              //status = new DeleteManager().deleteLinkedDocs(deleteUuidXml);
81              LOG.info("status" + status);
82              checkUuidInSolr(uuidsList, category);
83              checkUuidInDocStore(uuidsList, session);
84          } catch (Exception e) {
85              LOG.info(e.getMessage() , e);
86              status = "Failure";
87              LOG.info("status" + status);
88          } finally {
89              RepositoryManager.getRepositoryManager().logout(session);
90          }
91      }
92  
93      private String deleteuuidsXml(List<String> uuidsList, List<String> categoryList, List<String> typeList,
94                                    List<String> formatList) {
95  
96          StringBuilder sb = new StringBuilder();
97          sb.append("<request>\n");
98          sb.append("	<user>").append("ole-khuntley").append("</user>\n");
99          sb.append("	<operation>").append("batchDeleteWithLinks").append("</operation>\n");
100         sb.append("<requestDocuments>\n");
101         for (int i = 0; i < uuidsList.size(); i++) {
102             sb.append("<ingestdocument id=\"" + uuidsList.get(i) + "\" category=\"" + categoryList.get(i) + "\" type=\""
103                     + typeList.get(i) + "\" format=\"" + formatList.get(i) + "\">" + "</ingestdocument>\n");
104         }
105         sb.append("	</requestDocuments>\n");
106         sb.append("	</request>");
107         LOG.info("sb.tostring" + sb.toString());
108 
109         return sb.toString();
110     }
111 
112     private void checkUuidInSolr(List<String> uuidsList, String category) throws Exception {
113         if (category.equalsIgnoreCase("bibliographic")) {
114             category = "bib";
115         }
116         SolrServer solr = new CommonsHttpSolrServer(
117                 ConfigContext.getCurrentContextConfig().getProperty("docSearchURL") + "/" + category);
118         SolrQuery query = new SolrQuery();
119         for (int i = 0; i < uuidsList.size(); i++) {
120             query.setQuery("uniqueId:" + uuidsList.get(i));
121             QueryResponse response = solr.query(query);
122             LOG.info("query..." + query);
123             SolrDocumentList doc = response.getResults();
124             LOG.info("NumFound..." + doc.getNumFound());
125             assertEquals(0, (int) (doc.getNumFound()));
126         }
127     }
128 
129     private void checkUuidInDocStore(List<String> uuidsList, Session session) throws Exception {
130         for (int i = 0; i < uuidsList.size(); i++) {
131             Node deleteNode = new NodeHandler().getNodeByUUID(session, uuidsList.get(i));
132             LOG.info("deleteNodes..." + deleteNode);
133             assertNull(deleteNode);
134         }
135     }
136 }
137 
138