(no commit message)
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / taxon / TaxonBase.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
10 package eu.etaxonomy.cdm.model.taxon;
11
12 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
13 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
14 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
15 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
16
17 import org.apache.log4j.Logger;
18 import org.hibernate.annotations.Cascade;
19 import org.hibernate.annotations.CascadeType;
20 import org.hibernate.annotations.Index;
21 import org.hibernate.annotations.Table;
22
23 import java.lang.reflect.Method;
24
25 import javax.persistence.*;
26
27 /**
28 * Upmost abstract class for the use of a taxon name by a reference either
29 * as a taxon ("accepted/correct" name) or as a (junior) synonym.
30 * For instance: "Juncus longirostris Kuvaev sec. Kirschner, J. et al. 2002".
31 * Within a taxonomic view/treatment a taxon name can be used only once.
32 *
33 * @author m.doering
34 * @version 1.0
35 * @created 08-Nov-2007 13:06:56
36 */
37 @Entity
38 @Table(appliesTo="TaxonBase", indexes = { @Index(name = "taxonBaseTitleCacheIndex", columnNames = { "titleCache" }) })
39 public abstract class TaxonBase extends IdentifiableEntity {
40 static Logger logger = Logger.getLogger(TaxonBase.class);
41
42 private static Method methodTaxonNameAddTaxonBase;
43
44 private static void initMethods() {
45 if (methodTaxonNameAddTaxonBase == null){
46 try {
47 methodTaxonNameAddTaxonBase = TaxonNameBase.class.getDeclaredMethod("addTaxonBase", TaxonBase.class);
48 methodTaxonNameAddTaxonBase.setAccessible(true);
49 } catch (Exception e) {
50 e.printStackTrace();
51 //TODO handle exception
52 }
53 }
54 }
55
56 protected TaxonBase(){
57 super();
58 }
59
60 protected TaxonBase(TaxonNameBase taxonNameBase, ReferenceBase sec){
61 super();
62 if (taxonNameBase != null){
63 initMethods();
64 this.invokeSetMethod(methodTaxonNameAddTaxonBase, taxonNameBase);
65 }
66 this.setSec(sec);
67 }
68
69 //The assignment to the Taxon or to the Synonym class is not definitive
70 private boolean isDoubtful;
71 private TaxonNameBase name;
72 // The concept reference
73 private ReferenceBase sec;
74
75 @Override
76 public String generateTitle() {
77 String title;
78 if (name != null && name.getTitleCache() != null){
79 title = name.getTitleCache() + " sec. ";
80 if (sec != null){
81 title += sec.getTitleCache();
82 }else{
83 title += "???";
84 }
85 }else{
86 title = this.toString();
87 }
88 return title;
89 }
90
91 @ManyToOne
92 @JoinColumn(name="taxonName_fk")
93 @Cascade(CascadeType.SAVE_UPDATE)
94 public TaxonNameBase getName(){
95 return this.name;
96 }
97 @Deprecated //for hibernate use only, use taxon.addDescription() instead
98 private void setName(TaxonNameBase newName){
99 this.name = newName;
100 }
101
102 @Transient
103 public HomotypicalGroup getHomotypicGroup(){
104 if (this.getName() == null){
105 return null;
106 }else{
107 return this.getName().getHomotypicalGroup();
108 }
109 }
110
111 public boolean isDoubtful(){
112 return this.isDoubtful;
113 }
114 public void setDoubtful(boolean isDoubtful){
115 this.isDoubtful = isDoubtful;
116 }
117
118 @ManyToOne
119 @Cascade(CascadeType.SAVE_UPDATE)
120 public ReferenceBase getSec() {
121 return sec;
122 }
123
124 public void setSec(ReferenceBase sec) {
125 this.sec = sec;
126 }
127
128 @Transient
129 public boolean isSaveable(){
130 if ( (this.getName() == null) || (this.getSec() == null) ){
131 return false;
132 }else{
133 this.toString();
134 return true;
135 }
136 }
137
138
139 }