View Javadoc

1   package uk.ac.ebi.intenz.domain.reference;
2   
3   import uk.ac.ebi.xchars.SpecialCharacters;
4   import uk.ac.ebi.xchars.domain.EncodingType;
5   import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
6   import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
7   
8   import java.util.regex.Pattern;
9   import java.util.regex.Matcher;
10  
11  
12  /**
13   * This class stores additional information about a book.
14   * <p/>
15   * Instances of this class are immutable.
16   *
17   * @author Michael Darsow
18   * @version $Revision: 1.3 $ $Date: 2009/04/20 13:44:24 $
19   */
20  public class Book extends Reference {
21  
22    private String firstPage;
23  
24    private String lastPage;
25  
26    private String pubName;
27  
28    private String edition;
29  
30    private String editor;
31  
32    private String volume;
33  
34    private String publisher;
35  
36    private String publisherPlace;
37  
38    /**
39     * Returns a <code>Book</code> instance.
40     *
41     * @param id             The pubId.
42     * @param authors        String containing all author names.
43     * @param title          The title of the publication.
44     * @param year           The year of the publication.
45     * @param firstPage      The first page of the publication in the book.
46     * @param lastPage       The last page of the publication in the book.
47     * @param pubName        The name of the book.
48     * @param edition        The edition.
49     * @param editor         The editor(s).
50     * @param volume         The book's volume.
51     * @param publisher      The company that published this book.
52     * @param publisherPlace The place of the company.
53     * @throws NullPointerException if any paramter is <code>null</code>.
54     */
55    public Book(Long id, String authors, String title, String year, String firstPage, String lastPage, String pubName,
56                String edition, String editor, String volume, String publisher, String publisherPlace, EnzymeViewConstant view,
57                   EnzymeSourceConstant source) {
58      super(id, authors, title, year, view, source);
59      if (firstPage == null || lastPage == null || pubName == null || edition == null ||
60              editor == null || volume == null || publisher == null || publisherPlace == null)
61        throw new NullPointerException("No parameter must be 'null'");
62      this.firstPage = firstPage;
63      this.lastPage = lastPage;
64      this.pubName = pubName;
65      this.edition = edition;
66      this.editor = editor;
67      this.volume = volume;
68      this.publisher = publisher;
69      this.publisherPlace = publisherPlace;
70    }
71  
72    /**
73     * Returns a string representation of this book.
74     *
75     * @return the string representation.
76     */
77    public String toString() {
78      StringBuffer referenceString = new StringBuffer();
79  
80      referenceString.append(authors);
81      referenceString.append(" ");
82      referenceString.append(title);
83      referenceString.append(" ");
84      if (!editor.equals("")) {
85        referenceString.append("In: ");
86        referenceString.append(getEditor(true));
87        referenceString.append(", ");
88      }
89      if (!pubName.equals("")) {
90        referenceString.append("<i>");
91        referenceString.append(pubName);
92        referenceString.append("</i>, ");
93      }
94      if (!edition.equals("")) {
95        referenceString.append(getEdition(true));
96        referenceString.append(", ");
97      }
98      if (!volume.equals("")) {
99        referenceString.append("vol. ");
100       referenceString.append(volume);
101       referenceString.append(", ");
102     }
103     if (!publisher.equals("")) {
104       referenceString.append(publisher);
105       referenceString.append(", ");
106     }
107     if (!publisherPlace.equals("")) {
108       referenceString.append(publisherPlace);
109       referenceString.append(", ");
110     }
111     if (!year.equals("")) {
112       if (firstPage.equals("") && lastPage.equals(""))
113         referenceString.append(year);
114       else {
115         referenceString.append(year);
116         referenceString.append(", ");
117       }
118     }
119     if (!firstPage.equals("")) {
120       if (!lastPage.equals("")) {
121         referenceString.append("pp. ");
122       } else {
123         referenceString.append("p. ");
124       }
125       referenceString.append(firstPage);
126       if (!lastPage.equals("")) {
127         referenceString.append("-");
128         referenceString.append(lastPage);
129       }
130       referenceString.append(".");
131     }
132 
133     return referenceString.toString();
134   }
135   /**
136    * Standard <code>equals()</code> method.
137    *
138    * @param o The object to be compared to this one.
139    * @return <code>true</code> if <code>o</code> is equal to this object.
140    */
141   public boolean equals(Object o) {
142     if (this == o) return true;
143     if (!(o instanceof Book)) return false;
144     if (!super.equals(o)) return false;
145 
146     final Book book = (Book) o;
147 
148     if (edition != null ? !edition.equals(book.edition) : book.edition != null) return false;
149     if (editor != null ? !editor.equals(book.editor) : book.editor != null) return false;
150     if (firstPage != null ? !firstPage.equals(book.firstPage) : book.firstPage != null) return false;
151     if (pubName != null ? !pubName.equals(book.pubName) : book.pubName != null) return false;
152     if (lastPage != null ? !lastPage.equals(book.lastPage) : book.lastPage != null) return false;
153     if (publisher != null ? !publisher.equals(book.publisher) : book.publisher != null) return false;
154     if (publisherPlace != null ? !publisherPlace.equals(book.publisherPlace) : book.publisherPlace != null) return false;
155     if (volume != null ? !volume.equals(book.volume) : book.volume != null) return false;
156 
157     return true;
158   }
159 
160   /**
161    * Generates a hash code for this object.
162    *
163    * @return the hash code.
164    */
165   public int hashCode() {
166     int result = super.hashCode();
167     result = 29 * result + (firstPage != null ? firstPage.hashCode() : 0);
168     result = 29 * result + (lastPage != null ? lastPage.hashCode() : 0);
169     result = 29 * result + (pubName != null ? pubName.hashCode() : 0);
170     result = 29 * result + (edition != null ? edition.hashCode() : 0);
171     result = 29 * result + (editor != null ? editor.hashCode() : 0);
172     result = 29 * result + (volume != null ? volume.hashCode() : 0);
173     result = 29 * result + (publisher != null ? publisher.hashCode() : 0);
174     result = 29 * result + (publisherPlace != null ? publisherPlace.hashCode() : 0);
175     return result;
176   }
177 
178 
179   // --------------------  GETTER -----------------------
180 
181   public String getFirstPage() {
182     return firstPage;
183   }
184 
185   public String getLastPage() {
186     return lastPage;
187   }
188 
189   /**
190    * Returns the edition string as it is stored in the database or formatted.
191    *
192    * @param formatted <code>true</code>, if it should be formatted.
193    * @return the (formatted) edition string.
194    */
195   public String getEdition(boolean formatted) {
196     if (formatted) return formatEditionString();
197     return edition;
198   }
199 
200   /**
201    *
202    * @return the formatted edition string.
203    */
204   public String getEdition(){
205      return formatEditionString();
206   }
207 
208   /**
209    * Returns the editor string as it is stored in the database or formatted.
210    *
211    * @param formatted <code>true</code>, if it should be formatted.
212    * @return the (formatted) editor string.
213    */
214   public String getEditor(boolean formatted) {
215     if (formatted) return formatEditorString();
216     return editor;
217   }
218 
219   /**
220    *
221    * @return The formatted editor string.
222    */
223   public String getEditor(){
224       return formatEditorString();
225   }
226 
227   public String getPublisher() {
228     return publisher;
229   }
230 
231   public String getPublisherPlace() {
232     return publisherPlace;
233   }
234 
235   public String getPubName() {
236     return pubName;
237   }
238 
239   public String getVolume() {
240     return volume;
241   }
242 
243 
244   // ------------------- PRIVATE METHODS ------------------------
245 
246   /**
247    * Formats the editor string according the usual layout (using "(Ed.)" or "(Eds.)").
248    *
249    * @return the formatted editor string.
250    */
251   private String formatEditorString() {
252     if (!editor.equals("")) {
253       int index = 0;
254       int commaCount = 0;
255       while (editor.indexOf(",", index) > -1) {
256         commaCount++;
257         index = editor.indexOf(",", index) + 1;
258       }
259 
260       if (commaCount > 1) {
261         return editor + " (Eds.)";
262 
263       } else {
264         return editor + " (Ed.)";
265       }
266     }
267 
268     return editor;
269   }
270 
271   /**
272    * Formats the edition string according the usual layout (using <code>"1st ed."</code>,
273    * <code>"2nd ed."</code> etc.).
274    *
275    * @return the formatted edition string.
276    */
277   private String formatEditionString() {
278     String formattedEdition = "";
279     if (!edition.equals("")) {
280       String lastNumber = edition.substring(edition.length() - 1);
281 
282       try {
283         if (edition.length() > 1) {
284           switch (Integer.parseInt(lastNumber)) {
285             case 1:
286               formattedEdition = edition.substring(0, edition.length() - 2) + "1st ed.";
287               break;
288             case 2:
289               formattedEdition = edition.substring(0, edition.length() - 2) + "2nd ed.";
290               break;
291             case 3:
292               formattedEdition = edition.substring(0, edition.length() - 2) + "3rd ed.";
293               break;
294             default:
295               formattedEdition = edition.substring(0, edition.length()) + "th ed.";
296               break;
297           }
298         } else {
299           switch (Integer.parseInt(lastNumber)) {
300             case 1:
301               formattedEdition = "1st ed.";
302               break;
303             case 2:
304               formattedEdition = "2nd ed.";
305               break;
306             case 3:
307               formattedEdition = "3rd ed.";
308               break;
309             default:
310               formattedEdition += "th ed.";
311               break;
312           }
313         }
314       } catch (NumberFormatException e) {
315         return edition;
316       }
317     }
318 
319     return formattedEdition;
320   }
321 
322   // TODO: put method's content somewhere else.
323   public String toXML(int number, boolean intenzTextXML) {
324     StringBuffer xmlStringBuffer = new StringBuffer();
325 
326     xmlStringBuffer.append(super.toXML(number, intenzTextXML));
327     if (intenzTextXML) {
328       SpecialCharacters encoding = SpecialCharacters.getInstance(null);
329 //      SpecialCharacters encoding = SpecialCharacters.getInstance("/projects/intenz/prg/webapps/curator/", null);
330 
331       xmlStringBuffer.append("<pubName>");
332       xmlStringBuffer.append(pubName);
333       xmlStringBuffer.append("</pubName>");
334       xmlStringBuffer.append("<patent_no>");
335       xmlStringBuffer.append("</patent_no>");
336       xmlStringBuffer.append("<first_page>");
337       xmlStringBuffer.append(firstPage);
338       xmlStringBuffer.append("</first_page>");
339       xmlStringBuffer.append("<last_page>");
340       xmlStringBuffer.append(lastPage);
341       xmlStringBuffer.append("</last_page>");
342       xmlStringBuffer.append("<edition>");
343       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(edition)));
344       xmlStringBuffer.append("</edition>");
345 //      if (encoding.isSpecialCharactersElement(edition)) { // Store text only version as well.
346       xmlStringBuffer.append("<edition>");
347       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(edition, EncodingType.SWISSPROT_CODE)));
348       xmlStringBuffer.append("</edition>");
349 //      }
350       xmlStringBuffer.append("<editor>");
351       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(editor)));
352       xmlStringBuffer.append("</editor>");
353 //      if (encoding.isSpecialCharactersElement(editor)) { // Store text only version as well.
354       xmlStringBuffer.append("<editor>");
355       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(editor, EncodingType.SWISSPROT_CODE)));
356       xmlStringBuffer.append("</editor>");
357 //      }
358       xmlStringBuffer.append("<volume>");
359       xmlStringBuffer.append(volume);
360       xmlStringBuffer.append("</volume>");
361       xmlStringBuffer.append("<pub_place>");
362       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(publisherPlace)));
363       xmlStringBuffer.append("</pub_place>");
364 //      if (encoding.isSpecialCharactersElement(publisherPlace)) { // Store text only version as well.
365       xmlStringBuffer.append("<pub_place>");
366       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(publisherPlace, EncodingType.SWISSPROT_CODE)));
367       xmlStringBuffer.append("</pub_place>");
368 //      }
369       xmlStringBuffer.append("<pub_company>");
370       xmlStringBuffer.append(removeFormatting(encoding.xml2Display(publisher)));
371       xmlStringBuffer.append("</pub_company>");
372 //      if (encoding.isSpecialCharactersElement(publisher)) { // Store text only version as well.
373         xmlStringBuffer.append("<pub_company>");
374         xmlStringBuffer.append(removeFormatting(encoding.xml2Display(publisher, EncodingType.SWISSPROT_CODE)));
375         xmlStringBuffer.append("</pub_company>");
376 //      }
377       xmlStringBuffer.append("<pub_med>");
378       xmlStringBuffer.append("</pub_med>");
379       xmlStringBuffer.append("<medline>");
380       xmlStringBuffer.append("</medline>");
381     } else {
382       xmlStringBuffer.append("      <pubName>");
383       xmlStringBuffer.append(pubName);
384       xmlStringBuffer.append("</pubName>\n");
385       xmlStringBuffer.append("      <patent_no>");
386       xmlStringBuffer.append("</patent_no>\n");
387       xmlStringBuffer.append("      <first_page>");
388       xmlStringBuffer.append(firstPage);
389       xmlStringBuffer.append("</first_page>\n");
390       xmlStringBuffer.append("      <last_page>");
391       xmlStringBuffer.append(lastPage);
392       xmlStringBuffer.append("</last_page>\n");
393       xmlStringBuffer.append("      <edition>");
394       xmlStringBuffer.append(encodeTags(edition));
395       xmlStringBuffer.append("</edition>\n");
396       xmlStringBuffer.append("      <editor>");
397       xmlStringBuffer.append(encodeTags(editor));
398       xmlStringBuffer.append("</editor>\n");
399       xmlStringBuffer.append("      <volume>");
400       xmlStringBuffer.append(volume);
401       xmlStringBuffer.append("</volume>\n");
402       xmlStringBuffer.append("      <pub_place>");
403       xmlStringBuffer.append(encodeTags(publisherPlace));
404       xmlStringBuffer.append("</pub_place>\n");
405       xmlStringBuffer.append("      <pub_company>");
406       xmlStringBuffer.append(encodeTags(publisher));
407       xmlStringBuffer.append("</pub_company>\n");
408       xmlStringBuffer.append("      <pub_med>");
409       xmlStringBuffer.append("</pub_med>\n");
410       xmlStringBuffer.append("      <medline>");
411       xmlStringBuffer.append("</medline>\n");
412     }
413     return xmlStringBuffer.toString();
414   }
415 
416   private String encodeTags(String name) {
417     Pattern p = Pattern.compile("<");
418     Matcher m = p.matcher(name);
419 
420     Pattern p2 = Pattern.compile(">");
421     Matcher m2 = p2.matcher(m.replaceAll("&lt;"));
422 
423     return m2.replaceAll("&gt;");
424   }
425 
426   private String removeFormatting(String text) {
427     text = text.replaceAll("\\<small\\>", "");
428     text = text.replaceAll("\\<\\/small\\>", "");
429     text = text.replaceAll("\\<b\\>", "");
430     text = text.replaceAll("\\<\\/b\\>", "");
431     text = text.replaceAll("\\<i\\>", "");
432     text = text.replaceAll("\\<\\/i\\>", "");
433     return text;
434   }
435 }