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.basic;
25  
26  import java.util.Collection;
27  import java.util.List;
28  import javax.persistence.Entity;
29  import javax.persistence.EntityManager;
30  import javax.persistence.Id;
31  import javax.persistence.NamedQueries;
32  import javax.persistence.NamedQuery;
33  import javax.persistence.NoResultException;
34  import javax.persistence.Query;
35  import javax.persistence.Table;
36  import org.hibernate.annotations.Index;
37  
38  /**
39   * A record of a reference from one node to another.
40   */
41  @Entity
42  @Table( name = "DNA_BASIC_REFERENCES" )
43  @org.hibernate.annotations.Table( appliesTo = "DNA_BASIC_REFERENCES", indexes = {
44      @Index( name = "REFINDEX_INX", columnNames = {"WORKSPACE_ID", "FROM_UUID", "TO_UUID"} ),
45      @Index( name = "REFTOUUID_INX", columnNames = {"WORKSPACE_ID", "TO_UUID"} )} )
46  @NamedQueries( {
47      @NamedQuery( name = "ReferenceEntity.removeReferencesFrom", query = "delete ReferenceEntity where id.workspaceId = :workspaceId and id.fromUuidString = :fromUuid" ),
48      @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 )" ),
49      @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 )" ),
50      @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 )" ),
51      @NamedQuery( name = "ReferenceEntity.findInWorkspace", query = "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId" ),
52      @NamedQuery( name = "ReferenceEntity.getInwardReferencesForList", query = "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString in (:toUuidList)" )} )
53  public class ReferenceEntity {
54  
55      @Id
56      private ReferenceId id;
57  
58      /**
59       * 
60       */
61      public ReferenceEntity() {
62      }
63  
64      /**
65       * @param id the id
66       */
67      public ReferenceEntity( ReferenceId id ) {
68          this.id = id;
69      }
70  
71      /**
72       * @return id
73       */
74      public ReferenceId getId() {
75          return id;
76      }
77  
78      /**
79       * @param id Sets id to the specified value.
80       */
81      public void setId( ReferenceId id ) {
82          this.id = id;
83      }
84  
85      /**
86       * {@inheritDoc}
87       * 
88       * @see java.lang.Object#hashCode()
89       */
90      @Override
91      public int hashCode() {
92          return id.hashCode();
93      }
94  
95      /**
96       * {@inheritDoc}
97       * 
98       * @see java.lang.Object#equals(java.lang.Object)
99       */
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 
192     /**
193      * Returns a list of all references to UUIDs in the given list within the given workspace
194      * 
195      * @param workspaceId the ID of the workspace; may not be null
196      * @param uuids the UUIDs (as strings) of the nodes to check; may not be null
197      * @param manager the manager; may not be null
198      * @return the number of deleted references
199      */
200     @SuppressWarnings( "unchecked" )
201     public static List<ReferenceEntity> getReferencesToUuids( Long workspaceId,
202                                                               Collection<String> uuids,
203                                                               EntityManager manager ) {
204         assert manager != null;
205 
206         Query query = manager.createNamedQuery("ReferenceEntity.getInwardReferencesForList");
207         query.setParameter("workspaceId", workspaceId);
208         query.setParameter("toUuidList", uuids);
209         return query.getResultList();
210     }
211 }