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 core.tut.pori.users;
017
018import java.util.Collection;
019import java.util.EnumSet;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlEnum;
025import javax.xml.bind.annotation.XmlEnumValue;
026import javax.xml.bind.annotation.XmlRootElement;
027
028import org.apache.commons.lang3.StringUtils;
029
030/**
031 * A class, which contains details of a single account connection belonging to a user.
032 */
033@XmlRootElement(name=Definitions.ELEMENT_EXTERNAL_ACCOUNT_CONNECTION)
034@XmlAccessorType(XmlAccessType.NONE)
035public class ExternalAccountConnection {
036  @XmlElement(name=Definitions.ELEMENT_EXTERNAL_ID)
037  private String _externalId = null;
038  @XmlElement(name=Definitions.ELEMENT_SERVICE_TYPE)
039  private UserServiceType _serviceType = null;
040  
041  /**
042   * Service types for external account connections.
043   *
044   */
045  @XmlEnum
046  public enum UserServiceType{
047    /** Service type for Google */
048    @XmlEnumValue(value=Definitions.SERVICE_TYPE_GOOGLE)
049    GOOGLE(1),
050    /** Service type for Facebook */
051    @XmlEnumValue(value=Definitions.SERVICE_TYPE_FACEBOOK)
052    FACEBOOK(2),
053    /** Service type Twitter */
054    @XmlEnumValue(value=Definitions.SERVICE_TYPE_TWITTER)
055    TWITTER(3);
056    
057    private int _userServiceId;
058    
059    /**
060     * 
061     * @param userServiceId
062     */
063    private UserServiceType(int userServiceId){
064      _userServiceId = userServiceId;
065    }
066    
067    /**
068     * 
069     * @return this service type as integer
070     */
071    public int toInt(){
072      return _userServiceId;
073    }
074    
075    /**
076     * 
077     * @param value
078     * @return the value converted to user service type
079     * @throws IllegalArgumentException on bad value
080     */
081    public static UserServiceType fromInt(int value) throws IllegalArgumentException{
082      for(UserServiceType t : UserServiceType.values()){
083        if(t._userServiceId == value){
084          return t;
085        }
086      }
087      throw new IllegalArgumentException("Bad "+UserServiceType.class.toString()+" : "+value);
088    }
089    
090    /**
091     * 
092     * @param types
093     * @return the types as integers or null if null or empty list was passed
094     */
095    public static int[] toInt(EnumSet<UserServiceType> types){
096      if(types == null || types.isEmpty()){
097        return null;
098      }
099      int[] values = new int[types.size()];
100      int index = -1;
101      for(UserServiceType t : types){
102        values[++index] = t.toInt();
103      }
104      return values;
105    }
106    
107    /**
108     * 
109     * @return this type as service type string
110     */
111    public String toUserServiceTypeString() {
112      switch(this){
113        case FACEBOOK:
114          return Definitions.SERVICE_TYPE_FACEBOOK;
115        case GOOGLE:
116          return Definitions.SERVICE_TYPE_GOOGLE;
117        case TWITTER:
118          return Definitions.SERVICE_TYPE_TWITTER;
119        default:
120          throw new UnsupportedOperationException("Unhandeled service type : "+name());
121      }
122    }
123    
124    /**
125     * 
126     * @param value
127     * @return the value converted to user service type
128     * @throws IllegalArgumentException on bad value
129     */
130    public static UserServiceType fromUserServiceTypeString(String value) throws IllegalArgumentException {
131      if(!StringUtils.isBlank(value)){
132        for(UserServiceType t : UserServiceType.values()){
133          if(t.toUserServiceTypeString().equalsIgnoreCase(value)){
134            return t;
135          }
136        }
137      }
138      throw new IllegalArgumentException("Bad "+UserServiceType.class.toString()+" : "+value);
139    }
140    
141    /**
142     * 
143     * @param values
144     * @return a set containing all of the given values converted to user service types or null if null or empty collection was given
145     * @throws IllegalArgumentException on bad value
146     */
147    public static EnumSet<UserServiceType> fromUserServiceTypeStrings(Collection<String> values) throws IllegalArgumentException {
148      if(values == null || values.isEmpty()){
149        return null;
150      }
151      EnumSet<UserServiceType> set = EnumSet.noneOf(UserServiceType.class);
152      for(String value : values){
153        set.add(fromUserServiceTypeString(value));
154      }
155      return set;
156    }
157  } // enum UserServiceType
158
159  /**
160   * 
161   */
162  public ExternalAccountConnection(){
163    // nothing needed
164  }
165
166  /**
167   * 
168   * @param externalId
169   * @param serviceType
170   */
171  public ExternalAccountConnection(String externalId, UserServiceType serviceType) {
172    _serviceType = serviceType;
173    _externalId = externalId;
174  }
175
176  /**
177   * @return the serviceType
178   */
179  public UserServiceType getServiceType() {
180    return _serviceType;
181  }
182
183  /**
184   * @param serviceType the serviceType to set
185   */
186  public void setServiceType(UserServiceType serviceType) {
187    _serviceType = serviceType;
188  }
189
190  /**
191   * @return the externalId
192   */
193  public String getExternalId() {
194    return _externalId;
195  }
196
197  /**
198   * @param externalId the externalId to set
199   */
200  public void setExternalId(String externalId) {
201    _externalId = externalId;
202  }
203  
204  /**
205   * for sub-classing, use the static
206   * 
207   * @return true if this external account connection object is valid
208   */
209  protected boolean isValid(){
210    if(_serviceType == null || StringUtils.isBlank(_externalId)){
211      return false;
212    }else{
213      return true;
214    }
215  }
216  
217  /**
218   * 
219   * @param connection
220   * @return false if connection is null or invalid
221   */
222  public static boolean isValid(ExternalAccountConnection connection){
223    return (connection == null ? null : connection.isValid());
224  }
225}