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.contentanalysistest;
017
018import java.util.Collection;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.commons.lang3.tuple.Pair;
024import org.apache.log4j.Logger;
025
026import core.tut.pori.dao.clause.AndClause;
027import core.tut.pori.dao.clause.SQLClause.SQLType;
028import core.tut.pori.dao.SQLDAO;
029import core.tut.pori.dao.SQLSelectBuilder;
030
031/**
032 * Class for handling picasa album information.
033 * 
034 * By-passes {@link service.tut.pori.contentstorage.PicasaDAO}
035 * 
036 */
037@Deprecated
038public class PicasaAlbumDAO extends SQLDAO {
039  private static final Logger LOGGER = Logger.getLogger(PicasaAlbumDAO.class);
040  /* tables */
041  private static final String TABLE_PICASA_ENTRIES = DATABASE+".ca_picasa_entries";
042  /* columns */
043  private static final String COLUMN_ALBUM_ID = "album_id";
044  private static final String COLUMN_GOOGLE_USER_ID = "google_user_id";
045  private static final String COLUMN_PHOTO_ID = "photo_id";
046  
047  private static final String SQL_GET_IDS = "SELECT "+COLUMN_COUNT+", "+COLUMN_ALBUM_ID+", "+COLUMN_PHOTO_ID+" FROM "+TABLE_PICASA_ENTRIES+" WHERE "+COLUMN_GOOGLE_USER_ID+"=? AND "+COLUMN_GUID+"=?";
048  private static final int[] SQL_GET_IDS_SQL_TYPES = {SQLType.STRING.toInt(), SQLType.STRING.toInt()};
049  
050  private static final String SQL_GET_GUIDS = "SELECT "+COLUMN_GUID+" FROM "+TABLE_PICASA_ENTRIES+" WHERE "+COLUMN_ALBUM_ID+"=?";
051  private static final int[] SQL_GET_GUIDS_SQL_TYPES = {SQLType.STRING.toInt()};
052  
053  /**
054   * 
055   * @param googleUserId
056   * @param guid
057   * @return album id/photoId pair for the given GUID or null if not found
058   */
059  public Pair<String, String> getIdPair(String googleUserId, String guid){
060    Map<String, Object> map = getJdbcTemplate().queryForMap(SQL_GET_IDS, new Object[]{googleUserId, guid}, SQL_GET_IDS_SQL_TYPES);
061    String albumId = (String) map.get(COLUMN_ALBUM_ID);
062    if(StringUtils.isBlank(albumId)){
063      LOGGER.warn("Could not find album id for guid : "+guid+", google user id : "+googleUserId);
064      return null;
065    }
066    String photoId = (String) map.get(COLUMN_PHOTO_ID);
067    if(StringUtils.isBlank(photoId)){
068      LOGGER.warn("Could not find photo id for guid : "+guid+", google user id : "+googleUserId);
069      return null;
070    }
071    return Pair.of(albumId, photoId);
072  }
073  
074  /**
075   * 
076   * @param albumIds optional filter
077   * @param googleUserId
078   * @return list of album ids or null if none was found
079   */
080  public List<String> getAlbumIds(Collection<String> albumIds, String googleUserId){
081    SQLSelectBuilder sql = new SQLSelectBuilder(TABLE_PICASA_ENTRIES);
082    sql.addSelectColumn(COLUMN_ALBUM_ID);
083    sql.addWhereClause(new AndClause(COLUMN_GOOGLE_USER_ID, googleUserId, SQLType.STRING));
084    sql.addGroupBy(COLUMN_ALBUM_ID);
085    
086    if(albumIds != null && !albumIds.isEmpty()){
087      sql.addWhereClause(new AndClause(COLUMN_ALBUM_ID, albumIds, SQLType.STRING));
088    }
089    
090    
091    List<String> ids = getJdbcTemplate().queryForList(sql.toSQLString(), sql.getValues(), sql.getValueTypes(), String.class);
092    return (ids.isEmpty() ? null : ids);
093  }
094  
095  /**
096   * 
097   * @param albumId
098   * @return list of GUIDs for the album or null if none was found
099   */
100  public List<String> getGUIDs(String albumId){
101    List<String> guids = getJdbcTemplate().queryForList(SQL_GET_GUIDS, new Object[]{albumId}, SQL_GET_GUIDS_SQL_TYPES, String.class);
102    return (guids.isEmpty() ? null : guids);
103  }
104}