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.contentanalysis;
017
018import java.util.Collections;
019import java.util.EnumSet;
020import java.util.List;
021
022import service.tut.pori.contentanalysis.PhotoParameters.AnalysisType;
023import service.tut.pori.contentanalysis.CAContentCore.ServiceType;
024import core.tut.pori.context.ServiceInitializer;
025import core.tut.pori.http.parameters.DataGroups;
026import core.tut.pori.http.parameters.Limits;
027import core.tut.pori.users.UserIdentity;
028
029
030/**
031 * 
032 * This class includes functions related to search queries
033 * 
034 */
035public final class CASearchCore {
036
037  /**
038   * 
039   */
040  private CASearchCore() {
041    // nothing needed
042  }
043
044  /**
045   * 
046   * @param authenticatedUser
047   * @param keywords
048   * @param dataGroups
049   * @param limits
050   * @param serviceTypes
051   * @param userIdFilters
052   * @return list of photos or null if none was found
053   */
054  public static PhotoList searchByKeyword(UserIdentity authenticatedUser, List<String> keywords, DataGroups dataGroups, Limits limits, EnumSet<ServiceType> serviceTypes, long[] userIdFilters) {
055    return ServiceInitializer.getDAOHandler().getSolrDAO(PhotoDAO.class).search(authenticatedUser, dataGroups, null, limits, MediaObjectList.getMediaObjectListFromKeywords(keywords), serviceTypes, userIdFilters);
056  }
057
058  /**
059   * 
060   * This will synchronously execute a content based search
061   * 
062   * The searchTask is not stored in the database 
063   * (because in case of server crash or other error the delay caused by restart will 
064   *  probably make the search irrelevant to the user anyway)
065   * 
066   * @param authenticatedUser
067   * @param analysisTypes 
068   * @param url
069   * @param dataGroups
070   * @param limits
071   * @param serviceTypes
072   * @param userIdFilters
073   * @return list of photos or null if none was found
074   */
075  public static PhotoList searchByContent(UserIdentity authenticatedUser, EnumSet<AnalysisType> analysisTypes, String url, DataGroups dataGroups, Limits limits, EnumSet<ServiceType> serviceTypes, long[] userIdFilters) {
076    return PhotoSearchTask.getTaskByUrl(authenticatedUser, analysisTypes, dataGroups, limits, serviceTypes, url, userIdFilters).execute(); 
077  }
078
079  /**
080   * 
081   * @param authenticatedUser
082   * @param objects
083   * @param dataGroups
084   * @param limits
085   * @param serviceTypes
086   * @param userIdFilters
087   * @return list of photos or null if none was found
088   * @throws IllegalArgumentException on bad input data
089   */
090  public static PhotoList similarPhotosByObject(UserIdentity authenticatedUser, MediaObjectList objects, DataGroups dataGroups, Limits limits, EnumSet<ServiceType> serviceTypes, long[] userIdFilters) throws IllegalArgumentException{
091    if(!MediaObjectList.isEmpty(objects)){
092      return ServiceInitializer.getDAOHandler().getSolrDAO(PhotoDAO.class).search(authenticatedUser, dataGroups, null, limits, objects, serviceTypes, userIdFilters);
093    }else{
094      throw new IllegalArgumentException("Empty object list.");
095    }
096  }
097
098  /**
099   * 
100   * @param authenticatedUser
101   * @param analysisTypes 
102   * @param guid
103   * @param dataGroups
104   * @param limits
105   * @param serviceTypes
106   * @param userIdFilters
107   * @return list of photos or null if none was found
108   * @throws IllegalArgumentException
109   */
110  public static PhotoList searchByGUID(UserIdentity authenticatedUser, EnumSet<AnalysisType> analysisTypes, String guid, DataGroups dataGroups, Limits limits, EnumSet<ServiceType> serviceTypes, long[] userIdFilters) throws IllegalArgumentException{
111    if(PhotoList.isEmpty(ServiceInitializer.getDAOHandler().getSolrDAO(PhotoDAO.class).search(authenticatedUser, null, Collections.nCopies(1, guid), null, null, null, null))){  // do not use serviceTypes or userIdFilters for existence check of reference photo
112      throw new IllegalArgumentException("Invalid "+Definitions.PARAMETER_GUID+" or permission denied.");
113    }
114    
115    return PhotoSearchTask.getTaskByGUID(authenticatedUser, analysisTypes, dataGroups, guid, limits, serviceTypes, userIdFilters).execute();
116  }
117}