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;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang3.tuple.Pair;
023import org.apache.log4j.Logger;
024import org.springframework.beans.factory.annotation.Autowired;
025
026import service.tut.pori.contentanalysis.AbstractTaskDetails;
027import service.tut.pori.contentanalysis.AnalysisBackend;
028import service.tut.pori.contentanalysis.AsyncTask.TaskType;
029import service.tut.pori.contentanalysis.BackendDAO;
030import service.tut.pori.contentanalysis.Definitions;
031import service.tut.pori.contentanalysis.MediaObjectDAO;
032import service.tut.pori.contentanalysis.MediaTaskDAO;
033import core.tut.pori.context.ServiceInitializer;
034import core.tut.pori.http.parameters.DataGroups;
035import core.tut.pori.http.parameters.Limits;
036import core.tut.pori.users.UserIdentity;
037import core.tut.pori.utils.MediaUrlValidator.MediaType;
038
039/**
040 * DAO for saving and retrieving FacebookJazz tasks.
041 */
042public class FBTaskDAO extends MediaTaskDAO {
043  private static final Logger LOGGER = Logger.getLogger(FBTaskDAO.class);
044  private static final EnumSet<MediaType> MEDIA_TYPES = EnumSet.allOf(MediaType.class);
045  @Autowired
046  private BackendDAO _backendDAO = null;
047  
048  /**
049   * 
050   * @param details
051   * @return created row id or null on failure
052   * @throws UnsupportedOperationException on unsupported task type
053   * @throws IllegalArgumentException on bad task content
054   */
055  public Long insertTask(FBFeedbackTaskDetails details) throws UnsupportedOperationException, IllegalArgumentException {
056    TaskType type = details.getTaskType();
057    if(type != TaskType.FACEBOOK_PROFILE_SUMMARIZATION_FEEDBACK){
058      throw new UnsupportedOperationException("TaskType not supported.");
059    }
060    
061    Long taskId = insertTask((AbstractTaskDetails)details);
062    if(taskId == null){
063      throw new IllegalArgumentException("Failed to add new task.");
064    }
065    
066    insertTaskMediaObjects(null, taskId, details.getTags());
067    return taskId;
068  }
069  
070  /**
071   * 
072   * @param details
073   * @return created row id or null on failure
074   * @throws UnsupportedOperationException on unsupported task type
075   * @throws IllegalArgumentException on bad task content
076   */
077  public Long insertTask(FBSummarizationTaskDetails details) throws UnsupportedOperationException, IllegalArgumentException {
078    TaskType type = details.getTaskType();
079    if(type != TaskType.FACEBOOK_PROFILE_SUMMARIZATION){
080      throw new UnsupportedOperationException("TaskType not supported.");
081    }
082    
083    Long taskId = insertTask((AbstractTaskDetails)details);
084    if(taskId == null){
085      throw new IllegalArgumentException("Failed to add new task.");
086    }
087    return taskId;
088  }
089  
090  @Override
091  public AbstractTaskDetails getTask(Integer backendId, DataGroups dataGroups, Limits limits, Long taskId) throws IllegalArgumentException, UnsupportedOperationException {
092    Pair<TaskType, UserIdentity> type = getTaskType(backendId, taskId);
093    if(type == null){
094      LOGGER.warn("Failed to resolve task type.");
095      return null;
096    }
097    
098    switch(type.getLeft()){
099      case FACEBOOK_PROFILE_SUMMARIZATION:
100        return getSummarizationTask(dataGroups, taskId, type.getRight());
101      case FACEBOOK_PROFILE_SUMMARIZATION_FEEDBACK:
102        return getFeedbackTask(backendId, dataGroups, limits, taskId, type.getRight());
103      default:
104        throw new UnsupportedOperationException(("Using super: Unsupported task type: "+type.getLeft().name()));
105    }
106  }
107  
108  /**
109   * 
110   * @param backendId
111   * @param dataGroups
112   * @param limits optional limits filter
113   * @param taskId
114   * @param userId
115   * @return the feedback task or null if not found
116   * @throws IllegalArgumentException on bad values
117   * @throws UnsupportedOperationException on unsupported task type (not a feedback task)
118   */
119  private FBFeedbackTaskDetails getFeedbackTask(Integer backendId, DataGroups dataGroups, Limits limits, Long taskId, UserIdentity userId){
120    if(DataGroups.isEmpty(dataGroups)){
121      LOGGER.debug("No datagroups given, retrieving default data groups.");
122      AnalysisBackend backend = _backendDAO.getBackend(backendId);
123      if(backend == null){
124        throw new IllegalArgumentException("Backend, id: "+backendId+" does not exist.");
125      }
126      dataGroups = backend.getDefaultTaskDataGroups();
127    }
128    
129    FBFeedbackTaskDetails details = new FBFeedbackTaskDetails();
130    details.setBackendId(backendId);
131    details.setTaskId(taskId);
132    details.setTaskType(TaskType.FACEBOOK_PROFILE_SUMMARIZATION_FEEDBACK);
133    details.setUserId(userId);
134    
135    getTaskMetadata(details);
136    
137    Map<String, List<String>> map = getMediaObjectIds(limits, taskId);
138    if(map == null){
139      LOGGER.warn("No valid media objects for task, id: "+taskId);
140      return null;
141    }
142    
143    if(DataGroups.hasDataGroup(Definitions.DATA_GROUP_BACKEND_STATUS, dataGroups)){
144      LOGGER.debug("Retrieving backend status list.");
145      getBackendStatusList(details);
146    }
147    
148    details.setTags(ServiceInitializer.getDAOHandler().getSolrDAO(MediaObjectDAO.class).getMediaObjects(dataGroups, null, MEDIA_TYPES, null, map.get(null), null)); 
149    if(FBFeedbackTaskDetails.isEmpty(details)){
150      LOGGER.warn("Task, id: "+taskId+" has no content.");
151      return null;
152    }else{
153      return details;
154    }
155  }
156  
157  /**
158   * 
159   * @param dataGroups
160   * @param taskId
161   * @param userIdentity 
162   * @return the summarization task
163   */
164  private FBSummarizationTaskDetails getSummarizationTask(DataGroups dataGroups, Long taskId, UserIdentity userIdentity) {
165    FBSummarizationTaskDetails details = new FBSummarizationTaskDetails();
166    details.setTaskId(taskId);
167    details.setTaskType(TaskType.FACEBOOK_PROFILE_SUMMARIZATION);
168    details.setUserId(userIdentity);
169    
170    getTaskMetadata(details);
171    
172    if(DataGroups.hasDataGroup(Definitions.DATA_GROUP_BACKEND_STATUS, dataGroups)){
173      LOGGER.debug("Retrieving backend status list.");
174      getBackendStatusList(details);
175    }
176    
177    return details;
178  }
179}