com.trendmicro.grid.acl.ds.jpa.util
Class JpaUtils

java.lang.Object
  extended by com.trendmicro.grid.acl.ds.jpa.util.JpaUtils

public class JpaUtils
extends Object

A small set of common helper methods used to simplify working with JPA.

Version:
1.0
Author:
juergen_kellerer, 2010-06-07

Nested Class Summary
static interface JpaUtils.Callback<E,V>
          Defines a callback to use for converting types.
 
Field Summary
static boolean EMPTY_METADATA_EQUALS_NULL
          Toggles whether EMPTY metadata and 'NULL' are treated equal.
static JpaUtils.Callback SINGLE_ELEMENT_CALLBACK
          Straight converter callback that simply passed single or array input to a single output.
 
Method Summary
static
<T> void
appendSingleElementToList(Collection<T> result, javax.persistence.TypedQuery<? extends T> query)
          Appends exactly one element to the given result list (using 'null' if the query produces no results).
static
<Q extends javax.persistence.Query>
Q
applyPage(Q query, int pageNumber, int pageSize)
          Applies page number and size to the query.
static com.trendmicro.grid.acl.metadata.Metadata deserializeMetadata(Source rawData)
          Deserizalizes the given XML serialized metadata.
static com.trendmicro.grid.acl.metadata.Metadata deserializeMetadata(String rawData)
          Deserizalizes the given XML serialized metadata.
static String[] deserializeTags(String tags)
          Deserizalizes the given serialized tags.
static String extractName(String name, int delimiterCount)
          Extracts a name part of a hierarchic package, family or vendor name.
static UUID fromBytes(byte[] publicGUID)
          Decodes a UUID from its byte representation.
static Boolean isTaggedWith(com.trendmicro.grid.acl.l0.datatypes.Tagged element, String[] tags)
          Returns true if the element is tagged with the given tags.
static boolean metadataDiffers(com.trendmicro.grid.acl.l0.datatypes.MetadataOwner a, com.trendmicro.grid.acl.l0.datatypes.MetadataOwner b)
          Returns true if the 2 metadata owning instances differ.
static String serializeMetadata(com.trendmicro.grid.acl.metadata.Metadata metadata)
          Serializes the given metadata to XML.
static String serializeTags(String[] tags)
          Serializes the given tag array to text.
static boolean tagsDiffer(com.trendmicro.grid.acl.l0.datatypes.Tagged a, com.trendmicro.grid.acl.l0.datatypes.Tagged b)
          Returns true if the 2 tagged instances differ.
static byte[] toBytes(UUID guid)
          Encodes a UUID to its byte representation.
static
<E,V,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>>
T
toListPage(javax.persistence.Query query, JpaUtils.Callback<V,E> converterCallback, T emptyListPage)
          Converts the given query to a list page using converterCallback to convert results.
static
<E,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>>
T
toListPage(javax.persistence.TypedQuery<E> query, T emptyListPage)
          Converts the given query to a list page.
static
<E,V,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>>
T
toListPage(javax.persistence.TypedQuery<V> query, JpaUtils.Callback<V,E> converterCallback, T emptyListPage)
          Converts the given query to a list page using converterCallback to convert results.
static com.trendmicro.grid.acl.l0.datatypes.NameListPage toNameListPage(javax.persistence.Query query, int pageNumber, int pageSize)
          Converts the given query to a name list page.
static com.trendmicro.grid.acl.l0.datatypes.NameListPage toNameListPage(javax.persistence.TypedQuery<String> query, int pageNumber, int pageSize)
          Converts the given query to a name list page.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_METADATA_EQUALS_NULL

public static final boolean EMPTY_METADATA_EQUALS_NULL
Toggles whether EMPTY metadata and 'NULL' are treated equal. (defaults to 'true')

When 'true' the ACL normalizes empty metadata to 'null' to reduce the storage overhead inside the database.


SINGLE_ELEMENT_CALLBACK

public static final JpaUtils.Callback SINGLE_ELEMENT_CALLBACK
Straight converter callback that simply passed single or array input to a single output.

Method Detail

toBytes

public static byte[] toBytes(UUID guid)
Encodes a UUID to its byte representation.

Parameters:
guid - the UUID to convert.
Returns:
the encoded UUID bytes.

fromBytes

public static UUID fromBytes(byte[] publicGUID)
Decodes a UUID from its byte representation.

Parameters:
publicGUID - the UUID to convert.
Returns:
the decoded UUID.

serializeTags

public static String serializeTags(String[] tags)
Serializes the given tag array to text.

Parameters:
tags - the tags to encode.
Returns:
the text encoded tags.

deserializeTags

public static String[] deserializeTags(String tags)
Deserizalizes the given serialized tags.

Parameters:
tags - the text encoded tags.
Returns:
the deserialized tag array.

serializeMetadata

public static String serializeMetadata(com.trendmicro.grid.acl.metadata.Metadata metadata)
Serializes the given metadata to XML.

Parameters:
metadata - the metadata to encode.
Returns:
the XML encoded metadata.

deserializeMetadata

public static com.trendmicro.grid.acl.metadata.Metadata deserializeMetadata(String rawData)
Deserizalizes the given XML serialized metadata.

Parameters:
rawData - the XML encoded metadata.
Returns:
the deserialized metadata.

deserializeMetadata

public static com.trendmicro.grid.acl.metadata.Metadata deserializeMetadata(Source rawData)
Deserizalizes the given XML serialized metadata.

Parameters:
rawData - the XML encoded metadata.
Returns:
the deserialized metadata.

metadataDiffers

public static boolean metadataDiffers(com.trendmicro.grid.acl.l0.datatypes.MetadataOwner a,
                                      com.trendmicro.grid.acl.l0.datatypes.MetadataOwner b)
Returns true if the 2 metadata owning instances differ.

Parameters:
a - the first instance to check.
b - the second instance to check.
Returns:
true if the 2 metadata owning instances differ.

tagsDiffer

public static boolean tagsDiffer(com.trendmicro.grid.acl.l0.datatypes.Tagged a,
                                 com.trendmicro.grid.acl.l0.datatypes.Tagged b)
Returns true if the 2 tagged instances differ.

Parameters:
a - the first instance to check.
b - the second instance to check.
Returns:
true if the 2 tagged instances differ.

isTaggedWith

public static Boolean isTaggedWith(com.trendmicro.grid.acl.l0.datatypes.Tagged element,
                                   String[] tags)
Returns true if the element is tagged with the given tags.

Parameters:
element - the element to check.
tags - the tags to verify.
Returns:
true if the element contains all the tags, false if not, 'null' if the element was 'null'.

extractName

public static String extractName(String name,
                                 int delimiterCount)
Extracts a name part of a hierarchic package, family or vendor name.

Parameters:
name - the name to work on.
delimiterCount - the count of delimiters to include (e.g. 1 => vendorName, 2 => familyName, ...).
Returns:
A name with all trailing name parts stripped.

appendSingleElementToList

public static <T> void appendSingleElementToList(Collection<T> result,
                                                 javax.persistence.TypedQuery<? extends T> query)
Appends exactly one element to the given result list (using 'null' if the query produces no results).

Parameters:
result - the result list to add the element of the given query or 'null' if the query produced no results.
query - the query that fetches the element to add to the result.
Throws:
javax.persistence.NonUniqueResultException - In case of the query produced more than one result.

applyPage

public static <Q extends javax.persistence.Query> Q applyPage(Q query,
                                                              int pageNumber,
                                                              int pageSize)
Applies page number and size to the query.

Parameters:
query - the query to apply the limits on.
pageNumber - the number of the page to query.
pageSize - the size of a single page.
Returns:
a positioned and limited query.

toListPage

public static <E,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>> T toListPage(javax.persistence.TypedQuery<E> query,
                                                                                                  T emptyListPage)
Converts the given query to a list page.

Parameters:
query - the query to convert.
emptyListPage - the list page template instance to use.
Returns:
the given list page, after filling the query results.

toListPage

public static <E,V,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>> T toListPage(javax.persistence.TypedQuery<V> query,
                                                                                                    JpaUtils.Callback<V,E> converterCallback,
                                                                                                    T emptyListPage)
Converts the given query to a list page using converterCallback to convert results.

Parameters:
query - the query to convert.
converterCallback - a converter used to convert the query results to the type needed within the list page.
emptyListPage - the list page template instance to use.
Returns:
the given list page, after filling the query results.

toListPage

public static <E,V,T extends com.trendmicro.grid.acl.l0.datatypes.AbstractListPage<E>> T toListPage(javax.persistence.Query query,
                                                                                                    JpaUtils.Callback<V,E> converterCallback,
                                                                                                    T emptyListPage)
Converts the given query to a list page using converterCallback to convert results.

Parameters:
query - the query to convert.
converterCallback - a converter used to convert the query results to the type needed within the list page.
emptyListPage - the list page template instance to use.
Returns:
the given list page, after filling the query results.

toNameListPage

public static com.trendmicro.grid.acl.l0.datatypes.NameListPage toNameListPage(javax.persistence.Query query,
                                                                               int pageNumber,
                                                                               int pageSize)
Converts the given query to a name list page.

Parameters:
query - the query to convert.
pageNumber - the number of the page to extract.
pageSize - the size of the page to return.
Returns:
the list page.

toNameListPage

public static com.trendmicro.grid.acl.l0.datatypes.NameListPage toNameListPage(javax.persistence.TypedQuery<String> query,
                                                                               int pageNumber,
                                                                               int pageSize)
Converts the given query to a name list page.

Parameters:
query - the query to convert.
pageNumber - the number of the page to extract.
pageSize - the size of the page to return.
Returns:
the list page.