001package ball.tv.epg.entity;
002/*-
003 * ##########################################################################
004 * TV H/W, EPGs, and Recording
005 * $Id: Lineup.java 7215 2021-01-03 18:39:51Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-tv/trunk/src/main/java/ball/tv/epg/entity/Lineup.java $
007 * %%
008 * Copyright (C) 2013 - 2021 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import com.fasterxml.jackson.databind.JsonNode;
024import java.util.Iterator;
025import java.util.Set;
026import java.util.TreeMap;
027import javax.persistence.Access;
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.Id;
031import javax.persistence.Table;
032import javax.persistence.Transient;
033import lombok.EqualsAndHashCode;
034import lombok.Getter;
035import lombok.NoArgsConstructor;
036import lombok.Setter;
037
038import static java.util.stream.Collectors.toSet;
039import static javax.persistence.AccessType.PROPERTY;
040import static lombok.AccessLevel.PROTECTED;
041
042/**
043 * {@link Lineup} {@link Entity}.
044 *
045 * {@bean.info}
046 *
047 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
048 * @version $Revision: 7215 $
049 */
050@Entity
051@Table(catalog = "epg", name = "lineups")
052@NoArgsConstructor @EqualsAndHashCode(callSuper = false)
053public class Lineup extends AbstractEntity {
054    private static final long serialVersionUID = -3653917021345933188L;
055
056    /** @serial */
057    @Id @Column(length = 32, nullable = false) @Access(PROPERTY)
058    @Setter
059    private String lineup = null;
060    /*
061     * @Transient
062     * private Set<Station> stations = null;
063     */
064    public String getLineup() {
065        if (lineup == null) {
066            lineup = textAt("/metadata/lineup");
067        }
068
069        return lineup;
070    }
071
072    public Set<Channel> asChannelSet() {
073        TreeMap<String,Channel> map = new TreeMap<>();
074
075        try {
076            Iterator<JsonNode> iterator = nodeAt("/map").elements();
077
078            while (iterator.hasNext()) {
079                MapEntry entry =
080                    mapper.treeToValue(iterator.next(), MapEntry.class);
081                Channel channel = new Channel();
082
083                channel.setLineup(getLineup());
084                channel.setChannel(entry.channel().toString());
085                channel.setStationID(entry.getStationID());
086
087                map.put(channel.getChannel(), channel);
088            }
089        } catch (RuntimeException exception) {
090            throw exception;
091        } catch (Exception exception) {
092            throw new IllegalStateException(exception);
093        }
094
095        return map.values().stream().collect(toSet());
096    }
097
098    public Set<Station> asStationSet() {
099        TreeMap<Integer,Station> map = new TreeMap<>();
100
101        try {
102            Iterator<JsonNode> iterator = nodeAt("/stations").elements();
103
104            while (iterator.hasNext()) {
105                Station station =
106                    mapper.treeToValue(iterator.next(), Station.class);
107
108                map.put(station.getStationID(), station);
109            }
110        } catch (RuntimeException exception) {
111            throw exception;
112        } catch (Exception exception) {
113            throw new IllegalStateException(exception);
114        }
115
116        return map.values().stream().collect(toSet());
117    }
118
119    /**
120     * Abstract {@link MapEntry} {@link Entity}.
121     *
122     * {@bean.info}
123     */
124    @NoArgsConstructor(access = PROTECTED) @EqualsAndHashCode(callSuper = false)
125    protected static abstract class MapEntry extends AbstractEntity {
126        private static final long serialVersionUID = -151173051125744327L;
127
128        /** @serial */ @Getter private int stationID = -1;
129
130        public abstract Object channel();
131    }
132
133    /**
134     * {@link Cable} {@link Entity}.
135     *
136     * {@bean.info}
137     */
138    @NoArgsConstructor @EqualsAndHashCode(callSuper = true)
139    public static class Cable extends MapEntry {
140        private static final long serialVersionUID = -1574882754578381978L;
141
142        /** @serial */ @Getter private int channel = -1;
143
144        @Override
145        public Integer channel() { return getChannel(); }
146    }
147
148    /**
149     * {@link OTA} {@link Entity}.
150     *
151     * {@bean.info}
152     */
153    @NoArgsConstructor @EqualsAndHashCode(callSuper = true)
154    public static class OTA extends MapEntry {
155        private static final long serialVersionUID = 626722624913278995L;
156
157        /* @Getter * private int uhfVhf = -1; */
158        /** @serial */ @Getter private int atscMajor = -1;
159        /** @serial */ @Getter private int atscMinor = -1;
160
161        @Override
162        public String channel() {
163            return getAtscMajor() + "." + getAtscMinor();
164        }
165    }
166}