001/**
002 * Copyright 2016 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.apilta.alerts;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang3.ArrayUtils;
022import org.apache.log4j.Logger;
023
024import core.tut.pori.dao.SQLDAO;
025import core.tut.pori.dao.SQLSelectBuilder;
026import core.tut.pori.dao.clause.AndClause;
027import core.tut.pori.dao.clause.SQLClause.SQLType;
028import core.tut.pori.users.UserIdentity;
029import service.tut.pori.apilta.alerts.datatypes.AlertUserIdentity.UserPermission;
030
031/**
032 * 
033 * dao for accessing alert groups
034 *
035 */
036public class AlertGroupsDAO extends SQLDAO {
037  private static final Logger LOGGER = Logger.getLogger(AlertGroupsDAO.class);
038  /* tables */
039//  private static final String TABLE_ALERTS_GROUPS = DATABASE+".alerts_groups";
040  private static final String TABLE_ALERTS_GROUPS_USERS = DATABASE+".alerts_groups_users";
041  /* columns */
042  private static final String COLUMN_ALERT_GROUP_ID = "alert_group_id";
043//  private static final String COLUMN_DESCRIPTION = "description";
044//  private static final String COLUMN_NAME = "name";
045  private static final String COLUMN_PERMISSION = "permission";
046  
047  /**
048   * 
049   * @param alertGroupIds only search for these identifiers
050   * @param permission the user must have the given permission for the group (or the permission must be global for the group)
051   * @param userId if null, only global permissions are matched
052   * @return list of alert group identifiers matching the given term or null if none was found
053   */
054  public List<Long> getAlertGroupIds(long[] alertGroupIds, UserPermission permission, UserIdentity userId) {
055    SQLSelectBuilder sql = new SQLSelectBuilder(TABLE_ALERTS_GROUPS_USERS);
056    sql.addSelectColumn(COLUMN_ALERT_GROUP_ID);
057    
058    if(UserIdentity.isValid(userId)){
059      ArrayList<Long> userIds = new ArrayList<>(2);
060      userIds.add(null);
061      userIds.add(userId.getUserId());
062      sql.addWhereClause(new AndClause(COLUMN_USER_ID, userIds, SQLType.LONG));
063    }else{
064      sql.addWhereClause(new AndClause(COLUMN_USER_ID, (Object) null, SQLType.LONG));
065    }
066    
067    sql.addWhereClause(new AndClause(COLUMN_PERMISSION, permission.toInt(), SQLType.INTEGER));
068    
069    if(!ArrayUtils.isEmpty(alertGroupIds)){
070      LOGGER.debug("Using alert group id filter...");
071      sql.addWhereClause(new AndClause(COLUMN_ALERT_GROUP_ID, alertGroupIds));
072    }
073    
074    List<Long> ids = getJdbcTemplate().queryForList(sql.toSQLString(), sql.getValues(), sql.getValueTypes(), Long.class);
075    return (ids.isEmpty() ? null : ids);
076  }
077  
078  //TODO add/modify/delete/get alert group methods
079}