View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.connector.store.jpa.model.common;
25  
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.EntityManager;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.GenerationType;
31  import javax.persistence.Id;
32  import javax.persistence.NamedQueries;
33  import javax.persistence.NamedQuery;
34  import javax.persistence.NoResultException;
35  import javax.persistence.Query;
36  import javax.persistence.Table;
37  import org.hibernate.annotations.Index;
38  import org.modeshape.common.util.CheckArg;
39  
40  /**
41   * A NamespaceEntity represents a namespace that has been used in the store. NamespaceEntity records are immutable and shared by
42   * one or more enities.
43   */
44  @Entity
45  @Table( name = "DNA_NAMESPACES" )
46  @org.hibernate.annotations.Table( appliesTo = "DNA_NAMESPACES", indexes = @Index( name = "NS_URI_INX", columnNames = {"URI"} ) )
47  @NamedQueries( {@NamedQuery( name = "NamespaceEntity.findAll", query = "select ns from NamespaceEntity as ns" ),
48      @NamedQuery( name = "NamespaceEntity.findByUri", query = "select ns from NamespaceEntity as ns where ns.uri = ?1" )} )
49  public class NamespaceEntity {
50  
51      @Id
52      @GeneratedValue( strategy = GenerationType.AUTO )
53      private Long id;
54  
55      @Column( name = "URI", nullable = true, unique = false, length = 512, updatable = false )
56      private String uri;
57  
58      /**
59       * 
60       */
61      public NamespaceEntity() {
62      }
63  
64      /**
65       * @param uri the namespace URI
66       */
67      public NamespaceEntity( String uri ) {
68          setUri(uri);
69      }
70  
71      /**
72       * @return id
73       */
74      public Long getId() {
75          return id;
76      }
77  
78      /**
79       * @param id Sets id to the specified value.
80       */
81      public void setId( Long id ) {
82          this.id = id;
83      }
84  
85      /**
86       * @return uri
87       */
88      public String getUri() {
89          return uri;
90      }
91  
92      /**
93       * @param uri Sets uri to the specified value.
94       */
95      public void setUri( String uri ) {
96          this.uri = uri;
97      }
98  
99      /**
100      * {@inheritDoc}
101      * 
102      * @see java.lang.Object#hashCode()
103      */
104     @Override
105     public int hashCode() {
106         return id.hashCode();
107     }
108 
109     /**
110      * {@inheritDoc}
111      * 
112      * @see java.lang.Object#equals(java.lang.Object)
113      */
114     @Override
115     public boolean equals( Object obj ) {
116         if (obj == this) return true;
117         if (obj instanceof NamespaceEntity) {
118             NamespaceEntity that = (NamespaceEntity)obj;
119             if (!this.id.equals(that.id)) return false;
120             if (!this.uri.equals(that.uri)) return false;
121             return true;
122         }
123         return false;
124     }
125 
126     /**
127      * {@inheritDoc}
128      * 
129      * @see java.lang.Object#toString()
130      */
131     @Override
132     public String toString() {
133         return uri;
134     }
135 
136     /**
137      * Find an existing namespace by its URI, or create and return one if it does not already exist.
138      * 
139      * @param manager the entity manager
140      * @param uri the URI
141      * @return the existing namespace, or null if one does not exist
142      * @throws IllegalArgumentException if the manager or URI are null
143      */
144     public static NamespaceEntity findByUri( EntityManager manager,
145                                              String uri ) {
146         return findByUri(manager, uri, true);
147     }
148 
149     /**
150      * Find an existing namespace by its URI.
151      * 
152      * @param manager the entity manager
153      * @param uri the URI
154      * @param createIfRequired if the namespace should be persisted if it does not yet exist
155      * @return the existing namespace, or null if one does not exist
156      * @throws IllegalArgumentException if the manager or URI are null
157      */
158     public static NamespaceEntity findByUri( EntityManager manager,
159                                              String uri,
160                                              boolean createIfRequired ) {
161         CheckArg.isNotNull(manager, "manager");
162         CheckArg.isNotNull(uri, "uri");
163         Query query = manager.createNamedQuery("NamespaceEntity.findByUri");
164         query.setParameter(1, uri);
165         try {
166             return (NamespaceEntity)query.getSingleResult();
167         } catch (NoResultException e) {
168             if (!createIfRequired) return null;
169             NamespaceEntity namespace = new NamespaceEntity(uri);
170             manager.persist(namespace);
171             return namespace;
172         }
173     }
174 }