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.transcription.persistence;
22  
23  import static org.opencastproject.db.Queries.namedQuery;
24  
25  import org.apache.commons.lang3.tuple.Pair;
26  
27  import java.io.Serializable;
28  import java.util.Optional;
29  import java.util.function.Function;
30  
31  import javax.persistence.Column;
32  import javax.persistence.Entity;
33  import javax.persistence.EntityManager;
34  import javax.persistence.GeneratedValue;
35  import javax.persistence.GenerationType;
36  import javax.persistence.Id;
37  import javax.persistence.NamedQueries;
38  import javax.persistence.NamedQuery;
39  import javax.persistence.Table;
40  
41  /**
42   * Entity object for the providers.
43   */
44  @Entity(name = "TranscriptionProvider")
45  @Table(name = "oc_transcription_service_provider")
46  @NamedQueries({
47      @NamedQuery(
48          name = "TranscriptionProvider.findProviderById",
49          query = "SELECT c FROM TranscriptionProvider c WHERE c.id= :id"
50      ),
51      @NamedQuery(
52          name = "TranscriptionProvider.findIdByProvider",
53          query = "SELECT c FROM TranscriptionProvider c WHERE c.provider= :provider"
54      ),
55  })
56  public class TranscriptionProviderControlDto implements Serializable {
57  
58    @Id
59    @GeneratedValue(strategy = GenerationType.AUTO)
60    @Column(name = "id", length = 128)
61    private long id;
62  
63    @Column(name = "provider", nullable = false)
64    private String provider;
65  
66    /**
67     * Default constructor
68     */
69    public TranscriptionProviderControlDto() {
70    }
71  
72    /**
73     * Creates a provider
74     *
75     * @param provider the provider
76     */
77    public TranscriptionProviderControlDto(String provider) {
78      super();
79      this.provider = provider;
80    }
81  
82    /**
83     * Convert into business object.
84     */
85    public TranscriptionProviderControl toTranscriptionProviderControl() {
86      return new TranscriptionProviderControl(id, provider);
87    }
88  
89    /**
90     * Store new provider
91     */
92    public static Function<EntityManager, TranscriptionProviderControlDto> storeProviderQuery(String provider) {
93      return em -> {
94        TranscriptionProviderControlDto dto = new TranscriptionProviderControlDto(provider);
95        em.persist(dto);
96        return dto;
97      };
98    }
99  
100   /**
101    * Find a transcription provider by its id.
102    */
103   public static Function<EntityManager, Optional<TranscriptionProviderControlDto>> findProviderByIdQuery(long id) {
104     return namedQuery.findOpt(
105         "TranscriptionProvider.findProviderById",
106         TranscriptionProviderControlDto.class,
107         Pair.of("id", id)
108     );
109   }
110 
111   /**
112    * Find a transcription provider id by provider name.
113    */
114   public static Function<EntityManager, Optional<TranscriptionProviderControlDto>> findIdByProviderQuery(
115       String provider) {
116     return namedQuery.findOpt(
117         "TranscriptionProvider.findIdByProvider",
118         TranscriptionProviderControlDto.class,
119         Pair.of("provider", provider)
120     );
121   }
122 }