001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/context/MapModel.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005     - Department of Geography, University of Bonn -
006     and
007     - lat/lon GmbH -
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035     ----------------------------------------------------------------------------*/
036    package org.deegree.portal.context;
037    
038    import java.util.ArrayList;
039    
040    import java.util.Collections;
041    import java.util.List;
042    
043    import org.deegree.portal.PortalException;
044    
045    /**
046     * TODO add class documentation here
047     * 
048     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
049     * @author last edited by: $Author: apoth $
050     * 
051     * @version $Revision: 20771 $, $Date: 2009-11-12 18:37:45 +0100 (Do, 12. Nov 2009) $
052     */
053    public class MapModel {
054    
055        private List<LayerGroup> layerGroups;
056    
057        /**
058         * 
059         * @param layerGroups
060         */
061        public void setLayerGroups( List<LayerGroup> layerGroups ) {
062            this.layerGroups = layerGroups;
063        }
064    
065        /**
066         * @return the layerGroups
067         */
068        public List<LayerGroup> getLayerGroups() {
069            return layerGroups;
070        }
071    
072        /**
073         * 
074         * @param action
075         * @return list of {@link Layer} selected for the passed action
076         */
077        public List<MMLayer> getLayersSelectedForAction( String action ) {
078            List<MMLayer> tmp = new ArrayList<MMLayer>();
079            getLayersForAction( layerGroups, action, tmp );
080            return Collections.unmodifiableList( tmp );
081        }
082    
083        private void getLayersForAction( List<LayerGroup> lgs, String action, List<MMLayer> collector ) {
084            for ( LayerGroup layerGroup : lgs ) {
085                List<MMLayer> mapModelEntries = layerGroup.getLayers();
086                for ( MMLayer mapModelEntrry : mapModelEntries ) {
087                    if ( mapModelEntrry.getSelectedFor().contains( action ) ) {
088                        collector.add( mapModelEntrry );
089                    }
090                }
091                getLayersForAction( layerGroup.getLayerGroups(), action, collector );
092            }
093        }
094    
095        /**
096         * 
097         * @param mapModelEntry
098         *            {@link MapModelEntry} to be inserted
099         * @param parent
100         *            if <code>null</code> root node of layertree will be used as parent
101         * @param antecessor
102         *            if <code>null</code> layer will be inserted directly underneath its parent
103         * @param first
104         *            if true layer will be inserted as first layer of a group if antecessor == null
105         * @throws Exception
106         */
107        public void insert( final MapModelEntry mapModelEntry, LayerGroup parent, MapModelEntry antecessor, boolean first )
108                                throws Exception {
109    
110            // check if layer already exists in map model
111            walkLayerTree( new MapModelVisitor() {
112    
113                public void visit( MMLayer layer )
114                                        throws Exception {
115                    if ( layer.getIdentifier().equals( mapModelEntry.getIdentifier() ) ) {
116                        throw new PortalException( "layer: " + layer.getTitle() + " already contained in tree" );
117                    }
118                }
119    
120                public void visit( LayerGroup layerGroup )
121                                        throws Exception {
122                    if ( layerGroup.getIdentifier().equals( mapModelEntry.getIdentifier() ) ) {
123                        throw new PortalException( "layergroup: " + layerGroup.getTitle() + " already contained in tree" );
124                    }
125                }
126    
127            } );
128    
129            if ( mapModelEntry instanceof MMLayer ) {
130                insertLayer( (MMLayer) mapModelEntry, parent, antecessor, first );
131            } else if ( mapModelEntry instanceof LayerGroup ) {
132                insertLayerGroup( (LayerGroup) mapModelEntry, parent, antecessor, first );
133            }
134        }
135    
136        /**
137         * 
138         * @param layer
139         * @param parent
140         *            if <code>null</code> root node of layertree will be used as parent
141         * @param antecessor
142         *            if <code>null</code> layer will be inserted directly underneath its parent
143         * @param first
144         *            if true layer will be inserted as first layer of a group if antecessor == null
145         */
146        private void insertLayer( MMLayer layer, LayerGroup parent, MapModelEntry antecessor, boolean first ) {
147            insertLayer( layer, parent, antecessor, layerGroups, first );
148        }
149    
150        private void insertLayer( MMLayer layer, LayerGroup parent, MapModelEntry antecessor, List<LayerGroup> lgs,
151                                  boolean first ) {
152            for ( LayerGroup layerGroup : lgs ) {
153                if ( parent != null && parent.equals( layerGroup ) ) {
154                    layerGroup.insert( layer, antecessor, first );
155                    break;
156                } else {
157                    insertLayer( layer, parent, antecessor, layerGroup.getLayerGroups(), first );
158                }
159            }
160        }
161    
162        /**
163         * 
164         * @param layerGroup
165         * @param parent
166         *            if <code>null</code> root node of layer tree will be used as parent
167         * @param antecessor
168         *            if <code>null</code> layer will be inserted directly underneath its parent
169         * @param first
170         *            if true layer will be inserted as first layer group of a group if antecessor == null
171         */
172        private void insertLayerGroup( LayerGroup layerGroup, LayerGroup parent, MapModelEntry antecessor, boolean first ) {
173            if ( parent == null ) {
174                layerGroups.add( layerGroup );
175            }
176            insertLayerGroup( layerGroup, parent, antecessor, layerGroups, first );
177        }
178    
179        private void insertLayerGroup( LayerGroup lg, LayerGroup parent, MapModelEntry antecessor, List<LayerGroup> lgs,
180                                       boolean first ) {
181            for ( LayerGroup layerGroup : lgs ) {
182                if ( parent != null && parent.equals( layerGroup ) ) {
183                    layerGroup.insert( lg, antecessor, first );
184                    break;
185                } else {
186                    insertLayerGroup( lg, parent, antecessor, layerGroup.getLayerGroups(), first );
187                }
188            }
189        }
190    
191        /**
192         * 
193         * @param visitor
194         * @throws Exception
195         */
196        public void walkLayerTree( MapModelVisitor visitor )
197                                throws Exception {
198            for ( LayerGroup layerGroup : layerGroups ) {
199                applyVisitor( layerGroup, visitor );
200            }
201        }
202    
203        private void applyVisitor( LayerGroup layerGroup, MapModelVisitor visitor )
204                                throws Exception {
205            visitor.visit( layerGroup );
206            List<MapModelEntry> entries = layerGroup.getMapModelEntries();
207            for ( MapModelEntry entry : entries ) {
208                if ( entry instanceof MMLayer ) {
209                    visitor.visit( (MMLayer) entry );
210                } else {
211                    applyVisitor( (LayerGroup) entry, visitor );
212                }
213            }
214        }
215    
216        /**
217         * 
218         * @param identifier
219         * @return {@link MapModelEntry} matching passed identifier
220         */
221        public MapModelEntry getMapModelEntryByIdentifier( final String identifier ) {
222            final List<MapModelEntry> list = new ArrayList<MapModelEntry>();
223            try {
224                walkLayerTree( new MapModelVisitor() {
225    
226                    public void visit( LayerGroup layerGroup )
227                                            throws Exception {
228                        if ( identifier.equals( layerGroup.getIdentifier() ) ) {
229                            list.add( layerGroup );
230                        }
231    
232                    }
233    
234                    public void visit( MMLayer layer )
235                                            throws Exception {                    
236                        if ( identifier.equals( layer.getIdentifier() ) ) {
237                            list.add( layer );
238                        }
239    
240                    }
241                } );
242            } catch ( Exception e ) {
243                e.printStackTrace();
244            }
245            if ( list.size() > 0 ) {
246                return list.get( 0 );
247            } else {
248                return null;
249            }
250        }
251    }