001/**
002 * Copyright 2014 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.facebookjazz;
017
018import java.util.EnumSet;
019
020import service.tut.pori.contentanalysis.PhotoTaskResponse;
021import service.tut.pori.facebookjazz.FacebookExtractor.ContentType;
022import core.tut.pori.http.Response;
023import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
024import core.tut.pori.http.annotations.HTTPMethodParameter;
025import core.tut.pori.http.annotations.HTTPService;
026import core.tut.pori.http.annotations.HTTPServiceMethod;
027import core.tut.pori.http.parameters.AuthenticationParameter;
028import core.tut.pori.http.parameters.BooleanParameter;
029import core.tut.pori.http.parameters.DataGroups;
030import core.tut.pori.http.parameters.InputStreamParameter;
031import core.tut.pori.http.parameters.Limits;
032import core.tut.pori.http.parameters.LongParameter;
033import core.tut.pori.http.parameters.SortOptions;
034import core.tut.pori.http.parameters.StringParameter;
035import core.tut.pori.utils.XMLFormatter;
036
037
038/**
039 * This service enables facebook profile summarization
040 * 
041 * @see service.tut.pori.facebookjazz.reference.ClientService
042 * @see service.tut.pori.facebookjazz.reference.ServerService
043 */
044@HTTPService(name = Definitions.SERVICE_FBJ)
045public class FacebookJazzService {
046  private XMLFormatter _formatter = new XMLFormatter();
047
048  /**
049   * @see service.tut.pori.facebookjazz.reference.ClientService#retrieveTagsForUser(AuthenticationParameter, DataGroups, Limits, SortOptions)
050   * 
051   * @param authenticatedUser
052   * @param sortOptions
053   * @param dataGroups
054   * @param limits
055   * @return response
056   */
057  @HTTPServiceMethod(name = Definitions.METHOD_RETRIEVE_TAGS_FOR_USER, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
058  public Response retrieveTagsForUser(
059      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
060      @HTTPMethodParameter(name = DataGroups.PARAMETER_DEFAULT_NAME, required = false) DataGroups dataGroups,
061      @HTTPMethodParameter(name = Limits.PARAMETER_DEFAULT_NAME, required = false) Limits limits,
062      @HTTPMethodParameter(name = service.tut.pori.contentanalysis.Definitions.PARAMETER_SORT, required = false) SortOptions sortOptions
063      )
064  {
065    return new Response(FBJContentCore.retrieveTagsForUser(authenticatedUser.getUserIdentity(), dataGroups, limits, sortOptions));
066  }
067
068  /**
069   * @see service.tut.pori.facebookjazz.reference.ClientService#setRank(AuthenticationParameter, StringParameter)
070   * 
071   * @param authenticatedUser
072   * @param ranks
073   */
074  @HTTPServiceMethod(name = Definitions.METHOD_SET_RANK, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
075  public void setRank(
076      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
077      @HTTPMethodParameter(name = Definitions.PARAMETER_RANK) StringParameter ranks)
078  {
079    FBJContentCore.setRanks(authenticatedUser.getUserIdentity(), FBJContentCore.parseRankStrings(ranks.getValues()));
080  }
081  
082  /**
083   * @see service.tut.pori.facebookjazz.reference.ClientService#retrieveTagWeights(AuthenticationParameter, LongParameter)
084   * 
085   * @param authenticatedUser
086   * @param userIdFilter
087   * @return response
088   */
089  @HTTPServiceMethod(name = Definitions.METHOD_RETRIEVE_TAG_WEIGHTS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_GET})
090  public Response retrieveTagWeights(
091      @HTTPAuthenticationParameter(required = false) AuthenticationParameter authenticatedUser,
092      @HTTPMethodParameter(name = service.tut.pori.users.Definitions.PARAMETER_USER_ID, required = false) LongParameter userIdFilter
093      )
094  {
095    return new Response(FBJContentCore.retrieveTagWeights(authenticatedUser.getUserIdentity(), userIdFilter.getValue()));
096  }
097  
098  /**
099   * @see service.tut.pori.facebookjazz.reference.ClientService#setTagWeights(AuthenticationParameter, InputStreamParameter)
100   * 
101   * @param authenticatedUser
102   * @param xml
103   */
104  @HTTPServiceMethod(name = Definitions.METHOD_SET_TAG_WEIGHTS, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
105  public void setTagWeights(
106      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
107      @HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml
108      )
109  {
110    FBJContentCore.setTagWeights(authenticatedUser.getUserIdentity(), _formatter.toObject(xml.getValue(), WeightModifierList.class));
111  }
112  
113  /**
114   * @see service.tut.pori.facebookjazz.reference.ServerService#taskFinished(InputStreamParameter)
115   * 
116   * @param xml
117   */
118  @HTTPServiceMethod(name = service.tut.pori.contentanalysis.Definitions.METHOD_TASK_FINISHED, acceptedMethods={core.tut.pori.http.Definitions.METHOD_POST})
119  public void taskFinished(@HTTPMethodParameter(name = InputStreamParameter.PARAMETER_DEFAULT_NAME, bodyParameter = true) InputStreamParameter xml) {
120    FBJContentCore.taskFinished(_formatter.toObject(xml.getValue(), FBTaskResponse.class, PhotoTaskResponse.class));
121  }
122  
123  /**
124   * @see service.tut.pori.facebookjazz.reference.ClientService#summarize(AuthenticationParameter, StringParameter, BooleanParameter)
125   * 
126   * @param authenticatedUser
127   * @param contentTypes
128   * @param synchronize
129   */
130  @HTTPServiceMethod(name = Definitions.METHOD_SUMMARIZE)
131  public void summarize(
132      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
133      @HTTPMethodParameter(name = Definitions.PARAMETER_CONTENT_TYPES, required = false) StringParameter contentTypes,
134      @HTTPMethodParameter(name = Definitions.PARAMETER_SYNCHRONIZE, required = false) BooleanParameter synchronize)
135  {
136    FBSummarizationTaskDetails details = new FBSummarizationTaskDetails();
137    details.setUserId(authenticatedUser.getUserIdentity());
138    details.setContentTypes((contentTypes.hasValues() ? ContentType.fromString(contentTypes.getValues()) : EnumSet.allOf(ContentType.class)));
139    details.setSynchronize(synchronize.isTrue());
140    FBJContentCore.summarize(details);
141  }
142}