View Javadoc
1   package org.kuali.ole.docstore.engine.service.rest;
2   
3   import org.kuali.ole.docstore.common.client.DocstoreRestClient;
4   import org.kuali.ole.docstore.common.document.Item;
5   import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
6   import org.kuali.ole.docstore.common.search.SearchParams;
7   import org.kuali.ole.docstore.common.search.SearchResponse;
8   import org.kuali.ole.docstore.model.enums.DocType;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import java.util.concurrent.Callable;
13  
14  /**
15   * Created by jayabharathreddy on 26/7/14.
16   */
17  public class TestUpdateAndSearchCals implements Callable<Long> {
18  
19      private DocstoreRestClient restClient = new DocstoreRestClient();
20      ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
21      private static final Logger LOG = LoggerFactory.getLogger(TestUpdateAndSearchCals.class);
22  
23      private String itemId;
24      private String enumeration;
25      private static Long count = new Long(0);
26  
27      public Long getCount() {
28          return count;
29      }
30  
31      public void setCount(Long count) {
32          this.count = count;
33      }
34  
35      public String getItemId() {
36          return itemId;
37      }
38  
39      public void setItemId(String itemId) {
40          this.itemId = itemId;
41      }
42  
43      public String getEnumeration() {
44          return enumeration;
45      }
46  
47      public void setEnumeration(String enumeration) {
48          this.enumeration = enumeration;
49      }
50  
51      public TestUpdateAndSearchCals(String itemId, String enumeration) {
52          this.itemId = itemId;
53          this.enumeration = enumeration;
54      }
55  
56      @Override
57      public Long call() throws Exception {
58          count++;
59          testUpdateAndSearch(itemId, enumeration);
60  //        updateItem(itemId, enumeration);
61          return count;
62      }
63  
64  
65  
66      public void testUpdateAndSearch(String itemId, String enumeration) {
67          updateItem(itemId, enumeration);
68  //        searchItem(itemId, enumeration);
69      }
70  
71      private void searchItem(String itemId, String enumeration) {
72  
73          SearchParams searchParams = new SearchParams();
74          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "id", itemId), "AND"));
75          searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "Enumeration_search", enumeration), "AND"));
76          SearchResponse response = restClient.search(searchParams);
77  //        LOG.info("no. of item with id " + itemId + " and with enumeration " + enumeration + " is " + response.getSearchResults().size());
78          System.out.println("no. of item with id " + itemId + " and with enumeration " + enumeration + " is " + response.getSearchResults().size());
79      }
80  
81      private void updateItem(String itemId, String enumeration) {
82          try {
83              Item item = restClient.retrieveItem(itemId);
84              org.kuali.ole.docstore.common.document.content.instance.Item oleItem = itemOlemlRecordProcessor.fromXML(item.getContent());
85              oleItem.setEnumeration(enumeration);
86              item.setContent(itemOlemlRecordProcessor.toXML(oleItem));
87              long startTime = System.currentTimeMillis();
88              restClient.updateItem(item);
89              long endTime = System.currentTimeMillis();
90  //            LOG.info("Updating the item with id " + itemId + " with enumeration " + enumeration);
91              System.out.println("Updating the item with id " + itemId + " with enumeration " + enumeration + " and time taken is  " + (endTime-startTime));
92          }
93          catch (Exception e) {
94              LOG.info("item not found " + itemId);
95          }
96      }
97  
98      private void searchItem(String itemId) {
99          SearchParams searchParams = new SearchParams();
100         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.ITEM.getCode(), "id", itemId), "AND"));
101         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(DocType.ITEM.getCode(), "ENUMERATION"));
102         SearchResponse response = restClient.search(searchParams);
103         if(response.getSearchResults() != null && response.getSearchResults().size() > 0 && response.getSearchResults().get(0).getSearchResultFields() != null && response.getSearchResults().get(0).getSearchResultFields().size() > 0 && response.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue() != null) {
104             enumeration = response.getSearchResults().get(0).getSearchResultFields().get(0).getFieldValue();
105         }
106         System.out.println("item id " + itemId + " with enumeration " + enumeration);
107 
108     }
109 
110 }