001package ball.tv.epg.entity;
002/*-
003 * ##########################################################################
004 * TV H/W, EPGs, and Recording
005 * $Id: Lineup.java 5285 2020-02-05 04:23:21Z 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 - 2020 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.ArrayList;
025import java.util.Iterator;
026import java.util.List;
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 javax.persistence.AccessType.PROPERTY;
039import static lombok.AccessLevel.PROTECTED;
040
041/**
042 * {@link Lineup} {@link Entity}.
043 *
044 * {@bean.info}
045 *
046 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
047 * @version $Revision: 5285 $
048 */
049@Entity
050@Table(catalog = "epg", name = "lineups")
051@NoArgsConstructor @EqualsAndHashCode(callSuper = false)
052public class Lineup extends AbstractEntity {
053    private static final long serialVersionUID = 8677286530171266875L;
054
055    /** @serial */
056    @Id @Column(length = 32, nullable = false) @Access(PROPERTY)
057    @Setter
058    private String lineup = null;
059    /*
060     * @Transient
061     * private List<Station> stations = null;
062     */
063    public String getLineup() {
064        if (lineup == null) {
065            lineup = textAt("/metadata/lineup");
066        }
067
068        return lineup;
069    }
070
071    public List<Channel> asChannelList() {
072        ArrayList<Channel> list = new ArrayList<>();
073
074        try {
075            Iterator<JsonNode> iterator = nodeAt("/map").elements();
076
077            while (iterator.hasNext()) {
078                MapEntry entry =
079                    mapper.treeToValue(iterator.next(), MapEntry.class);
080                Channel channel = new Channel();
081
082                channel.setLineup(getLineup());
083                channel.setChannel(entry.channel().toString());
084                channel.setStationID(entry.getStationID());
085
086                list.add(channel);
087            }
088        } catch (RuntimeException exception) {
089            throw exception;
090        } catch (Exception exception) {
091            throw new IllegalStateException(exception);
092        }
093
094        return list;
095    }
096
097    public List<Station> asStationList() {
098        ArrayList<Station> list = new ArrayList<>();
099
100        try {
101            Iterator<JsonNode> iterator = nodeAt("/stations").elements();
102
103            while (iterator.hasNext()) {
104                Station station =
105                    mapper.treeToValue(iterator.next(), Station.class);
106
107                list.add(station);
108            }
109        } catch (RuntimeException exception) {
110            throw exception;
111        } catch (Exception exception) {
112            throw new IllegalStateException(exception);
113        }
114
115        return list;
116    }
117
118    /**
119     * Abstract {@link MapEntry} {@link Entity}.
120     *
121     * {@bean.info}
122     */
123    @NoArgsConstructor(access = PROTECTED) @EqualsAndHashCode(callSuper = false)
124    protected static abstract class MapEntry extends AbstractEntity {
125        private static final long serialVersionUID = -151173051125744327L;
126
127        /** @serial */ @Getter private int stationID = -1;
128
129        public abstract Object channel();
130    }
131
132    /**
133     * {@link Cable} {@link Entity}.
134     *
135     * {@bean.info}
136     */
137    @NoArgsConstructor @EqualsAndHashCode(callSuper = true)
138    public static class Cable extends MapEntry {
139        private static final long serialVersionUID = -1574882754578381978L;
140
141        /** @serial */ @Getter private int channel = -1;
142
143        @Override
144        public Integer channel() { return getChannel(); }
145    }
146
147    /**
148     * {@link OTA} {@link Entity}.
149     *
150     * {@bean.info}
151     */
152    @NoArgsConstructor @EqualsAndHashCode(callSuper = true)
153    public static class OTA extends MapEntry {
154        private static final long serialVersionUID = 626722624913278995L;
155
156        /* @Getter * private int uhfVhf = -1; */
157        /** @serial */ @Getter private int atscMajor = -1;
158        /** @serial */ @Getter private int atscMinor = -1;
159
160        @Override
161        public String channel() {
162            return getAtscMajor() + "." + getAtscMinor();
163        }
164    }
165}