minor
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / strategy / cache / reference / BookSectionDefaultCacheStrategy.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.strategy.cache.reference;
10
11 import java.util.UUID;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.CdmUtils;
17 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
18 import eu.etaxonomy.cdm.model.common.TimePeriod;
19 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
20 import eu.etaxonomy.cdm.model.reference.Reference;
21
22 public class BookSectionDefaultCacheStrategy <T extends Reference> extends NomRefDefaultCacheStrategyBase<T> implements INomenclaturalReferenceCacheStrategy<T> {
23 private static final Logger logger = Logger.getLogger(BookSectionDefaultCacheStrategy.class);
24
25 public static final String UNDEFINED_BOOK = "- undefined book -";
26 private String afterSectionAuthor = " - ";
27 private String afterNomRefBookAuthor = ", ";
28 private String inBook = "in ";
29 private String blank = " ";
30
31 final static UUID uuid = UUID.fromString("f9c53f20-addd-4d2f-9697-ef1fe727deba");
32
33 /* (non-Javadoc)
34 * @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
35 */
36 @Override
37 protected UUID getUuid() {
38 return uuid;
39 }
40
41
42 /**
43 * Factory method
44 * @return
45 */
46 public static BookSectionDefaultCacheStrategy NewInstance(){
47 return new BookSectionDefaultCacheStrategy();
48 }
49
50 /**
51 * Constructor
52 */
53 private BookSectionDefaultCacheStrategy(){
54 super();
55 }
56
57
58
59 /* (non-Javadoc)
60 * @see eu.etaxonomy.cdm.strategy.cache.reference.INomenclaturalReferenceCacheStrategy#getTokenizedNomenclaturalTitel(eu.etaxonomy.cdm.model.reference.INomenclaturalReference)
61 */
62 @Override
63 public String getTokenizedNomenclaturalTitel(T bookSection) {
64 if (bookSection == null /* || bookSection.getInReference() == null */){
65 return null;
66 }
67 Reference inBook = bookSection.getInReference();
68 String result;
69 //use booksection's publication date if it exists
70 if ( (bookSection.getDatePublished() != null && bookSection.getDatePublished().getStart() != null) || inBook == null){
71 BookDefaultCacheStrategy<Reference> bookStrategy = BookDefaultCacheStrategy.NewInstance();
72 result = inBook == null ? "" : bookStrategy.getNomRefTitleWithoutYearAndAuthor(inBook);
73 result += INomenclaturalReference.MICRO_REFERENCE_TOKEN;
74 result = addYear(result, bookSection);
75 }else{
76 //else use book's publication date
77 result = inBook.getNomenclaturalCitation(INomenclaturalReference.MICRO_REFERENCE_TOKEN);
78 result = result.replace(beforeMicroReference + INomenclaturalReference.MICRO_REFERENCE_TOKEN, INomenclaturalReference.MICRO_REFERENCE_TOKEN);
79 }
80 result = getBookAuthorPart(bookSection.getInReference(), afterNomRefBookAuthor) + result;
81 result = "in " + result;
82 return result;
83 }
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.cdm.strategy.cache.reference.INomenclaturalReferenceCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.reference.INomenclaturalReference)
87 */
88 @Override
89 public String getTitleCache(T bookSection) {
90 boolean hasBook = (bookSection.getInBook() != null);
91 String result;
92 // get book part
93 if (hasBook){
94 result = bookSection.getInReference().getTitleCache();
95 }else{
96 result = "- undefined book -";
97 }
98
99 //in
100 result = inBook + result;
101
102 //section title
103 String title = CdmUtils.Nz(bookSection.getTitle());
104 if (title.length() > 0){
105 result = title + blank + result;
106 }
107
108 //section author
109 TeamOrPersonBase<?> sectionTeam = bookSection.getAuthorTeam();
110 String sectionAuthor = CdmUtils.Nz(sectionTeam == null ? "" : sectionTeam.getTitleCache());
111 result = sectionAuthor + afterSectionAuthor + result;
112
113 //date
114 if (bookSection.getDatePublished() != null && ! bookSection.getDatePublished().isEmpty()){
115 String bookSectionDate = bookSection.getDatePublished().toString();
116 if (hasBook && bookSection.getInBook().getDatePublished() != null){
117 TimePeriod bookDate = bookSection.getInBook().getDatePublished();
118 String bookDateString = bookDate.getYear();
119 if (CdmUtils.isNotEmpty(bookDateString)){
120 int pos = StringUtils.lastIndexOf(result, bookDateString);
121 if (pos > -1 ){
122 result = result.substring(0, pos) + bookSectionDate + result.substring(pos + bookDateString.length());
123 }else{
124 logger.warn("BookDateString (" + bookDateString + ") could not be found in result (" + result +")");
125 }
126 }else{
127 result = result + beforeYear + bookSectionDate + afterYear;
128 }
129 }else{
130 result = result + beforeYear + bookSectionDate + afterYear;
131 }
132 }
133 return result;
134 }
135
136 private String getBookAuthorPart(Reference book, String seperator){
137 if (book == null){
138 return "";
139 }
140 TeamOrPersonBase<?> team = book.getAuthorTeam();
141 String result = CdmUtils.Nz( team == null ? "" : team.getTitleCache());
142 if (! result.trim().equals("")){
143 result = result + seperator;
144 }
145 return result;
146 }
147
148
149 /* (non-Javadoc)
150 * @see eu.etaxonomy.cdm.strategy.cache.reference.NomRefDefaultCacheStrategyBase#getNomRefTitleWithoutYearAndAuthor(eu.etaxonomy.cdm.model.reference.Reference)
151 */
152 @Override
153 protected String getNomRefTitleWithoutYearAndAuthor(T reference) {
154 // not needed in BookSection
155 logger.warn("Questionable procedure call. Procedure not implemented because not needed. ");
156 return null;
157 }
158
159
160 }