001package ball.tv.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * TV H/W, EPGs, and Recording
005 * $Id: SDClientTask.java 5830 2020-04-18 18:35:22Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-tv/trunk/src/main/java/ball/tv/ant/taskdefs/SDClientTask.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 ball.http.ant.taskdefs.HTTPTask;
024import ball.swing.table.MapTableModel;
025import ball.tv.epg.sd.SDClient;
026import ball.util.ant.taskdefs.AntTask;
027import ball.util.ant.taskdefs.NotNull;
028import com.fasterxml.jackson.databind.JsonNode;
029import java.io.File;
030import java.util.Arrays;
031import java.util.List;
032import java.util.Map;
033import java.util.concurrent.Callable;
034import lombok.Getter;
035import lombok.NoArgsConstructor;
036import lombok.Setter;
037import lombok.ToString;
038import org.apache.http.entity.FileEntity;
039import org.apache.tools.ant.BuildException;
040
041import static ball.tv.ObjectMapperConfiguration.MAPPER;
042import static lombok.AccessLevel.PROTECTED;
043import static org.apache.http.entity.ContentType.APPLICATION_JSON;
044
045/**
046 * Abstract {@link.uri http://ant.apache.org/ Ant} base
047 * {@link org.apache.tools.ant.Task} for tasks that invoke
048 * {@link SDClient} methods.
049 *
050 * {@ant.task}
051 *
052 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
053 * @version $Revision: 5830 $
054 */
055@NoArgsConstructor(access = PROTECTED)
056public abstract class SDClientTask extends HTTPTask
057                                   implements Callable<Object> {
058    protected SDClient client = null;
059
060    @Override
061    public void execute() throws BuildException {
062        super.execute();
063
064        try {
065            client = new SDClientImpl();
066
067            Object result = call();
068
069            if (result instanceof Map) {
070                log(new MapTableModel((Map) result));
071            } else if (result instanceof JsonNode) {
072                log(MAPPER.writeValueAsString((JsonNode) result));
073            } else if (result instanceof Iterable) {
074                for (Object object : (Iterable) result) {
075                    log(String.valueOf(object));
076                }
077            } else if (result != Void.class) {
078                log(String.valueOf(result));
079            }
080        } catch (BuildException exception) {
081            throw exception;
082        } catch (RuntimeException exception) {
083            throw exception;
084        } catch (Throwable throwable) {
085            throw new BuildException(throwable);
086        }
087    }
088
089    @Override
090    public abstract Object call() throws Exception;
091
092    protected List<String> split(String string) {
093        return Arrays.asList(string.trim().split("[\\p{Space}]+"));
094    }
095
096    /**
097     * {@link.uri http://ant.apache.org/ Ant}
098     * {@link org.apache.tools.ant.Task} to initialize
099     * {@link SDClient} API.
100     *
101     * {@ant.task}
102     */
103    @AntTask("sd-initialize")
104    @NoArgsConstructor @ToString
105    public static class Intialize extends SDClientTask {
106        @Getter @Setter
107        private String username = null;
108        @Getter @Setter
109        private String password = null;
110
111        @Override
112        public Object call() throws Exception {
113            String username = getUsername();
114            String password = getPassword();
115
116            client.initialize(username, password);
117
118            return Void.class;
119        }
120    }
121
122    /**
123     * {@link.uri http://ant.apache.org/ Ant}
124     * {@link org.apache.tools.ant.Task} to invoke
125     * {@link SDClient#getToken()}.
126     *
127     * {@ant.task}
128     */
129    @AntTask("sd-get-token")
130    @NoArgsConstructor @ToString
131    public static class GetToken extends SDClientTask {
132        @Override
133        public Object call() throws Exception { return client.getToken(); }
134    }
135
136    /**
137     * {@link.uri http://ant.apache.org/ Ant}
138     * {@link org.apache.tools.ant.Task} to invoke
139     * {@link SDClient#getStatus()}.
140     *
141     * {@ant.task}
142     */
143    @AntTask("sd-get-status")
144    @NoArgsConstructor @ToString
145    public static class GetStatus extends SDClientTask {
146        @Override
147        public Object call() throws Exception { return client.getStatus(); }
148    }
149
150    /**
151     * {@link.uri http://ant.apache.org/ Ant}
152     * {@link org.apache.tools.ant.Task} to invoke
153     * {@link ball.tv.epg.sd.SDProtocol#getAvailable()}.
154     *
155     * {@ant.task}
156     */
157    @AntTask("sd-get-available")
158    @NoArgsConstructor @ToString
159    public static class GetAvailable extends SDClientTask {
160        @Override
161        public Object call() throws Exception {
162            return client.proxy().getAvailable();
163        }
164    }
165
166    /**
167     * {@link.uri http://ant.apache.org/ Ant}
168     * {@link org.apache.tools.ant.Task} to invoke
169     * {@link ball.tv.epg.sd.SDProtocol#getAvailableCountries()}.
170     *
171     * {@ant.task}
172     */
173    @AntTask("sd-get-available-countries")
174    @NoArgsConstructor @ToString
175    public static class GetAvailableCountries extends SDClientTask {
176        @Override
177        public Object call() throws Exception {
178            return client.proxy().getAvailableCountries();
179        }
180    }
181
182    /**
183     * {@link.uri http://ant.apache.org/ Ant}
184     * {@link org.apache.tools.ant.Task} to invoke
185     * {@link ball.tv.epg.sd.SDProtocol#getAvailableLanguages()}.
186     *
187     * {@ant.task}
188     */
189    @AntTask("sd-get-available-languages")
190    @NoArgsConstructor @ToString
191    public static class GetAvailableLanguages extends SDClientTask {
192        @Override
193        public Object call() throws Exception {
194            return client.proxy().getAvailableLanguages();
195        }
196    }
197
198    /**
199     * {@link.uri http://ant.apache.org/ Ant}
200     * {@link org.apache.tools.ant.Task} to invoke
201     * {@link SDClient#getHeadends(String,String)}.
202     *
203     * {@ant.task}
204     */
205    @AntTask("sd-get-headends")
206    @NoArgsConstructor @ToString
207    public static class GetHeadends extends SDClientTask {
208        @Getter @Setter
209        private String country = null;
210        @Getter @Setter
211        private String postalcode = null;
212
213        @Override
214        public Object call() throws Exception {
215            return client.getHeadends(getCountry(), getPostalcode());
216        }
217    }
218
219    /**
220     * Abstract class for class implementations that require a "line-up"
221     * attribute.
222     *
223     * {@ant.task}
224     */
225    @NoArgsConstructor(access = PROTECTED)
226    public static abstract class Lineup extends SDClientTask {
227        @NotNull @Getter @Setter
228        private String lineup = null;
229
230        /**
231         * {@link.uri http://ant.apache.org/ Ant}
232         * {@link org.apache.tools.ant.Task} to invoke
233         * {@link SDClient#putLineup(String)}.
234         *
235         * {@ant.task}
236         */
237        @AntTask("sd-put-lineup")
238        @NoArgsConstructor @ToString
239        public static class Put extends Lineup {
240            @Override
241            public Object call() throws Exception {
242                return client.putLineup(getLineup());
243            }
244        }
245
246        /**
247         * {@link.uri http://ant.apache.org/ Ant}
248         * {@link org.apache.tools.ant.Task} to invoke
249         * {@link SDClient#deleteLineup(String)}.
250         *
251         * {@ant.task}
252         */
253        @AntTask("sd-delete-lineup")
254        @NoArgsConstructor @ToString
255        public static class Delete extends Lineup {
256            @Override
257            public Object call() throws Exception {
258                return client.deleteLineup(getLineup());
259            }
260        }
261
262        /**
263         * {@link.uri http://ant.apache.org/ Ant}
264         * {@link org.apache.tools.ant.Task} to invoke
265         * {@link SDClient#postLineup(String,HttpEntity)}.
266         *
267         * {@ant.task}
268         */
269        @AntTask("sd-post-lineup")
270        @NoArgsConstructor @ToString
271        public static class Post extends Lineup {
272            @NotNull @Getter @Setter
273            private File file = null;
274
275            @Override
276            public Object call() throws Exception {
277                FileEntity entity =
278                    new FileEntity(getFile(), APPLICATION_JSON);
279
280                return client.postLineup(getLineup(), entity);
281            }
282        }
283    }
284
285    /**
286     * {@link.uri http://ant.apache.org/ Ant}
287     * {@link org.apache.tools.ant.Task} to invoke
288     * {@link SDClient#getLineups()}.
289     *
290     * {@ant.task}
291     */
292    @AntTask("sd-get-lineups")
293    @NoArgsConstructor @ToString
294    public static class GetLineups extends SDClientTask {
295        @Override
296        public Object call() throws Exception { return client.getLineups(); }
297    }
298
299    /**
300     * {@link.uri http://ant.apache.org/ Ant}
301     * {@link org.apache.tools.ant.Task} to invoke
302     * {@link SDClient#getLineup(String)}.
303     *
304     * {@ant.task}
305     */
306    @AntTask("sd-get-lineup")
307    @NoArgsConstructor @ToString
308    public static class Get extends Lineup {
309        @Override
310        public Object call() throws Exception {
311            return client.getLineup(getLineup());
312        }
313    }
314
315    /**
316     * {@link.uri http://ant.apache.org/ Ant}
317     * {@link org.apache.tools.ant.Task} to invoke
318     * {@link SDClient#postLineup(String,HttpEntity)}.
319     *
320     * {@ant.task}
321     */
322    @AntTask("sd-automap-lineup")
323    @NoArgsConstructor @ToString
324    public static class AutomapLineup extends SDClientTask {
325        @NotNull @Getter @Setter
326        private File file = null;
327
328        @Override
329        public Object call() throws Exception {
330            FileEntity entity = new FileEntity(getFile(), APPLICATION_JSON);
331
332            return client.postLineup(entity);
333        }
334    }
335
336    /**
337     * {@link.uri http://ant.apache.org/ Ant}
338     * {@link org.apache.tools.ant.Task} to invoke
339     * {@link SDClient#getPrograms(java.util.Collection)}.
340     *
341     * {@ant.task}
342     */
343    @AntTask("sd-get-programs")
344    @NoArgsConstructor @ToString
345    public static class GetPrograms extends SDClientTask {
346        @NotNull @Getter @Setter
347        private String programs = null;
348
349        @Override
350        public Object call() throws Exception {
351            return client.getPrograms(split(getPrograms()));
352        }
353
354        /**
355         * {@link.uri http://ant.apache.org/ Ant}
356         * {@link org.apache.tools.ant.Task} to invoke
357         * {@link SDClient#getProgramsDescription(java.util.Collection)}.
358         *
359         * {@ant.task}
360         */
361        @AntTask("sd-get-programs-description")
362        @NoArgsConstructor @ToString
363        public static class Description extends GetPrograms {
364            @Override
365            public Object call() throws Exception {
366                return client.getProgramsDescription(split(getPrograms()));
367            }
368        }
369
370        /**
371         * {@link.uri http://ant.apache.org/ Ant}
372         * {@link org.apache.tools.ant.Task} to invoke
373         * {@link SDClient#getProgramsMetadata(java.util.Collection)}.
374         *
375         * {@ant.task}
376         */
377        @AntTask("sd-get-programs-metadata")
378        @NoArgsConstructor @ToString
379        public static class Metadata extends GetPrograms {
380            @Override
381            public Object call() throws Exception {
382                return client.getProgramsMetadata(split(getPrograms()));
383            }
384        }
385    }
386
387    /**
388     * {@link.uri http://ant.apache.org/ Ant}
389     * {@link org.apache.tools.ant.Task} to invoke
390     * {@link SDClient#getProgramMetadata(String)}.
391     *
392     * {@ant.task}
393     */
394    @AntTask("sd-get-program-metadata")
395    @NoArgsConstructor @ToString
396    public static class GetProgramMetadata extends SDClientTask {
397        @NotNull @Getter @Setter
398        private String root = null;
399
400        @Override
401        public Object call() throws Exception {
402            return client.getProgramMetadata(getRoot());
403        }
404    }
405
406    /**
407     * {@link.uri http://ant.apache.org/ Ant}
408     * {@link org.apache.tools.ant.Task} to invoke
409     * {@link SDClient#getSchedules(java.util.Collection)}.
410     *
411     * {@ant.task}
412     */
413    @AntTask("sd-get-schedules")
414    @NoArgsConstructor @ToString
415    public static class GetSchedules extends SDClientTask {
416        @NotNull @Getter @Setter
417        private String stations = null;
418
419        @Override
420        public Object call() throws Exception {
421            return client.getSchedules(split(getStations()));
422        }
423
424        /**
425         * {@link.uri http://ant.apache.org/ Ant}
426         * {@link org.apache.tools.ant.Task} to invoke
427         * {@link SDClient#getSchedulesMD5(java.util.Collection)}.
428         *
429         * {@ant.task}
430         */
431        @AntTask("sd-get-schedules-md5")
432        @NoArgsConstructor @ToString
433        public static class MD5 extends GetSchedules {
434            @Override
435            public Object call() throws Exception {
436                return client.getSchedulesMD5(split(getStations()));
437            }
438        }
439    }
440
441    /**
442     * {@link.uri http://ant.apache.org/ Ant}
443     * {@link org.apache.tools.ant.Task} to invoke
444     * {@link SDClient#getCelebrityMetadata(String)}.
445     *
446     * {@ant.task}
447     */
448    @AntTask("sd-get-celebrity-metadata")
449    @NoArgsConstructor @ToString
450    public static class GetCelebrityMetadata extends SDClientTask {
451        @NotNull @Getter @Setter
452        private String celebrity = null;
453
454        @Override
455        public Object call() throws Exception {
456            return client.getCelebrityMetadata(getCelebrity());
457        }
458    }
459
460    /**
461     * {@link.uri http://ant.apache.org/ Ant}
462     * {@link org.apache.tools.ant.Task} to invoke
463     * {@link SDClient#getImage(String)}.
464     *
465     * {@ant.task}
466     */
467    @AntTask("sd-get-image")
468    @NoArgsConstructor @ToString
469    public static class GetImage extends SDClientTask {
470        @NotNull @Getter @Setter
471        private String uri = null;
472
473        @Override
474        public Object call() throws Exception {
475            return client.getImage(getUri());
476        }
477    }
478
479    private class SDClientImpl extends SDClient {
480        public SDClientImpl() { super(null, builder()); }
481    }
482}