001/*
002 * ============================================================================
003 *  Copyright © 2002-2021 by Thomas Thrien.
004 *  All Rights Reserved.
005 * ============================================================================
006 *  Licensed to the public under the agreements of the GNU Lesser General Public
007 *  License, version 3.0 (the "License"). You may obtain a copy of the License at
008 *
009 *       http://www.gnu.org/licenses/lgpl.html
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014 *  License for the specific language governing permissions and limitations
015 *  under the License.
016 */
017
018package org.tquadrat.foundation.config.ap.impl.codebuilders;
019
020import static javax.lang.model.element.Modifier.FINAL;
021import static org.apiguardian.api.API.Status.STABLE;
022import static org.tquadrat.foundation.config.SpecialPropertyType.CONFIG_PROPERTY_SESSION;
023import static org.tquadrat.foundation.config.ap.ConfigAnnotationProcessor.MSG_SessionPropertyMissing;
024
025import org.apiguardian.api.API;
026import org.tquadrat.foundation.annotation.ClassVersion;
027import org.tquadrat.foundation.ap.CodeGenerationError;
028import org.tquadrat.foundation.config.ap.PropertySpec;
029import org.tquadrat.foundation.lang.Objects;
030
031/**
032 *  <p>{@summary The
033 *  {@linkplain org.tquadrat.foundation.config.ap.impl.CodeBuilder code builder implementation}
034 *  for the generation of the code that let the configuration bean
035 *  implement the interface
036 *  {@link org.tquadrat.foundation.config.SessionBeanSpec}.}</p>
037 *
038 *  @version $Id: SessionBeanBuilder.java 933 2021-07-03 13:32:17Z tquadrat $
039 *  @extauthor Thomas Thrien - thomas.thrien@tquadrat.org
040 *  @UMLGraph.link
041 *  @since 0.1.0
042 */
043@ClassVersion( sourceVersion = "$Id: SessionBeanBuilder.java 933 2021-07-03 13:32:17Z tquadrat $" )
044@API( status = STABLE, since = "0.1.0" )
045public final class SessionBeanBuilder extends CodeBuilderBase
046{
047        /*--------------*\
048    ====** Constructors **=====================================================
049        \*--------------*/
050    /**
051     *  Creates a new instance of {@code SessionBeanBuilder}.
052     *
053     *  @param  context The code generator context.
054     */
055    public SessionBeanBuilder( final CodeGeneratorContext context )
056    {
057        super( context );
058    }   //  SessionBeanBuilder()
059
060        /*---------*\
061    ====** Methods **==========================================================
062        \*---------*/
063    /**
064     *  {@inheritDoc}
065     */
066    @Override
067    public final void build()
068    {
069        //---* Get the property *----------------------------------------------
070        final var propertySpec = getConfiguration().getProperty( CONFIG_PROPERTY_SESSION.getPropertyName() )
071            .map( PropertySpec::merge )
072            .orElseThrow( () -> new CodeGenerationError( MSG_SessionPropertyMissing ) );
073        final var field = propertySpec.getFieldName();
074
075        //---* Add the constructor argument *----------------------------------
076        final var argument = getComposer().parameterBuilder( propertySpec.getPropertyType(), "sessionKey", FINAL )
077            .addJavadoc( "The session key." )
078            .build();
079        addConstructorArgument( argument );
080
081        //---* Create the code for the constructor *---------------------------
082        final var code = getComposer().codeBlockBuilder()
083            .add( """
084
085                /*
086                 * Set the session key.
087                 */
088                """ )
089            .addStatement( "$1N = requireNotEmptyArgument( $2N, $2S )", field, argument.name() )
090            .addStaticImport( Objects.class, "requireNotEmptyArgument" )
091            .build();
092        addConstructorCode( code );
093    }   //  build()
094}
095//  class SessionBeanBuilder
096
097/*
098 *  End of File
099 */