org.silverpeas.token.persistent
Class PersistentResourceToken

java.lang.Object
  extended by org.silverpeas.token.persistent.PersistentResourceToken
All Implemented Interfaces:
Serializable, Token

@Entity
@TokenGenerator(value=PersistentResourceTokenGenerator.class)
public class PersistentResourceToken
extends Object
implements Token

A persistent token used to identify uniquely a resource. This token has the particularity to be persisted in a data source and to refer the resource it identifies uniquely both by the resource identifier and by the resource type.

Author:
Yohann Chastagnier
See Also:
Serialized Form

Field Summary
static PersistentResourceToken NoneToken
          Represents none token to replace in more typing way the null keyword.
 
Constructor Summary
protected PersistentResourceToken()
           
protected PersistentResourceToken(EntityReference resource, String value)
          Constructs a new persistent token for the specified resource and with the specified value.
 
Method Summary
static PersistentResourceToken createToken(EntityReference resource)
          Creates a token for the specified resource.
 boolean exists()
          Indicates if the token is well registred
 Long getId()
           
static PersistentResourceToken getOrCreateToken(EntityReference resource)
          Gets a token for the specified resource and creates it if it doesn't exist.
<E,R extends EntityReference<E>>
R
getResource(Class<R> referenceClass)
          Gets a reference to the resource this token is for.
 int getSaveCount()
           
 Date getSaveDate()
           
static PersistentResourceToken getToken(String token)
          Gets a the token from the specified value.
 String getValue()
          Gets the String representation of this token.
 boolean isDefined()
          Is this token defined?
 boolean notExists()
          Indicates if the token isn't registered.
 void setId(Long id)
           
 void setResource(EntityReference resource)
          Sets the resource to which this token belongs.
 void setSaveCount(int saveCount)
           
 void setSaveDate(Date saveDate)
           
 void setValue(String value)
           
 String toString()
           
 void validate()
          Validates data
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NoneToken

public static final PersistentResourceToken NoneToken
Represents none token to replace in more typing way the null keyword.

Constructor Detail

PersistentResourceToken

protected PersistentResourceToken()

PersistentResourceToken

protected PersistentResourceToken(EntityReference resource,
                                  String value)
Constructs a new persistent token for the specified resource and with the specified value.

Parameters:
resource - a reference to the resource for which this token is constructed.
value - the token value.
Method Detail

createToken

public static PersistentResourceToken createToken(EntityReference resource)
                                           throws TokenException
Creates a token for the specified resource. If the specified resource has already a token, then renews it. Otherwise a new token is generated and persisted into the data source.

Parameters:
resource - the resource for which the token has to be generated.
Returns:
a token for the specified resource.Quota
Throws:
TokenException

getOrCreateToken

public static PersistentResourceToken getOrCreateToken(EntityReference resource)
                                                throws TokenException
Gets a token for the specified resource and creates it if it doesn't exist. If the specified resource has already a token, then returns it. Otherwise a new token is generated and persisted into the data source.

Parameters:
resource - the resource for which the token has to be generated.
Returns:
a token for the specified resource.
Throws:
TokenException

getToken

public static PersistentResourceToken getToken(String token)
Gets a the token from the specified value. If not token exist with the specified value, then NoneToken is returned.

Parameters:
token - the value of the token to get.
Returns:
the token that matches the specified value.

exists

public boolean exists()
Indicates if the token is well registred

Returns:
a boolean indicating if this token exists in the data source.

notExists

public boolean notExists()
Indicates if the token isn't registered.

Returns:
a boolean indicating if this token doesn't exist in the data source.

validate

public void validate()
              throws TokenValidationException
Validates data

Throws:
TokenValidationException

getId

public Long getId()
Returns:
the id

setId

public void setId(Long id)
Parameters:
id - the id to set

getResource

public <E,R extends EntityReference<E>> R getResource(Class<R> referenceClass)
Gets a reference to the resource this token is for.

Type Parameters:
E - the concrete type of the entity.
R - the concrete type of the reference to the entity.
Parameters:
referenceClass - the expected concrete class of the EntityReference. This class must be conform to the type of the resource.
Returns:
a reference to the resource that owns this token or null if there is neither no resource defined for this token nor no reference defined for the targeted type of resource.

setResource

public void setResource(EntityReference resource)
Sets the resource to which this token belongs.

Parameters:
resource - an identifier of the resource for which this token is.

getValue

public String getValue()
Description copied from interface: Token
Gets the String representation of this token.

Specified by:
getValue in interface Token
Returns:
the value

setValue

public void setValue(String value)
Parameters:
value - the value to set

getSaveCount

public int getSaveCount()
Returns:
the saveCount

setSaveCount

public void setSaveCount(int saveCount)
Parameters:
saveCount - the saveCount to set

getSaveDate

public Date getSaveDate()
Returns:
the saveDate

setSaveDate

public void setSaveDate(Date saveDate)
Parameters:
saveDate - the saveDate to set

toString

public String toString()
Overrides:
toString in class Object

isDefined

public boolean isDefined()
Description copied from interface: Token
Is this token defined? A token is defined if it was generated and represents correctly a well-valued token. If it is empty (a none token), then it is considered as undefined. It is expected a token can be never null and a none-token concept is used instead. This method is to check the token is not a none-token, that is to say it is well initialized.

Specified by:
isDefined in interface Token
Returns:
true if this token is well-initialized, false otherwise.


Copyright © 2016 Silverpeas. All Rights Reserved.