001/**
002 * Copyright 2015 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.contentanalysis.video;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022
023import javax.xml.bind.annotation.XmlAccessType;
024import javax.xml.bind.annotation.XmlAccessorType;
025import javax.xml.bind.annotation.XmlElement;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.log4j.Logger;
029
030import core.tut.pori.http.ResponseData;
031import service.tut.pori.contentanalysis.ResultInfo;
032
033/**
034 * 
035 * This is a container class for video elements, which can be used directly with Response to provide XML output.
036 * 
037 * <h3>XML Example</h3>
038 * 
039 * {@doc.restlet service="[service.tut.pori.contentanalysis.video.reference.Definitions#SERVICE_VCA_REFERENCE_EXAMPLE]" method="[service.tut.pori.contentanalysis.video.Definitions#ELEMENT_VIDEOLIST]" type="GET" query="" body_uri=""}
040 *  
041 * @see service.tut.pori.contentanalysis.video.Video
042 * @see service.tut.pori.contentanalysis.ResultInfo
043 */
044@XmlRootElement(name=Definitions.ELEMENT_VIDEOLIST)
045@XmlAccessorType(value=XmlAccessType.NONE)
046public class VideoList extends ResponseData {
047  private static final Logger LOGGER = Logger.getLogger(VideoList.class);
048  @XmlElement(name = service.tut.pori.contentanalysis.Definitions.ELEMENT_RESULT_INFO)
049  private ResultInfo _resultInfo = null;
050  @XmlElement(name = Definitions.ELEMENT_VIDEO)
051  private List<Video> _videos = null;
052
053  
054  /**
055   * 
056   * @param videos
057   * @see #getVideos()
058   */
059  public void addVideos(VideoList videos){
060    if(VideoList.isEmpty(videos)){
061      LOGGER.debug("Ignored empty photo list.");
062      return;
063    }
064    if(_videos == null){
065      _videos = new ArrayList<>(videos.getVideos());
066    }else{
067      _videos.addAll(videos.getVideos());
068    }
069  }
070  
071  /**
072   * 
073   * @param guid
074   * @return video with the given GUID or null if not in the list
075   */
076  public Video getVideo(String guid){
077    if(isEmpty()){
078      return null;
079    }
080    if(guid == null){
081      LOGGER.debug("Ignored null guid.");
082      return null;
083    }
084    for(Iterator<Video> iter = _videos.iterator(); iter.hasNext();){
085      Video p = iter.next();
086      if(guid.equals(p.getGUID())){
087        return p;
088      }
089    }
090    return null;
091  }
092  
093  /**
094   * 
095   * @return list of GUIDs in this list or null if the list contains no GUIDs.
096   */
097  public List<String> getGUIDs(){
098    return getGUIDs(this);
099  }
100  
101  /**
102   * 
103   * @param videoList
104   * @return GUIDs contained in the given video list or null if none. Videos without GUID will be ignored.
105   */
106  public static List<String> getGUIDs(VideoList videoList){
107    if(isEmpty(videoList)){
108      LOGGER.debug("Empty video list.");
109      return null;
110    }else{
111      List<Video> videos = videoList.getVideos();
112      List<String> guids = new ArrayList<>(videos.size());
113      for(Iterator<Video> iter = videos.iterator(); iter.hasNext();){
114        String guid = iter.next().getGUID();
115        if(guid == null){
116          LOGGER.debug("Skipped video without GUID.");
117        }else{
118          guids.add(guid);
119        }
120      }
121      return (guids.isEmpty() ? null : guids);
122    }
123  }
124  
125  /**
126   * @return the resultInfo
127   * @see #setResultInfo(ResultInfo)
128   */
129  public ResultInfo getResultInfo() {
130    return _resultInfo;
131  }
132
133  /**
134   * @param resultInfo the resultInfo to set
135   * @see #getResultInfo()
136   */
137  public void setResultInfo(ResultInfo resultInfo) {
138    _resultInfo = resultInfo;
139  }
140
141  /**
142   * @return the videos
143   * @see #setVideos(List)
144   */
145  public List<Video> getVideos() {
146    return _videos;
147  }
148
149  /**
150   * @param videos the videos to set
151   * @see #getVideos()
152   */
153  public void setVideos(List<Video> videos) {
154    _videos = videos;
155  }
156  
157  /**
158   * 
159   * @param videos
160   * @param resultInfo optional resultInfo
161   * @return new video list of null if null or empty videos given
162   */
163  public static VideoList getVideoList(Collection<Video> videos, ResultInfo resultInfo){
164    if(videos == null || videos.isEmpty()){
165      return null;
166    }
167    
168    VideoList videoList = new VideoList();
169    videoList.setVideos(new ArrayList<>(videos));
170    videoList.setResultInfo(resultInfo);
171    return videoList;
172  } 
173  
174  /**
175   * @param list
176   * @return true if the given list is empty or null
177   */
178  public static boolean isEmpty(VideoList list){
179    if(list == null || list.isEmpty()){
180      return true;
181    }else{
182      return false;
183    }
184  }
185
186  /**
187   * use the static, only for sub-classing
188   * @return true if this list is empty
189   * @see #isEmpty(VideoList)
190   */
191  protected boolean isEmpty(){
192    return (_videos == null ? true : _videos.isEmpty());
193  }
194
195  /**
196   * 
197   * @param list can be null
198   * @return true if the given list is valid
199   */
200  public static boolean isValid(VideoList list){
201    return (list == null ? false : list.isValid());
202  }
203
204  /**
205   * use the static, only for sub-classing
206   * @return true if this list is valid
207   * @see #isValid(VideoList)
208   */
209  protected boolean isValid(){
210    if(isEmpty()){
211      return false;
212    }else{
213      for(Video video : _videos){
214        if(!Video.isValid(video)){
215          return false;
216        }
217      }
218      return true;
219    }
220  }
221  
222  /**
223   * use the static, only for sub-classing
224   * @return number of videos in the list
225   * @see #count(VideoList)
226   */
227  protected int count(){
228    return (_videos == null ? 0 : _videos.size());
229  }
230  
231  /**
232   * 
233   * @param list
234   * @return number of videos in the list or 0 if null or empty list passed
235   */
236  public static int count(VideoList list){
237    return (list == null ? 0 : list.count());
238  }
239
240  /**
241   * 
242   * @param video
243   */
244  public void addVideo(Video video) {
245    if(_videos == null){
246      _videos = new ArrayList<>();
247    }
248    _videos.add(video);
249  }
250}