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 service.tut.pori.contentanalysis.AbstractTaskDetails;
019import service.tut.pori.contentanalysis.PhotoParameters.AnalysisType;
020import service.tut.pori.contentanalysis.CAContentCore.ServiceType;
021import service.tut.pori.contentanalysis.MediaObjectList;
022import core.tut.pori.http.RedirectResponse;
023import core.tut.pori.http.Response;
024import core.tut.pori.http.Response.Status;
025import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
026import core.tut.pori.http.annotations.HTTPMethodParameter;
027import core.tut.pori.http.annotations.HTTPService;
028import core.tut.pori.http.annotations.HTTPServiceMethod;
029import core.tut.pori.http.parameters.AuthenticationParameter;
030import core.tut.pori.http.parameters.DataGroups;
031import core.tut.pori.http.parameters.InputStreamParameter;
032import core.tut.pori.http.parameters.IntegerParameter;
033import core.tut.pori.http.parameters.Limits;
034import core.tut.pori.http.parameters.LongParameter;
035import core.tut.pori.http.parameters.StringParameter;
036import core.tut.pori.utils.XMLFormatter;
037
038/**
039 * This service enables content analysis and provides search features.
040 * 
041 * @see service.tut.pori.contentanalysis.video.reference.ClientService
042 * @see service.tut.pori.contentanalysis.video.reference.ServerService
043 */
044@HTTPService(name=Definitions.SERVICE_VCA)
045public class VideoAnalysisService {
046  private XMLFormatter _formatter = new XMLFormatter();
047  
048  /**
049   * @see service.tut.pori.contentanalysis.video.reference.ClientService#r(AuthenticationParameter, IntegerParameter, StringParameter)
050   * 
051   * @param authenticatedUser
052   * @param serviceId
053   * @param guid
054   * @return response
055   */
056  @HTTPServiceMethod(name = service.tut.pori.contentanalysis.Definitions.METHOD_REDIRECT, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
057  public RedirectResponse r(
058      @HTTPAuthenticationParameter(required = false) AuthenticationParameter authenticatedUser,
059      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_SERVICE_ID) IntegerParameter serviceId,
060      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_GUID) StringParameter guid
061      )
062  {
063    return VideoContentCore.generateTargetUrl(authenticatedUser.getUserIdentity(), ServiceType.fromServiceId(serviceId.getValue()), guid.getValue());
064  }
065  
066  /**
067   * @see service.tut.pori.contentanalysis.video.reference.ServerService#queryTaskDetails(IntegerParameter, LongParameter, DataGroups, Limits)
068   * 
069   * @param taskId
070   * @param backendId
071   * @param dataGroups
072   * @param limits
073   * @return response
074   */
075  @HTTPServiceMethod(name = service.tut.pori.contentanalysis.Definitions.METHOD_QUERY_TASK_DETAILS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
076  public Response queryTaskDetails(
077      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_BACKEND_ID) IntegerParameter backendId,
078      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_TASK_ID) LongParameter taskId,
079      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
080      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits
081      ) 
082  {
083    AbstractTaskDetails details = VideoContentCore.queryTaskDetails(backendId.getValue(), taskId.getValue(), dataGroups, limits);
084    if(details == null){
085      return new Response(Status.BAD_REQUEST, "Invalid "+service.tut.pori.contentanalysis.Definitions.PARAMETER_TASK_ID+" or "+service.tut.pori.contentanalysis.Definitions.PARAMETER_BACKEND_ID+".");
086    }else{
087      return new Response(details);
088    }
089  }
090
091  /**
092   * @see service.tut.pori.contentanalysis.video.reference.ServerService#taskFinished(InputStreamParameter)
093   * 
094   * @param xml
095   */
096  @HTTPServiceMethod(name = service.tut.pori.contentanalysis.Definitions.METHOD_TASK_FINISHED, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
097  public void taskFinished(@HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml) {
098    VideoContentCore.taskFinished(_formatter.toObject(xml.getValue(), VideoTaskResponse.class));
099  }
100  
101  /**
102   * @see service.tut.pori.contentanalysis.video.reference.ClientService#similarVideosByObject(AuthenticationParameter, DataGroups, Limits, IntegerParameter, LongParameter, InputStreamParameter)
103   * 
104   * @param authenticatedUser
105   * @param dataGroups
106   * @param limits
107   * @param serviceIds
108   * @param userIdFilters
109   * @param xml
110   * @return response
111   */
112  @HTTPServiceMethod(name = Definitions.METHOD_SEARCH_SIMILAR_BY_OBJECT, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
113  public Response similarVideosByObject(
114      @HTTPAuthenticationParameter(required = false) AuthenticationParameter authenticatedUser,
115      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
116      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits,
117      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_SERVICE_ID, required = false) IntegerParameter serviceIds,
118      @HTTPMethodParameter(name = service.tut.pori.users.Definitions.PARAMETER_USER_ID, required = false) LongParameter userIdFilters,
119      @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml
120      )
121  {
122    return new Response(VideoSearchCore.similarVideosByObject(authenticatedUser.getUserIdentity(), _formatter.toObject(xml.getValue(), MediaObjectList.class), dataGroups, limits, ServiceType.fromIdArray(serviceIds.getValues()), userIdFilters.getValues()));
123  }
124  
125  /**
126   * @see service.tut.pori.contentanalysis.video.reference.ClientService#getVideos(AuthenticationParameter, StringParameter, DataGroups, Limits, IntegerParameter, LongParameter)
127   * 
128   * @param authenticatedUser
129   * @param guid
130   * @param dataGroups
131   * @param limits
132   * @param serviceIds
133   * @param userIds 
134   * @return response
135   */
136  @HTTPServiceMethod(name = Definitions.METHOD_RETRIEVE_VIDEO_METADATA, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
137  public Response getVideos(
138      @HTTPAuthenticationParameter(required = false) AuthenticationParameter authenticatedUser,
139      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_GUID, required = false) StringParameter guid,
140      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
141      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits,
142      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_SERVICE_ID, required = false) IntegerParameter serviceIds,
143      @HTTPMethodParameter(name = service.tut.pori.users.Definitions.PARAMETER_USER_ID, required = false) LongParameter userIds
144      )
145  {
146    return new Response(VideoContentCore.getVideos(authenticatedUser.getUserIdentity(), guid.getValues(), dataGroups, limits, ServiceType.fromIdArray(serviceIds.getValues()), userIds.getValues()));
147  }
148  
149  /**
150   * Returns videos that are similar with the video associated with the given id.
151   * 
152   * @see service.tut.pori.contentanalysis.video.reference.ClientService#searchSimilarById(AuthenticationParameter, StringParameter, StringParameter, DataGroups, Limits, IntegerParameter, LongParameter)
153   * 
154   * @param authenticatedUser
155   * @param guid
156   * @param analysisType 
157   * @param dataGroups
158   * @param limits
159   * @param serviceIds
160   * @param userIdFilters
161   * @return response
162   */
163  @HTTPServiceMethod(name = Definitions.METHOD_SEARCH_SIMILAR_BY_ID, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
164  public Response searchSimilarById(
165      @HTTPAuthenticationParameter(required = false) AuthenticationParameter authenticatedUser,
166      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_GUID) StringParameter guid,
167      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_ANALYSIS_TYPE, required = false) StringParameter analysisType,
168      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
169      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits,
170      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_SERVICE_ID, required = false) IntegerParameter serviceIds,
171      @HTTPMethodParameter(name = service.tut.pori.users.Definitions.PARAMETER_USER_ID, required = false) LongParameter userIdFilters
172      ) 
173  {
174    return new Response(VideoSearchCore.searchByGUID(authenticatedUser.getUserIdentity(), AnalysisType.fromAnalysisTypeString(analysisType.getValues()), guid.getValue(), dataGroups, limits, ServiceType.fromIdArray(serviceIds.getValues()), userIdFilters.getValues()));
175  }
176}