Fixing and changing the MobotUrlServiceWrapper to make it easier to use
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / openurl / OpenUrlReference.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.ext.openurl;
11
12 import java.net.URI;
13 import java.net.URISyntaxException;
14
15 import org.apache.log4j.Logger;
16
17 import eu.etaxonomy.cdm.ext.openurl.MobotOpenUrlServiceWrapper.ReferenceType;
18 import eu.etaxonomy.cdm.model.reference.Reference;
19 import eu.etaxonomy.cdm.strategy.cache.reference.IReferenceBaseCacheStrategy;
20
21 /**
22 * @author a.kohlbecker
23 * @date 16.12.2010
24 *
25 */
26 public class OpenUrlReference<S extends IReferenceBaseCacheStrategy> extends Reference<S> {
27
28
29 private static final String PAGETHUMB_BASE_URI = "http://www.biodiversitylibrary.org/pagethumb/";
30
31 public static final Logger logger = Logger.getLogger(OpenUrlReference.class);
32
33 private static final long serialVersionUID = 1L;
34
35 private URI itemUri;
36
37 private URI titleUri;
38
39 private ReferenceType referenceType = null;
40
41 /**
42 * Links to the specific book or journal, that is to the front page
43 *
44 * @param itemUri the itemUri to set
45 */
46 public void setItemUri(URI itemUri) {
47 this.itemUri = itemUri;
48 }
49
50 /**
51 * Links to the according entry in the bibliography.
52 *
53 * @return the itemUri
54 */
55 public URI getItemUri() {
56 return itemUri;
57 }
58
59 /**
60 * @param titleUri the titleUri to set
61 */
62 public void setTitleUri(URI titleUri) {
63 this.titleUri = titleUri;
64 }
65
66
67 /**
68 * @return the titleUri
69 */
70 public URI getTitleUri() {
71 return titleUri;
72 }
73
74
75 /**
76 * Splits the id from the base ulr of the id urls used in bhl. For example the url string http://www.biodiversitylibrary.org/item/16772 will be split into
77 * <ol>
78 * <li>http://www.biodiversitylibrary.org/item</li>
79 * <li>16772</li>
80 * </ol>
81 * @param uri
82 * @return
83 */
84 private String[] splitPathAndId(URI uri) {
85 String[] tokens = new String[2];
86 if(uri != null){
87 String titleUriString = uri.toString();
88 tokens[1] = titleUriString.substring(titleUriString.lastIndexOf('/') + 1);
89 tokens[0] = titleUriString.substring(0, titleUriString.lastIndexOf('/'));
90 return tokens;
91 } else {
92 return null;
93 }
94 }
95
96 /**
97 * @param referenceType the referenceType to set
98 */
99 public void setReferenceType(ReferenceType referenceType) {
100 this.referenceType = referenceType;
101 }
102
103 /**
104 * @return the referenceType
105 */
106 public ReferenceType getReferenceType() {
107 return referenceType;
108 }
109
110 /**
111 * This method will construct an URI pointing to a service which creates an
112 * jpeg image. This may take a while. For more information please refer to
113 * http://biodivlib.wikispaces.com/Developer+Tools+and+API. If the width or
114 * height of the of the image given as parameter are null or 0 the BHL
115 * service will respond with the default thumbnail which seems to be cached.
116 * This is usually much faster than requesting for a custom imge
117 * size.
118 * <p>
119 *
120 * @param width
121 * width of the image, may be null or 0
122 * @param height
123 * height of the image, may be null or 0
124 * @return
125 */
126 public URI getJpegImage(Integer width, Integer height){
127
128 URI imageURI = null;
129 try {
130 String sizeStr = "";
131 if(width != null && height != null && width > 0 && height > 0){
132 sizeStr = "," + width + "," + height;
133 }
134 String[] tokens = splitPathAndId(getUri());
135 if(tokens.length == 2){
136 imageURI = new URI(PAGETHUMB_BASE_URI + tokens[1] + sizeStr);
137 }
138 } catch (URISyntaxException e) {
139 // should never happen, but let's report it anyway
140 logger.error(e);
141 }
142 return imageURI;
143 }
144 }