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.basic; 025 026 import java.util.List; 027 import javax.persistence.Entity; 028 import javax.persistence.EntityManager; 029 import javax.persistence.Id; 030 import javax.persistence.NamedQueries; 031 import javax.persistence.NamedQuery; 032 import javax.persistence.NoResultException; 033 import javax.persistence.Query; 034 import javax.persistence.Table; 035 import org.hibernate.annotations.Index; 036 037 /** 038 * A record of a reference from one node to another. 039 * 040 * @author Randall Hauch 041 */ 042 @Entity 043 @Table( name = "DNA_BASIC_REFERENCES" ) 044 @org.hibernate.annotations.Table( appliesTo = "DNA_BASIC_REFERENCES", indexes = { 045 @Index( name = "REFINDEX_INX", columnNames = {"WORKSPACE_ID", "FROM_UUID", "TO_UUID"} ), 046 @Index( name = "REFTOUUID_INX", columnNames = {"WORKSPACE_ID", "TO_UUID"} )} ) 047 @NamedQueries( { 048 @NamedQuery( name = "ReferenceEntity.removeReferencesFrom", query = "delete ReferenceEntity where id.workspaceId = :workspaceId and id.fromUuidString = :fromUuid" ), 049 @NamedQuery( name = "ReferenceEntity.removeNonEnforcedReferences", query = "delete ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.fromUuidString not in ( select props.id.uuidString from PropertiesEntity props where props.referentialIntegrityEnforced = true and props.id.workspaceId = :workspaceId )" ), 050 @NamedQuery( name = "ReferenceEntity.countUnresolveReferences", query = "select count(*) from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString not in ( select props.id.uuidString from PropertiesEntity props where props.referentialIntegrityEnforced = true and props.id.workspaceId = :workspaceId )" ), 051 @NamedQuery( name = "ReferenceEntity.getUnresolveReferences", query = "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString not in ( select props.id.uuidString from PropertiesEntity props where props.referentialIntegrityEnforced = true and props.id.workspaceId = :workspaceId )" ), 052 @NamedQuery( name = "ReferenceEntity.findInWorkspace", query = "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId" )} ) 053 public class ReferenceEntity { 054 055 @Id 056 private ReferenceId id; 057 058 /** 059 * 060 */ 061 public ReferenceEntity() { 062 } 063 064 /** 065 * @param id the id 066 */ 067 public ReferenceEntity( ReferenceId id ) { 068 this.id = id; 069 } 070 071 /** 072 * @return id 073 */ 074 public ReferenceId getId() { 075 return id; 076 } 077 078 /** 079 * @param id Sets id to the specified value. 080 */ 081 public void setId( ReferenceId id ) { 082 this.id = id; 083 } 084 085 /** 086 * {@inheritDoc} 087 * 088 * @see java.lang.Object#hashCode() 089 */ 090 @Override 091 public int hashCode() { 092 return id.hashCode(); 093 } 094 095 /** 096 * {@inheritDoc} 097 * 098 * @see java.lang.Object#equals(java.lang.Object) 099 */ 100 @Override 101 public boolean equals( Object obj ) { 102 if (obj == this) return true; 103 if (obj instanceof ReferenceEntity) { 104 ReferenceEntity that = (ReferenceEntity)obj; 105 if (this.getId().equals(that.getId())) return true; 106 } 107 return false; 108 } 109 110 /** 111 * {@inheritDoc} 112 * 113 * @see java.lang.Object#toString() 114 */ 115 @Override 116 public String toString() { 117 return this.id.toString(); 118 } 119 120 /** 121 * Delete all references that start from the node with the supplied UUID. 122 * 123 * @param workspaceId the ID of the workspace; may not be null 124 * @param uuid the UUID of the node from which the references start 125 * @param manager the manager; may not be null 126 * @return the number of deleted references 127 */ 128 public static int deleteReferencesFrom( Long workspaceId, 129 String uuid, 130 EntityManager manager ) { 131 assert manager != null; 132 Query delete = manager.createNamedQuery("ReferenceEntity.removeReferencesFrom"); 133 delete.setParameter("fromUuid", uuid); 134 delete.setParameter("workspaceId", workspaceId); 135 int result = delete.executeUpdate(); 136 manager.flush(); 137 return result; 138 } 139 140 /** 141 * Delete all references (in all workspaces) that start from nodes that do not require enforced referential integrity. 142 * 143 * @param workspaceId the ID of the workspace; may not be null 144 * @param manager the manager; may not be null 145 * @return the number of deleted references 146 */ 147 public static int deleteUnenforcedReferences( Long workspaceId, 148 EntityManager manager ) { 149 assert manager != null; 150 Query delete = manager.createNamedQuery("ReferenceEntity.removeNonEnforcedReferences"); 151 delete.setParameter("workspaceId", workspaceId); 152 int result = delete.executeUpdate(); 153 manager.flush(); 154 return result; 155 } 156 157 /** 158 * Delete all references that start from nodes that do not support enforced referential integrity. 159 * 160 * @param workspaceId the ID of the workspace; may not be null 161 * @param manager the manager; may not be null 162 * @return the number of deleted references 163 */ 164 public static int countAllReferencesResolved( Long workspaceId, 165 EntityManager manager ) { 166 assert manager != null; 167 Query query = manager.createNamedQuery("ReferenceEntity.getUnresolveReferences"); 168 query.setParameter("workspaceId", workspaceId); 169 try { 170 return (Integer)query.getSingleResult(); 171 } catch (NoResultException e) { 172 return 0; 173 } 174 } 175 176 /** 177 * Delete all references that start from nodes that do not support enforced referential integrity. 178 * 179 * @param workspaceId the ID of the workspace; may not be null 180 * @param manager the manager; may not be null 181 * @return the number of deleted references 182 */ 183 @SuppressWarnings( "unchecked" ) 184 public static List<ReferenceEntity> verifyAllReferencesResolved( Long workspaceId, 185 EntityManager manager ) { 186 assert manager != null; 187 Query query = manager.createNamedQuery("ReferenceEntity.getUnresolveReferences"); 188 query.setParameter("workspaceId", workspaceId); 189 return query.getResultList(); 190 } 191 }