001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/model/feature/XLinkedFeatureProperty.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.model.feature;
037    
038    import org.deegree.datatypes.QualifiedName;
039    
040    /**
041     * Feature property instance that does not specify it's content inline, but references a feature
042     * instance via an XLink.
043     *
044     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
045     * @author last edited by: $Author: mschneider $
046     *
047     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
048     */
049    public class XLinkedFeatureProperty implements FeatureProperty {
050    
051        private QualifiedName name;
052    
053        private String targetFeatureId;
054    
055        private Feature targetFeature;
056    
057        /**
058         * Creates a new instance of <code>XLinkedFeatureProperty</code> from the given parameters.
059         * <p>
060         * NOTE: After creating, this property has no value. The reference to the target feature has to
061         * be resolved first by calling #setValue(java.lang.Object).
062         *
063         * @see #setValue(java.lang.Object)
064         *
065         * @param name
066         *            feature name
067         * @param targetFeatureId
068         *            id of the feature that this property contains
069         */
070        public XLinkedFeatureProperty( QualifiedName name, String targetFeatureId ) {
071            this.name = name;
072            this.targetFeatureId = targetFeatureId;
073        }
074    
075        /**
076         * Returns the name of the property.
077         *
078         * @return the name of the property.
079         */
080        public QualifiedName getName() {
081            return this.name;
082        }
083    
084        /**
085         * Returns the value of the property.
086         *
087         * @return the value of the property.
088         */
089        public Object getValue() {
090            checkResolved();
091            return this.targetFeature;
092        }
093    
094        /**
095         * Returns the value of the property.
096         *
097         * @return the value of the property.
098         */
099        public Object getValue( Object defaultValue ) {
100            checkResolved();
101            if ( this.targetFeature == null ) {
102                return defaultValue;
103            }
104            return this.targetFeature;
105        }
106    
107        /**
108         * Sets the target feature instance that this feature property refers to.
109         *
110         * @param value
111         *            feature instance that this feature property refers to
112         * @throws RuntimeException
113         *             if the reference has already been resolved
114         */
115        public void setValue( Object value ) {
116            if ( this.targetFeature != null ) {
117                String msg = Messages.format( "ERROR_REFERENCE_ALREADY_RESOLVED", this.targetFeatureId );
118                throw new RuntimeException( msg );
119            }
120            this.targetFeature = (Feature) value;
121        }
122    
123        /*
124         * (non-Javadoc)
125         *
126         * @see org.deegree.model.feature.FeatureProperty#getOwner()
127         */
128        public Feature getOwner() {
129            return null;
130        }
131    
132        /**
133         * Returns the feature id of the target feature.
134         *
135         * @return the feature id of the target feature.
136         */
137        public String getTargetFeatureId() {
138            return this.targetFeatureId;
139        }
140    
141        /**
142         * Ensures that the reference to the target feature has been resolved.
143         *
144         * @throws RuntimeException
145         *             if the reference has not been resolved
146         */
147        private void checkResolved() {
148            if ( this.targetFeature == null ) {
149                String msg = Messages.format( "ERROR_XLINK_NOT_RESOLVED", this.targetFeatureId );
150                throw new RuntimeException( msg );
151            }
152        }
153    }