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;
017
018import java.util.Date;
019
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlElement;
023
024import org.apache.log4j.Logger;
025import org.apache.solr.client.solrj.beans.Field;
026
027import service.tut.pori.contentanalysis.CAContentCore.ServiceType;
028import service.tut.pori.contentanalysis.CAContentCore.Visibility;
029import core.tut.pori.dao.SolrDAO;
030import core.tut.pori.users.UserIdentity;
031import core.tut.pori.utils.MediaUrlValidator.MediaType;
032
033/**
034 * Abstract base class for media objects
035 *
036 */
037@XmlAccessorType(value=XmlAccessType.NONE)
038public abstract class Media {
039  private static final Logger LOGGER = Logger.getLogger(Media.class);
040  @XmlElement(name = Definitions.ELEMENT_BACKEND_STATUS_LIST)
041  private BackendStatusList _backendStatus = null;
042  @Field(Definitions.SOLR_FIELD_CREDITS)
043  @XmlElement(name = Definitions.ELEMENT_CREDITS)
044  private String _credits = null;
045  @Field(Definitions.SOLR_FIELD_DESCRIPTION)
046  @XmlElement(name = Definitions.ELEMENT_DESCRIPTION)
047  private String _description = null;
048  @Field(SolrDAO.SOLR_FIELD_ID)
049  @XmlElement(name = Definitions.ELEMENT_GUID)
050  private String _guid = null;
051  private MediaType _mediaType = MediaType.UNKNOWN;
052  @Field(Definitions.SOLR_FIELD_NAME)
053  @XmlElement(name = Definitions.ELEMENT_NAME)
054  private String _name = null;
055  @XmlElement(name = Definitions.ELEMENT_SERVICE_ID)
056  private ServiceType _serviceType = null;
057  private String _url = null;
058  private UserIdentity _userId = null;   // the owner of the media
059  @XmlElement(name = Definitions.ELEMENT_VISIBILITY)
060  private Visibility _visibility = null;
061  @XmlElement(name = Definitions.ELEMENT_MEDIA_OBJECTLIST)
062  private MediaObjectList _mediaObjects = null;
063  @Field(Definitions.SOLR_FIELD_UPDATED)
064  private Date _updated = null;
065
066  /**
067   * 
068   * @param url
069   * @see #getUrl()
070   */
071  public void setUrl(String url) {
072    _url = url;
073  }
074
075  /**
076   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}, {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_DEFAULTS}
077   * 
078   * @return url
079   * @see #setUrl(String)
080   */  
081  @XmlElement(name = Definitions.ELEMENT_URL)
082  public String getUrl() {
083    return _url;
084  }
085
086  /**
087   * Default data groups: {@value service.tut.pori.contentanalysis.Definitions#DATA_GROUP_BACKEND_STATUS}
088   * 
089   * @return back-end status for this media or null if not available
090   * @see #setBackendStatus(BackendStatusList)
091   */
092  public BackendStatusList getBackendStatus() {
093    return _backendStatus;
094  }
095
096  /**
097   * 
098   * @param backendStatus
099   * @see #getBackendStatus()
100   */
101  public void setBackendStatus(BackendStatusList backendStatus) {
102    _backendStatus = backendStatus;
103  }
104
105  /**
106   * 
107   * @param backend
108   * @see #getBackendStatus()
109   */
110  public void addackendStatus(BackendStatus backend) {
111    if(_backendStatus == null){
112      _backendStatus = new BackendStatusList();
113    }
114    _backendStatus.setBackendStatus(backend);
115  }
116
117  /**
118   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_DEFAULTS}
119   * 
120   * @return media objects or null if not available
121   * @see #setMediaObjects(MediaObjectList)
122   */
123  public MediaObjectList getMediaObjects() {
124    return _mediaObjects;
125  }
126
127  /**
128   * 
129   * @param mediaObjects
130   * @see #getMediaObjects()
131   */
132  public void setMediaObjects(MediaObjectList mediaObjects) {
133    _mediaObjects = mediaObjects;
134  }
135
136  /**
137   * Adds the given object, NOTE: this will NOT check for duplicates.
138   * 
139   * @param object 
140   * @see #getMediaObjects()
141   */
142  public void addMediaObject(MediaObject object){
143    if(object == null){
144      LOGGER.debug("Ignored null object.");
145      return;
146    }
147    if(_mediaObjects == null){
148      _mediaObjects = new MediaObjectList();
149    }
150    _mediaObjects.addMediaObject(object);
151  }
152  
153  /**
154   * Adds the given objects, NOTE: this will NOT check for duplicates.
155   * 
156   * @param mediaObjects
157   * @see #getMediaObjects()
158   */
159  public void addMediaObjects(MediaObjectList mediaObjects){
160    if(MediaObjectList.isEmpty(mediaObjects)){
161      LOGGER.debug("Ignored empty object list.");
162      return;
163    }
164    ResultInfo info = mediaObjects.getResultInfo();
165    if(_mediaObjects == null){
166      if(info != null){ // duplicate the info to preserve original
167        ResultInfo oInfo = info;
168        info = new ResultInfo();
169        info.setEndItem(oInfo.getEndItem());
170        info.setStartItem(oInfo.getStartItem());
171        info.setResultCount(oInfo.getResultCount());
172      }
173      _mediaObjects = MediaObjectList.getMediaObjectList(mediaObjects.getMediaObjects(), info);
174    }else{
175      _mediaObjects.addMediaObjects(mediaObjects);
176    }
177  }
178
179  /**
180   * 
181   * @param type
182   * @see #getServiceType()
183   */
184  public void setServiceType(ServiceType type) {
185    _serviceType = type;
186  }  
187
188  /**
189   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}, {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_DEFAULTS}
190   * 
191   * @return the service this media originated from
192   * @see #setServiceType(service.tut.pori.contentanalysis.CAContentCore.ServiceType)
193   */
194  public ServiceType getServiceType() {
195    return _serviceType;
196  }
197  
198  /**
199   * 
200   * @param serviceId
201   */
202  @Field(Definitions.SOLR_FIELD_SERVICE_ID)
203  private void setServiceId(Integer serviceId){
204    _serviceType = (serviceId == null ? null : ServiceType.fromServiceId(serviceId));
205  }
206  
207  /**
208   * @see #getServiceType()
209   * 
210   * @return the service id
211   */
212  public Integer getServiceId() {
213    return (_serviceType == null ? null : _serviceType.getServiceId());
214  }
215
216  /**
217   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}, {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_DEFAULTS}
218   * 
219   * @return guid
220   * @see #setGUID(String)
221   */
222  public String getGUID() {
223    return _guid;
224  }
225
226  /**
227   * 
228   * @param guid
229   * @see #getGUID()
230   */
231  public void setGUID(String guid) {
232    _guid = guid;
233  }
234
235  /**
236   * @see #getOwnerUserId()
237   * 
238   * @return user identity value
239   */
240  @XmlElement(name = core.tut.pori.users.Definitions.ELEMENT_USER_ID)
241  public Long getOwnerUserIdValue() {
242    return (_userId == null ? null : _userId.getUserId());
243  }
244
245  /**
246   * for serialization
247   * @param userId
248   * @see #setOwnerUserId(UserIdentity)
249   */
250  @Field(Definitions.SOLR_FIELD_USER_ID)
251  private void setOwnerUserIdValue(Long userId) {
252    _userId = (userId == null ? null : new UserIdentity(userId));
253  }
254
255  /**
256   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}, {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_DEFAULTS}
257   * 
258   * @return media owner
259   * @see #setOwnerUserId(UserIdentity)
260   */
261  public UserIdentity getOwnerUserId() {
262    return _userId;
263  }
264
265  /**
266   * 
267   * @param ownerId the media's owner's user id
268   * @see #getOwnerUserId()
269   */
270  public void setOwnerUserId(UserIdentity ownerId) {
271    _userId = ownerId;
272  } 
273
274  /**
275   * for serialization, must be public for solr.
276   */
277  public Media(){
278    // nothing needed
279  }
280  
281  /**
282   * 
283   * @param guid
284   */
285  public Media(String guid){
286    _guid = guid;
287  }
288  
289  /**
290   * 
291   * @param guid
292   * @param ownerUserId
293   * @param serviceType
294   * @param visibility
295   */
296  public Media(String guid, UserIdentity ownerUserId, ServiceType serviceType, Visibility visibility){
297    _guid = guid;
298    _userId = ownerUserId;
299    _serviceType = serviceType;
300    _visibility = visibility;
301  }
302
303  /**
304   * 
305   * @param media can be null
306   * @return true if the given media is valid
307   */
308  public static boolean isValid(Media media){
309    if(media == null){
310      return false;
311    }else{
312      return media.isValid();
313    }
314  }
315
316  /**
317   * only for sub-classing, use the static
318   * @return true if this media is valid
319   * @see #isValid(Media)
320   */
321  protected boolean isValid(){
322    if(_guid == null || _serviceType == null || !UserIdentity.isValid(_userId) || _visibility == null){
323      return false;
324    }else if(MediaObjectList.isEmpty(_mediaObjects)){
325      return true;
326    }else{
327      return MediaObjectList.isValid(_mediaObjects);
328    }
329  }
330
331  /**
332   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}
333   * 
334   * @return credits information for the media (e.g. Creative Commons) or null if not available
335   * @see #setCredits(String)
336   */  
337  public String getCredits() {
338    return _credits;
339  }
340
341  /**
342   * 
343   * @param credits
344   * @see #getCredits()
345   */
346  public void setCredits(String credits) {
347    _credits = credits;
348  }
349
350  /**
351   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}
352   * 
353   * @return name of the media or null if not available
354   * @see #setName(String)
355   */  
356  public String getName() {
357    return _name;
358  }
359
360  /**
361   * 
362   * @param name
363   * @see #getName()
364   */
365  public void setName(String name) {
366    _name = name;
367  }
368
369  /**
370   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}
371   * 
372   * @return description of the media or null if not available
373   * @see #setDescription(String)
374   */  
375  public String getDescription() {
376    return _description;
377  }
378
379  /**
380   * 
381   * @param description
382   * @see #getDescription()
383   */
384  public void setDescription(String description) {
385    _description = description;
386  }
387
388  /**
389   * 
390   * @param visibility 
391   * @see #getVisibility()
392   */
393  public void setVisibility(Visibility visibility){
394    _visibility = visibility;
395  }
396
397  /**
398   * Default data groups: {@value core.tut.pori.http.parameters.DataGroups#DATA_GROUP_BASIC}, {@value service.tut.pori.contentanalysis.Definitions#DATA_GROUP_VISIBILITY}
399   * 
400   * If visibility is not available, it should be assumed to be {@link service.tut.pori.contentanalysis.CAContentCore.Visibility#PRIVATE}
401   * 
402   * @return visibility or null if not available
403   * @see #setVisibility(service.tut.pori.contentanalysis.CAContentCore.Visibility)
404   */
405  public Visibility getVisibility(){
406    return _visibility;
407  }
408  
409  /**
410   * @see #getVisibility()
411   * 
412   * @return visibility value
413   */
414  public Integer getVisibilityValue(){
415    return (_visibility == null ? null : _visibility.toInt());
416  }
417  
418  /**
419   * 
420   * @param visibility
421   * @see #setVisibility(service.tut.pori.contentanalysis.CAContentCore.Visibility)
422   */
423  @Field(Definitions.SOLR_FIELD_VISIBILITY)
424  private void setVisibilityValue(Integer visibility){
425    _visibility = (visibility == null ? null : Visibility.fromInt(visibility));
426  }
427
428  /**
429   * @return the updated
430   */
431  public Date getUpdated() {
432    return _updated;
433  }
434
435  /**
436   * @param updated the updated to set
437   */
438  public void setUpdated(Date updated) {
439    _updated = updated;
440  }
441
442  /**
443   * @return the mediaType
444   */
445  @XmlElement(name = Definitions.ELEMENT_MEDIA_TYPE)
446  public MediaType getMediaType() {
447    return _mediaType;
448  }
449
450  /**
451   * @param mediaType the mediaType to set
452   */
453  public void setMediaType(MediaType mediaType) {
454    _mediaType = mediaType;
455  }
456  
457  /**
458   * @see #getMediaType()
459   * 
460   * @return mediaType value
461   */
462  public Integer getMediaTypeValue(){
463    return (_mediaType == null ? null : _mediaType.toInt());
464  }
465  
466  /**
467   * 
468   * @param mediaType
469   * @see #setMediaType(core.tut.pori.utils.MediaUrlValidator.MediaType)
470   */
471  @Field(Definitions.SOLR_FIELD_MEDIA_TYPE)
472  private void setMediaTypeValue(Integer mediaType){
473    _mediaType = (mediaType == null ? null : MediaType.fromInt(mediaType));
474  }
475}