/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *  
 *    http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License. 
 *  
 */
package org.apache.directory.server.dns.store.operations;


import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchResult;

import org.apache.directory.server.dns.messages.QuestionRecord;
import org.apache.directory.server.dns.messages.RecordClass;
import org.apache.directory.server.dns.messages.RecordType;
import org.apache.directory.server.dns.messages.ResourceRecord;
import org.apache.directory.server.dns.messages.ResourceRecordModifier;
import org.apache.directory.server.dns.store.DnsAttribute;
import org.apache.directory.server.protocol.shared.store.ContextOperation;


/**
 * A JNDI context operation for looking up a Resource Record with flat attributes.
 *
 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
 * @version $Rev$, $Date$
 */
public class GetFlatRecord implements ContextOperation
{
    private static final long serialVersionUID = 4931303293468915435L;

    /** The name of the question to get. */
    private final QuestionRecord question;


    /**
     * Creates the action to be used against the embedded JNDI provider.
     */
    public GetFlatRecord(QuestionRecord question)
    {
        this.question = question;
    }


    /**
     * Note that the base is a relative path from the exiting context.
     * It is not a DN.
     */
    public Object execute( DirContext ctx, Name base ) throws Exception
    {
        if ( question == null )
        {
            return null;
        }

        Attributes matchAttrs = new BasicAttributes( true );

        matchAttrs.put( new BasicAttribute( DnsAttribute.NAME, question.getDomainName() ) );
        matchAttrs.put( new BasicAttribute( DnsAttribute.TYPE, question.getRecordType().getCode() ) );
        matchAttrs.put( new BasicAttribute( DnsAttribute.CLASS, question.getRecordClass().getCode() ) );

        ResourceRecord record = null;

        NamingEnumeration answer = ctx.search( base, matchAttrs );

        if ( answer.hasMore() )
        {
            SearchResult result = ( SearchResult ) answer.next();

            Attributes attrs = result.getAttributes();

            if ( attrs == null )
            {
                return null;
            }

            record = getRecord( attrs );
        }

        return record;
    }


    /**
     * Marshals a RecordStoreEntry from an Attributes object.
     *
     * @param attrs the attributes of the DNS question
     * @return the entry for the question
     * @throws NamingException if there are any access problems
     */
    private ResourceRecord getRecord( Attributes attrs ) throws NamingException
    {
        ResourceRecordModifier modifier = new ResourceRecordModifier();

        Attribute attr;

        String dnsName = ( attr = attrs.get( DnsAttribute.NAME ) ) != null ? ( String ) attr.get() : null;
        String dnsType = ( attr = attrs.get( DnsAttribute.TYPE ) ) != null ? ( String ) attr.get() : null;
        String dnsClass = ( attr = attrs.get( DnsAttribute.CLASS ) ) != null ? ( String ) attr.get() : null;
        String dnsTtl = ( attr = attrs.get( DnsAttribute.TTL ) ) != null ? ( String ) attr.get() : null;

        modifier.setDnsName( dnsName );
        modifier.setDnsType( RecordType.getTypeByName( dnsType ) );
        modifier.setDnsClass( RecordClass.getTypeByName( dnsClass ) );
        modifier.setDnsTtl( Integer.parseInt( dnsTtl ) );

        NamingEnumeration ids = attrs.getIDs();

        while ( ids.hasMore() )
        {
            String id = ( String ) ids.next();
            modifier.put( id, ( String ) attrs.get( id ).get() );
        }

        return modifier.getEntry();
    }
}

@@ -1074,101 +1074,8 @@
 es;%0A
-import javax.naming.directory.BasicAttribute;%0Aimport javax.naming.directory.BasicAttributes;%0A
 impo
@@ -1612,16 +1612,143 @@
 ration;%0A
+import org.apache.directory.shared.ldap.message.AttributeImpl;%0Aimport org.apache.directory.shared.ldap.message.AttributesImpl;%0A
 %0A%0A/**%0A *
@@ -2646,29 +2646,24 @@
 Attrs = new 
-Basic
 Attributes( 
@@ -2660,16 +2660,20 @@
 tributes
+Impl
 ( true )
@@ -2695,37 +2695,32 @@
 hAttrs.put( new 
-Basic
 Attribute( DnsAt
@@ -2704,32 +2704,36 @@
 t( new Attribute
+Impl
 ( DnsAttribute.N
@@ -2787,37 +2787,32 @@
 hAttrs.put( new 
-Basic
 Attribute( DnsAt
@@ -2796,32 +2796,36 @@
 t( new Attribute
+Impl
 ( DnsAttribute.T
@@ -2901,13 +2901,8 @@
 new 
-Basic
 Attr
@@ -2906,16 +2906,20 @@
 ttribute
+Impl
 ( DnsAtt

Patch for DIRSERVER-783. - No more BasicAttribute and BasicAttributes. Use AttributeImpl and AttributesImpl now. - No more ModificationItem. use ModificationItemImpl now - lot of refactoring of imports : no more import xxx.*;