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.service.impl;
017
018import org.apache.log4j.Logger;
019import org.kuali.ole.select.businessobject.BibInfoBean;
020
021import java.util.HashMap;
022import java.util.StringTokenizer;
023
024public class BuildOpenUrlBibInfoBean {
025    private Logger LOG = org.apache.log4j.Logger.getLogger(BuildOpenUrlBibInfoBean.class);
026    private BibInfoBean bibInfoBean;
027    private HashMap<String, String> openUrlMap;
028    private String openUrlString;
029
030    public BibInfoBean getBean(BibInfoBean bibInfoBean, String openUrl) throws Exception {
031        this.bibInfoBean = bibInfoBean;
032        this.openUrlString = openUrl;
033        if (openUrlString != null) {
034            replaceEncodedValues();
035            parseOpenUrl();
036            populateBibInfoBean();
037        }
038        return bibInfoBean;
039    }
040
041    private void replaceEncodedValues() {
042        openUrlString = openUrlString.replaceAll("\\+", " ");
043        openUrlString = openUrlString.replaceAll("%20", " ");
044        openUrlString = openUrlString.replaceAll("%23", "#");
045        openUrlString = openUrlString.replaceAll("%25", "%");
046        openUrlString = openUrlString.replaceAll("%26", "&");
047        openUrlString = openUrlString.replaceAll("%2B", "+");
048        openUrlString = openUrlString.replaceAll("%2F", "/");
049        openUrlString = openUrlString.replaceAll("%3C", "<");
050        openUrlString = openUrlString.replaceAll("%3D", "=");
051        openUrlString = openUrlString.replaceAll("%3E", ">");
052        openUrlString = openUrlString.replaceAll("%3F", "?");
053        openUrlString = openUrlString.replaceAll("%3A", ":");
054    }
055
056    private void parseOpenUrl() {
057        openUrlMap = new HashMap();
058        String[] url = openUrlString.split("\\?");
059        if (url.length > 1) {
060            StringTokenizer st = new StringTokenizer(url[1], "&");
061            while (st.hasMoreElements()) {
062                String[] values = ((String) st.nextElement()).split("=");
063                if (values.length > 1)
064                    openUrlMap.put(values[0], values[1]);
065            }
066        }
067    }
068
069    private void populateBibInfoBean() {
070        if (openUrlMap.get("rft.btitle") != null)
071            bibInfoBean.setTitle(openUrlMap.get("rft.btitle"));
072
073        if (bibInfoBean.getTitle() == null) {
074            if (openUrlMap.get("rft.title") != null)
075                bibInfoBean.setTitle(openUrlMap.get("rft.title"));
076        }
077
078        if (openUrlMap.get("rft.isbn") != null) {
079            bibInfoBean.setStandardNumber(openUrlMap.get("rft.isbn"));
080            bibInfoBean.setTypeOfStandardNumber("ISBN");
081        }
082
083        if (openUrlMap.get("rft.issn") != null) {
084            bibInfoBean.setStandardNumber(openUrlMap.get("rft.issn"));
085            bibInfoBean.setTypeOfStandardNumber("ISSN");
086        }
087
088        if (openUrlMap.get("rft.epage") != null)
089            bibInfoBean.setEndPage(new Long(openUrlMap.get("rft.epage")));
090
091        if (openUrlMap.get("rft.spage") != null)
092            bibInfoBean.setStartPage(new Long(openUrlMap.get("rft.spage")));
093
094        if (openUrlMap.get("rft.genre") != null)
095            bibInfoBean.setCategory(openUrlMap.get("rft.genre"));
096
097        if (openUrlMap.get("rft.edition") != null)
098            bibInfoBean.setEdition(openUrlMap.get("rft.edition"));
099
100        if (openUrlMap.get("rft.series") != null)
101            bibInfoBean.setSeries(openUrlMap.get("rft.series"));
102
103        if (openUrlMap.get("rft.place") != null)
104            bibInfoBean.setPlaceOfPublication(openUrlMap.get("rft.place"));
105
106        if (openUrlMap.get("rft.date") != null)
107            bibInfoBean.setYearOfPublication(openUrlMap.get("rft.date"));
108
109        if (openUrlMap.get("rft.pub") != null)
110            bibInfoBean.setPublisher(openUrlMap.get("rft.pub"));
111
112        if (openUrlMap.get("rft.au") != null)
113            bibInfoBean.setAuthor(openUrlMap.get("rft.au"));
114    }
115}