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  package org.opencastproject.workflow.api;
23  
24  import org.opencastproject.mediapackage.MediaPackage;
25  
26  import org.apache.commons.lang3.builder.EqualsBuilder;
27  import org.apache.commons.lang3.builder.HashCodeBuilder;
28  
29  import java.util.Collections;
30  import java.util.Date;
31  import java.util.List;
32  import java.util.Optional;
33  import java.util.Set;
34  import java.util.stream.Collectors;
35  
36  import javax.xml.bind.annotation.XmlAccessType;
37  import javax.xml.bind.annotation.XmlAccessorType;
38  import javax.xml.bind.annotation.XmlAttribute;
39  import javax.xml.bind.annotation.XmlElement;
40  import javax.xml.bind.annotation.XmlElementWrapper;
41  import javax.xml.bind.annotation.XmlRootElement;
42  import javax.xml.bind.annotation.XmlType;
43  
44  /** 1:1 serialization of a {@link WorkflowInstance}. */
45  @XmlAccessorType(XmlAccessType.FIELD)
46  @XmlType(name = "workflow", namespace = "http://workflow.opencastproject.org")
47  @XmlRootElement(name = "workflow", namespace = "http://workflow.opencastproject.org")
48  public class JaxbWorkflowInstance {
49  
50    @XmlAttribute()
51    private long id;
52  
53    @XmlAttribute()
54    private WorkflowInstance.WorkflowState state;
55  
56    @XmlElement(name = "template")
57    private String template;
58  
59    @XmlElement(name = "title")
60    private String title;
61  
62    @XmlElement(name = "description")
63    private String description;
64  
65    @XmlElement(name = "creator-id", namespace = "http://org.opencastproject.security")
66    private String creatorName;
67  
68    @XmlElement(name = "organization-id", namespace = "http://org.opencastproject.security")
69    private String organizationId;
70  
71    @XmlElement
72    private Date dateCreated = null;
73  
74    @XmlElement
75    private Date dateCompleted = null;
76  
77    @XmlElement(name = "mediapackage", namespace = "http://mediapackage.opencastproject.org")
78    private MediaPackage mediaPackage;
79  
80    @XmlElement(name = "operation")
81    @XmlElementWrapper(name = "operations")
82    protected List<JaxbWorkflowOperationInstance> operations;
83  
84    @XmlElement(name = "configuration")
85    @XmlElementWrapper(name = "configurations")
86    protected Set<JaxbWorkflowConfiguration> configurations;
87  
88    @XmlElement
89    protected String mediaPackageId;
90  
91    @XmlElement
92    protected String seriesId;
93  
94    /**
95     * Default no-arg constructor needed by JAXB
96     */
97    public JaxbWorkflowInstance() {
98    }
99  
100   public JaxbWorkflowInstance(WorkflowInstance workflow) {
101     this();
102     this.id = workflow.getId();
103     this.state = workflow.getState();
104     this.template = workflow.getTemplate();
105     this.title = workflow.getTitle();
106     this.description = workflow.getDescription();
107     this.creatorName = workflow.getCreatorName();
108     this.organizationId = workflow.getOrganizationId();
109     this.dateCreated = workflow.getDateCreated();
110     this.dateCompleted = workflow.getDateCompleted();
111     this.mediaPackage = workflow.getMediaPackage();
112     this.operations = workflow.getOperations()
113             .stream()
114             .map(JaxbWorkflowOperationInstance::new)
115             .collect(Collectors.toList());
116     this.configurations = workflow.getConfigurations().entrySet()
117             .stream()
118             .map(config -> new JaxbWorkflowConfiguration(config.getKey(), config.getValue()))
119             .collect(Collectors.toSet());
120 
121     this.mediaPackageId = mediaPackage == null ? null : mediaPackage.getIdentifier().toString();
122     this.seriesId = mediaPackage == null ? null : mediaPackage.getSeries();
123   }
124 
125   public WorkflowInstance toWorkflowInstance() {
126     WorkflowInstance wf = new WorkflowInstance(id, state, template, title, description, creatorName, organizationId, dateCreated,
127             dateCompleted, mediaPackage,
128             Optional.ofNullable(operations).orElseGet(Collections::emptyList)
129                     .stream().map(JaxbWorkflowOperationInstance::toWorkflowOperationInstance)
130                     .collect(Collectors.toList()),
131             Optional.ofNullable(configurations).orElseGet(Collections::emptySet)
132                     .stream()
133                     .collect(Collectors.toMap(JaxbWorkflowConfiguration::getKey, JaxbWorkflowConfiguration::getValue)),
134             mediaPackageId, seriesId);
135     // Set workflow reference in each operation
136     wf.getOperations().stream().forEach(wo -> wo.setWorkflowInstance(wf));
137     return wf;
138   }
139 
140   @Override
141   public boolean equals(Object o) {
142     if (this == o)
143       return true;
144 
145     if (o == null || getClass() != o.getClass())
146       return false;
147 
148     JaxbWorkflowInstance jaxbWorkflow = (JaxbWorkflowInstance) o;
149 
150     return new EqualsBuilder()
151             .append(id, jaxbWorkflow.id)
152             .append(state, jaxbWorkflow.state)
153             .append(template, jaxbWorkflow.template)
154             .append(title, jaxbWorkflow.title)
155             .append(description, jaxbWorkflow.description)
156             .append(creatorName, jaxbWorkflow.creatorName)
157             .append(organizationId, jaxbWorkflow.organizationId)
158             .append(dateCreated, jaxbWorkflow.dateCreated)
159             .append(dateCompleted, jaxbWorkflow.dateCompleted)
160             .append(mediaPackage, jaxbWorkflow.mediaPackage)
161             .append(operations, jaxbWorkflow.operations)
162             .append(configurations, jaxbWorkflow.configurations)
163             .append(mediaPackageId, jaxbWorkflow.mediaPackageId)
164             .append(seriesId, jaxbWorkflow.seriesId)
165             .isEquals();
166   }
167 
168   @Override
169   public int hashCode() {
170     return new HashCodeBuilder(17, 37)
171         .append(id)
172         .append(state)
173         .append(template)
174         .append(title)
175         .append(description)
176         .append(creatorName)
177         .append(organizationId)
178         .append(dateCreated)
179         .append(dateCompleted)
180         .append(mediaPackage)
181         .append(operations)
182         .append(configurations)
183         .append(mediaPackageId)
184         .append(seriesId)
185         .toHashCode();
186   }
187 }