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.List;
019import java.util.Map;
020
021import org.apache.log4j.Logger;
022import org.springframework.jdbc.core.JdbcTemplate;
023
024import service.tut.pori.contentanalysis.Definitions;
025import service.tut.pori.facebookjazz.WeightModifier.WeightModifierType;
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;
030import core.tut.pori.users.UserIdentity;
031
032
033/**
034 * DAO for storing facebook summarization information.
035 * 
036 * Note that this class stores details about the user's preferences for the summarization, the actual results are stored as media objects, using {@link service.tut.pori.contentanalysis.MediaObjectDAO}.
037 *
038 */
039public class FacebookJazzDAO extends SQLDAO{
040  private static final Logger LOGGER = Logger.getLogger(FacebookJazzDAO.class);
041  /* table */
042  private static final String TABLE_WEIGHT_MODIFIERS = DATABASE+".fbj_weight_modifiers";
043  /* columns */
044  private static final String COLUMN_WEIGHT_MODIFIER_TYPE = "modifier_type";
045  /* sql scripts */
046  private static final String[] SQL_COLUMNS_GET_WEIGHT_MODIFIERS = new String[]{Definitions.COLUMN_VALUE,COLUMN_WEIGHT_MODIFIER_TYPE};  
047  
048  private static final String SQL_REMOVE_WEIGHT_MODIFIERS = "DELETE FROM "+TABLE_WEIGHT_MODIFIERS+" WHERE "+COLUMN_USER_ID+"=?";
049  private static final int[] SQL_REMOVE_WEIGHT_MODIFIERS_SQL_TYPES = new int[]{SQLType.LONG.toInt()};
050
051  private static final String SQL_SET_WEIGHT_MODIFIERS = "INSERT INTO "+TABLE_WEIGHT_MODIFIERS+" ("+COLUMN_USER_ID+", "+Definitions.COLUMN_VALUE+", "+COLUMN_WEIGHT_MODIFIER_TYPE+", "+COLUMN_ROW_CREATED+") VALUES (?,?,?,NOW()) ON DUPLICATE KEY UPDATE "+Definitions.COLUMN_VALUE+"=?";
052  private static final int[] SQL_SET_WEIGHT_MODIFIERS_SQL_TYPES = new int[]{SQLType.LONG.toInt(), SQLType.INTEGER.toInt(), SQLType.INTEGER.toInt(), SQLType.INTEGER.toInt()};
053  
054  /**
055   * 
056   * @param userId use null to retrieve defaults
057   * @return list of weight modifiers for the given user of null if none available
058   */
059  public WeightModifierList getWeightModifiers(UserIdentity userId){
060    SQLSelectBuilder sql = new SQLSelectBuilder(TABLE_WEIGHT_MODIFIERS);
061    sql.addSelectColumns(SQL_COLUMNS_GET_WEIGHT_MODIFIERS);
062    Long userIdValue = null;
063    if(!UserIdentity.isValid(userId)){
064      LOGGER.debug("Invalid user id, returning default values...");
065    }else{
066      userIdValue = userId.getUserId();
067    }
068    sql.addWhereClause(new AndClause(COLUMN_USER_ID, userIdValue, SQLType.LONG));
069      
070    List<Map<String,Object>> rows = getJdbcTemplate().queryForList(sql.toSQLString(), sql.getValues(), sql.getValueTypes());
071    if(rows.isEmpty()){
072      LOGGER.debug("No modifiers.");
073      return null;
074    }
075    
076    WeightModifierList list = new WeightModifierList();
077    for(Map<String, Object> row : rows){
078      list.setWeightModifier(new WeightModifier(WeightModifierType.fromInt((int) row.get(COLUMN_WEIGHT_MODIFIER_TYPE)), (Integer) row.get(Definitions.COLUMN_VALUE)));
079    }
080    return list;
081  }
082  
083  /**
084   * Set weight modifiers for the given user
085   * 
086   * @param userId use null to set defaults
087   * @param modifiers
088   */
089  public void setWeightModifiers(UserIdentity userId, WeightModifierList modifiers){
090    if(!WeightModifierList.isValid(modifiers)){
091      LOGGER.warn("Invalid modifiers.");
092      return;
093    }
094    
095    Object[] ob = new Object[]{null, null, null, null};
096    if(!UserIdentity.isValid(userId)){
097      LOGGER.debug("Invalid userId, setting default weights...");
098    }else{
099      ob[0] = userId.getUserId();
100      LOGGER.debug("Clearing all previous weight values for user, id: "+ob[0]);
101      removeWeightModifers(userId);
102    }
103    
104    JdbcTemplate t = getJdbcTemplate();
105    for(WeightModifier modifier : modifiers.getModifiers()){
106      ob[1] = modifier.getValue();
107      ob[2] = modifier.getType().toInt();
108      ob[3] = ob[1];
109      t.update(SQL_SET_WEIGHT_MODIFIERS, ob, SQL_SET_WEIGHT_MODIFIERS_SQL_TYPES);
110    }
111  }
112  
113  /**
114   * Note: this will NOT allow removal of default values
115   * @param userId non-null user
116   */
117  public void removeWeightModifers(UserIdentity userId){
118    if(!UserIdentity.isValid(userId)){
119      LOGGER.warn("Invalid user.");
120    }else{
121      getJdbcTemplate().update(SQL_REMOVE_WEIGHT_MODIFIERS, new Object[]{userId.getUserId()}, SQL_REMOVE_WEIGHT_MODIFIERS_SQL_TYPES);
122    }
123  }
124}