001/*
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: Guid.java,v 1.4 2009/01/28 05:34:50 ww203982 Exp $
026 *
027 * Portions Copyright 2015 ForgeRock AS.
028 */
029
030package com.iplanet.ums;
031
032import org.forgerock.opendj.ldap.DN;
033
034/**
035 * This class represents an LDAP entry and it provides
036 * access to the ID (dn) and GUID of the given name. Every persistent object
037 * (that is, entry in the Directory server) has a GUID (Globally Unique
038 * Identifier) associated with it. Upon doing a getGuid() (getGuid() is a method
039 * in the PersistentObject class) on an LDAP entry, this GUID object would be
040 * returned (not a DN string or a guid in the LDAP sense). Methods of the Guid
041 * object could then be used to get the actual DN, etc.
042 *
043 * @supported.all.api
044 */
045public class Guid {
046
047    // holds the LDAP dn for the LDAP entry associated with this Guid object
048    private DN _dn;
049
050    // holds the unique ID for the LDAP entry associated with this Guid object
051    private long _uniqueId;
052
053    /**
054     * Constructs a Guid object from the specified distinguished name.
055     * 
056     * @param dn
057     *            string representation of the distinguished name
058     */
059    public Guid(String dn) {
060        _dn = DN.valueOf(dn);
061        _uniqueId = -1;
062    }
063
064    /**
065     * Constructs a Guid object from the specified unique ID.
066     * 
067     * @param id
068     *            unique ID
069     */
070    public Guid(long id) {
071        _dn = DN.valueOf("");
072        _uniqueId = id;
073    }
074
075    /**
076     * Constructs a Guid object from the specified distinguished name and unique
077     * ID.
078     * 
079     * @param dn
080     *            string representation of the distinguished name
081     * @param id
082     *            unique ID
083     */
084    public Guid(String dn, long id) {
085        _dn = DN.valueOf(dn);
086        _uniqueId = id;
087    }
088
089    /**
090     * Returns the string representation of the distinguished name.
091     * 
092     * @return the string representation of the distinguished name
093     */
094    public String getDn() {
095        return _dn.toString();
096    }
097
098    /**
099     * Sets the dn for this object. Note that the value is not persisted in
100     * LDAP.
101     * 
102     * @param dn
103     *            string representation of the distinguished name
104     */
105    protected void setDn(String dn) {
106        _dn = DN.valueOf(dn);
107    }
108
109    /**
110     * Returns the nsuniqueID name in the Guid object associated with an LDAP
111     * entry.
112     * 
113     * @return the nsuniqueID name in the Guid object associated with an LDAP
114     *         entry
115     */
116    public long getId() {
117        return _uniqueId;
118    }
119
120    /**
121     * Sets the nsuniqueID name in the Guid object associated with an LDAP entry
122     * Note that the value is not persisted in LDAP.
123     * 
124     * @param id
125     *            the nsuniqueID name
126     */
127    protected void setId(long id) {
128        _uniqueId = id;
129    }
130
131    /**
132     * Determines if the current Guid is equal to the specified Guid.
133     * 
134     * @param guid
135     *            Guid to compare against the current Guid
136     * @return true if the two Guids are the same
137     */
138    public boolean equals(Guid guid) {
139        return _dn.equals(DN.valueOf(guid.getDn()));
140    }
141
142    /**
143     * Compares two dn's for equality.
144     * 
145     * @param dn1
146     *            the first dn to compare
147     * @param dn2
148     *            the second dn to compare
149     * @return true if the two dn's are equal
150     */
151    static boolean equals(String dn1, String dn2) {
152        return DN.valueOf(dn1).equals(DN.valueOf(dn2));
153    }
154
155    /**
156     * Returns the String form of this Guid object.
157     * 
158     * @return the string representation of the Guid
159     */
160    public String toString() {
161        return _dn.toString();
162        // For future use
163        // StringBuffer buff = new StringBuffer();
164        // buff.append("DN : " + _dn + "\n");
165        // buff.append("ID : " + _uniqueId + "\n");
166        // return buff.toString();
167    }
168}