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.select.lookup;
17  
18  import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
19  import org.kuali.ole.docstore.common.document.Bib;
20  import org.kuali.ole.select.businessobject.BibInfoBean;
21  import org.kuali.ole.select.businessobject.DocInfoBean;
22  import org.kuali.ole.select.service.BibInfoWrapperService;
23  import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
24  import org.kuali.ole.select.service.impl.BuildDocInfoBean;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.service.NonTransactional;
28  import org.kuali.rice.core.api.config.property.ConfigurationService;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  
31  import java.util.*;
32  
33  
34  @NonTransactional
35  public class DocLookupSearch extends PlatformAwareDaoBaseOjb implements IDocLookupSearch {
36      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocLookupSearch.class);
37      static HashMap<String, String> cache = new HashMap<String, String>();
38      private DocstoreClientLocator docstoreClientLocator;
39  
40      public DocstoreClientLocator getDocstoreClientLocator() {
41          if (docstoreClientLocator == null) {
42              docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
43          }
44          return docstoreClientLocator;
45      }
46  /*    public boolean createCriteria(Object arg0, String arg1, String arg2, Object arg3) {
47          // TODO Auto-generated method stub
48          return false;
49      }
50  
51      
52      public boolean createCriteria(Object arg0, String arg1, String arg2, boolean arg3, boolean arg4, Object arg5) {
53          // TODO Auto-generated method stub
54          return false;
55      }*/
56  
57      private List<DocData> getAllData() {
58          List<DocData> ress = new ArrayList<DocData>(0);
59          Iterator<String> iter = cache.values().iterator();
60          for (String st : cache.values()) {
61              String[] vals = st.split("\\^");
62              DocData ss = new DocData(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7], vals[8], vals[9], vals[10], vals[11]);
63              ress.add(ss);
64          }
65          return ress;
66      }
67  
68      private void setCache(Map arg1) throws Exception {
69          BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
70          List<BibInfoBean> bibInfoBeanList = docStore.searchBibInfo(arg1);
71          for (BibInfoBean bean : bibInfoBeanList) {
72              String bibString = bean.getAuthor() + "^" + bean.getTitle() + "^" + bean.getStandardNumber() + "^" + bean.getPublisher() + "^" + bean.getLocalIdentifier() + "^" + bean.getPlaceOfPublication() + "^" + bean.getYearOfPublication() + "^" + bean.getFormat() + "^" + bean.getPrice() + "^" + bean.getTitleId();
73              cache.put(bean.getTitleId(), bibString);
74          }
75      }
76  
77      /*
78          private List<DocData> getDocResult(Map arg1) throws Exception {
79  
80          BibInfoBean bibInfoBean = new BibInfoBean();
81          bibInfoBean.setTitleId((String) arg1.get("titleId"));
82          BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
83          List<DocInfoBean> docStoreResult = docStore.searchBibInfo(bibInfoBean);
84          List<DocData> ress = new ArrayList<DocData>(0);
85          ress = getDocResult(docStoreResult);
86          return ress;
87          }
88  
89          private List<DocData> getDocResult(Bib bib) throws Exception {
90             List<DocData> ress = new ArrayList<DocData>(0);
91              DocData res = new DocData();
92              for (DocInfoBean docInfoBean : docInfoBeanList) {
93                  res = new DocData(docInfoBean.getAuthor_display(), docInfoBean.getTitle_display().trim(), docInfoBean.getIsbn_display(), docInfoBean.getLocalIdentifier_search(), docInfoBean.getPublisher_display(), docInfoBean.getPublicationPlace_search(), docInfoBean.getDateOfPublication(), docInfoBean.getDocType(), docInfoBean.getPrice_f(), docInfoBean.getTitleId(), docInfoBean.getUniqueId(), docInfoBean.getBibIdentifier());
94                  ress.add(res);
95              }
96              return ress;
97          }
98          */
99      private List<DocData> getDocResult(Map arg1) throws Exception {
100         BibInfoBean bibInfoBean = new BibInfoBean();
101         List<Bib> bibs=new ArrayList<>();
102         bibInfoBean.setTitleId((String) arg1.get("titleId"));
103         Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib((String) arg1.get("titleId"));
104         List<DocData> ress = new ArrayList<DocData>(0);
105         bibs.add(bib);
106         ress = getDocResult(bibs);
107         return ress;
108     }
109 
110     private List<DocData> getDocResult(List<Bib> bibs) throws Exception {
111         List<DocData> ress = new ArrayList<DocData>(0);
112         for(Bib bib:bibs){
113         DocData res = new DocData(bib.getAuthor(), bib.getTitle(), bib.getIsbn(), bib.getLocalId(), bib.getPublisher(), "", bib.getPublicationDate(), bib.getType(), "", bib.getId(), bib.getId(), bib.getId());
114         ress.add(res);
115         }
116         return ress;
117     }
118 /*    public Collection findCollectionBySearchHelper(Class arg0, Map arg1, boolean arg2, boolean arg3) {
119         try{
120             return getDocResult(arg1);
121         }catch(Exception e){
122             return null;
123         }
124     }
125 
126     
127     public Collection findCollectionBySearchHelper(Class arg0, Map arg1, boolean arg2, boolean arg3, Object arg4) {
128         try{
129             return getDocResult(arg1);
130         }catch(Exception e){
131             return null;
132         }
133     }
134 
135     
136     public Long findCountByMap(Object arg0, Map arg1) {
137         // TODO Auto-generated method stub
138         return null;
139     }*/
140 
141 
142     public Object findObjectByMap(Object arg0, Map arg1) {
143         // TODO Auto-generated method stub
144         return null;
145     }
146 
147     public List getResult(Class cl, String attr, List<Object> vals) throws Exception {
148         List result = new ArrayList(0);
149         int maxLimit = Integer.parseInt(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.DOCSEARCH_ORDERQUEUE_LIMIT_KEY));
150         HashMap titleIdMap = new HashMap();
151         List<String> bibIds=new ArrayList<>();
152         List<Bib> bibs=new ArrayList<>();
153         String bibId = null;
154         for (Object iv : vals) {
155             String id = iv.toString();
156             boolean isIdExists = titleIdMap.containsValue(id);
157             titleIdMap.put(id, id);
158             if (!isIdExists) {
159                bibIds.add(id);
160             }
161         }
162         if(bibIds.size()>0){
163             for(String id:bibIds){
164                 Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(id);
165                 bibs.add(bib);
166             }
167         }
168         result = getDocResult(bibs);
169         return result;
170        /* List result = new ArrayList(0);
171         int maxLimit = Integer.parseInt(SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.DOCSEARCH_ORDERQUEUE_LIMIT_KEY));
172 
173         List<DocInfoBean> docInfoBeanList = new ArrayList<DocInfoBean>(0);
174 
175         StringBuilder query = new StringBuilder("q=");
176         query.append("(");
177         HashMap titleIdMap = new HashMap();
178         int loop = 0;
179         for (Object iv : vals) {
180             String id = iv.toString();
181             boolean isIdExists = titleIdMap.containsValue(id);
182             titleIdMap.put(id, id);
183             if (!isIdExists) {
184                 if (loop != 0) query.append("OR");
185                 query.append("(id:" + id + ")");
186                 loop++;
187             }
188             if (loop == maxLimit)
189                 break;
190         }
191         query.append(")");
192         query.append("&fl=Title,id,NameOfPublisher,MainEntryPersonalName");
193         // Changes to include userId in docstore URl.
194         if (GlobalVariables.getUserSession() != null) {
195             query.append("&userId=" + GlobalVariables.getUserSession().getPerson().getPrincipalName());
196         }
197         query.append("&rows=" + loop);
198         if (LOG.isDebugEnabled())
199             LOG.debug("Doc Store Query :" + query.toString());
200 
201         if (loop > 0) {
202             docInfoBeanList = getResponse(query.toString());
203             result = getDocResult(docInfoBeanList);
204         }
205         return result;*/
206     }
207 
208     public List getResult(Class cl, Map<String, List<Object>> val) {
209         List resul = new ArrayList(0);
210         for (String key : val.keySet()) {
211             List ll;
212             try {
213                 ll = getResult(cl, key, val.get(key));
214                 if (ll != null && ll.size() > 0)
215                     resul.addAll(ll);
216             } catch (Exception e) {
217                 e.printStackTrace();
218             }
219 
220 
221         }
222         return resul;
223 
224     }
225 
226     public List getResult(Class cl, String attr, List<Object> val, Map vas) {
227         try {
228             if (vas == null || vas.size() < 1) {
229                 return getResult(cl, attr, val);
230             } else {
231                 return getDocResult(vas);
232             }
233         } catch (Exception e) {
234             return null;
235         }
236     }
237 
238     private List<DocInfoBean> getResponse(String query) {
239         List<DocInfoBean> docInfoBeanList = new ArrayList<DocInfoBean>(0);
240         BuildDocInfoBean buildDocInfoBean = new BuildDocInfoBean();
241         docInfoBeanList = buildDocInfoBean.getDocInfoBeanList(query);
242         return docInfoBeanList;
243     }
244 }