/*
 * Copyright (C) 2005-2010 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco. If not, see 
['a','b'] is equivalent to ['a','b',null] in all cases except where multiple search results
 * are possible. Keys can be any simple Serializable type, typically being convertable using
 * {@link DefaultTypeConverter}. The attribute values persisted can be any Serializable
 * (including collections) but the raw values should be convertable by the {@link DefaultTypeConverter} for
 * the most efficient persistence.
 * 
 * @author Derek Hulley
 * @since 3.4
 */
public interface AttributeService
{
    /**
     * Determine if a particular attribute exists.
     * 
     * @param keys                  List of 1 to 3 keys to uniquely identify the attribute
     * @return                      true if the attribute exists (regardless of its value)
     *                              or false if it doesn't exist
     */
    public boolean exists(Serializable ... keys);
    /**
     * Get an attribute using a list of unique keys
     *
     * @param keys                  List of 1 to 3 keys to uniquely identify the attribute
     * @return                      The attribute value or null
     */
    public Serializable getAttribute(Serializable ... keys);
    
    /**
     * Callback used for querying for lists of attributes.
     * 
     * @author Derek Hulley
     * @since 3.4
     */
    public interface AttributeQueryCallback
    {
        /**
         * Handle an attribute value
         * 
         * @param id                the unique attribute ID
         * @param value             the value associated with the attribute
         * @param keys              the unique attribute keys
         * @return                  true to continue sending results if any are available
         */
        boolean handleAttribute(Long id, Serializable value, Serializable[] keys);
    }
    
    /**
     * Get all attributes that share the starter keys provided.  If 3 key values are given,
     * there can be, at most, one result.
     * 
     * @param callback              the callback that handles the results
     * @param keys                  0 to 3 key values to search against
     */
    public void getAttributes(AttributeQueryCallback callback, Serializable ... keys);
    
    /**
     * Set an attribute, overwriting its prior value if it already existed.  null
     * values are treated as unique i.e. if the value set is null then
     * {@link #exists(String...)} will still return true.  If the attribute doesn't
     * exist, it will be created otherwise it will be modified.
     *
     * @param value                 The value to store (can be a collection or null)
     * @param keys                  List of 1 to 3 keys to uniquely identify the attribute
     */
    public void setAttribute(Serializable value, Serializable ... keys);
    /**
     * Create an attribute with an optional value, assuming there is no existing attribute
     * using the same keys.
     * 
     * @param value                 The value to store (can be a collection or null)
     * @param keys                  List of 1 to 3 keys to uniquely identify the attribute
     * 
     * @throws DuplicateAttributeException if the attribute already exists
     */
    public void createAttribute(Serializable value, Serializable ... keys);
    
    /**
     * Update an attribute key whilst preserving the associated value (if any).  If there is
     * no existing key matching the original value, then nothing will happen.
     * 
     * @param keyBefore1            the first part of the original unique key (never null)
     * @param keyBefore2            the second part of the original unique key (null allowed)
     * @param keyBefore3            the third part of the original unique key (null allowed)
     * @param keyAfter1             the first part of the new unique key (never null)
     * @param keyAfter2             the second part of the new unique key (null allowed)
     * @param keyAfter3             the third part of the new unique key (null allowed)
     */
    public void updateOrCreateAttribute(
            Serializable keyBefore1, Serializable keyBefore2, Serializable keyBefore3,
            Serializable keyAfter1, Serializable keyAfter2, Serializable keyAfter3);
    
    /**
     * Remove a specific attribute.
     * 
     * @param keys                  up to 3 keys to uniquely identify the attribute
     */
    public void removeAttribute(Serializable ... keys);
    
    /**
     * Remove all attributes that share a set of keys (in order)
     * 
     * @param keys                  up to 3 keys to identify attributes to remove
     */
    public void removeAttributes(Serializable ... keys);
}