/**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: IdCacheBlock.java,v 1.5 2008/06/25 05:43:30 qcheng Exp $
 *
 * Portions Copyrighted 2016 ForgeRock AS.
 */

package com.sun.identity.idm.common;

import java.util.Set;

import com.iplanet.am.sdk.common.CacheBlockBase;
import com.sun.identity.shared.debug.Debug;
import com.iplanet.am.util.SystemProperties;

/**
 * This class represents the value part stored in the cache. Each IdCacheBlock
 * object would represent a Directory entry. It caches the attributes
 * corresponding to that entry. It also keeps track of red other details such as
 * the Organization DN for the entry.
 * 
 * <p>
 * Also, this cache block can be used to serve as dummy block representing a
 * non-existent directory entry (negative caching). This prevents making
 * un-necessary directory calls for non-existent directory entries.
 * 
 * <p>
 * Since the attributes that can be retrieved depends on the principal
 * performing the operation (ACI's set), the result set would vary. The
 * attributes that are returned are the ones that are readable by the principal.
 * Each cache block keeps account of these differences in result sets by storing
 * all the attributes readable (and writable) on a per principal basis. This
 * information is stored in a PrincipalAccess object. In order to avoid
 * duplicate copy of the values, the all attribute values are not cached per
 * principal. A single copy of the attributes is stored in the IdCacheBlock
 * object. Also this copy of attributes stored in the cache block keeps track of
 * non-existent directory attributes (invalid attributes). This would also
 * prevent un-necessary directory calls for non-existent entry attributes.
 * 
 * The attribute copy is dirtied by removing the entries which get modified.
 */
public class IdCacheBlock extends CacheBlockBase {

    // CONSTANTS
    protected static final String ENTRY_EXPIRATION_ENABLED_KEY = 
        "com.sun.identity.idm.cache.entry.expire.enabled";

    protected static final String ENTRY_USER_EXPIRE_TIME_KEY = 
        "com.sun.identity.idm.cache.entry.user.expire.time";

    protected static final String ENTRY_DEFAULT_EXPIRE_TIME_KEY = 
        "com.sun.identity.idm.cache.entry.default.expire.time";

    protected static boolean ENTRY_EXPIRATION_ENABLED_FLAG = false;

    protected static long ENTRY_USER_EXPIRE_TIME;

    protected static long ENTRY_DEFAULT_EXPIRE_TIME;

    private static final Debug DEBUG = Debug.getInstance("amIdm");

    // Variable to store the fully qualified names for identities
    private Set fullyQualifiedNames;

    static {
        ENTRY_EXPIRATION_ENABLED_FLAG = SystemProperties.getAsBoolean(ENTRY_EXPIRATION_ENABLED_KEY, false);
        if (ENTRY_EXPIRATION_ENABLED_FLAG) {
            // Read the expiration times for user and non user entries, convert to milliseconds
            ENTRY_USER_EXPIRE_TIME = SystemProperties.getAsInt(ENTRY_USER_EXPIRE_TIME_KEY, 15) * 60000;
            ENTRY_DEFAULT_EXPIRE_TIME = SystemProperties.getAsInt(ENTRY_DEFAULT_EXPIRE_TIME_KEY, 30) * 60000;
        }
    }

    public Set getFullyQualifiedNames() {
        return (fullyQualifiedNames);
    }

    public void setFullyQualifiedNames(Set fqn) {
        fullyQualifiedNames = fqn;
    }

    public boolean isEntryExpirationEnabled() {
        return ENTRY_EXPIRATION_ENABLED_FLAG;
    }

    public long getUserEntryExpirationTime() {
        return ENTRY_USER_EXPIRE_TIME;
    }

    public long getDefaultEntryExpirationTime() {
        return ENTRY_DEFAULT_EXPIRE_TIME;
    }

    public Debug getDebug() {
        return DEBUG;
    }

    public IdCacheBlock(String entryDN, boolean validEntry) {
        super(entryDN, validEntry);
    }

    public IdCacheBlock(String entryDN, String orgDN, boolean validEntry) {
        super(entryDN, orgDN, validEntry);
    }
}
