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.service.impl;
17  
18  import org.kuali.ole.select.businessobject.DocInfoBean;
19  import org.kuali.ole.sys.OLEConstants;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.config.property.ConfigurationService;
22  import org.xml.sax.Attributes;
23  import org.xml.sax.SAXException;
24  import org.xml.sax.helpers.DefaultHandler;
25  
26  import javax.xml.parsers.SAXParser;
27  import javax.xml.parsers.SAXParserFactory;
28  import java.io.ByteArrayInputStream;
29  import java.io.InputStream;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Properties;
33  
34  public class BuildDocInfoBean {
35      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BuildDocInfoBean.class);
36  
37      private DocInfoBean docInfoBean;
38      private List<DocInfoBean> docInfoBeanList = null;
39      private StringBuilder stringBuilder = null;
40      private String noOfRecords = null;
41      private String attribute = null;
42  
43  
44      public List<DocInfoBean> getDocInfoBeanList(String query) {
45          docInfoBeanList = new ArrayList<DocInfoBean>();
46          try {
47              initiateDocument(query);
48          } catch (Exception ex) {
49              LOG.error("Exception while getting DocInfoBeanList"+ex);
50          }
51          return docInfoBeanList;
52      }
53  
54      private void initiateDocument(String query) throws Exception {
55  
56          if (query == null || query.length() == 0) {
57              Properties properties = new Properties();
58              try {
59                  //properties.load(new FileInputStream("org/kuali/ole/select/service/impl/bibinfo.properties"));
60                  query = properties.getProperty("filename");
61              } catch (Exception e) {
62                  LOG.error("Exception while initiating the document"+e);
63              }
64          }
65          SAXParserFactory factory = SAXParserFactory.newInstance();
66          SAXParser saxParser = factory.newSAXParser();
67  
68          DefaultHandler handler = new DefaultHandler() {
69  
70              //boolean author = false;
71              boolean author_display = false;
72  
73              boolean dateOfPublication = false;
74  
75              boolean date_d = false;
76  
77              //boolean description = false;
78              boolean description_display = false;
79  
80              boolean dimentions = false;
81  
82              boolean docType = false;
83  
84              boolean extent = false;
85  
86              boolean generalNote = false;
87  
88              //boolean isbn = false;
89              boolean isbn_display = false;
90  
91              boolean issn_display = false;
92  
93              boolean mainEntryPersonalNameComposite = false;
94  
95              boolean mainEntryPersonalNameComposite_suggest = false;
96  
97              boolean modifyingAgency = false;
98  
99              boolean modifyingAgency_suggest = false;
100 
101             //boolean nameOfPublisher = false;
102             boolean publisher_display = false;
103 
104             //boolean placeOfPublication = false;
105             boolean publicationPlace_search = false;
106 
107             boolean placeOfPublication_suggest = false;
108 
109             boolean price_f = false;
110 
111             boolean remainderOfTitle = false;
112 
113             boolean statementOfResponsibility = false;
114 
115             //boolean subject = false;
116             boolean subject_display = false;
117 
118             //boolean title = false;
119             boolean title_display = false;
120 
121             boolean titleStatementComposite = false;
122 
123             boolean title_suggest = false;
124 
125             boolean topicalTermorgeographicnameElement = false;
126 
127             boolean yearOfPublication = false;
128 
129             boolean all_controlFields = false;
130 
131             boolean all_subfields = false;
132 
133             boolean all_text = false;
134 
135             boolean itemLinks = false;
136 
137             boolean uniqueId = false;
138 
139             boolean instanceId = false;
140 
141             boolean bibIdentifier = false;
142 
143             boolean publisher_search = false;
144 
145             boolean localIdentifier_search = false;
146 
147             boolean publicationDate_search = false;
148 
149 
150             public void startElement(String uri, String localName,
151                                      String qName, Attributes attributes)
152                     throws SAXException {
153 
154 
155                 if (qName.equalsIgnoreCase("doc")) {
156                     docInfoBean = new DocInfoBean();
157                 }
158                 if (qName.equalsIgnoreCase("result")) {
159                     noOfRecords = attributes.getValue("numFound");
160                 }
161 
162 
163                 if (qName.equalsIgnoreCase("str")) {
164                     String attribute = attributes.getValue("name");
165                     if (attributes.getValue("name") != null) {
166                         if ("Description_display".equalsIgnoreCase(attribute)) {
167                             stringBuilder = new StringBuilder();
168                             description_display = true;
169                         } else if ("ISBN_display".equalsIgnoreCase(attribute)) {
170                             stringBuilder = new StringBuilder();
171                             isbn_display = true;
172                         } else if ("Subject_display ".equalsIgnoreCase(attribute)) {
173                             stringBuilder = new StringBuilder();
174                             subject_display = true;
175                         } else if ("all_controlFields".equalsIgnoreCase(attribute)) {
176                             stringBuilder = new StringBuilder();
177                             all_controlFields = true;
178                         } else if ("all_subfields".equalsIgnoreCase(attribute)) {
179                             stringBuilder = new StringBuilder();
180                             all_subfields = true;
181                         } else if ("all_text".equalsIgnoreCase(attribute)) {
182                             stringBuilder = new StringBuilder();
183                             all_text = true;
184                         }
185                     }
186                 }
187 
188 
189                 if (qName.equalsIgnoreCase("arr")) {
190 
191                     attribute = attributes.getValue("name");
192 
193                     if ("Author_display".equalsIgnoreCase(attributes.getValue("name"))) {
194                         stringBuilder = new StringBuilder();
195                         author_display = true;
196                     } else if ("Description_display".equalsIgnoreCase(attributes.getValue("name"))) {
197                         stringBuilder = new StringBuilder();
198                         description_display = true;
199                     } else if ("Title_display".equalsIgnoreCase(attributes.getValue("name"))) {
200                         stringBuilder = new StringBuilder();
201                         title_display = true;
202                     } else if ("Publisher_display".equalsIgnoreCase(attributes.getValue("name"))) {
203                         stringBuilder = new StringBuilder();
204                         //nameOfPublisher= true;
205                         publisher_display = true;
206                     } else if ("Publisher_search".equalsIgnoreCase(attributes.getValue("name"))) {
207                         stringBuilder = new StringBuilder();
208                         //nameOfPublisher= true;
209                         publisher_search = true;
210                     } else if ("LocalId_search".equalsIgnoreCase(attributes.getValue("name"))) {
211                         stringBuilder = new StringBuilder();
212                         localIdentifier_search = true;
213                     } else if ("LocalId_display".equalsIgnoreCase(attributes.getValue("name"))) {
214                         stringBuilder = new StringBuilder();
215                         localIdentifier_search = true;
216                     } else if ("PublicationDate_search".equalsIgnoreCase(attributes.getValue("name"))) {
217                         stringBuilder = new StringBuilder();
218                         //nameOfPublisher= true;
219                         publicationDate_search = true;
220                     } else if ("ISBN_display".equalsIgnoreCase(attributes.getValue("name"))) {
221                         stringBuilder = new StringBuilder();
222                         isbn_display = true;
223                     } else if ("Subject_display".equalsIgnoreCase(attributes.getValue("name"))) {
224                         stringBuilder = new StringBuilder();
225                         subject_display = true;
226                     } else if ("all_controlFields".equalsIgnoreCase(attributes.getValue("name"))) {
227                         stringBuilder = new StringBuilder();
228                         all_controlFields = true;
229                     } else if ("all_subfields".equalsIgnoreCase(attributes.getValue("name"))) {
230                         stringBuilder = new StringBuilder();
231                         all_subfields = true;
232                     } else if ("all_text".equalsIgnoreCase(attributes.getValue("name"))) {
233                         stringBuilder = new StringBuilder();
234                         all_text = true;
235                     } else if ("ItemLinks".equalsIgnoreCase(attributes.getValue("name"))) {
236                         stringBuilder = new StringBuilder();
237                         itemLinks = true;
238                     } else if ("instanceIdentifier".equalsIgnoreCase(attributes.getValue("name"))) {
239                         stringBuilder = new StringBuilder();
240                         instanceId = true;
241                     } else if ("bibIdentifier".equalsIgnoreCase(attributes.getValue("name"))) {
242                         stringBuilder = new StringBuilder();
243                         bibIdentifier = true;
244                     }
245                 }
246 
247 
248                 if (qName.equalsIgnoreCase("str") || qName.equalsIgnoreCase("date") || qName.equalsIgnoreCase("float")) {
249 
250                     if ("DateOfPublication".equalsIgnoreCase(attributes.getValue("name"))) {
251                         stringBuilder = new StringBuilder();
252                         dateOfPublication = true;
253                     } else if ("Date_d".equalsIgnoreCase(attributes.getValue("name"))) {
254                         stringBuilder = new StringBuilder();
255                         date_d = true;
256                     } else if ("Dimentions".equalsIgnoreCase(attributes.getValue("name"))) {
257                         stringBuilder = new StringBuilder();
258                         dimentions = true;
259                     } else if ("DocType".equalsIgnoreCase(attributes.getValue("name"))) {
260                         stringBuilder = new StringBuilder();
261                         docType = true;
262                     } else if ("Extent".equalsIgnoreCase(attributes.getValue("name"))) {
263                         stringBuilder = new StringBuilder();
264                         extent = true;
265                     } else if ("GeneralNote".equalsIgnoreCase(attributes.getValue("name"))) {
266                         stringBuilder = new StringBuilder();
267                         generalNote = true;
268                     } else if ("MainEntryPersonalNameComposite".equalsIgnoreCase(attributes.getValue("name"))) {
269                         stringBuilder = new StringBuilder();
270                         mainEntryPersonalNameComposite = true;
271                     } else if ("MainEntryPersonalNameComposite_suggest".equalsIgnoreCase(attributes.getValue("name"))) {
272                         stringBuilder = new StringBuilder();
273                         mainEntryPersonalNameComposite_suggest = true;
274                     } else if ("ModifyingAgency".equalsIgnoreCase(attributes.getValue("name"))) {
275                         stringBuilder = new StringBuilder();
276                         modifyingAgency = true;
277                     } else if ("ModifyingAgency_suggest".equalsIgnoreCase(attributes.getValue("name"))) {
278                         stringBuilder = new StringBuilder();
279                         modifyingAgency_suggest = true;
280                     } else if ("PublicationPlace_search".equalsIgnoreCase(attributes.getValue("name"))) {
281                         stringBuilder = new StringBuilder();
282                         publicationPlace_search = true;
283                     } else if ("PlaceOfPublication_suggest".equalsIgnoreCase(attributes.getValue("name"))) {
284                         stringBuilder = new StringBuilder();
285                         placeOfPublication_suggest = true;
286                     } else if ("Price_f".equalsIgnoreCase(attributes.getValue("name"))) {
287                         stringBuilder = new StringBuilder();
288                         price_f = true;
289                     } else if ("RemainderOfTitle".equalsIgnoreCase(attributes.getValue("name"))) {
290                         stringBuilder = new StringBuilder();
291                         remainderOfTitle = true;
292                     } else if ("StatementOfResponsibility".equalsIgnoreCase(attributes.getValue("name"))) {
293                         stringBuilder = new StringBuilder();
294                         statementOfResponsibility = true;
295                     } else if ("TitleStatementComposite".equalsIgnoreCase(attributes.getValue("name"))) {
296                         stringBuilder = new StringBuilder();
297                         titleStatementComposite = true;
298                     } else if ("Title_suggest".equalsIgnoreCase(attributes.getValue("name"))) {
299                         stringBuilder = new StringBuilder();
300                         title_suggest = true;
301                     } else if ("TopicalTermorgeographicnameElement".equalsIgnoreCase(attributes.getValue("name"))) {
302                         stringBuilder = new StringBuilder();
303                         topicalTermorgeographicnameElement = true;
304                     } else if ("YearOfPublication".equalsIgnoreCase(attributes.getValue("name"))) {
305                         stringBuilder = new StringBuilder();
306                         yearOfPublication = true;
307                     }
308             /* else if ("id".equalsIgnoreCase(attributes.getValue("name"))) {
309                  id= true;
310               }*/
311                     else if ("uniqueId".equalsIgnoreCase(attributes.getValue("name"))) {
312                         stringBuilder = new StringBuilder();
313                         uniqueId = true;
314                     }
315                 }
316             }
317 
318             public void endElement(String uri, String localName,
319                                    String qName)
320                     throws SAXException {
321                 String value = null;
322                 if (stringBuilder != null)
323                     if (!"".equals(stringBuilder.toString())) {
324                         value = stringBuilder.toString();
325                     }
326                 if (author_display) {
327                     docInfoBean.setAuthor_display(value);
328                     stringBuilder = null;
329                     author_display = false;
330                 } else if (dateOfPublication) {
331                     docInfoBean.setDateOfPublication(value);
332                     stringBuilder = null;
333                     dateOfPublication = false;
334                 } else if (date_d) {
335                     docInfoBean.setDate_d(value);
336                     stringBuilder = null;
337                     date_d = false;
338                 } else if (description_display) {
339                     docInfoBean.setDescription_display(value);
340                     stringBuilder = null;
341                     //System.out.println("description===    "+new String(ch, start, length));
342                     description_display = false;
343                 } else if (dimentions) {
344                     docInfoBean.setDimentions(value);
345                     stringBuilder = null;
346                     dimentions = false;
347                 } else if (docType) {
348                     docInfoBean.setDocType(value);
349                     stringBuilder = null;
350                     docType = false;
351                 } else if (extent) {
352                     docInfoBean.setExtent(value);
353                     stringBuilder = null;
354                     extent = false;
355                 } else if (generalNote) {
356                     docInfoBean.setGeneralNote(value);
357                     stringBuilder = null;
358                     generalNote = false;
359                 } else if (isbn_display) {
360                     docInfoBean.setIsbn_display(value);
361                     stringBuilder = null;
362                     // System.out.println(new String(ch, start, length));
363                     isbn_display = false;
364                 } else if (mainEntryPersonalNameComposite) {
365                     docInfoBean.setMainEntryPersonalNameComposite(value);
366                     stringBuilder = null;
367                     mainEntryPersonalNameComposite = false;
368                 } else if (mainEntryPersonalNameComposite_suggest) {
369                     docInfoBean.setMainEntryPersonalNameComposite_suggest(value);
370                     stringBuilder = null;
371                     mainEntryPersonalNameComposite_suggest = false;
372                 } else if (modifyingAgency) {
373                     docInfoBean.setModifyingAgency(value);
374                     stringBuilder = null;
375                     modifyingAgency = false;
376                 } else if (modifyingAgency_suggest) {
377                     docInfoBean.setModifyingAgency_suggest(value);
378                     stringBuilder = null;
379                     modifyingAgency_suggest = false;
380                 } else if (publisher_display) {
381                     docInfoBean.setPublisher_display(value);
382                     stringBuilder = null;
383                     publisher_display = false;
384                 } else if (publisher_search) {
385                     docInfoBean.setPublisher_search(value);
386                     stringBuilder = null;
387                     publisher_search = false;
388                 } else if (localIdentifier_search) {
389                     docInfoBean.setLocalIdentifier_search(value);
390                     stringBuilder = null;
391                     localIdentifier_search = false;
392                 } else if (publicationDate_search) {
393                     if ("Date could not be determined".equalsIgnoreCase(value)) {
394                         docInfoBean.setPublicationDate_search("");
395                     } else {
396                         docInfoBean.setPublicationDate_search(value);
397                     }
398                     stringBuilder = null;
399                     publicationDate_search = false;
400                 } else if (publicationPlace_search) {
401                     docInfoBean.setPublicationPlace_search(value);
402                     stringBuilder = null;
403                     publicationPlace_search = false;
404                 } else if (placeOfPublication_suggest) {
405                     docInfoBean.setPlaceOfPublication_suggest(value);
406                     stringBuilder = null;
407                     placeOfPublication_suggest = false;
408                 } else if (price_f) {
409                     docInfoBean.setPrice_f(value);
410                     stringBuilder = null;
411                     price_f = false;
412                 } else if (remainderOfTitle) {
413                     docInfoBean.setRemainderOfTitle(value);
414                     stringBuilder = null;
415                     remainderOfTitle = false;
416                 } else if (statementOfResponsibility) {
417                     docInfoBean.setStatementOfResponsibility(value);
418                     stringBuilder = null;
419                     statementOfResponsibility = false;
420                 } else if (subject_display) {
421                     docInfoBean.setSubject_display(value);
422                     stringBuilder = null;
423                     subject_display = false;
424                 } else if (title_display) {
425                     docInfoBean.setTitle_display(value);
426                     stringBuilder = null;
427                     title_display = false;
428                 } else if (titleStatementComposite) {
429                     docInfoBean.setTitleStatementComposite(value);
430                     stringBuilder = null;
431                     titleStatementComposite = false;
432                 } else if (title_suggest) {
433                     docInfoBean.setTitle_suggest(value);
434                     stringBuilder = null;
435                     title_suggest = false;
436                 } else if (topicalTermorgeographicnameElement) {
437                     docInfoBean.setTopicalTermorgeographicnameElement(value);
438                     stringBuilder = null;
439                     topicalTermorgeographicnameElement = false;
440                 } else if (yearOfPublication) {
441                     docInfoBean.setYearOfPublication(value);
442                     stringBuilder = null;
443                     yearOfPublication = false;
444                 } else if (all_controlFields) {
445                     docInfoBean.setAll_controlFields(value);
446                     stringBuilder = null;
447                     // System.out.println(value);
448                     all_controlFields = false;
449                 } else if (all_subfields) {
450                     docInfoBean.setAll_subfields(value);
451                     stringBuilder = null;
452                     // System.out.println(value);
453                     all_subfields = false;
454                 } else if (all_text) {
455                     docInfoBean.setAll_text(value);
456                     stringBuilder = null;
457                     // System.out.println(value);
458                     all_text = false;
459                 } else if (uniqueId) {
460                     docInfoBean.setUniqueId(value);
461                     stringBuilder = null;
462                     uniqueId = false;
463                 } else if (bibIdentifier) {
464                     docInfoBean.setBibIdentifier(value);
465                     stringBuilder = null;
466                     bibIdentifier = false;
467                 } else if (itemLinks) {
468                     docInfoBean.setTitleId(value);
469                     stringBuilder = null;
470                     itemLinks = false;
471                 } else if (qName.equalsIgnoreCase("doc")) {
472                     docInfoBean.setNoOfRecords(noOfRecords);
473                     docInfoBeanList.add(docInfoBean);
474                 } else if (instanceId) {
475                     docInfoBean.setTitleId(value);
476                     stringBuilder = null;
477                     instanceId = false;
478                 }
479             /* if(id){
480             //docInfoBean.setId(stringBuilder.toString());
481             docInfoBean.setTitleId(stringBuilder.toString());
482             id=false;
483         }*/
484 
485             }
486 
487             public void characters(char ch[], int start, int length)
488                     throws SAXException {
489 
490                 if (stringBuilder != null)
491                     stringBuilder.append(ch, start, length);
492             }
493 
494         };
495 
496         WebClientServiceImpl webclientService = new WebClientServiceImpl();
497         String url = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.DOCSEARCH_URL_KEY);
498         String contentType = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.DOCSTORE_APP_CONTENT_TYPE_KEY);
499         String response = webclientService.sendRequest(url, contentType, query);
500         InputStream is = new ByteArrayInputStream(response.getBytes("UTF-8"));
501         saxParser.parse(is, handler);
502     }
503 
504 }
505 
506