001/*
002 * Copyright 2011 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.select.lookup;
017
018import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
019import org.kuali.ole.docstore.common.document.Bib;
020import org.kuali.ole.select.businessobject.BibInfoBean;
021import org.kuali.ole.select.businessobject.DocInfoBean;
022import org.kuali.ole.select.service.BibInfoWrapperService;
023import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
024import org.kuali.ole.select.service.impl.BuildDocInfoBean;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.context.SpringContext;
027import org.kuali.ole.sys.service.NonTransactional;
028import org.kuali.rice.core.api.config.property.ConfigurationService;
029import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
030
031import java.util.*;
032
033
034@NonTransactional
035public class DocLookupSearch extends PlatformAwareDaoBaseOjb implements IDocLookupSearch {
036    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocLookupSearch.class);
037    static HashMap<String, String> cache = new HashMap<String, String>();
038    private DocstoreClientLocator docstoreClientLocator;
039
040    public DocstoreClientLocator getDocstoreClientLocator() {
041        if (docstoreClientLocator == null) {
042            docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
043        }
044        return docstoreClientLocator;
045    }
046/*    public boolean createCriteria(Object arg0, String arg1, String arg2, Object arg3) {
047        // TODO Auto-generated method stub
048        return false;
049    }
050
051    
052    public boolean createCriteria(Object arg0, String arg1, String arg2, boolean arg3, boolean arg4, Object arg5) {
053        // TODO Auto-generated method stub
054        return false;
055    }*/
056
057    private List<DocData> getAllData() {
058        List<DocData> ress = new ArrayList<DocData>(0);
059        Iterator<String> iter = cache.values().iterator();
060        for (String st : cache.values()) {
061            String[] vals = st.split("\\^");
062            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]);
063            ress.add(ss);
064        }
065        return ress;
066    }
067
068    private void setCache(Map arg1) throws Exception {
069        BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
070        List<BibInfoBean> bibInfoBeanList = docStore.searchBibInfo(arg1);
071        for (BibInfoBean bean : bibInfoBeanList) {
072            String bibString = bean.getAuthor() + "^" + bean.getTitle() + "^" + bean.getStandardNumber() + "^" + bean.getPublisher() + "^" + bean.getLocalIdentifier() + "^" + bean.getPlaceOfPublication() + "^" + bean.getYearOfPublication() + "^" + bean.getFormat() + "^" + bean.getPrice() + "^" + bean.getTitleId();
073            cache.put(bean.getTitleId(), bibString);
074        }
075    }
076
077    /*
078        private List<DocData> getDocResult(Map arg1) throws Exception {
079
080        BibInfoBean bibInfoBean = new BibInfoBean();
081        bibInfoBean.setTitleId((String) arg1.get("titleId"));
082        BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
083        List<DocInfoBean> docStoreResult = docStore.searchBibInfo(bibInfoBean);
084        List<DocData> ress = new ArrayList<DocData>(0);
085        ress = getDocResult(docStoreResult);
086        return ress;
087        }
088
089        private List<DocData> getDocResult(Bib bib) throws Exception {
090           List<DocData> ress = new ArrayList<DocData>(0);
091            DocData res = new DocData();
092            for (DocInfoBean docInfoBean : docInfoBeanList) {
093                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());
094                ress.add(res);
095            }
096            return ress;
097        }
098        */
099    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}