001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.tools.makeldif;
028    
029    
030    
031    import java.util.ArrayList;
032    import java.util.LinkedHashMap;
033    import java.util.LinkedHashSet;
034    import java.util.List;
035    
036    import org.opends.server.core.DirectoryServer;
037    import org.opends.server.types.Attribute;
038    import org.opends.server.types.AttributeType;
039    import org.opends.server.types.AttributeValue;
040    import org.opends.server.types.DN;
041    import org.opends.server.types.Entry;
042    import org.opends.server.types.ObjectClass;
043    import org.opends.server.types.RDN;
044    
045    import static org.opends.server.util.StaticUtils.*;
046    
047    
048    
049    /**
050     * This class defines an entry that is generated using a MakeLDIF branch or
051     * template.
052     */
053    public class TemplateEntry
054    {
055      // The branch used to generate this entry (if it is associated with a branch).
056      private Branch branch;
057    
058      // The DN for this template entry, if it is known.
059      private DN dn;
060    
061      // The DN of the parent entry for this template entry, if it is available.
062      private DN parentDN;
063    
064      // The set of attributes associated with this template entry, mapped from the
065      // lowercase name of the attribute to the list of generated values.
066      private LinkedHashMap<AttributeType,ArrayList<TemplateValue>> attributes;
067    
068      // The template used to generate this entry (if it is associated with a
069      // template).
070      private Template template;
071    
072    
073    
074      /**
075       * Creates a new template entry that will be associated with the provided
076       * branch.
077       *
078       * @param  branch  The branch to use when creating this template entry.
079       */
080      public TemplateEntry(Branch branch)
081      {
082        this.branch = branch;
083    
084        dn         = branch.getBranchDN();
085        template   = null;
086        parentDN   = null;
087        attributes = new LinkedHashMap<AttributeType,ArrayList<TemplateValue>>();
088      }
089    
090    
091    
092      /**
093       * Creates a new template entry that will be associated with the provided
094       * template.
095       *
096       * @param  template  The template used to generate this entry.
097       * @param  parentDN  The DN of the parent entry for this template entry.
098       */
099      public TemplateEntry(Template template, DN parentDN)
100      {
101        this.template = template;
102        this.parentDN = parentDN;
103    
104        dn         = null;
105        branch     = null;
106        attributes = new LinkedHashMap<AttributeType,ArrayList<TemplateValue>>();
107      }
108    
109    
110    
111      /**
112       * Retrieves the branch used to generate this entry.
113       *
114       * @return  The branch used to generate this entry, or <CODE>null</CODE> if it
115       *          is associated with a template instead of a branch.
116       */
117      public Branch getBranch()
118      {
119        return branch;
120      }
121    
122    
123    
124      /**
125       * Retrieves the template used to generate this entry.
126       *
127       * @return  The template used to generate this entry, or <CODE>null</CODE> if
128       *          it is associated with a branch instead of a template.
129       */
130      public Template getTemplate()
131      {
132        return template;
133      }
134    
135    
136    
137      /**
138       * Retrieves the DN of the parent entry for this template entry.
139       *
140       * @return  The DN of the parent entry for this template entry, or
141       *          <CODE>null</CODE> if there is no parent DN.
142       */
143      public DN getParentDN()
144      {
145        return parentDN;
146      }
147    
148    
149    
150      /**
151       * Retrieves the DN for this template entry, if it is known.
152       *
153       * @return  The DN for this template entry if it is known, or
154       *          <CODE>null</CODE> if it cannot yet be determined.
155       */
156      public DN getDN()
157      {
158        if (dn == null)
159        {
160          RDN rdn;
161          AttributeType[] rdnAttrs = template.getRDNAttributes();
162          if (rdnAttrs.length == 1)
163          {
164            AttributeType t = rdnAttrs[0];
165            TemplateValue v = getValue(t);
166            if (v == null)
167            {
168              return null;
169            }
170    
171            AttributeValue value = new AttributeValue(t, v.getValue().toString());
172            rdn = new RDN(t, value);
173          }
174          else
175          {
176            String[]         names  = new String[rdnAttrs.length];
177            AttributeValue[] values = new AttributeValue[rdnAttrs.length];
178            for (int i=0; i < rdnAttrs.length; i++)
179            {
180              AttributeType t = rdnAttrs[i];
181              TemplateValue v = getValue(t);
182              if (v == null)
183              {
184                return null;
185              }
186    
187              names[i]  = t.getPrimaryName();
188              values[i] = new AttributeValue(t, v.getValue().toString());
189            }
190    
191            rdn = new RDN(rdnAttrs, names, values);
192          }
193    
194          dn = parentDN.concat(rdn);
195        }
196    
197        return dn;
198      }
199    
200    
201    
202      /**
203       * Indicates whether this entry contains one or more values for the specified
204       * attribute type.
205       *
206       * @param  attributeType  The attribute type for which to make the
207       *                        determination.
208       *
209       * @return  <CODE>true</CODE> if this entry contains one or more values for
210       *          the specified attribute type, or <CODE>false</CODE> if not.
211       */
212      public boolean hasAttribute(AttributeType attributeType)
213      {
214        return attributes.containsKey(attributeType);
215      }
216    
217    
218    
219      /**
220       * Retrieves the value for the specified attribute, if defined.  If the
221       * specified attribute has multiple values, then the first will be returned.
222       *
223       * @param  attributeType  The attribute type for which to retrieve the value.
224       *
225       * @return  The value for the specified attribute, or <CODE>null</CODE> if
226       *          there are no values for that attribute type.
227       */
228      public TemplateValue getValue(AttributeType attributeType)
229      {
230        ArrayList<TemplateValue> valueList = attributes.get(attributeType);
231        if ((valueList == null) || valueList.isEmpty())
232        {
233          return null;
234        }
235        else
236        {
237          return valueList.get(0);
238        }
239      }
240    
241    
242    
243      /**
244       * Retrieves the set of values for the specified attribute, if defined.
245       *
246       * @param  attributeType  The attribute type for which to retrieve the set of
247       *                        values.
248       *
249       * @return  The set of values for the specified attribute, or
250       *          <CODE>null</CODE> if there are no values for that attribute type.
251       */
252      public List<TemplateValue> getValues(AttributeType attributeType)
253      {
254        ArrayList<TemplateValue> valueList = attributes.get(attributeType);
255        return valueList;
256      }
257    
258    
259    
260      /**
261       * Adds the provided template value to this entry.
262       *
263       * @param  value  The value to add to this entry.
264       */
265      public void addValue(TemplateValue value)
266      {
267        ArrayList<TemplateValue> valueList =
268             attributes.get(value.getAttributeType());
269        if (valueList == null)
270        {
271          valueList = new ArrayList<TemplateValue>();
272          valueList.add(value);
273          attributes.put(value.getAttributeType(), valueList);
274        }
275        else
276        {
277          valueList.add(value);
278        }
279      }
280    
281    
282    
283      /**
284       * Retrieves this template entry as an <CODE>Entry</CODE> object.
285       *
286       * @return  The <CODE>Entry</CODE> object for this template entry.
287       */
288      public Entry toEntry()
289      {
290        // Process all of the attributes for this entry.
291        LinkedHashMap<ObjectClass,String> objectClasses =
292             new LinkedHashMap<ObjectClass,String>();
293        LinkedHashMap<AttributeType,List<Attribute>> userAttributes =
294             new LinkedHashMap<AttributeType,List<Attribute>>();
295        LinkedHashMap<AttributeType,List<Attribute>> operationalAttributes =
296             new LinkedHashMap<AttributeType,List<Attribute>>();
297    
298        for (AttributeType t : attributes.keySet())
299        {
300          ArrayList<TemplateValue> valueList = attributes.get(t);
301          if (t.isObjectClassType())
302          {
303            for (TemplateValue v : valueList)
304            {
305              String ocName = toLowerCase(v.getValue().toString());
306              ObjectClass oc = DirectoryServer.getObjectClass(ocName, true);
307              objectClasses.put(oc, ocName);
308            }
309          }
310          else if (t.isOperational())
311          {
312            LinkedHashSet<AttributeValue> values =
313                 new LinkedHashSet<AttributeValue>();
314            for (TemplateValue v : valueList)
315            {
316              values.add(new AttributeValue(t, v.getValue().toString()));
317            }
318    
319            ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
320            attrList.add(new Attribute(t, t.getNameOrOID(), values));
321            operationalAttributes.put(t, attrList);
322          }
323          else
324          {
325            LinkedHashSet<AttributeValue> values =
326                 new LinkedHashSet<AttributeValue>();
327            for (TemplateValue v : valueList)
328            {
329              values.add(new AttributeValue(t, v.getValue().toString()));
330            }
331    
332            ArrayList<Attribute> attrList = new ArrayList<Attribute>(1);
333            attrList.add(new Attribute(t, t.getNameOrOID(), values));
334            userAttributes.put(t, attrList);
335          }
336        }
337    
338        return new Entry(getDN(), objectClasses, userAttributes,
339                         operationalAttributes);
340      }
341    }
342