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.users;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025
026import org.apache.log4j.Logger;
027
028import core.tut.pori.http.ResponseData;
029import core.tut.pori.users.UserIdentity;
030
031/**
032 * List of users usable with Response.
033 */
034@XmlRootElement(name=Definitions.ELEMENT_USER_IDENTITY_LIST)
035@XmlAccessorType(XmlAccessType.NONE)
036public class UserIdentityList extends ResponseData {
037  private static final Logger LOGGER = Logger.getLogger(UserIdentityList.class);
038  @XmlElement(name=core.tut.pori.users.Definitions.ELEMENT_USER_IDENTITY)
039  private List<UserIdentity> _userIds = null;
040
041  /**
042   * @return the userIds
043   */
044  public List<UserIdentity> getUserIds() {
045    return _userIds;
046  }
047
048  /**
049   * @param userIds the userIds to set
050   */
051  public void setUserIds(List<UserIdentity> userIds) {
052    _userIds = userIds;
053  }
054  
055  /**
056   * 
057   * @param userId null userId is ignored
058   */
059  public void addUserId(UserIdentity userId){
060    if(userId == null){
061      LOGGER.debug("Ignored null userId.");
062      return;
063    }
064    
065    if(_userIds == null){
066      _userIds = new ArrayList<>();
067    }
068    _userIds.add(userId);
069  }
070  
071  /**
072   * for sub-classing, use the static.
073   * 
074   * @return true if this list is valid
075   */
076  protected boolean isValid(){
077    if(isEmpty()){
078      return false;
079    }else{
080      for(UserIdentity userId : _userIds){
081        if(!UserIdentity.isValid(userId)){
082          return false;
083        }
084      }
085      return true;
086    }
087  }
088  
089  /**
090   * for sub-classing, use the static
091   * @return true if this list is empty
092   */
093  protected boolean isEmpty(){
094    return (_userIds == null ? true : _userIds.isEmpty());
095  }
096  
097  /**
098   * 
099   * @param list
100   * @return true if the list is empty or null
101   */
102  public static boolean isEmpty(UserIdentityList list){
103    return (list == null ? true : list.isEmpty());
104  }
105  
106  /**
107   * 
108   * @param list
109   * @return false if list is null, empty or invalid
110   */
111  public static boolean isValid(UserIdentityList list){
112    return (list == null ? false : list.isValid());
113  }
114}