View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  package org.opencastproject.oaipmh.persistence;
22  
23  import org.apache.commons.lang3.StringUtils;
24  
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.LinkedList;
29  import java.util.List;
30  import java.util.Map;
31  
32  public class OaiPmhSetDefinitionImpl implements OaiPmhSetDefinition {
33  
34    private String setSpec;
35    private String name;
36    private String description;
37    private Map<String, OaiPmhSetDefinitionFilter> filters = new HashMap<>();
38  
39    @Override
40    public String getSetSpec() {
41      return setSpec;
42    }
43  
44    @Override
45    public String getName() {
46      return name;
47    }
48  
49    @Override
50    public String getDescription() {
51      return description;
52    }
53  
54    @Override
55    public Collection<OaiPmhSetDefinitionFilter> getFilters() {
56      return Collections.unmodifiableCollection(filters.values());
57    }
58  
59    public OaiPmhSetDefinitionFilter addFilter(String filterId, String flavor, String criterion, String criterionValue) {
60      if (StringUtils.isEmpty(filterId)) {
61        throw new IllegalArgumentException(String.format("Set definition '%s' filter identifier not set.", setSpec));
62      }
63      if (StringUtils.isEmpty(flavor)) {
64        throw new IllegalArgumentException(String.format(
65            "Set definition '%s' flavor not set for filter identified by '%s'.", setSpec, filterId));
66      }
67      if (!StringUtils.equals(OaiPmhSetDefinitionFilter.CRITERION_CONTAINS, criterion)
68          && !StringUtils.equals(OaiPmhSetDefinitionFilter.CRITERION_CONTAINSNOT, criterion)
69          && !StringUtils.equals(OaiPmhSetDefinitionFilter.CRITERION_MATCH, criterion)) {
70        throw new IllegalArgumentException(String.format(
71            "Set definition '%s' filter (idenfied by '%s') criterion '%s' should be one of %s",
72            setSpec, filterId, criterion, StringUtils.joinWith(", ",
73                OaiPmhSetDefinitionFilter.CRITERION_CONTAINS,
74                OaiPmhSetDefinitionFilter.CRITERION_CONTAINSNOT,
75                OaiPmhSetDefinitionFilter.CRITERION_MATCH)));
76      }
77      if (StringUtils.isEmpty(criterionValue)) {
78        throw new IllegalArgumentException(String.format(
79            "Set definition '%s' filter (idenfied by '%s') criterion '%s' value not set.",
80            setSpec, filterId, criterion));
81      }
82      OaiPmhSetDefinitionFilter filter = filters.get(filterId);
83      Map<String, List<String>> criteria = new HashMap<>();
84      if (filter != null) {
85        filters.remove(filter);
86        criteria.putAll(filter.getCriteria());
87      }
88      if (criteria.containsKey(criterion)) {
89        List<String> criteriaValues = new LinkedList<>(criteria.get(criterion));
90        criteriaValues.add(criterionValue);
91        criteria.replace(criterion, Collections.unmodifiableList(criteriaValues));
92      } else {
93        List<String> criteriaValues = new LinkedList<>();
94        criteriaValues.add(criterionValue);
95        criteria.put(criterion, Collections.unmodifiableList(criteriaValues));
96      }
97      filter = new OaiPmhSetDefinitionFilter() {
98        @Override
99        public String getFlavor() {
100         return flavor;
101       }
102 
103       @Override
104       public Map<String, List<String>> getCriteria() {
105         return Collections.unmodifiableMap(criteria);
106       }
107     };
108     filters.put(filterId, filter);
109     return filter;
110   }
111 
112   public static OaiPmhSetDefinitionImpl build(String setSpec, String name, String description) {
113     if (StringUtils.isEmpty(setSpec)) {
114       throw new IllegalArgumentException("setSpec not set.");
115     }
116     if (StringUtils.isEmpty(name)) {
117       throw new IllegalArgumentException("name not set.");
118     }
119     OaiPmhSetDefinitionImpl instance = new OaiPmhSetDefinitionImpl();
120     instance.setSpec = setSpec;
121     instance.name = name;
122     instance.description = description;
123     return instance;
124   }
125 }