001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.connector.store.jpa.model.common;
025    
026    import javax.persistence.Column;
027    import javax.persistence.Entity;
028    import javax.persistence.EntityManager;
029    import javax.persistence.GeneratedValue;
030    import javax.persistence.GenerationType;
031    import javax.persistence.Id;
032    import javax.persistence.NamedQueries;
033    import javax.persistence.NamedQuery;
034    import javax.persistence.NoResultException;
035    import javax.persistence.Query;
036    import javax.persistence.Table;
037    import org.hibernate.annotations.Index;
038    import org.jboss.dna.common.util.CheckArg;
039    
040    /**
041     * A NamespaceEntity represents a namespace that has been used in the store. NamespaceEntity records are immutable and shared by
042     * one or more enities.
043     * 
044     * @author Randall Hauch
045     */
046    @Entity
047    @Table( name = "DNA_NAMESPACES" )
048    @org.hibernate.annotations.Table( appliesTo = "DNA_NAMESPACES", indexes = @Index( name = "NS_URI_INX", columnNames = {"URI"} ) )
049    @NamedQueries( {@NamedQuery( name = "NamespaceEntity.findAll", query = "select ns from NamespaceEntity as ns" ),
050        @NamedQuery( name = "NamespaceEntity.findByUri", query = "select ns from NamespaceEntity as ns where ns.uri = ?1" )} )
051    public class NamespaceEntity {
052    
053        @Id
054        @GeneratedValue( strategy = GenerationType.AUTO )
055        private Long id;
056    
057        @Column( name = "URI", nullable = false, unique = false, length = 512, updatable = false )
058        private String uri;
059    
060        /**
061         * 
062         */
063        public NamespaceEntity() {
064        }
065    
066        /**
067         * @param uri the namespace URI
068         */
069        public NamespaceEntity( String uri ) {
070            setUri(uri);
071        }
072    
073        /**
074         * @return id
075         */
076        public Long getId() {
077            return id;
078        }
079    
080        /**
081         * @param id Sets id to the specified value.
082         */
083        public void setId( Long id ) {
084            this.id = id;
085        }
086    
087        /**
088         * @return uri
089         */
090        public String getUri() {
091            return uri;
092        }
093    
094        /**
095         * @param uri Sets uri to the specified value.
096         */
097        public void setUri( String uri ) {
098            this.uri = uri;
099        }
100    
101        /**
102         * {@inheritDoc}
103         * 
104         * @see java.lang.Object#hashCode()
105         */
106        @Override
107        public int hashCode() {
108            return id.hashCode();
109        }
110    
111        /**
112         * {@inheritDoc}
113         * 
114         * @see java.lang.Object#equals(java.lang.Object)
115         */
116        @Override
117        public boolean equals( Object obj ) {
118            if (obj == this) return true;
119            if (obj instanceof NamespaceEntity) {
120                NamespaceEntity that = (NamespaceEntity)obj;
121                if (!this.id.equals(that.id)) return false;
122                if (!this.uri.equals(that.uri)) return false;
123                return true;
124            }
125            return false;
126        }
127    
128        /**
129         * {@inheritDoc}
130         * 
131         * @see java.lang.Object#toString()
132         */
133        @Override
134        public String toString() {
135            return uri;
136        }
137    
138        /**
139         * Find an existing namespace by its URI, or create and return one if it does not already exist.
140         * 
141         * @param manager the entity manager
142         * @param uri the URI
143         * @return the existing namespace, or null if one does not exist
144         * @throws IllegalArgumentException if the manager or URI are null
145         */
146        public static NamespaceEntity findByUri( EntityManager manager,
147                                                 String uri ) {
148            return findByUri(manager, uri, true);
149        }
150    
151        /**
152         * Find an existing namespace by its URI.
153         * 
154         * @param manager the entity manager
155         * @param uri the URI
156         * @param createIfRequired if the namespace should be persisted if it does not yet exist
157         * @return the existing namespace, or null if one does not exist
158         * @throws IllegalArgumentException if the manager or URI are null
159         */
160        public static NamespaceEntity findByUri( EntityManager manager,
161                                                 String uri,
162                                                 boolean createIfRequired ) {
163            CheckArg.isNotNull(manager, "manager");
164            CheckArg.isNotNull(uri, "uri");
165            Query query = manager.createNamedQuery("NamespaceEntity.findByUri");
166            query.setParameter(1, uri);
167            try {
168                return (NamespaceEntity)query.getSingleResult();
169            } catch (NoResultException e) {
170                if (!createIfRequired) return null;
171                NamespaceEntity namespace = new NamespaceEntity(uri);
172                manager.persist(namespace);
173                return namespace;
174            }
175        }
176    }