Project

General

Profile

« Previous | Next » 

Revision 2caedd4f

Added by Andreas Müller over 6 years ago

add uuid list as return type for CdmPreference.getValue

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/metadata/CdmPreference.java
9 9
package eu.etaxonomy.cdm.model.metadata;
10 10

  
11 11
import java.io.Serializable;
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
12 15

  
13 16
import javax.persistence.Column;
14 17
import javax.persistence.Embeddable;
15 18
import javax.persistence.EmbeddedId;
16 19
import javax.persistence.Entity;
17 20

  
21
import org.apache.commons.lang3.StringUtils;
22

  
18 23

  
19 24
/**
20 25
 * This class may hold all prefrences data for a CDM database.
......
191 196
	}
192 197

  
193 198

  
199
	/**
200
	 * @return the subject of the preference
201
	 */
194 202
	public String getSubject() {
195 203
		return key.subject;
196 204
	}
197 205

  
206
	/**
207
	 * @return the predicate of the preference
208
	 */
198 209
	public String getPredicate() {
199 210
		return key.predicate;
200 211
	}
201 212

  
213
	/**
214
	 * @return the value of the preference
215
	 */
202 216
	public String getValue() {
203 217
		return value;
204 218
	}
205 219

  
220
	/**
221
	 * Returns the {@link #getValue() value} as {@link UUID} List.
222
	 * Throws an exception if the value can not be parsed as UUID list.
223
	 * @return
224
	 * @throws IllegalArgumentException
225
	 */
226
	public List<UUID> getValueUuidList() throws IllegalArgumentException {
227
	    List<UUID> result = new ArrayList<>();
228
	    if (StringUtils.isBlank(value)){
229
	        return result;
230
	    }
231
	    String[] splits = getValue().split("[,;]");
232
	    for (String split : splits ){
233
	        try {
234
                if (StringUtils.isBlank(split)){
235
                    continue; //neglect trailing separators
236
                }
237
	            UUID uuid = UUID.fromString(split.trim());
238
                result.add(uuid);
239
            } catch (IllegalArgumentException e) {
240
                throw e;
241
            }
242
	    }
243
	    return result;
244
    }
245

  
246

  
206 247
//
207 248
//  we try to avoid setting of values
208 249
//  public void setValue(String value) {
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/metadata/CdmPreferenceTest.java
8 8
*/
9 9
package eu.etaxonomy.cdm.model.metadata;
10 10

  
11
import java.util.List;
12
import java.util.UUID;
13

  
11 14
import org.junit.Assert;
12 15
import org.junit.Before;
13 16
import org.junit.Test;
......
20 23
public class CdmPreferenceTest {
21 24

  
22 25
	private String subject;
23
	private String subject2;
24 26
	private String predicate;
25 27
	private String value;
26 28

  
......
130 132
        } catch (Exception e) {
131 133
            //ok
132 134
        }
135
    }
133 136

  
137
    @Test
138
    public void testGetValueUuidList() {
139
        //null
140
        CdmPreference prefs = new CdmPreference(subject, predicate, null);
141
        List<UUID> list = prefs.getValueUuidList();
142
        Assert.assertTrue(list.isEmpty());
143
        //empty
144
        prefs = new CdmPreference(subject, predicate, " ");
145
        list = prefs.getValueUuidList();
146
        Assert.assertTrue(list.isEmpty());
147
        //singleUuid
148
        prefs = new CdmPreference(subject, predicate, "1ef35078-fb4a-451d-a15a-127235245358");
149
        list = prefs.getValueUuidList();
150
        Assert.assertEquals(1, list.size());
151
        Assert.assertEquals(UUID.fromString("1ef35078-fb4a-451d-a15a-127235245358"), list.get(0));
152
        //mulitple uuids and varying separators
153
        prefs = new CdmPreference(subject, predicate, "1ef35078-fb4a-451d-a15a-127235245358,a1184db8-e476-4410-b085-d8844ada47e8;43b2cd7d-401b-4565-853f-88ae1c43c55a");
154
        list = prefs.getValueUuidList();
155
        Assert.assertEquals(3, list.size());
156
        Assert.assertEquals(UUID.fromString("1ef35078-fb4a-451d-a15a-127235245358"), list.get(0));
157
        Assert.assertEquals(UUID.fromString("43b2cd7d-401b-4565-853f-88ae1c43c55a"), list.get(2));
158
        //trailing and preceding separators and whitespaces
159
        prefs = new CdmPreference(subject, predicate, " ; 1ef35078-fb4a-451d-a15a-127235245358 , a1184db8-e476-4410-b085-d8844ada47e8;43b2cd7d-401b-4565-853f-88ae1c43c55a , ");
160
        list = prefs.getValueUuidList();
161
        Assert.assertEquals(3, list.size());
162
        Assert.assertEquals(UUID.fromString("1ef35078-fb4a-451d-a15a-127235245358"), list.get(0));
163
        Assert.assertEquals(UUID.fromString("43b2cd7d-401b-4565-853f-88ae1c43c55a"), list.get(2));
164
        //non uuids
165
        prefs = new CdmPreference(subject, predicate, "xxx 1ef35078-fb4a-451d-a15a-127235245358");
166
        try {
167
            list = prefs.getValueUuidList();
168
            Assert.fail("Parsing non UUIDs must throw exception");
169
        } catch (IllegalArgumentException e) {
170
            //correct
171
        }
134 172
    }
135 173

  
174

  
136 175
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IPreferenceService.java
14 14
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
15 15
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
16 16
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
17
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
17 18
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
18 19

  
19 20
/**
......
47 48

  
48 49
     /**
49 50
     * Retrieve all matching values for the given preference key.
50
     * @param subject
51
     * @param predicate
51
     * @param subject the {@link PreferenceSubject} represented as string
52
     * @param predicate the predicate to retrieve
52 53
     * @return
53 54
     */
54 55
     public List<CdmPreference> list(String subject, String predicate);
......
56 57

  
57 58
     /**
58 59
      * Retrieve the best matching value for the given preference key.
59
      * @param key
60
      * @param key the key defining the data to retrieve
60 61
      * @return
61 62
      */
62 63
     public CdmPreference find(PrefKey key);

Also available in: Unified diff