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.twitterjazz;
017
018import org.apache.commons.lang3.tuple.Pair;
019import org.apache.log4j.Logger;
020
021import service.tut.pori.contentanalysis.AsyncTask.TaskType;
022import service.tut.pori.contentanalysis.Definitions;
023import service.tut.pori.contentanalysis.TaskDAO;
024import core.tut.pori.http.parameters.DataGroups;
025import core.tut.pori.http.parameters.Limits;
026import core.tut.pori.users.UserIdentity;
027
028/**
029 * DAO for inserting and retrieving Twitter summarization and synchronization tasks.
030 * 
031 */
032public class TwitterTaskDAO extends TaskDAO {
033  private static final Logger LOGGER = Logger.getLogger(TwitterTaskDAO.class);
034
035  @Override
036  public TwitterSummarizationTaskDetails getTask(Integer backendId, DataGroups dataGroups, Limits limits, Long taskId) throws IllegalArgumentException, UnsupportedOperationException {
037    Pair<TaskType, UserIdentity> type = getTaskType(backendId, taskId);
038    if(type == null){
039      LOGGER.warn("Failed to resolve task type.");
040      return null;
041    }
042
043    switch(type.getLeft()){
044      case TWITTER_PROFILE_SUMMARIZATION:
045        return getSummarizationTask(dataGroups, taskId, type.getRight());
046      default:
047        throw new UnsupportedOperationException("Unsupported task type: "+type.getLeft().name());
048    }
049  }
050
051  /**
052   * 
053   * @param dataGroups
054   * @param taskId
055   * @param userIdentity 
056   * @return task details
057   */
058  private TwitterSummarizationTaskDetails getSummarizationTask(DataGroups dataGroups, Long taskId, UserIdentity userIdentity) {
059    TwitterSummarizationTaskDetails details = new TwitterSummarizationTaskDetails();
060    details.setTaskId(taskId);
061    details.setTaskType(TaskType.TWITTER_PROFILE_SUMMARIZATION);
062    details.setUserId(userIdentity);
063
064    getTaskMetadata(details);
065
066    if(DataGroups.hasDataGroup(Definitions.DATA_GROUP_BACKEND_STATUS, dataGroups)){
067      LOGGER.debug("Retrieving backend status list.");
068      getBackendStatusList(details);
069    }
070
071    return details;
072  }
073}