1
|
/**
|
2
|
* Copyright (C) 2016 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.model.metadata;
|
10
|
|
11
|
import java.util.Arrays;
|
12
|
import java.util.List;
|
13
|
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
|
16
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
17
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
|
18
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
19
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
20
|
|
21
|
/**
|
22
|
* This is helping class to ease the usage of {@link CdmPreference#getSubjectString()
|
23
|
* subject part} of a {@link CdmPreference} for some typical subjects.
|
24
|
* Currently only very few non hierarchical basic subjects are supported.
|
25
|
*
|
26
|
* For how to use subjects see {@link CdmPreference}. For how to resolve
|
27
|
* them see {@link PreferenceResolver#resolve(List, PrefKey)}
|
28
|
*
|
29
|
* @author a.mueller
|
30
|
* @since 03.06.2016
|
31
|
*/
|
32
|
public class PreferenceSubject {
|
33
|
|
34
|
public static final String ROOT = "/";
|
35
|
public static final String SEP = "/";
|
36
|
public static final String VAADIN = "vaadin";
|
37
|
public static final String TAX_EDITOR = "taxeditor";
|
38
|
public static final String DISTR_EDITOR = "distributionEditor";
|
39
|
|
40
|
|
41
|
private String subject;
|
42
|
|
43
|
public static PreferenceSubject NewDatabaseInstance(){
|
44
|
return new PreferenceSubject(ROOT);
|
45
|
}
|
46
|
|
47
|
public static PreferenceSubject NewInstance(Classification classification){
|
48
|
return NewInstance(classification.getRootNode());
|
49
|
}
|
50
|
|
51
|
public static PreferenceSubject NewInstance(TaxonNode taxonNode){
|
52
|
String result = ROOT + "TaxonNode[" + taxonNode.treeIndex() + "]" + SEP;
|
53
|
return new PreferenceSubject(result);
|
54
|
}
|
55
|
|
56
|
public static PreferenceSubject NewVaadinInstance(){
|
57
|
return new PreferenceSubject(ROOT + VAADIN + SEP);
|
58
|
}
|
59
|
|
60
|
public static PreferenceSubject NewTaxEditorInstance(){
|
61
|
return new PreferenceSubject(ROOT + TAX_EDITOR + SEP);
|
62
|
}
|
63
|
public static PreferenceSubject NewDistributionEditorInstance(){
|
64
|
return new PreferenceSubject(ROOT + DISTR_EDITOR + SEP);
|
65
|
}
|
66
|
public static PreferenceSubject NewInstance(PreferenceSubjectEnum subject){
|
67
|
return new PreferenceSubject(ROOT + subject.getKey() + SEP);
|
68
|
}
|
69
|
public static PreferenceSubject NewInstance(String subjectString){
|
70
|
return new PreferenceSubject(subjectString);
|
71
|
}
|
72
|
|
73
|
|
74
|
// *****************************************************/
|
75
|
|
76
|
private PreferenceSubject (String subject){
|
77
|
this.subject = subject;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Adds a sub subject and returns the new PreferenceSubject
|
82
|
* @param subject
|
83
|
* @return
|
84
|
*/
|
85
|
public PreferenceSubject with(PreferenceSubjectEnum subject){
|
86
|
PreferenceSubject result = new PreferenceSubject(this.subject + subject.getKey() + SEP);
|
87
|
return result;
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public String toString() {
|
92
|
return subject;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* @return
|
97
|
*/
|
98
|
public List<String> getParts() {
|
99
|
String[] splits;
|
100
|
if (ROOT.equals(subject)){
|
101
|
splits = new String[]{""};
|
102
|
}else{
|
103
|
splits = subject.split("/");
|
104
|
}
|
105
|
|
106
|
return Arrays.asList(splits);
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
*
|
111
|
*/
|
112
|
public String getLastPart() {
|
113
|
List<String> parts = getParts();
|
114
|
if (parts.isEmpty() || parts.size() == 1 && StringUtils.isBlank(parts.get(0))){
|
115
|
return "";
|
116
|
}else{
|
117
|
return parts.get(parts.size()-1);
|
118
|
}
|
119
|
}
|
120
|
|
121
|
public PreferenceSubject getNextHigher(){
|
122
|
List<String> parts = getParts();
|
123
|
if (parts.isEmpty() || parts.size() == 1 && StringUtils.isBlank(parts.get(0))){
|
124
|
return null;
|
125
|
}else{
|
126
|
String subject = CdmUtils.concat(SEP, parts.subList(1, parts.size()-1).toArray(new String[0]));
|
127
|
return new PreferenceSubject(CdmUtils.concat("", ROOT,subject));
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* @return
|
133
|
*/
|
134
|
public boolean isRoot() {
|
135
|
return ROOT.equals(subject);
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* @param preference
|
140
|
* @return
|
141
|
*/
|
142
|
public static PreferenceSubject fromPreference(CdmPreference preference) {
|
143
|
PreferenceSubject result = new PreferenceSubject(preference.getKey().getSubject());
|
144
|
return result;
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @param key
|
149
|
* @return
|
150
|
*/
|
151
|
public static PreferenceSubject fromKey(PrefKey key) {
|
152
|
PreferenceSubject result = new PreferenceSubject(key.getSubject());
|
153
|
return result;
|
154
|
}
|
155
|
}
|