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 java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.EntityManager;
32  import javax.persistence.GeneratedValue;
33  import javax.persistence.GenerationType;
34  import javax.persistence.Id;
35  import javax.persistence.NamedQueries;
36  import javax.persistence.NamedQuery;
37  import javax.persistence.NoResultException;
38  import javax.persistence.Query;
39  import javax.persistence.Table;
40  import org.hibernate.annotations.Index;
41  import org.modeshape.common.util.CheckArg;
42  
43  /**
44   * A WorkspaceEntity represents a workspace that has been create in the store. WorkspaceEntity records are immutable and shared by
45   * one or more enities.
46   */
47  @Entity
48  @Table( name = "DNA_WORKSPACES" )
49  @org.hibernate.annotations.Table( appliesTo = "DNA_WORKSPACES", indexes = @Index( name = "WS_NAME_INX", columnNames = {"NAME"} ) )
50  @NamedQueries( {@NamedQuery( name = "WorkspaceEntity.findAll", query = "select ws from WorkspaceEntity as ws" ),
51      @NamedQuery( name = "WorkspaceEntity.findByName", query = "select ws from WorkspaceEntity as ws where ws.name = :name" ),
52      @NamedQuery( name = "WorkspaceEntity.findAllNames", query = "select ws.name from WorkspaceEntity as ws" )} )
53  public class WorkspaceEntity {
54  
55      @Id
56      @GeneratedValue( strategy = GenerationType.AUTO )
57      private Long id;
58  
59      @Column( name = "NAME", nullable = false, unique = false, length = 128, updatable = false )
60      private String name;
61  
62      /**
63       * 
64       */
65      public WorkspaceEntity() {
66      }
67  
68      /**
69       * @return id
70       */
71      public Long getId() {
72          return id;
73      }
74  
75      /**
76       * @param id Sets id to the specified value.
77       */
78      public void setId( Long id ) {
79          this.id = id;
80      }
81  
82      /**
83       * @return name
84       */
85      public String getName() {
86          return name;
87      }
88  
89      /**
90       * @param name Sets name to the specified value.
91       */
92      public void setName( String name ) {
93          this.name = name;
94      }
95  
96      /**
97       * {@inheritDoc}
98       * 
99       * @see java.lang.Object#hashCode()
100      */
101     @Override
102     public int hashCode() {
103         return id.hashCode();
104     }
105 
106     /**
107      * {@inheritDoc}
108      * 
109      * @see java.lang.Object#equals(java.lang.Object)
110      */
111     @Override
112     public boolean equals( Object obj ) {
113         if (obj == this) return true;
114         if (obj instanceof WorkspaceEntity) {
115             WorkspaceEntity that = (WorkspaceEntity)obj;
116             if (!this.id.equals(that.id)) return false;
117             if (!this.name.equals(that.name)) return false;
118             return true;
119         }
120         return false;
121     }
122 
123     /**
124      * {@inheritDoc}
125      * 
126      * @see java.lang.Object#toString()
127      */
128     @Override
129     public String toString() {
130         return name;
131     }
132 
133     /**
134      * Find an existing workspace by its name, or create and return one if it does not already exist.
135      * 
136      * @param manager the entity manager
137      * @param name the name of the workspace
138      * @return the existing workspace
139      * @throws IllegalArgumentException if the manager or name are null
140      */
141     public static WorkspaceEntity findByName( EntityManager manager,
142                                               String name ) {
143         return findByName(manager, name, true);
144     }
145 
146     /**
147      * Find an existing workspace by its name.
148      * 
149      * @param manager the entity manager
150      * @param name the name of the workspace
151      * @param createIfRequired if the workspace should be persisted if it does not yet exist
152      * @return the existing workspace, or null if one does not exist
153      * @throws IllegalArgumentException if the manager or name are null
154      */
155     public static WorkspaceEntity findByName( EntityManager manager,
156                                               String name,
157                                               boolean createIfRequired ) {
158         CheckArg.isNotNull(manager, "manager");
159         CheckArg.isNotNull(name, "name");
160         Query query = manager.createNamedQuery("WorkspaceEntity.findByName");
161         query.setParameter("name", name);
162         try {
163             return (WorkspaceEntity)query.getSingleResult();
164         } catch (NoResultException e) {
165             if (!createIfRequired) return null;
166             WorkspaceEntity workspace = new WorkspaceEntity();
167             workspace.setName(name);
168             manager.persist(workspace);
169             return workspace;
170         }
171     }
172 
173     /**
174      * Find the set of names for the existing workspaces.
175      * 
176      * @param manager the entity manager
177      * @return the names of the existing workspaces; never null
178      * @throws IllegalArgumentException if the manager or name are null
179      */
180     @SuppressWarnings( "unchecked" )
181     public static Set<String> findAllNames( EntityManager manager ) {
182         CheckArg.isNotNull(manager, "manager");
183         Query query = manager.createNamedQuery("WorkspaceEntity.findAllNames");
184         List<String> names = query.getResultList();
185         return new HashSet<String>(names);
186     }
187 }