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  
22  
23  package org.opencastproject.workflow.api;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.stream.Collectors;
29  
30  import javax.xml.bind.annotation.XmlAccessType;
31  import javax.xml.bind.annotation.XmlAccessorType;
32  import javax.xml.bind.annotation.XmlElement;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.adapters.XmlAdapter;
35  
36  /**
37   * The search result represents a set of result items that has been compiled as a result for a search operation.
38   */
39  @XmlAccessorType(XmlAccessType.FIELD)
40  @XmlRootElement(name = "workflows", namespace = "http://workflow.opencastproject.org")
41  public class WorkflowSetImpl implements WorkflowSet {
42  
43    /** A list of search items. */
44    @XmlElement(name = "workflow")
45    private List<JaxbWorkflowInstance> resultSet = null;
46  
47    /**
48     * A no-arg constructor needed by JAXB
49     */
50    public WorkflowSetImpl() {
51    }
52  
53    public WorkflowSetImpl(List<WorkflowInstance> workflows) {
54      this.resultSet = workflows.stream()
55          .map(JaxbWorkflowInstance::new)
56          .collect(Collectors.toList());
57    }
58  
59  
60    /**
61     * {@inheritDoc}
62     *
63     * @see org.opencastproject.workflow.api.WorkflowSet#getItems()
64     */
65    @Override
66    public List<WorkflowInstance> getItems() {
67      if (resultSet == null) {
68        return Collections.emptyList();
69      }
70      return resultSet.stream()
71          .map(JaxbWorkflowInstance::toWorkflowInstance)
72          .collect(Collectors.toList());
73    }
74  
75    /**
76     * Adds an item to the result set.
77     *
78     * @param item
79     *          the item to add
80     */
81    public void addItem(WorkflowInstance item) {
82      if (item == null)
83        throw new IllegalArgumentException("Parameter item cannot be null");
84      if (resultSet == null)
85        resultSet = new ArrayList<JaxbWorkflowInstance>();
86      resultSet.add(new JaxbWorkflowInstance(item));
87    }
88  
89    /**
90     * {@inheritDoc}
91     *
92     * @see org.opencastproject.workflow.api.WorkflowSet#size()
93     */
94    @Override
95    public long size() {
96      return resultSet != null ? resultSet.size() : 0;
97    }
98  
99    public static class Adapter extends XmlAdapter<WorkflowSetImpl, WorkflowSet> {
100     public WorkflowSetImpl marshal(WorkflowSet set) throws Exception {
101       return (WorkflowSetImpl) set;
102     }
103 
104     public WorkflowSet unmarshal(WorkflowSetImpl set) throws Exception {
105       return set;
106     }
107   }
108 
109 }