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.assetmanager.impl.query;
22  
23  import static com.entwinemedia.fn.Equality.eq;
24  import static com.entwinemedia.fn.Equality.hash;
25  
26  import org.opencastproject.assetmanager.api.Property;
27  import org.opencastproject.assetmanager.api.Snapshot;
28  import org.opencastproject.assetmanager.api.query.ARecord;
29  import org.opencastproject.assetmanager.impl.persistence.SnapshotDto;
30  
31  import com.entwinemedia.fn.Fn;
32  import com.entwinemedia.fn.data.Opt;
33  
34  import java.util.List;
35  import java.util.Optional;
36  
37  public final class ARecordImpl implements ARecord {
38    private final long snapshotId;
39    private final String mediaPackageId;
40    private final List<Property> properties;
41    private Optional<Snapshot> snapshot;
42    private Opt<SnapshotDto> snapshotDto;
43  
44    public ARecordImpl(
45            long snapshotId,
46            String mediaPackageId,
47            List<Property> properties) {
48      this.snapshotId = snapshotId;
49      this.mediaPackageId = mediaPackageId;
50      this.properties = properties;
51      this.snapshot = Optional.empty();
52      this.snapshotDto = Opt.none(SnapshotDto.class);
53    }
54  
55    public ARecordImpl(
56            long snapshotId,
57            String mediaPackageId,
58            List<Property> properties,
59            Snapshot snapshot) {
60      this(snapshotId, mediaPackageId, properties);
61      if (snapshot != null) {
62        this.snapshot = Optional.of(snapshot);
63      }
64    }
65  
66    public ARecordImpl(
67            long snapshotId,
68            String mediaPackageId,
69        List<Property> properties,
70            SnapshotDto snapshotDto) {
71      this(snapshotId, mediaPackageId, properties);
72      if (snapshotDto != null) {
73        this.snapshotDto = Opt.some(snapshotDto);
74      }
75    }
76  
77  
78    /** Get the database ID of the snapshot. */
79    public long getSnapshotId() {
80      return snapshotId;
81    }
82  
83    @Override public String getMediaPackageId() {
84      return mediaPackageId;
85    }
86  
87    @Override public List<Property> getProperties() {
88      return properties;
89    }
90  
91    /**
92     * Get the snapshot if set.
93     * Otherwise try to convert snapshotDto to {@link Snapshot} with method call {@link SnapshotDto#toSnapshot()},
94     * cache and return the result.
95     *
96     * @return the snapshot
97     */
98    @Override public Optional<Snapshot> getSnapshot() {
99      if (snapshot.isEmpty() && snapshotDto.isSome()) {
100       snapshot = Optional.of(snapshotDto.get().toSnapshot());
101     }
102     return snapshot;
103   }
104 
105   /** Get the snapshotDto. */
106   public Opt<SnapshotDto> getSnapshotDto() {
107     return snapshotDto;
108   }
109 
110   @Override public int hashCode() {
111     return hash(snapshotId);
112   }
113 
114   /** Two records are considered equal if their database IDs are equal. */
115   @Override public boolean equals(Object that) {
116     return (this == that) || (that instanceof ARecordImpl && eqFields((ARecordImpl) that));
117   }
118 
119   private boolean eqFields(ARecordImpl that) {
120     return eq(snapshotId, that.snapshotId);
121   }
122 
123   public static final Fn<ARecordImpl, String> getMediaPackageId = new Fn<ARecordImpl, String>() {
124     @Override public String apply(ARecordImpl a) {
125       return a.getMediaPackageId();
126     }
127   };
128 }