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 java.util.HashSet; 027 import java.util.List; 028 import java.util.Set; 029 import javax.persistence.Column; 030 import javax.persistence.Entity; 031 import javax.persistence.EntityManager; 032 import javax.persistence.GeneratedValue; 033 import javax.persistence.GenerationType; 034 import javax.persistence.Id; 035 import javax.persistence.NamedQueries; 036 import javax.persistence.NamedQuery; 037 import javax.persistence.NoResultException; 038 import javax.persistence.Query; 039 import javax.persistence.Table; 040 import org.hibernate.annotations.Index; 041 import org.jboss.dna.common.util.CheckArg; 042 043 /** 044 * A WorkspaceEntity represents a workspace that has been create in the store. WorkspaceEntity records are immutable and shared by 045 * one or more enities. 046 * 047 * @author Randall Hauch 048 */ 049 @Entity 050 @Table( name = "DNA_WORKSPACES" ) 051 @org.hibernate.annotations.Table( appliesTo = "DNA_WORKSPACES", indexes = @Index( name = "WS_NAME_INX", columnNames = {"NAME"} ) ) 052 @NamedQueries( {@NamedQuery( name = "WorkspaceEntity.findAll", query = "select ws from WorkspaceEntity as ws" ), 053 @NamedQuery( name = "WorkspaceEntity.findByName", query = "select ws from WorkspaceEntity as ws where ws.name = :name" ), 054 @NamedQuery( name = "WorkspaceEntity.findAllNames", query = "select ws.name from WorkspaceEntity as ws" )} ) 055 public class WorkspaceEntity { 056 057 @Id 058 @GeneratedValue( strategy = GenerationType.AUTO ) 059 private Long id; 060 061 @Column( name = "NAME", nullable = false, unique = false, length = 128, updatable = false ) 062 private String name; 063 064 /** 065 * 066 */ 067 public WorkspaceEntity() { 068 } 069 070 /** 071 * @return id 072 */ 073 public Long getId() { 074 return id; 075 } 076 077 /** 078 * @param id Sets id to the specified value. 079 */ 080 public void setId( Long id ) { 081 this.id = id; 082 } 083 084 /** 085 * @return name 086 */ 087 public String getName() { 088 return name; 089 } 090 091 /** 092 * @param name Sets name to the specified value. 093 */ 094 public void setName( String name ) { 095 this.name = name; 096 } 097 098 /** 099 * {@inheritDoc} 100 * 101 * @see java.lang.Object#hashCode() 102 */ 103 @Override 104 public int hashCode() { 105 return id.hashCode(); 106 } 107 108 /** 109 * {@inheritDoc} 110 * 111 * @see java.lang.Object#equals(java.lang.Object) 112 */ 113 @Override 114 public boolean equals( Object obj ) { 115 if (obj == this) return true; 116 if (obj instanceof WorkspaceEntity) { 117 WorkspaceEntity that = (WorkspaceEntity)obj; 118 if (!this.id.equals(that.id)) return false; 119 if (!this.name.equals(that.name)) return false; 120 return true; 121 } 122 return false; 123 } 124 125 /** 126 * {@inheritDoc} 127 * 128 * @see java.lang.Object#toString() 129 */ 130 @Override 131 public String toString() { 132 return name; 133 } 134 135 /** 136 * Find an existing workspace by its name, or create and return one if it does not already exist. 137 * 138 * @param manager the entity manager 139 * @param name the name of the workspace 140 * @return the existing workspace 141 * @throws IllegalArgumentException if the manager or name are null 142 */ 143 public static WorkspaceEntity findByName( EntityManager manager, 144 String name ) { 145 return findByName(manager, name, true); 146 } 147 148 /** 149 * Find an existing workspace by its name. 150 * 151 * @param manager the entity manager 152 * @param name the name of the workspace 153 * @param createIfRequired if the workspace should be persisted if it does not yet exist 154 * @return the existing workspace, or null if one does not exist 155 * @throws IllegalArgumentException if the manager or name are null 156 */ 157 public static WorkspaceEntity findByName( EntityManager manager, 158 String name, 159 boolean createIfRequired ) { 160 CheckArg.isNotNull(manager, "manager"); 161 CheckArg.isNotNull(name, "name"); 162 Query query = manager.createNamedQuery("WorkspaceEntity.findByName"); 163 query.setParameter("name", name); 164 try { 165 return (WorkspaceEntity)query.getSingleResult(); 166 } catch (NoResultException e) { 167 if (!createIfRequired) return null; 168 WorkspaceEntity workspace = new WorkspaceEntity(); 169 workspace.setName(name); 170 manager.persist(workspace); 171 return workspace; 172 } 173 } 174 175 /** 176 * Find the set of names for the existing workspaces. 177 * 178 * @param manager the entity manager 179 * @return the names of the existing workspaces; never null 180 * @throws IllegalArgumentException if the manager or name are null 181 */ 182 @SuppressWarnings( "unchecked" ) 183 public static Set<String> findAllNames( EntityManager manager ) { 184 CheckArg.isNotNull(manager, "manager"); 185 Query query = manager.createNamedQuery("WorkspaceEntity.findAllNames"); 186 List<String> names = query.getResultList(); 187 return new HashSet<String>(names); 188 } 189 }