001    /*
002     *   Copyright (C) Christian Schulte, 2005-206
003     *   All rights reserved.
004     *
005     *   Redistribution and use in source and binary forms, with or without
006     *   modification, are permitted provided that the following conditions
007     *   are met:
008     *
009     *     o Redistributions of source code must retain the above copyright
010     *       notice, this list of conditions and the following disclaimer.
011     *
012     *     o Redistributions in binary form must reproduce the above copyright
013     *       notice, this list of conditions and the following disclaimer in
014     *       the documentation and/or other materials provided with the
015     *       distribution.
016     *
017     *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018     *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019     *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020     *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021     *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022     *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023     *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024     *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026     *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     *
028     *   $JOMC: ModelHelper.java 3838 2011-10-08 20:15:41Z schulte2005 $
029     *
030     */
031    package org.jomc.model.modlet;
032    
033    import java.text.MessageFormat;
034    import java.util.Locale;
035    import java.util.ResourceBundle;
036    import javax.xml.bind.JAXBElement;
037    import org.jomc.model.ModelObject;
038    import org.jomc.model.Modules;
039    import org.jomc.model.ObjectFactory;
040    import org.jomc.modlet.Model;
041    
042    /**
043     * Object management and configuration {@code Model} helper.
044     *
045     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
046     * @version $JOMC: ModelHelper.java 3838 2011-10-08 20:15:41Z schulte2005 $
047     */
048    public abstract class ModelHelper
049    {
050    
051        /** Creates a new {@code ModelHelper} instance. */
052        public ModelHelper()
053        {
054            super();
055        }
056    
057        /**
058         * Gets the {@code Modules} of a {@code Model}.
059         *
060         * @param model The {@code Model} to get {@code Modules} of.
061         *
062         * @return The {@code Modules} of {@code Model} or {@code null}.
063         *
064         * @throws NullPointerException if {@code model} is {@code null}.
065         *
066         * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
067         * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
068         */
069        public static Modules getModules( final Model model )
070        {
071            if ( model == null )
072            {
073                throw new NullPointerException( "model" );
074            }
075    
076            final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );
077            return e != null ? e.getValue() : null;
078        }
079    
080        /**
081         * Sets the {@code Modules} of a {@code Model}.
082         *
083         * @param model The {@code Model} to set {@code modules} of.
084         * @param modules The {@code Modules} to set.
085         *
086         * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
087         * @throws IllegalStateException if {@code model} already holds {@code Modules}.
088         *
089         * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
090         * @see #removeModules(org.jomc.modlet.Model)
091         */
092        public static void setModules( final Model model, final Modules modules )
093        {
094            if ( model == null )
095            {
096                throw new NullPointerException( "model" );
097            }
098            if ( modules == null )
099            {
100                throw new NullPointerException( "modules" );
101            }
102            if ( getModules( model ) != null )
103            {
104                throw new IllegalStateException( getMessage( "illegalState", model.getIdentifier() ) );
105            }
106    
107            model.getAny().add( new ObjectFactory().createModules( modules ) );
108        }
109    
110        /**
111         * Adds {@code Modules} to a {@code Model}.
112         *
113         * @param model The {@code Model} to add {@code modules} to.
114         * @param modules The {@code Modules} to add to {@code model}.
115         *
116         * @throws NullPointerException if {@code model} or {@code modules} is {@code null}.
117         *
118         * @see #removeModules(org.jomc.modlet.Model)
119         * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
120         */
121        public static void addModules( final Model model, final Modules modules )
122        {
123            if ( model == null )
124            {
125                throw new NullPointerException( "model" );
126            }
127            if ( modules == null )
128            {
129                throw new NullPointerException( "modules" );
130            }
131    
132            final Modules current = getModules( model );
133    
134            if ( current != null )
135            {
136                current.getModule().addAll( modules.getModule() );
137            }
138            else
139            {
140                setModules( model, modules );
141            }
142        }
143    
144        /**
145         * Removes {@code Modules} from a {@code Model}.
146         *
147         * @param model The {@code Model} to remove {@code modules} from.
148         *
149         * @throws NullPointerException if {@code model} is {@code null}.
150         *
151         * @see #addModules(org.jomc.modlet.Model, org.jomc.model.Modules)
152         * @see #setModules(org.jomc.modlet.Model, org.jomc.model.Modules)
153         */
154        public static void removeModules( final Model model )
155        {
156            if ( model == null )
157            {
158                throw new NullPointerException( "model" );
159            }
160    
161            final JAXBElement<Modules> e = model.getAnyElement( ModelObject.MODEL_PUBLIC_ID, "modules", Modules.class );
162    
163            if ( e != null )
164            {
165                model.getAny().remove( e );
166            }
167        }
168    
169        private static String getMessage( final String key, final Object... args )
170        {
171            return MessageFormat.format( ResourceBundle.getBundle(
172                ModelHelper.class.getName().replace( '.', '/' ), Locale.getDefault() ).getString( key ), args );
173    
174        }
175    
176    }