001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.blog.repository;
017
018import org.ametys.plugins.repository.AmetysObject;
019import org.ametys.plugins.repository.AmetysObjectIterable;
020import org.ametys.plugins.repository.AmetysObjectIterator;
021import org.ametys.plugins.repository.AmetysRepositoryException;
022import org.ametys.plugins.repository.metadata.CompositeMetadata;
023import org.ametys.web.repository.page.Page;
024import org.ametys.web.repository.page.Zone;
025import org.ametys.web.repository.page.ZoneItem;
026
027/**
028 * Wrapper to present a modifiable zone into a unmodifiable one.
029 */
030public class StaticZone implements Zone
031{
032    
033    /** The wrapped zone. */
034    protected Zone _zone;
035    
036    /**
037     * Construct a static zone, wrapping a given zone.
038     * @param zone the zone to wrap.
039     */
040    public StaticZone(Zone zone)
041    {
042        _zone = zone;
043    }
044    
045    @Override
046    public CompositeMetadata getMetadataHolder()
047    {
048        return _zone.getMetadataHolder();
049    }
050
051    @Override
052    public String getName() throws AmetysRepositoryException
053    {
054        return _zone.getName();
055    }
056
057    @Override
058    public String getPath() throws AmetysRepositoryException
059    {
060        return _zone.getPath();
061    }
062
063    @Override
064    public String getId() throws AmetysRepositoryException
065    {
066        return _zone.getId();
067    }
068
069    @Override
070    public <A extends AmetysObject> A getParent() throws AmetysRepositoryException
071    {
072        return _zone.getParent();
073    }
074
075    @Override
076    public String getParentPath() throws AmetysRepositoryException
077    {
078        return _zone.getParentPath();
079    }
080
081    @Override
082    public AmetysObjectIterable< ? extends ZoneItem> getZoneItems() throws AmetysRepositoryException
083    {
084        return new StaticZoneItemIterable(_zone.getZoneItems());
085    }
086
087    @Override
088    public Page getPage()
089    {
090        return _zone.getPage();
091    }
092    
093    /**
094     * Static zone item iterable.
095     */
096    protected class StaticZoneItemIterable implements AmetysObjectIterable<StaticZoneItem>
097    {
098        
099        /** The wrapped iterable. */
100        protected AmetysObjectIterable<? extends ZoneItem> _iterable;
101        
102        /**
103         * Construct a StaticZoneItemIterable, wrapping an iterable.
104         * @param iterable zone item iterable.
105         */
106        public StaticZoneItemIterable(AmetysObjectIterable<? extends ZoneItem> iterable)
107        {
108            _iterable = iterable;
109        }
110        
111        @Override
112        public AmetysObjectIterator<StaticZoneItem> iterator()
113        {
114            return new StaticZoneItemIterator(_iterable.iterator());
115        }
116
117        @Override
118        public long getSize()
119        {
120            return _iterable.getSize();
121        }
122
123        @Override
124        public void close()
125        {
126            _iterable.close();
127        }
128        
129    }
130    
131    class StaticZoneItemIterator implements AmetysObjectIterator<StaticZoneItem>
132    {
133        AmetysObjectIterator<? extends ZoneItem> _it;
134        
135        public StaticZoneItemIterator(AmetysObjectIterator<? extends ZoneItem> it)
136        {
137            _it = it;
138        }
139        
140        public boolean hasNext()
141        {
142            return _it.hasNext();
143        }
144
145        public StaticZoneItem next()
146        {
147            return new StaticZoneItem(_it.next());
148        }
149        
150        public void skip(long skipNum)
151        {
152            _it.skip(skipNum);
153        }
154
155        public long getPosition()
156        {
157            return _it.getPosition();
158        }
159
160        public long getSize()
161        {
162            return _it.getSize();
163        }
164        
165        public void remove()
166        {
167            _it.remove();
168        }
169    }
170}